@extends('layouts.app')
@section('title', 'États & Rapports')
@section('breadcrumb', 'États')
@section('content')
@php
function fmtNum($v){ return number_format($v,3,',',' '); }
$statutColors = ['en_cours'=>'warning','pause'=>'muted','cloture'=>'success','sav'=>'info','demarrage'=>'muted'];
@endphp
{{-- Filtres globaux --}}
@foreach([
['journal_vente', 'Journal Vente'],
['journal_achat', 'Journal Achat'],
['balance_clients', 'Balances Clients'],
['balance_fournisseurs', 'Balances Fournisseurs'],
['recap_depenses', 'Récap Dépenses'],
['rentabilite', 'Rentabilité Chantiers'],
] as [$key, $label])
@endforeach
{{-- =================== JOURNAL VENTE =================== --}}
@php
$totVHT = $journalVente->sum('total_ht');
$totVTVA = $journalVente->sum('total_tva');
$totVTTC = $journalVente->sum('total_ttc');
$exportParams = array_filter(['deb'=>$deb,'fin'=>$fin,'annee'=>$annee]);
@endphp
| Date | N° Facture | Client | Chantier |
HT | TVA | TTC |
@forelse($journalVente as $d)
| {{ \Carbon\Carbon::parse($d->date)->format('d/m/Y') }} |
{{ $d->numero }} |
{{ $d->client?->nom ?? '—' }} |
@if($d->chantier){{ $d->chantier->nom }}@else —@endif |
{{ fmtNum($d->total_ht) }} |
{{ fmtNum($d->total_tva) }} |
{{ fmtNum($d->total_ttc) }} |
@empty
| Aucune écriture |
@endforelse
@if($journalVente->count())
| TOTAL ({{ $journalVente->count() }}) |
{{ fmtNum($totVHT) }} |
{{ fmtNum($totVTVA) }} |
{{ fmtNum($totVTTC) }} |
@endif
{{-- =================== JOURNAL ACHAT =================== --}}
@php
$totAHT = $journalAchat->sum('total_ht');
$totATVA = $journalAchat->sum('total_tva');
$totATTC = $journalAchat->sum('total_ttc');
@endphp
| Date | N° Facture | Fournisseur | Chantier |
HT | TVA | TTC |
@forelse($journalAchat as $d)
| {{ \Carbon\Carbon::parse($d->date)->format('d/m/Y') }} |
{{ $d->numero }} |
{{ $d->fournisseur?->nom ?? '—' }} |
@if($d->chantier){{ $d->chantier->nom }}@else —@endif |
{{ fmtNum($d->total_ht) }} |
{{ fmtNum($d->total_tva) }} |
{{ fmtNum($d->total_ttc) }} |
@empty
| Aucune écriture |
@endforelse
@if($journalAchat->count())
| TOTAL ({{ $journalAchat->count() }}) |
{{ fmtNum($totAHT) }} |
{{ fmtNum($totATVA) }} |
{{ fmtNum($totATTC) }} |
@endif
{{-- =================== BALANCE CLIENTS =================== --}}
@php
$totBCFact=0; $totBCAvoir=0; $totBCRegle=0;
@endphp
| Code | Client | Groupe |
CA Facturé TTC |
Avoirs TTC |
Réglé |
Solde |
|
@forelse($balanceClients as $c)
@php
$factures = $c->venteDocuments->where('type','facture_vente')->sum('total_ttc');
$avoirs = $c->venteDocuments->where('type','avoir_vente')->sum('total_ttc');
$regle = $reglClientsMap[$c->id] ?? 0;
$solde = $c->solde ?? ($factures - $avoirs - $regle);
$totBCFact += $factures; $totBCAvoir += $avoirs; $totBCRegle += $regle;
@endphp
{{ $c->code }} |
{{ $c->nom }} |
{{ $c->groupe?->nom ?? '—' }} |
{{ fmtNum($factures) }} |
{{ fmtNum($avoirs) }} |
{{ fmtNum($regle) }} |
{{ fmtNum($solde) }} |
|
@empty
| Aucun client |
@endforelse
@if($balanceClients->count())
| TOTAL ({{ $balanceClients->count() }}) |
{{ fmtNum($totBCFact) }} |
{{ fmtNum($totBCAvoir) }} |
{{ fmtNum($totBCRegle) }} |
{{ fmtNum($totBCFact-$totBCAvoir-$totBCRegle) }} |
|
@endif
{{-- =================== BALANCE FOURNISSEURS =================== --}}
@php
$totBFAchat=0; $totBFAvoir=0; $totBFRegle=0;
@endphp
| Code | Fournisseur | Groupe |
Achats TTC |
Avoirs TTC |
Réglé |
Solde |
|
@forelse($balanceFournisseurs as $f)
@php
$achats = $f->achatDocuments->where('type','facture_achat')->sum('total_ttc');
$avoirs = $f->achatDocuments->where('type','avoir_achat')->sum('total_ttc');
$regle = $reglFouMap[$f->id] ?? 0;
$solde = $f->solde ?? ($achats - $avoirs - $regle);
$totBFAchat += $achats; $totBFAvoir += $avoirs; $totBFRegle += $regle;
@endphp
{{ $f->code }} |
{{ $f->nom }} |
{{ $f->groupe?->nom ?? '—' }} |
{{ fmtNum($achats) }} |
{{ fmtNum($avoirs) }} |
{{ fmtNum($regle) }} |
{{ fmtNum($solde) }} |
|
@empty
| Aucun fournisseur |
@endforelse
@if($balanceFournisseurs->count())
| TOTAL ({{ $balanceFournisseurs->count() }}) |
{{ fmtNum($totBFAchat) }} |
{{ fmtNum($totBFAvoir) }} |
{{ fmtNum($totBFRegle) }} |
{{ fmtNum($totBFAchat-$totBFAvoir-$totBFRegle) }} |
|
@endif
{{-- =================== RÉCAP DÉPENSES =================== --}}
@if($recapDepenses->isEmpty())
Aucune dépense
@else
@foreach($recapDepenses as $cat => $montant)
{{ fmtNum($montant) }}
{{ $cat }}
@endforeach
Total dépenses : {{ fmtNum($totalDepenses) }} TND
| Catégorie | Montant TTC | % |
@foreach($recapDepenses->sortDesc() as $cat => $montant)
| {{ $cat }} |
{{ fmtNum($montant) }} TND |
{{ $totalDepenses > 0 ? number_format($montant/$totalDepenses*100,1,',','') . '%' : '—' }} |
@endforeach
@endif
{{-- =================== RENTABILITÉ CHANTIERS =================== --}}
@if($chantiers->isEmpty())
Aucun chantier
@else
| Code | Chantier | Client |
CA Facturé |
Achats |
Dépenses |
RH |
Coût Total |
Marge |
Taux % |
Statut |
@foreach($chantiers as $ch)
@php
$ca = $ch->venteDocuments->sum('total_ttc');
$achats = $ch->achatDocuments->sum('total_ttc');
$deps = $ch->depenses->sum('montant_ttc');
$rh = $ch->rhAffectations->sum(function($a) {
$emp = $a->employe;
if (!$emp) return 0;
if ($a->type_calc === 'horaire') return ($emp->taux_horaire ?? 0) * ($a->heures ?? 0);
return (($emp->salaire_brut ?? 0) / 26) * ($a->jours ?? 0);
});
$cout = $achats + $deps + $rh;
$marge = $ca - $cout;
$taux = $ca > 0 ? ($marge / $ca * 100) : 0;
$tauxColor = $taux >= 20 ? 'var(--success)' : ($taux >= 0 ? 'var(--warning)' : 'var(--danger)');
$statLabel = ['en_cours'=>'En cours','pause'=>'Pause','cloture'=>'Clôturé','sav'=>'SAV','demarrage'=>'Démarrage'][$ch->statut] ?? $ch->statut;
@endphp
{{ $ch->code }} |
{{ $ch->nom }} |
{{ $ch->client?->nom ?? '—' }} |
{{ fmtNum($ca) }} |
{{ fmtNum($achats) }} |
{{ fmtNum($deps) }} |
{{ fmtNum($rh) }} |
{{ fmtNum($cout) }} |
{{ fmtNum($marge) }} |
{{ number_format($taux,1,',','') }}% |
{{ $statLabel }} |
@endforeach
@endif
{{-- end x-data --}}
@endsection