name : ProductionMono.php
<?php

namespace App\Livewire\Auto;

use Livewire\Component;
use App\Models\{Avenant,Compagnie};
use Illuminate\Support\Facades\DB;
use Livewire\WithPagination;
use Livewire\Attributes\Title;
use Illuminate\Support\Facades\Auth;

class ProductionMono extends Component
{
    use WithPagination;
    public $search ='';
    public $search_dateDebut='';
    public $search_dateFin='';
    public $avenantsDerniers =[];
    public $search_compagnie='';

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

        // $groupes = Avenant::select('refAvenant')->groupBy('refAvenant')->get(); // ou autre critère
        
        // $dernierAvenantsParNumero = $groupes->map(function ($group) {
        //     return $group->sortByDesc('created_at')->first();
        // });
        // dd($dernierAvenantsParNumero);

        $this->avenantsDerniers = Avenant::select('avenants.*')
            ->joinSub(
                // sous-requête pour obtenir le dernier created_at pour chaque refAvenant
                Avenant::select('refAvenant', DB::raw('MAX(created_at) as max_created_at'))
                    ->groupBy('refAvenant'),
                'latest',
                function ($join) {
                    $join->on('avenants.refAvenant', '=', 'latest.refAvenant')
                        ->on('avenants.created_at', '=', 'latest.max_created_at');
                }
            )
            ->get();
            // dd($this->avenantsDerniers);
    }
   
    #[Title('Liste des Borderaux émis')]
    public function render()
    {
        if (!empty($this->search) || !empty($this->search_compagnie) || (!empty($this->search_dateDebut) && !empty($this->search_dateFin))) {
                // Ajoutez des conditions pour la recherche texte
                $query = Avenant::select(
                    'avenants.*',
                    'c.Nom_compagnie',
                    'a.nomAssure',
                    'a.telAssure',
                    'ac.prime_ttc',
                    'ac.commission',
                    'ac.commission_nette',
                    'veh.immatriculation',
                    'veh.marque',
                    'ac.etat'
                )
                ->leftJoin('compagnies as c', 'c.id', '=', 'avenants.IDcompagnie')
                ->leftJoin('assures as a', 'a.id', '=', 'avenants.IDassure')
                ->leftJoin('autocontrats as ac', 'ac.IDavenant', '=', 'avenants.id')
                ->leftJoin('vehicules as veh', 'veh.id', '=', 'ac.IDvehicule');

                // 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_echeance', [$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');
            
                $Listeavenants = $query->orderBy('avenants.id', 'desc')->paginate(10);
        } else {
                // Cas par défaut si aucune recherche spécifique
                $Listeavenants = Avenant::select(
                    'avenants.*',
                    'c.Nom_compagnie',
                    'a.nomAssure',
                    'a.telAssure',
                    'ac.prime_ttc',
                    'ac.commission',
                    'ac.commission_nette',
                    'veh.immatriculation',
                    'veh.marque',
                    'ac.etat'
                )
                ->leftJoin('compagnies as c', 'c.id', '=', 'avenants.IDcompagnie')
                ->leftJoin('assures as a', 'a.id', '=', 'avenants.IDassure')
                ->leftJoin('autocontrats as ac', 'ac.IDavenant', '=', 'avenants.id')
                ->leftJoin('vehicules as veh', 'veh.id', '=', 'ac.IDvehicule')
                ->where('avenants.status_avenant', '=', 'production')
                ->where('avenants.typeAvenant', '=', 'mono')
                ->where('ac.etat', '=', 'production')
                ->orderBy('avenants.id', 'desc')
                ->paginate(10);
        }

        // dd($Listeavenants);
        return view('livewire.auto.production-mono',[
            'Listeavenants'=>$Listeavenants,
            'compagnies'=>Compagnie::get(),
        ]);
    }
}

© 2025 UnknownSec
afwwrfwafr45458465
Password