@extends('layouts.provider') @section('title', 'Détail de la facture') @section('page-title', 'Finances > Détail de la facture') @section('content')
@if(!$invoice->isCancelledInvoice()) @if($invoice->is_overdue && $invoice->remaining_amount > 0)

{{ format_currency($invoice->remaining_amount) }} TND impayés depuis {{ $invoice->days_overdue }} jours

Échéance dépassée : {{ $invoice->due_date->format('d/m/y') }}

@elseif($invoice->due_date && $invoice->due_date >= now()->startOfDay() && $invoice->due_date <= now()->addDays(7)->endOfDay() && $invoice->remaining_amount > 0)

{{ format_currency($invoice->remaining_amount) }} TND à échéance dans {{ now()->startOfDay()->diffInDays($invoice->due_date, false) }} jour{{ now()->startOfDay()->diffInDays($invoice->due_date, false) > 1 ? 's' : '' }}

Échéance : {{ $invoice->due_date->format('d/m/y') }}

@elseif($invoice->remaining_amount > 0 && !$invoice->isDraft())

{{ format_currency($invoice->remaining_amount) }} TND à encaisser

{{ $invoice->paid_amount > 0 ? 'Partiellement payée' : 'Facture non payée' }}{{ $invoice->due_date ? ' - Échéance : ' . $invoice->due_date->format('d/m/y') : '' }}

@endif @endif

{{ $invoice->invoice_number ?? $invoice->reference }}

@if($invoice->isCancelledInvoice()) Annulée @elseif($invoice->isDraft()) Brouillon @else @endif
@if($invoice->isDraft())
@endif
@if($invoice->client->type === 'professionnel') @else @endif

{{ $invoice->client->full_name ?? 'N/A' }}

Faite: {{ $invoice->payment_date->format('d/m/y') }} @if($invoice->due_date) · Échéance: {{ $invoice->due_date->format('d/m/y') }} @endif
@if($invoice->notes)

{{ $invoice->notes }}

@endif
@php $hasEditAction = true; // Toujours autoriser l'édition // Conditions pour afficher le bouton "Créer un avoir" $canCreateCreditNote = ( ($invoice->isOpen() || $invoice->isInvoicePaid() || $invoice->status === 'partiellement_payee') && $invoice->amount > 0 // Pas d'avoir sur avoir (avoir = montant négatif) // TODO: Ajouter condition montant total avoirs < montant total facture ); $hasDeleteAction = $invoice->canBeModified(); @endphp @if($hasDeleteAction) @endif @if($canCreateCreditNote) @endif

Net à payer

{{ format_currency($invoice->net_amount) }} {{ $invoice->currency }}

Payé

{{ format_currency($invoice->paid_amount) }} {{ $invoice->currency }}

Reste

{{ format_currency($invoice->remaining_amount) }} {{ $invoice->currency }}

@if($invoice->isCreditNote())

Facture annulée liée

@if($invoice->cancelledInvoice)

{{ $invoice->cancelledInvoice->invoice_number }}

Annulée le {{ $invoice->payment_date->format('d/m/Y') }}

{{ format_currency($invoice->cancelledInvoice->net_amount) }} {{ $invoice->cancelledInvoice->currency }}

@else

Aucune facture annulée liée

@endif
@endif @if(!$invoice->isCreditNote()) @if($invoice->isCancelledInvoice())

Facture d'avoir liée

@if($invoice->creditNote)

{{ $invoice->creditNote->invoice_number }}

Créé le {{ $invoice->creditNote->payment_date->format('d/m/Y') }}

{{ format_currency($invoice->creditNote->net_amount) }} {{ $invoice->creditNote->currency }}

@else

Aucune facture d'avoir liée

@endif
@else
@php $withholdingCount = $invoice->invoicePayments->filter(fn($p) => $p->payment_method === 'retenue_source')->count(); $normalPaymentsCount = $invoice->invoicePayments->filter(function($p) { return $p->payment_method !== 'retenue_source'; })->count(); @endphp

Paiements ({{ $normalPaymentsCount + $withholdingCount }})

@if($invoice->remaining_amount > 0 && !$invoice->isDraft()) @endif
@if($invoice->isDraft())

Finalisez la facture pour pouvoir enregistrer des paiements.

@elseif($normalPaymentsCount > 0 || $withholdingCount > 0)
Payé: {{ format_currency($invoice->paid_amount) }} / {{ format_currency($invoice->net_amount) }} {{ $invoice->currency }}
@php $paymentNumber = 0; $paymentNumbers = []; foreach($invoice->invoicePayments->sortBy('id') as $p) { $paymentNumber++; $paymentNumbers[$p->id] = $paymentNumber; } @endphp @foreach($invoice->invoicePayments->sortByDesc('id') as $payment)
#{{ str_pad($paymentNumbers[$payment->id] ?? 0, 2, '0', STR_PAD_LEFT) }} - {{ $payment->payment_date->format('d/m/Y') }} @if($payment->isReversed()) (Annulé) @endif
@if($payment->payment_method === 'retenue_source') {{ $payment->notes ?? 'Retenue' }} @elseif($payment->isReversal()) @php $reversedPaymentId = $payment->reversal_of_payment_id; $reversedPaymentNumber = str_pad($paymentNumbers[$reversedPaymentId] ?? 0, 2, '0', STR_PAD_LEFT); @endphp Annulation · #{{ $reversedPaymentNumber }} · {{ $payment->reversal_reason }} @else @switch($payment->payment_method) @case('cash') Espèces @break @case('bank_transfer') Virement @break @case('check') Chèque @break @case('card') Carte @break @endswitch @if($payment->treasuryAccount) · {{ $payment->treasuryAccount->name }} @endif @if($payment->reference) · Réf: {{ $payment->reference }} @endif @endif
{{ $payment->amount < 0 || $payment->payment_method === 'retenue_source' ? '-' : '' }}{{ format_currency(abs($payment->amount)) }}
@if($payment->canBeReversed() && $payment->payment_method !== 'retenue_source') @endif
@endforeach
@else

Aucun paiement

@endif
@endif @endif

Aperçu de la facture

@if(auth()->user()->provider->logo) Logo @endif

{{ auth()->user()->provider->legal_name ?? auth()->user()->provider->business_name }}

@if(auth()->user()->provider->tax_id)

Matricule fiscal : {{ auth()->user()->provider->tax_id }}

@endif @if(auth()->user()->provider->address)

{{ auth()->user()->provider->address }}

@endif @if(auth()->user()->provider->city || auth()->user()->provider->country)

{{ auth()->user()->provider->city }}{{ auth()->user()->provider->city && auth()->user()->provider->country ? ', ' : '' }}{{ auth()->user()->provider->country }}

@endif

FACTURE @if($invoice->isCreditNote())
D'AVOIR @elseif($invoice->isCancelledInvoice())
ANNULÉE @endif

Client

{{ $invoice->client->full_name }}

@if($invoice->client->is_vat_subject && $invoice->client->matricule_fiscal)

Matricule fiscal : {{ $invoice->client->matricule_fiscal }}

@endif @if($invoice->client->phone)

Tél : {{ $invoice->client->phone }}

@endif @if($invoice->client->email)

Email : {{ $invoice->client->email }}

@endif @if($invoice->client->address)

Adresse : {{ $invoice->client->address }}

@endif

N° Facture : {{ $invoice->invoice_number ?? $invoice->reference }}

Date : {{ $invoice->payment_date->format('d/m/Y') }}

@if($invoice->due_date)

Échéance : {{ $invoice->due_date->format('d/m/Y') }}

@endif
@if($invoice->invoiceItems && $invoice->invoiceItems->count() > 0) @foreach($invoice->invoiceItems as $item) @endforeach @elseif($invoice->appointments && $invoice->appointments->count() > 0) @foreach($invoice->appointments as $appointment) @php // price_at_booking est le prix HT $priceHT = $appointment->price_at_booking; $vatAmount = $appointment->vat_rate > 0 ? ($priceHT * $appointment->vat_rate / 100) : 0; $priceTTC = round($priceHT + $vatAmount, 2); @endphp @endforeach @else @endif
Description Quantité HT ({{ $invoice->currency }}) % TVA TVA ({{ $invoice->currency }}) Montant TTC ({{ $invoice->currency }})
@if($item->appointment_id)
{{ $item->description }}
{{ $item->service_date->format('d/m/Y') }}
{{ $item->service_time }}
@else
{{ $item->description }}
{{ $item->service_date->format('d/m/Y') }}
@if($item->service_time)
{{ $item->service_time }}
@endif @endif
{{ $item->quantity }} {{ format_currency($item->unit_price) }} {{ number_format($item->vat_rate, 0) }}% {{ format_currency($item->vat_amount) }} {{ format_currency($item->total_ttc) }}
{{ $appointment->service->name ?? 'Service' }}
{{ $appointment->start_time->format('d/m/Y') }}
{{ $appointment->start_time->format('H:i') }} - {{ $appointment->end_time ? $appointment->end_time->format('H:i') : $appointment->start_time->addHour()->format('H:i') }}
@php $duration = $appointment->end_time ? $appointment->start_time->diffInMinutes($appointment->end_time) : 60; $hours = floor($duration / 60); $minutes = $duration % 60; @endphp {{ $hours }}h{{ $minutes > 0 ? str_pad($minutes, 2, '0', STR_PAD_LEFT) : '' }} {{ format_currency($priceHT) }} {{ number_format($appointment->vat_rate, 0) }}% {{ format_currency($vatAmount) }} {{ format_currency($priceTTC) }}
Prestation de service - {{ format_currency($invoice->amount) }} 0% 0,00 {{ format_currency($invoice->amount) }}
@php // Calculer le total HT, TVA et TTC depuis les lignes du tableau $totalHT = 0; $totalVAT = 0; $totalTTC = 0; $vatByRate = []; if($invoice->invoiceItems && $invoice->invoiceItems->count() > 0) { foreach($invoice->invoiceItems as $item) { $totalHT += $item->unit_price; $totalVAT += $item->vat_amount; $totalTTC += $item->total_ttc; // Grouper par taux de TVA $rate = $item->vat_rate; if (!isset($vatByRate[$rate])) { $vatByRate[$rate] = ['base' => 0, 'amount' => 0]; } $vatByRate[$rate]['base'] += $item->unit_price; $vatByRate[$rate]['amount'] += $item->vat_amount; } } elseif($invoice->appointments && $invoice->appointments->count() > 0) { foreach($invoice->appointments as $appointment) { // price_at_booking est le prix HT $priceHT = $appointment->price_at_booking; $vatAmount = $appointment->vat_rate > 0 ? ($priceHT * $appointment->vat_rate / 100) : 0; $priceTTC = round($priceHT + $vatAmount, 2); $totalHT += $priceHT; $totalVAT += $vatAmount; $totalTTC += $priceTTC; // Grouper par taux de TVA $rate = $appointment->vat_rate; if (!isset($vatByRate[$rate])) { $vatByRate[$rate] = ['base' => 0, 'amount' => 0]; } $vatByRate[$rate]['base'] += $priceHT; $vatByRate[$rate]['amount'] += $vatAmount; } } else { $totalHT = $invoice->amount; $totalTTC = $invoice->amount; } $vatTax = $invoice->invoiceTaxes->where('is_vat_snapshot', true)->first(); $taxesBeforeVat = $invoice->invoiceTaxes->where('is_vat_snapshot', false)->where('apply_before_vat_snapshot', true); $withholdingTaxes = $invoice->invoiceTaxes->filter(fn($t) => $t->sign_snapshot === '-' && !$t->is_vat_snapshot && !$t->apply_before_vat_snapshot); $taxesAfterVat = $invoice->invoiceTaxes->filter(fn($t) => !$t->is_vat_snapshot && !$t->apply_before_vat_snapshot && !$withholdingTaxes->contains('id', $t->id)); @endphp
@if($totalVAT != 0)

Détail TVA

@foreach($vatByRate as $rate => $data) @endforeach
Taux Base HT Montant TVA
{{ number_format($rate, 0) }}% {{ format_currency($data['base']) }} {{ format_currency($data['amount']) }}
Total {{ format_currency($totalHT) }} {{ format_currency($totalVAT) }}
@endif

Totaux

@foreach($taxesBeforeVat as $tax) @endforeach @foreach($withholdingTaxes as $tax) @endforeach @foreach($taxesAfterVat as $tax) @endforeach
Total HT {{ format_currency($totalHT) }}
Total TVA {{ format_currency($totalVAT) }}
{{ $tax->label_snapshot ?? ucfirst(str_replace('_', ' ', $tax->name_snapshot)) }} @if($tax->type_snapshot === 'percent')({{ number_format($tax->value_snapshot, 2) }}%)@endif {{ $tax->sign_snapshot }}{{ format_currency($tax->amount_snapshot) }}
Total TTC {{ format_currency($invoice->total_ttc) }}
{{ $tax->label_snapshot ?? ucfirst(str_replace('_', ' ', $tax->name_snapshot)) }} @if($tax->type_snapshot === 'percent')({{ number_format($tax->value_snapshot, 2) }}%)@endif {{ $tax->sign_snapshot }}{{ format_currency($tax->amount_snapshot) }}
{{ $tax->label_snapshot ?? ucfirst(str_replace('_', ' ', $tax->name_snapshot)) }} @if($tax->type_snapshot === 'percent')({{ number_format($tax->value_snapshot, 2) }}%)@endif {{ $tax->sign_snapshot === '-' ? '-' : '' }}{{ format_currency($tax->amount_snapshot) }}
NET À PAYER {{ format_currency($invoice->net_amount) }}

Arrête la présente facture à la somme de :

{{ \App\Helpers\NumberToWordsHelper::convert($invoice->net_amount) }}

@php $paymentMethods = $invoice->invoicePayments ->where('payment_method', '!=', 'retenue_source') ->pluck('payment_method') ->unique() ->map(fn($method) => match($method) { 'cash' => 'Espèces', 'bank_transfer' => 'Virement', 'check' => 'Chèque', 'card' => 'Carte', default => ucfirst($method) }) ->join(', '); @endphp @if(auth()->user()->provider->defaultBankAccount)

Coordonnées bancaires

Banque : {{ auth()->user()->provider->defaultBankAccount->name }}@if(auth()->user()->provider->defaultBankAccount->reference) · Référence : {{ auth()->user()->provider->defaultBankAccount->reference }}@endif

@endif @if(auth()->user()->provider->billing_settings['invoice_footer'] ?? null)
{!! nl2br(e(auth()->user()->provider->billing_settings['invoice_footer'])) !!}
@endif

Facture générée le {{ now()->format('d/m/Y à H:i') }}

{{ config('app.name') }} - Tous droits réservés

@php $modalIndex = 0; @endphp @foreach($invoice->invoicePayments->sortByDesc('id')->where('payment_method', '!=', 'retenue_source') as $payment) @if($payment->canBeReversed()) @php $modalIndex++; @endphp @endif @endforeach @endsection