name : Dashbord.php
<?php

namespace App\Livewire;

use Livewire\Component;
use App\Models\{Avenant,TypeVehicule,
    Vehicule, Energie, Produit,
    Compagnie, DureeContrat, Souscripteur,
    Assure, Pay, Ville, Garanties, Risque, Agence};
use App\Models\contrat\autocontrat;
use Barryvdh\DomPDF\Facade\Pdf;
use Carbon\Carbon;
use Livewire\Attributes\Title;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;

class Dashbord extends Component
{
    public $assure_count;
    public $vehicule_count;
    
    
    //
    public $search_risque ='';
    public $search_annee ='';
    public $search_agence ='';

    public $totalDifferences =0;
    public $sommePrimeTTC =0;
    public $sommeEncaisser =0;
    public $sommeCommissionBrute =0;
    //
    // 
    public $search_risque_recap ='';
    public $search_dateDebut_recap ='';
    public $search_dateFin_recap ='';
    public $search_agence_recap;

    public $total_accessoire_courtier=0;
    public $total_pn=0;
    public $total_pttc=0;
    public $total_commissionBrute=0;
    public $total_com_nette=0;
    public $sommeDonnees ='';

    
 
    public function mount()
    {
        if (!Auth::check()) {
            return redirect()->route('post.login');
        }

        $this->assure_count = Assure::count();
        $this->vehicule_count = Vehicule::count();
    }

    public function RecapDashbord(){
        if ($this->search_risque && $this->search_annee && $this->search_agence) {

            $this->sommePrimeTTC = autocontrat::where('IDrisque', $this->search_risque)  
                ->where('exercice', $this->search_annee)
                ->where('IDagence', $this->search_agence) // Assurez-vous que $this->search_exercice est défini 
                ->where('etat', '=', 'production')
                ->sum('prime_ttc');

                $derniersRestesAPayer = Avenant::select(  
                    'avenants.id AS IDavenant',  
                    'avenants.date_effet AS dateEffet',  
                    'avenants.date_echeance AS dateEcheance',  
                    DB::raw('SUM(p.montantRecu) AS totalMontantRecu'),  // Somme des montants reçus  
                    'a.nomAssure',  
                    'a.telAssure',  
                    'c.Nom_compagnie',  
                    'avenants.police',  
                    'ac.prime_ttc',  
                    DB::raw('ac.prime_ttc - SUM(p.montantRecu) AS difference') // Calcul de la différence  
                )
                ->leftJoin('paiements as p', 'p.IDavenant', '=', 'avenants.id')
                ->leftJoin('assures as a', 'a.id', '=', 'avenants.IDassure')  
                ->leftJoin('compagnies as c', 'c.id', '=', 'avenants.IDcompagnie')  
                ->leftJoin('autocontrats as ac', 'ac.IDavenant', '=', 'avenants.id')
                ->where('avenants.status_avenant', '=', 'production')  
                ->where('avenants.typeAvenant', '=', 'mono')  
                ->where('ac.etat', '=', 'production')
                ->where('ac.IDrisque', $this->search_risque)
                ->where('ac.exercice', $this->search_annee)
                ->where('ac.IDagence', $this->search_agence)
                ->groupBy('avenants.id', 'avenants.date_effet', 'avenants.date_echeance', 'a.nomAssure', 'a.telAssure', 'c.Nom_compagnie', 'avenants.police', 'ac.prime_ttc') // Groupement nécessaire  
                ->having('difference', '>', 0) // Filtrer où la différence est supérieure à 0  
                ->get(); // Obtenir les résultats

            // Calculer la somme de toutes les différences  
            $this->totalDifferences = $derniersRestesAPayer->sum(function ($item) {  
                 return max($item->difference, 0); // Assurez-vous de ne sommer que les différences positives  
            });

            $this->sommeEncaisser = (int)((int)$this->sommePrimeTTC - (int)$this->totalDifferences);

            $this->sommeCommissionBrute = autocontrat::where('IDrisque', $this->search_risque)  
                ->where('exercice', $this->search_annee) // Assurez-vous que $this->search_exercice est défini 
                ->where('IDagence', $this->search_agence) // Assurez-vous que $this->search_exercice est défini 
                ->where('etat', '=', 'production') 
                ->sum('commission');

        }else{
             $this->sommePrimeTTC =0;
             $this->totalDifferences=0;
             $this->sommeEncaisser =0;
             $this->sommeCommissionBrute =0;
        }
    }



    public function Recherche()  
    {  
        // dd($this->search_risque_recap, $this->search_dateDebut_recap, $this->search_dateFin_recap);
        if ($this->search_risque_recap && $this->search_agence_recap && $this->search_dateDebut_recap && $this->search_dateFin_recap) {
            // Définir vos dates
            $dateDebut = $this->search_dateDebut_recap;  
            $dateFin = $this->search_dateFin_recap;  

            // Effectuer la requête  
            $this->sommeDonnees = autocontrat::whereHas('avenant', function ($query) use ($dateDebut, $dateFin) {  
                $query->whereBetween('avenants.created_at', [$dateDebut, $dateFin])  
                    // ->whereBetween('avenants.created_at', [$dateDebut, $dateFin])  
                    ->where('avenants.status_avenant', 'production');  
            })  
            ->where('autocontrats.etat', 'production')  
            ->where('autocontrats.IDrisque', $this->search_risque_recap)
            ->where('autocontrats.IDagence', $this->search_agence_recap)   
            ->selectRaw('SUM(prime_nette) as totalPrimeNette, SUM(prime_ttc) as totalPrimeTTC, SUM(commission_nette) as totalCommissionNette, SUM(commission) as totalCommissionBrute, SUM(accs_courtier) as totalCoutier_acss')  
            ->first();
        }else{
            
        }
    }  

    #[Title('Dashboard')]
    public function render()
    {
        $risques = Risque::get();
        $exercices = autocontrat::select('exercice')->groupBy('exercice')->get();
        return view('livewire.dashbord',[
            'risques'=>$risques,
            'exercices'=>$exercices,
            'agences'=>Agence::get(),
         ]);
    }
}

© 2025 UnknownSec
afwwrfwafr45458465
Password