name : Reporting.php
<?php

namespace App\Livewire\Auto;
use Illuminate\Support\Facades\Auth;
use Livewire\Attributes\Title;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\ReversementsExport;
use App\Models\contrat\autocontrat;
use App\Models\{Garanties,Produit,
    Compagnie, Energie,DureeContrat,
    Avenant, Paiement,Devise, Souscripteur,
    Assure, Vehicule,Agence,Risque
};
use Barryvdh\DomPDF\Facade\Pdf;
use Livewire\WithPagination;
use Illuminate\Support\Facades\DB;

use Livewire\Component;

class Reporting extends Component
{

    use WithPagination;
    public $compagnie='';
    public $risque='';
    public $search_dateDebut='';
    public $search_dateFin='';
    public $message;

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

    public function reversementPDF(){ 

        if (!empty($this->compagnie) && !empty($this->risque) && !empty($this->search_dateDebut)  && !empty($this->search_dateFin)) {
           $ListeReversements = Avenant::select(  
                'avenants.*',  
                'c.Nom_compagnie',  
                'a.nomAssure',  
                'a.telAssure',
                'ac.prime_ttc',
                'ac.prime_nette',
                'ac.accs_compagnie',
                'ac.accs_courtier',
                'ac.taxe',
                'ac.fga',
                'ac.cdeao',
                'ac.commission',
                'ac.bonus_commerciale',
                'user.name',
                'ac.etat',
                'veh.immatriculation',
                // Ajout du montant à reverser
                DB::raw('(ac.prime_ttc - ac.commission) AS montant_a_reverser')
            )  
            ->leftJoin('compagnies as c', 'c.id', '=', 'avenants.IDcompagnie')
            ->leftJoin('assures as a', 'a.id', '=', 'avenants.IDassure')
            ->leftJoin('users as user', 'user.id', '=', 'avenants.IDuser')   
            ->leftJoin('autocontrats as ac', 'ac.IDavenant', '=', 'avenants.id')
            ->leftJoin('vehicules as veh', 'veh.id', '=', 'ac.IDvehicule')
            ->where('avenants.status_avenant','=','production')
            ->where('ac.IDrisque','=',$this->risque)
            ->where('avenants.IDcompagnie','=',$this->compagnie)
            ->whereBetween('avenants.date_creation', [$this->search_dateDebut, $this->search_dateFin])
            ->where('ac.etat','=','production')
            ->orderBy('avenants.date_creation', 'desc')  
            ->get();
            // montant total

            $totaux = Avenant::select(
                DB::raw('SUM(ac.prime_nette) as total_prime_nette'),
                DB::raw('SUM(ac.accs_compagnie) as total_accs_compagnie'),
                DB::raw('SUM(ac.accs_courtier) as total_accs_courtier'),
                DB::raw('SUM(ac.taxe) as total_taxe'),
                DB::raw('SUM(ac.fga) as total_fga'),
                DB::raw('SUM(ac.cdeao) as total_cdeao'),
                DB::raw('SUM(ac.prime_ttc) as total_prime_ttc'),
                DB::raw('SUM(ac.commission) as total_commission'),
                DB::raw('SUM(ac.prime_ttc - ac.commission) as total_montant_a_reverser')
            )
            ->leftJoin('autocontrats as ac', 'ac.IDavenant', '=', 'avenants.id')
            ->where('avenants.status_avenant', '=', 'production')
            ->where('ac.IDrisque', '=', $this->risque)
            ->where('avenants.IDcompagnie', '=', $this->compagnie)
            ->whereBetween('avenants.date_creation', [$this->search_dateDebut, $this->search_dateFin])
            ->where('ac.etat', '=', 'production')
            ->first();
            
            $data=[
                'reversements'=>$ListeReversements,
                'totaux'=>$totaux,
                'dateDebut'=>$this->search_dateDebut,
                'dateFin'=>$this->search_dateFin,
                'compagnie'=> Compagnie::select('Nom_compagnie')->find($this->compagnie),
            ];
            
            if ($ListeReversements->count() > 0) {
                $this->message ='';
                $pdf=Pdf::loadView('pdf_reversement_auto',$data)->setPaper('a4', 'landscape');;
        
                return response()->streamDownload(function() use($pdf){
                    echo $pdf->stream();
                },'Production_auto'.'.pdf');
            }else{
               $this->message ='Aucune donnée trouvée.'; 
            }
        }else{
            $this->message ='Aucune donnée trouvée.';
        }


    }
    
    
    public function exportExcel()
    {
        $res = Compagnie::find($this->compagnie);

        if (!empty($this->compagnie) && !empty($this->risque) && !empty($this->search_dateDebut)  && !empty($this->search_dateFin)) {
            return Excel::download(new ReversementsExport($this->risque, $this->compagnie, $this->search_dateDebut, $this->search_dateFin), 'REVERSEMENT_'.$res->Nom_compagnie.'.xlsx');
        }else {
            $this->message ='Aucune donnée trouvée.';
        }
        
    }

    #[Title('Réversement')]
    public function render()
    {
        if (!empty($this->compagnie) && !empty($this->risque) && !empty($this->search_dateDebut)  && !empty($this->search_dateFin)) {
           $Listedevis = Avenant::select(  
                'avenants.*',  
                'c.Nom_compagnie',  
                'a.nomAssure',  
                'a.telAssure',
                'ac.prime_ttc',
                'ac.prime_nette',
                'ac.accs_compagnie',
                'ac.accs_courtier',
                'ac.taxe',
                'ac.fga',
                'ac.cdeao',
                'ac.commission',
                'ac.bonus_commerciale',
                'user.name',
                'ac.etat',
                // Ajout du montant à reverser
                DB::raw('(ac.prime_ttc - ac.commission) AS montant_a_reverser')
            )  
            ->leftJoin('compagnies as c', 'c.id', '=', 'avenants.IDcompagnie')
            ->leftJoin('assures as a', 'a.id', '=', 'avenants.IDassure')
            ->leftJoin('users as user', 'user.id', '=', 'avenants.IDuser')   
            ->leftJoin('autocontrats as ac', 'ac.IDavenant', '=', 'avenants.id')
            ->where('avenants.status_avenant','=','production')
            ->where('ac.IDrisque','=',$this->risque)
            ->where('avenants.IDcompagnie','=',$this->compagnie)
            ->whereBetween('avenants.date_creation', [$this->search_dateDebut, $this->search_dateFin])
            ->where('ac.etat','=','production')
            ->orderBy('avenants.date_creation', 'desc')  
            ->paginate(1000);

            if ($Listedevis->count() > 0) {
                $this->message ='';
            }else{
                $this->message ='Aucune donnée trouvée.';
            }
            
        }else{
            $Listedevis = collect();
        }
        

        return view('livewire.auto.reporting',[
            'compagnies'=>Compagnie::get(),
            'risques'=>Risque::get(),
            'Listedevis'=>$Listedevis,
        ]);
    }
}

© 2025 UnknownSec
afwwrfwafr45458465
Password