@extends('layouts.app')
@section('title',$invoice->invoice_number)
@section('page_title','Customer Invoice')
@section('content')
@php($status=$invoice->status instanceof \App\Enums\DocumentStatus?$invoice->status->value:$invoice->status)
<div class="page-head"><div><h1>{{ $invoice->invoice_number }}</h1><p>{{ $invoice->party->legal_name }} · <span class="badge status-{{ $status }}">{{ ucwords(str_replace('_',' ',$status)) }}</span></p></div><div class="actions"><a class="btn btn-secondary" href="{{ route('invoices.index') }}">Back</a><a class="btn btn-secondary" target="_blank" href="{{ route('invoices.print',$invoice) }}">Print</a><a class="btn btn-secondary" href="{{ route('invoices.pdf',$invoice) }}">PDF</a>@if(in_array($status,['draft','rejected']) && auth()->user()->hasPermission('customer_invoices.edit_draft'))<a class="btn btn-primary" href="{{ route('invoices.edit',$invoice) }}">Edit</a>@endif</div></div>
<div class="grid grid-4"><div class="card metric"><div class="metric-label">Invoice Total</div><div class="metric-value">{{ $invoice->currency_code }} {{ number_format((float)$invoice->grand_total,2) }}</div></div><div class="card metric"><div class="metric-label">VAT</div><div class="metric-value">{{ number_format((float)$invoice->vat_total,2) }}</div></div><div class="card metric"><div class="metric-label">Allocated</div><div class="metric-value">{{ number_format((float)$invoice->allocated_total,2) }}</div></div><div class="card metric"><div class="metric-label">Balance Due</div><div class="metric-value">{{ number_format((float)$invoice->grand_total-(float)$invoice->allocated_total,2) }}</div></div></div>
<div class="card" style="margin-top:18px"><div class="card-head"><strong>Invoice Details</strong></div><div class="detail-grid" style="border:0;border-radius:0"><div class="detail"><div class="detail-label">Issue Date</div><div class="detail-value">{{ $invoice->issue_date->format('d M Y') }}</div></div><div class="detail"><div class="detail-label">Date of Supply</div><div class="detail-value">{{ optional($invoice->supply_date)->format('d M Y') ?: '—' }}</div></div><div class="detail"><div class="detail-label">Due Date</div><div class="detail-value">{{ optional($invoice->due_date)->format('d M Y') ?: '—' }}</div></div><div class="detail"><div class="detail-label">Customer PO</div><div class="detail-value">{{ $invoice->customer_po ?: '—' }}</div></div><div class="detail"><div class="detail-label">Branch</div><div class="detail-value">{{ $invoice->branch->name }}</div></div><div class="detail"><div class="detail-label">E-Invoice Status</div><div class="detail-value">{{ ucwords(str_replace('_',' ',$invoice->einvoice_status)) }}</div></div></div></div>
<div class="card" style="margin-top:18px"><div class="table-wrap"><table class="table"><thead><tr><th>#</th><th>Description</th><th>Qty</th><th>Rate</th><th>Discount</th><th>Taxable</th><th>VAT</th><th>Total</th></tr></thead><tbody>@foreach($invoice->items as $item)<tr><td>{{ $item->line_number }}</td><td>{{ $item->description }}</td><td>{{ number_format((float)$item->quantity,2) }} {{ $item->unit }}</td><td>{{ number_format((float)$item->unit_rate,2) }}</td><td>{{ number_format((float)$item->discount_amount,2) }}</td><td>{{ number_format((float)$item->taxable_amount,2) }}</td><td>{{ number_format((float)$item->vat_amount,2) }} <span class="muted">({{ number_format((float)$item->vat_rate,2) }}%)</span></td><td><strong>{{ number_format((float)$item->line_total,2) }}</strong></td></tr>@endforeach</tbody></table></div><div class="card-body"><div class="totals"><div class="total-row"><span>Subtotal</span><strong>{{ number_format((float)$invoice->subtotal,2) }}</strong></div><div class="total-row"><span>Discount</span><strong>{{ number_format((float)$invoice->discount_total,2) }}</strong></div><div class="total-row"><span>Taxable</span><strong>{{ number_format((float)$invoice->taxable_total,2) }}</strong></div><div class="total-row"><span>VAT</span><strong>{{ number_format((float)$invoice->vat_total,2) }}</strong></div><div class="total-row grand"><span>Total</span><strong>{{ $invoice->currency_code }} {{ number_format((float)$invoice->grand_total,2) }}</strong></div></div></div></div>
<div class="card" style="margin-top:18px"><div class="card-head"><strong>Workflow</strong></div><div class="card-body"><div class="actions">
@if(in_array($status,['draft','rejected']) && auth()->user()->hasPermission('customer_invoices.submit'))<form method="post" action="{{ route('invoices.submit',$invoice) }}">@csrf<button class="btn btn-warning">Submit for Approval</button></form>@endif
@if($status==='pending_approval' && auth()->user()->hasPermission('customer_invoices.approve'))<form method="post" action="{{ route('invoices.approve',$invoice) }}">@csrf<button class="btn btn-success">Approve</button></form>@endif
@if($status==='pending_approval' && auth()->user()->hasPermission('customer_invoices.reject'))<form method="post" action="{{ route('invoices.reject',$invoice) }}" style="display:flex;gap:8px"><input class="control" name="reason" placeholder="Rejection reason" required><button class="btn btn-danger">Reject</button></form>@endif
@if($status==='approved' && auth()->user()->hasPermission('customer_invoices.post'))<form method="post" action="{{ route('invoices.post',$invoice) }}">@csrf<button class="btn btn-primary" data-confirm="Post this invoice? Posted accounting records cannot be edited.">Post to Ledger</button></form>@endif
@if(in_array($status,['draft','rejected']) && auth()->user()->hasPermission('customer_invoices.delete_draft'))<form method="post" action="{{ route('invoices.destroy',$invoice) }}">@csrf @method('delete')<button class="btn btn-danger" data-confirm="Delete this invoice draft?">Delete Draft</button></form>@endif
</div>@if($invoice->journal)<div class="divider"></div><strong>Posted Journal: {{ $invoice->journal->journal_number }}</strong><div class="table-wrap"><table class="table"><thead><tr><th>Account</th><th>Description</th><th>Debit</th><th>Credit</th></tr></thead><tbody>@foreach($invoice->journal->lines as $line)<tr><td>{{ $line->account->code }} - {{ $line->account->name }}</td><td>{{ $line->description }}</td><td>{{ number_format((float)$line->debit,2) }}</td><td>{{ number_format((float)$line->credit,2) }}</td></tr>@endforeach</tbody></table></div>@endif</div></div>
@endsection
