@extends('layouts.provider')
@section('title', $client->full_name)
@section('page-title', 'Clients > Détail du client')
@section('content')
@php
$oldestOverduePayment = $client->payments()
->whereIn('status', ['non_payee', 'partiellement_payee'])
->where('invoice_status', '!=', 'draft')
->where('document_type', '!=', 'credit_note')
->whereNull('credit_note_id')
->where('due_date', '<', now()->startOfDay())
->orderBy('due_date', 'asc')
->first();
$daysOverdue = $oldestOverduePayment ? abs(now()->startOfDay()->diffInDays($oldestOverduePayment->due_date, false)) : 0;
$totalOverdue = $client->payments()
->whereIn('status', ['non_payee', 'partiellement_payee'])
->where('invoice_status', '!=', 'draft')
->where('document_type', '!=', 'credit_note')
->whereNull('credit_note_id')
->where('due_date', '<', now()->startOfDay())
->get()
->sum(function($payment) {
return $payment->net_amount - $payment->paid_amount;
});
$hasUnpaidInvoices = $client->payments()
->whereIn('status', ['non_payee', 'partiellement_payee'])
->where('invoice_status', '!=', 'draft')
->where('document_type', '!=', 'credit_note')
->whereNull('credit_note_id')
->exists();
$totalUnpaid = $client->payments()
->whereIn('status', ['non_payee', 'partiellement_payee'])
->where('invoice_status', '!=', 'draft')
->where('document_type', '!=', 'credit_note')
->whereNull('credit_note_id')
->get()
->sum(function($payment) {
return $payment->net_amount - $payment->paid_amount;
});
$unpaidNotOverdue = $totalUnpaid - $totalOverdue;
$upcomingDuePayments = $client->payments()
->whereIn('status', ['non_payee', 'partiellement_payee'])
->where('invoice_status', '!=', 'draft')
->where('document_type', '!=', 'credit_note')
->whereNull('credit_note_id')
->where('due_date', '>=', now()->startOfDay())
->where('due_date', '<=', now()->addDays(7)->endOfDay())
->get();
$totalUpcomingDue = $upcomingDuePayments->sum(function($payment) {
return $payment->net_amount - $payment->paid_amount;
});
$closestDuePayment = $upcomingDuePayments->sortBy('due_date')->first();
$daysUntilDue = $closestDuePayment ? now()->startOfDay()->diffInDays($closestDuePayment->due_date, false) : 0;
@endphp
@if($oldestOverduePayment && $daysOverdue > 0)
{{ format_currency($totalOverdue) }} TND impayés depuis {{ $daysOverdue }} jours
Facture en retard la plus ancienne : {{ $oldestOverduePayment->due_date->format('d/m/y') }}
@endif
@if($unpaidNotOverdue > 0)
{{ format_currency($totalUnpaid) }} TND à encaisser
Factures non payées ou partiellement payées
@endif
@if($closestDuePayment && $totalUpcomingDue > 0)
{{ format_currency($totalUpcomingDue) }} TND à échéance dans {{ $daysUntilDue }} jour{{ $daysUntilDue > 1 ? 's' : '' }}
Prochaine échéance : {{ $closestDuePayment->due_date->format('d/m/y') }}
@endif
@if($tab === 'overview')
@if($client->tags->count() > 0)
{{-- Tags avec limite mobile --}}
@php
$visibleTags = $client->tags->take(2);
$hiddenTagsCount = max(0, $client->tags->count() - 2);
@endphp
{{-- Icône tag --}}
@if($client->tags->count() > 0)
@endif
{{-- Tags visibles (2 max sur mobile) --}}
@foreach($visibleTags as $tag)
{{ $tag->name }}
@endforeach
{{-- Badge +N sur mobile uniquement --}}
@if($hiddenTagsCount > 0)
{{-- Tags cachés affichés inline sur mobile --}}
@foreach($client->tags->skip(2) as $tag)
{{ $tag->name }}
@endforeach
{{-- Tags cachés sur desktop --}}
@foreach($client->tags->skip(2) as $tag)
{{ $tag->name }}
@endforeach
@endif
@else
@endif
{{-- Lien Notes (X) --}}
@if($notesCount > 0)
@else
@endif
{{-- Layout Principal: 2 colonnes + sidebar --}}
{{-- Colonne principale gauche (60%) --}}
{{-- Bloc Profil Client --}}
@include('provider.clients.partials.profile-summary')
{{-- Section Situation financière --}}
Situation financière
CA Total:
{{ format_currency($kpis['total_revenue']) }} TND
Reçu:
{{ format_currency($kpis['paid_amount']) }} TND
@php
$paymentPercentage = $kpis['total_revenue'] > 0 ? round(($kpis['paid_amount'] / $kpis['total_revenue']) * 100) : 0;
@endphp
Taux de paiement
{{ $paymentPercentage }}%
@php
$lastInvoicePayment = \App\Models\InvoicePayment::whereHas('invoice', function($q) use ($client) {
$q->where('client_id', $client->id)
->where('invoice_status', '!=', 'draft');
})->with('invoice')->orderBy('id', 'desc')->first();
$overduePayments = $client->payments()->whereIn('status', ['non_payee', 'partiellement_payee'])->where('invoice_status', '!=', 'draft')->where('document_type', '!=', 'credit_note')->whereNull('credit_note_id')->where('due_date', '<', now()->startOfDay())->count();
$oldestOverduePayment = $client->payments()->whereIn('status', ['non_payee', 'partiellement_payee'])->where('invoice_status', '!=', 'draft')->where('document_type', '!=', 'credit_note')->whereNull('credit_note_id')->where('due_date', '<', now()->startOfDay())->orderBy('due_date', 'asc')->first();
$daysOverdue = $oldestOverduePayment ? now()->startOfDay()->diffInDays($oldestOverduePayment->due_date) : 0;
@endphp
Dernier paiement:
@if($lastInvoicePayment)
{{ $lastInvoicePayment->payment_date->format('d/m/y') }}
@else
Aucun
@endif
@if($overduePayments > 0)
@php
$oldestOverdue = $client->payments()
->whereIn('status', ['non_payee', 'partiellement_payee'])
->where('invoice_status', '!=', 'draft')
->where('document_type', '!=', 'credit_note')
->whereNull('credit_note_id')
->where('due_date', '<', now()->startOfDay())
->orderBy('due_date', 'asc')
->first();
$daysOverdue = $oldestOverdue ? abs(now()->startOfDay()->diffInDays($oldestOverdue->due_date, false)) : 0;
@endphp
{{ $overduePayments }} facture{{ $overduePayments > 1 ? 's' : '' }} en retard (-{{ $daysOverdue }} jour{{ $daysOverdue > 1 ? 's' : '' }})
@else
Pas de facture en retard
@endif
{{-- KPIs Compacts - 3 cartes statistiques RDV --}}
{{-- Section Informations compactée --}}
Informations
@if($client->type === 'particulier' && $client->tax_id)
CIN: {{ $client->tax_id }}
@endif
@if($client->type === 'particulier' && $client->birth_date)
{{ \Carbon\Carbon::parse($client->birth_date)->format('d/m/Y') }}
@endif
@if($client->type === 'professionnel' && $client->matricule_fiscal && $client->is_vat_subject)
MF: {{ format_matricule_ellipsis($client->matricule_fiscal) }}
{{ $client->matricule_fiscal }}
@endif
@if($client->type === 'professionnel' && $client->rib)
@endif
@if($client->email)
@endif
{{-- Statut TVA --}}
@if($client->type === 'professionnel')
{{ $client->is_vat_subject ? 'Assujetti à la TVA' : 'Non assujetti à la TVA' }}
@endif
@if($client->address)
@endif
{{-- Bouton modifier --}}
Modifier les coordonnées
{{-- Sidebar droite (40%) --}}
{{-- Section Notes compactée --}}
Notes
{{-- Message d'aide pour première note --}}
@if($notesCount === 0)
Ajoutez votre première note
@endif
{{-- Input toujours visible --}}
{{-- Liste des notes ou état vide --}}
@php
$pinnedNotes = $notes->where('is_pinned', true);
$regularNotes = $notes->where('is_pinned', false);
@endphp
@if($pinnedNotes->count() > 0)
Épinglées ({{ $pinnedNotes->count() }})
@foreach($pinnedNotes as $note)
@include('provider.clients.partials.note-card', ['note' => $note])
@endforeach
@endif
@if($regularNotes->count() > 0)
Toutes les notes ({{ $regularNotes->count() }})
@foreach($regularNotes as $note)
@include('provider.clients.partials.note-card', ['note' => $note])
@endforeach
@endif
@if($pinnedNotes->count() === 0 && $regularNotes->count() === 0)
@endif
{{-- Section Services Préférés --}}
Services préférés
@if(empty($preferredServices) || count($preferredServices) === 0)
Données insuffisantes (besoin de 3 RDV minimum)
@else
@foreach($preferredServices as $stat)
{{ $stat['service']->name }}
{{ $stat['count'] }} fois
{{ $stat['percentage'] }}% de ses visites
@endforeach
@endif
{{-- Activités récentes --}}
Activités récentes ({{ $client->activities()->count() }})
@if($recentActivities && $recentActivities->count() > 0)
@foreach($recentActivities as $activity)
@php
$activityLink = null;
if(str_contains($activity->type, 'payment') && isset($activity->metadata['payment_id'])) {
$payment = \App\Models\Payment::find($activity->metadata['payment_id']);
if($payment) {
$activityLink = route('provider.invoices.show', $payment->uuid);
}
} elseif(str_contains($activity->type, 'appointment') && isset($activity->metadata['date'])) {
$activityLink = route('provider.appointments.calendar', ['date' => \Carbon\Carbon::parse($activity->metadata['date'])->format('Y-m-d')]);
}
@endphp
@if(str_contains($activity->type, 'appointment'))
@elseif(str_contains($activity->type, 'payment'))
@else
@endif
{{ $activity->description }}
{{ $activity->created_at->format('d/m/y H:i') }}
@if($activity->metadata && (isset($activity->metadata['service']) || isset($activity->metadata['date']) || isset($activity->metadata['sessions_count']) || isset($activity->metadata['net_to_pay'])))
@if(isset($activity->metadata['sessions_count']) && $activity->metadata['sessions_count'] > 0)
{{ $activity->metadata['sessions_count'] }} séance{{ $activity->metadata['sessions_count'] > 1 ? 's' : '' }}
@endif
@if(isset($activity->metadata['net_to_pay']))
{{ format_currency($activity->metadata['net_to_pay']) }} TND
@endif
@if(isset($activity->metadata['service']))
{{ $activity->metadata['service'] }}
@endif
@endif
@endforeach
@else
Aucune activité récente
@endif
@if($recentActivities && $recentActivities->count() > 0)
Voir toutes les activités
@endif
@elseif($tab === 'appointments')
@elseif($tab === 'invoices')
@php
$invoiceStats = [
'count' => $paymentsSummary['count'],
'total' => $client->payments()
->where('invoice_status', '!=', 'draft')
->where('document_type', '!=', 'credit_note')
->where('status', '!=', 'annulee')
->whereNull('credit_note_id')
->sum('net_to_pay'),
'unpaid' => $client->payments()
->whereIn('status', ['non_payee', 'partiellement_payee'])
->where('invoice_status', '!=', 'draft')
->where('document_type', '!=', 'credit_note')
->whereNull('credit_note_id')
->get()
->sum(function($payment) {
return $payment->net_amount - $payment->paid_amount;
}),
'average' => $paymentsSummary['count'] > 0 ? $client->payments()
->where('invoice_status', '!=', 'draft')
->where('document_type', '!=', 'credit_note')
->where('status', '!=', 'annulee')
->whereNull('credit_note_id')
->avg('net_to_pay') : 0,
];
$currency = auth()->user()->provider->generalSettings['currency'] ?? 'TND';
@endphp
@elseif($tab === 'activity')
{{ $activityKpis['total'] }}
Total activités
{{ $activityKpis['appointments'] }}
Rendez-vous
{{ $activityKpis['payments'] }}
Factures
{{ $activityKpis['notes'] }}
Notes
Historique complet
@if($activities)
{{ $activities->links() }}
@endif
@endif
@endsection