<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 App\Models\Subscription;
use Carbon\Carbon;
use App\Models\Admin;
use App\Models\Brand;
use App\Models\CarFolder;
use App\Models\CashDesk;
use App\Models\ClassSchool;
use App\Models\Customer;
use App\Models\CustomerAffair;
use App\Models\Pension;
use App\Models\User;
use App\Models\Year;
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('convert_amount_v2')) {
    function convert_amount_v2($amount) {
        if (is_numeric($amount)) {
            return number_format($amount, 0, '.', ' ');
        }
        return '0.00';
    }
}

if (! function_exists('isAdminPrincipal')) {
    function isAdminPrincipal() {
        if(Auth::check()){
            $city = Auth::user()->int_id;
            if($city === 0) return true;
            else return false;
        } 
    }
}

if (! function_exists('getLogo')) {
    function getLogo() {
        return asset('la_mode.jpg');
    }
}
if (! function_exists('getNameApp')) {
    function getNameApp() {
        return "LA MODE";
    }
}
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('getClassNameSchool')) {
    function getClassNameSchool($user_id) {
        $subscribe = Subscription::where('usr_id',operator: $user_id)->latest("inscrip_id")->first();
        if($subscribe) return $subscribe->classSchool->class_name ?? null;
        else return null;
    }
}
if (! function_exists('getPensionClassSchool')) {
    function getPensionClassSchool($id) {
        $subscribe = Subscription::where('inscrip_id', $id)->latest("inscrip_id")->first();
        if($subscribe) return $subscribe->classSchool->class_pension ?? 0;
        else return 0;
    }
}
if (! function_exists('getClassNameById')) {
    function getClassNameById($class_id) {
        $class = ClassSchool::where('class_id',$class_id)->first();
        if($class) return $class->class_name ?? null;
        else return null;
    }
}
if (! function_exists(function: 'getLastYear')) {
    function getLastYear() {
        $year = Year::latest("annee_id")->first();
        if($year) return $year->annee_date ?? null;
        else return null;
    }
}
if (! function_exists('getDataPension')) {
    function getDataPension($inscription_id) {
        $pension = Pension::where('inscrip_id',$inscription_id)->latest(column: "pen_id")->get();
        if($pension) return $pension;
        else return null;
    }
}
if (! function_exists('getSumPension')) {
    function getSumPension($inscription_id, $date_limite = null) {
        $query = Pension::where('inscrip_id', $inscription_id);

        // Si une date est passée, on limite les paiements jusqu'à cette date incluse
        if (!empty($date_limite)) {
            $query->whereDate('pen_date', '<=', $date_limite);
        }

        // Ici on additionne bien le montant payé (ex: pen_amount)
        $pension = $query->sum("pen_montant");

        return $pension ?: 0;
    }
}

if (! function_exists('calculateAffairs')) {
    function calculateAffairs($type)
    {
        $currentDate = date('Y-m-d');
        $data = CustomerAffair::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('calculateStudents')) {
    function calculateStudents($type)
    {
        $currentDate = date('Y-m-d');
        $data = User::get();
        $count = 0;
        $weekStartDate = Carbon::now()->subDays(7)->format('Y-m-d');

        foreach ($data as $value) {
            $registrationDate = strtotime($value->date_reg);
            $dayRegistrationDate = date('Y-m-d', $registrationDate);
            $monthRegistrationDate = date('Y-m', $registrationDate);
            $yearRegistrationDate = date('Y', $registrationDate);
            if($value->usr_status == 1){
                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('getPensionByTypeOld')) {
    function getPensionByTypeOld($type) {
        $amount = 0;
        $currentDate = date('Y-m-d');
        $weekStartDate = Carbon::now()->subDays(7)->format('y-m-d');
        $data = Pension::get();
        if (isset($data) && count($data) > 0) {
            foreach ($data as $key => $value) {
                $created_at = Carbon::parse($value->pen_date)->format('y-m-d');
                $amountExpense = $value->pen_montant;
                $fineDate = strtotime($created_at);

                $dayfineDate = date('Y-m-d', $fineDate);
                $monthfineDate = date('Y-m', $fineDate);
                $yearfineDate = date('Y', $fineDate);

                // if($value->am_status == 1) {
                    switch ($type) {
                        case 'toDay':
                            if (date('Y-m-d', strtotime($currentDate)) == $dayfineDate) {
                                $amount += $amountExpense;
                            }
                            break;

                        case 'last_7_day':
                            if (Carbon::parse($fineDate)->greaterThanOrEqualTo($weekStartDate)) {
                                $amount += $amountExpense;
                            }
                            break;

                        case 'month':
                            if (date('Y-m', strtotime($currentDate)) == $monthfineDate) {
                                $amount += $amountExpense;
                            }
                            break;

                        case 'year':
                            if (date('Y', strtotime($currentDate)) == $yearfineDate) {
                                $amount += $amountExpense;
                            }
                            break;
                        case "lastYear":
                            $lastYear = date('Y', strtotime('-1 year'));
                            if (date('Y', strtotime($currentDate)) == $lastYear) {
                                $amount += $amountExpense;
                            }
                            break;
                        default:
                            $amount = 0;
                    }
                // }
            }
        }

        return convert_amount_v2($amount);
    }

}

if (!function_exists('getPensionByType')) {
    function getPensionByType($type) {
        $amount = 0;
        $currentDate = Carbon::today();
        $weekStartDate = Carbon::now()->subDays(7);
        $pensions = Pension::all();

        if ($pensions->isNotEmpty()) {
            foreach ($pensions as $pension) {
                $penDate = Carbon::parse($pension->pen_date);
                $amountExpense = $pension->pen_montant;

                switch ($type) {
                    case 'toDay':
                        if ($penDate->isSameDay($currentDate)) {
                            $amount += $amountExpense;
                        }
                        break;

                    case 'last_7_day':
                        if ($penDate->greaterThanOrEqualTo($weekStartDate)) {
                            $amount += $amountExpense;
                        }
                        break;

                    case 'month':
                        if ($penDate->isSameMonth($currentDate) && $penDate->year == $currentDate->year) {
                            $amount += $amountExpense;
                        }
                        break;

                    case 'year':
                        if ($penDate->isSameYear($currentDate)) {
                            $amount += $amountExpense;
                        }
                        break;

                    case 'lastYear':
                        $lastYear = $currentDate->subYear()->year;
                        if ($penDate->year == $lastYear) {
                            $amount += $amountExpense;
                        }
                        break;

                    default:
                        $amount = 0;
                        break;
                }
            }
        }

        return convert_amount_v2($amount);
    }
}


if (!function_exists('fetchResponseByValue')) {
    function fetchResponseByValue($value) {
        switch ($value) {
            case "0":
                return "Première tranche en cours";
            case "1":
                return "Première tranche terminée";
            case "1-1":
                return "Deuxième tranche en cours";
            case "1-2":
                return "Deuxième tranche terminée";
            case "1-2-2":
                return "Troisième tranche en cours";
            case "1-2-3":
                return "Troisième tranche terminée";
            default:
                return "Statut de tranche inconnu";
        }
    }
}
if (!function_exists('fetchResponseByValueV2')) {
    function fetchResponseByValueV2($value) {
        $message = "";

        if ($value === "0") {
            $message = "Prémière Tranche";
        } elseif ($value === "1") {
            $message = "Prémière Tranche";
        } elseif ($value === '1-1') {
            $message = "Deuxième Tranche";
        } elseif ($value === '1-2') {
            $message = "Deuxième Tranche";
        } elseif ($value === '1-2-2') {
            $message = "Troisième Tranche";
        } elseif ($value === '1-2-3') {
            $message = "Troisième Tranche";
        } else {
            $message = "Tranche Inconnue";
        }

        return $message;
    }
}
if (!function_exists('lastDateOfTranche')) {
    function lastDateOfTranche($value) {
        $date = "";
        $year = date('Y');
        if ($value === "first") {
            $date = $year."-09-30";
        } elseif ($value === "second") {
            $date = $year."-11-30";
        } elseif ($value === 'third') {
            $date = $year."-01-30";
        } else {
            $date = date('Y-m-d');
        }

        return Carbon::parse($date)->format('Y-m-d');
    }
}
if (!function_exists('translateDate')) {
    function translateDate($date, $lang)
    {
        if ($lang == 'fr') {
            Carbon::setLocale('fr');
        } else {
            Carbon::setLocale('en');
        }

        $carbonDate = Carbon::parse($date);

        $formattedDate = $carbonDate->translatedFormat('l j F Y');

        return $formattedDate;
    }
}
if (!function_exists('translateCycle')) {
    function translateCycle($cycle)
  