name : Impayesauto.php
<?php

namespace App\Livewire\Auto;

use Livewire\Component;

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

class Impayesauto extends Component
{
    use WithPagination;
    public $search ='';
    public $search_compagnie;
    public $search_dateDebut;
    public $search_dateFin;

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

    #[Title('Les contrats Impayés')]
    public function render()
    {

        if (!empty($this->search) || !empty($this->search_compagnie) || (!empty($this->search_dateDebut) && !empty($this->search_dateFin))) {
            $query = Avenant::select(  
                'avenants.id AS IDavenant',  
                'avenants.date_effet AS dateEffet',  
                'avenants.date_echeance AS dateEcheance',
                'avenants.date_creation as datecreate',
                'avenants.mvt_ordre',
                'avenants.mvt_avenant', 
                DB::raw('SUM(p.montantRecu) AS totalmontantRecu'),  // Calcul de la somme  
                '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
                DB::raw('MAX(p.created_at) AS date_creation') // Récupérer la date de création du paiement  
            ) 
            ->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', '=', 1)
            ->where(function ($query) {  
                $query->where('avenants.police', 'like', '%' . $this->search . '%')  
                      ->orWhere('a.nomAssure', 'like', '%' . $this->search . '%');  
            })  
            ->groupBy('avenants.id', 'avenants.date_effet', 'avenants.date_echeance', 'a.nomAssure', 'a.telAssure', 'c.Nom_compagnie', 'avenants.police', 'ac.prime_ttc','avenants.mvt_ordre','avenants.mvt_avenant') // Groupement nécessaire pour SUM()  
            ->having('difference', '!=', 0) // Filtrer les différences supérieures à 0  
            ->orderBy('date_creation', 'desc'); // Ordre par date de création du paiement

            // Ajoutez des conditions pour la recherche texte
                if (!empty($this->search)) {
                    $query->where(function($q) {
                        $q->where('avenants.police', 'like', '%' . $this->search . '%')
                          ->orWhere('a.nomAssure', 'like', '%' . $this->search . '%');
                    });
                }

                // Rechercher pa compagnie
                if (!empty($this->search_compagnie)) {
                    $query->where(function($q) {
                        $q->where('avenants.IDcompagnie', '=', $this->search_compagnie);
                    });
                }

                // Recherche par date échéance
                if (!empty($this->search_dateDebut) && !empty($this->search_dateFin)) {
                    $query->where(function($q) {
                        $q->whereBetween('avenants.date_creation', [$this->search_dateDebut, $this->search_dateFin]);
                    });
                }

                // Condition supplémentaire pour statut et type
                $query->where('ac.etat', '=', 'production');
            
                // Enfin, la recherche par statut si besoin
                $query->where('avenants.status_avenant', '=', 'production')
                      ->where('avenants.typeAvenant', '=', 'mono');
            
                $derniersRestesAPayer = $query->orderBy('avenants.id', 'desc')->paginate(10); 
         }else {
            
            $derniersRestesAPayer = Avenant::select(  
                'avenants.id AS IDavenant',  
                'avenants.date_effet AS dateEffet',  
                'avenants.date_echeance AS dateEcheance',
                'avenants.date_creation as datecreate',
                'avenants.mvt_ordre',
                'avenants.mvt_avenant', 
                DB::raw('SUM(p.montantRecu) AS totalmontantRecu'),  // Calcul de la somme  
                '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 
                DB::raw('MAX(p.created_at) AS date_creation') // Récupérer la date de création du paiement  
            )  
            ->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', '=', 1)
            ->groupBy('avenants.id', 'avenants.date_effet', 'avenants.date_echeance', 'a.nomAssure', 'a.telAssure', 'c.Nom_compagnie', 'avenants.police', 'ac.prime_ttc','avenants.mvt_ordre','avenants.mvt_avenant') // Groupement nécessaire pour SUM()  
            ->having('difference', '!=', 0) // Filtrer les différences supérieures à 0
            ->orderBy('date_creation', 'desc') // Ordre par date de création du paiement  
            ->paginate(10);
         }
        return view('livewire.auto.impayesauto',[
            'Listedevis'=>$derniersRestesAPayer,
            'compagnies'=>Compagnie::get(),
        ]);
    }
}

© 2025 UnknownSec
afwwrfwafr45458465
Password