<br />
<b>Warning</b>:  Undefined variable $auth in <b>/home/pevo0181/public_html/pia-soft.com/cleania/routes/index.php</b> on line <b>542</b><br />
<br />
<b>Warning</b>:  Trying to access array offset on value of type null in <b>/home/pevo0181/public_html/pia-soft.com/cleania/routes/index.php</b> on line <b>542</b><br />
<?php

use Carbon\Carbon;
use App\Models\Admin;
use App\Models\Brand;
use App\Models\CarFolder;
use App\Models\Cash;
use App\Models\CashDesk;
use App\Models\Customer;
use App\Models\CustomerAffair;
use App\Models\OthersInformation;
use App\Models\User;
use Illuminate\Support\Facades\Auth;

if (! function_exists('convert_amount')) {
    function convert_amount($amount) {
        $currency = "F CFA";
        if (is_numeric($amount)) {
            return number_format($amount, 2);
        }
        return 0;
    }
}
if (! function_exists('convert_number')) {
    function convert_number($amount) {
        if (is_numeric($amount)) {
            //return number_format($amount, 2);
            return number_format($amount, 2, ',',' ');
        }
        return 0;
    }
}
if (! function_exists('getLogo')) {
    function getLogo() {
        return asset('logo.jpg');
    }
}
if (! function_exists('getNameCabinet')) {
    function getNameCabinet() {
        return "SYGEIP PRO";
    }
}
if (! function_exists('getFetchImage')) {
    function getFetchImage($image) {
        return "https://pia-soft.com/gespers/images/".$image;
    }
}
if (! function_exists('getLastInfo')) {
    function getLastInfo($user_id,$returnValue) {
        $item = OthersInformation::where('usr_id',$user_id)->first();
        if($item) return $item->$returnValue;
            else return null;
    }
}
if (! function_exists('getSloganCabinet')) {
    function getSloganCabinet() {
        if(Auth::check()){
            $slogan = isset(auth()->user()->intitule) ? auth()->user()->intitule->int_slogan : null ;
            if(!is_null($slogan)) 
                return $slogan;
            else
                return "CANC";
        }
        return "CANC";
    }
}
if (! function_exists('getAbbreviationCabinet')) {
    function getAbbreviationCabinet() {
        if(Auth::check()){
            $logo = isset(auth()->user()->intitule) ? auth()->user()->intitule->int_abreviation : null ;
            if(!is_null($logo)) 
                return $logo;
            else
                return "CANC";
        }
        return "CANC";
    }
}

if (!function_exists('isExpired')) {
    /**
     * Vérifie si une date est expirée (antérieure à aujourd'hui)
     * 
     * @param string|Carbon $date La date à vérifier
     * @return bool true si la date est expirée, false sinon
     */
    function isExpired($date)
    {
        // Convertir en instance Carbon si nécessaire
        $givenDate = $date instanceof Carbon ? $date : Carbon::parse($date);
        
        // Obtenir la date d'aujourd'hui sans l'heure
        $today = Carbon::today();
        
        // Retourner true si la date donnée est antérieure à aujourd'hui
        return $givenDate->lt($today);
    }
}

// Version alternative avec plus d'options
if (!function_exists('checkExpiration')) {
    /**
     * Vérifie l'état d'expiration d'une date avec plus d'options
     * 
     * @param string|Carbon $date La date à vérifier
     * @param bool $includeTime Inclure l'heure dans la comparaison
     * @return array Informations détaillées sur l'expiration
     */
    function checkExpiration($date, $includeTime = false)
    {
        $givenDate = $date instanceof Carbon ? $date : Carbon::parse($date);
        $now = $includeTime ? Carbon::now() : Carbon::today();
        
        return [
            'is_expired' => $givenDate->lt($now),
            'is_today' => $givenDate->isSameDay($now),
            'is_future' => $givenDate->gt($now),
            'days_until_expiry' => $now->diffInDays($givenDate, false),
            'human_readable' => $givenDate->diffForHumans($now)
        ];
    }
}
if (! function_exists('isCabinet')) {
    function isCabinet() {
        if(Auth::check()){
            $city = Auth::user()->int_id;
            if($city !== 1) return true;
            else return false;
        } 
    }
}
if (! function_exists('isAdminPrincipal')) {
    function isAdminPrincipal() {
        if(Auth::check()){
            $city = Auth::user()->int_id;
            if($city == 1) return true;
            else return false;
        } 
    }
}

if (! function_exists('getCityByAdmin')) {
    function getCityByAdmin($admin_id) {
        $admin = Admin::where('ad_id', '=', $admin_id)->first();
        if($admin) return $admin->int_id;
        else return 0;
    }
}

if (! function_exists('getAdminByName')) {
    function getAdminByName($id) {
        $admin = Admin::where('ad_id', $id)->firstOrFail();
        return $admin->ad_name;
    }
}

if (! function_exists('calculateAffairs')) {
    function calculateAffairs($type)
    {
        $currentDate = date('Y-m-d');
        $data = CustomerAffair::query();
        if (isCabinet() === true) {
            $int_id = auth()->user()->int_id;
            $data->where('int_id', $int_id);
        }

        $data = $data->get();
        $count = 0;
        $weekStartDate = Carbon::now()->subDays(7)->format('Y-m-d');

        foreach ($data as $value) {
            $registrationDate = strtotime($value->ca_date);
            $dayRegistrationDate = date('Y-m-d', $registrationDate);
            $monthRegistrationDate = date('Y-m', $registrationDate);
            $yearRegistrationDate = date('Y', $registrationDate);

            switch ($type) {
                case "toDay":
                    if ($currentDate == $dayRegistrationDate) {
                        $count++;
                    }
                    break;
                case "month":
                    if (date('Y-m', strtotime($currentDate)) == $monthRegistrationDate) {
                        $count++;
                    }
                    break;
                case "year":
                    if (date('Y', strtotime($currentDate)) == $yearRegistrationDate) {
                        $count++;
                    }
                    break;
                case 'last_7_day':
                    if (Carbon::parse($registrationDate)->greaterThanOrEqualTo($weekStartDate)) {
                        $count++;
                    }
                    break;
                case "all":
                    $count++;
                    break;
                case "lastYear":
                    $lastYear = date('Y', strtotime('-1 year'));
                    if (date('Y', strtotime($currentDate)) == $lastYear) {
                        $count++;
                    }
                    break;
                case "active":
                    if ($value->ca_status == 1) {
                        $count++;
                    }
                    break;
                case "inactive":
                    if ($value->ca_status == 0) {
                        $count++;
                    }
                    break;
                default:
                    $count = 0;
            }
        }

        return $count;
    }

}
if (! function_exists('calculateCustomers')) {
    function calculateCustomers($type)
    {
        $currentDate = date('Y-m-d');
        $data = Customer::query();

        if (isCabinet() === true) {
            $int_id = auth()->user()->int_id;
            $data->where('int_id', $int_id);
        }

        $data = $data->get();
       
        $count = 0;
        $weekStartDate = Carbon::now()->subDays(7)->format('Y-m-d');

        foreach ($data as $value) {
            
            $registrationDate = strtotime($value->client_date);
            $dayRegistrationDate = date('Y-m-d', $registrationDate);
            $monthRegistrationDate = date('Y-m', $registrationDate);
            $yearRegistrationDate = date('Y', $registrationDate);

            switch ($type) {
                case "toDay":
                    if ($currentDate == $dayRegistrationDate) {
                        $count++;
                    }
                    break;
                case "month":
                    if (date('Y-m', strtotime($currentDate)) == $monthRegistrationDate) {
                        $count++;
                    }
                    break;
                case "year":
                    if (date('Y', strtotime($currentDate)) == $yearRegistrationDate) {
                        $count++;
                    }
                    break;
                case 'last_7_day':
                    if (Carbon::parse($registrationDate)->greaterThanOrEqualTo($weekStartDate)) {
                        $count++;
                    }
                    break;
                case "all":
                    $count++;
                    break;
                case "lastYear":
                    $lastYear = date('Y', strtotime('-1 year'));
                    if (date('Y', strtotime($currentDate)) == $lastYear) {
                        $count++;
                    }
                    break;
                default:
                    $count = 0;
            }
        }

        return $count;
    }

}
if (!function_exists('amountCashByType')) {
    function amountCashByType($filterType)
    {
        $currentDate = Carbon::now();
        $operations = Cash::get();
        $sum = 0;
        $sum_e = 0;
        $sum_s = 0;

        foreach ($operations as $value) {
            $orderDate = Carbon::parse($value->mc_date);
            $amount = $value->mc_montant;
            $status = $value->mc_status;

            switch ($filterType) {
                case "toDay":
                    if ($orderDate->isSameDay($currentDate)) {
                        $sum += $amount;
                    }
                    break;

                case "month":
                    if ($orderDate->isSameMonth($currentDate)) {
                        $sum += $amount;
                    }
                    break;

                case "year":
                    if ($orderDate->isSameYear($currentDate)) {
                        $sum += $amount;
                    }
                    break;

                case "last_7_day":
                    $sevenDaysAgo = Carbon::now()->subDays(7);
                    if ($orderDate->greaterThanOrEqualTo($sevenDaysAgo)) {
                        $sum += $amount;
                    }
                    break;

                case "all":
                    $sum += $amount;
                    break;

                case "lastYear":
                    $lastYear = Carbon::now()->subYear();
                    if ($orderDate->isSameYear($lastYear)) {
                        $sum += $amount;
                    }
                    break;

                case "ENTREE":
                    if ($status == "ENTREE") {
                        $sum += $amount;
                    }
                    break;

                case "SORTIE":
                    if ($status == "SORTIE") {
                        $sum += $amount;
                    }
                    break;

                case "residual":
                    if ($status == "ENTREE") {
                        $sum_e += $amount;
                    }
                    if ($status == "SORTIE") {
                        $sum_s += $amount;
                    }
                    $sum = $sum_e - $sum_s;
                    break;

                default:
                    $sum = 0;
                    break;
            }
        }

        return function_exists('convert_number') ? convert_number($sum) : $sum;
    }
}

if (!function_exists('dashboardCard')) {
    function dashboardCard($type)
    {
        // Dictionnaire type => [modèle, libellé]
        $types = [
            'occupants' => [\App\Models\Occupant::class, 'Occupants'],
            'zones'     => [\App\Models\Zone::class, 'Zones'],
            'camps'     => [\App\Models\Camp::class, 'Camps'],
            'locaux'    => [\App\Models\Local::class, 'Locaux'],
        ];

        if (!isset($types[$type])) {
            return '<!-- Type "' . $type . '" non reconnu -->';
        }

        [$model, $label] = $types[$type];

