@extends('dashboard.layouts.app') @section('content') @include('components.alert') @php $statusClass = match ($invoice->status) { 'paid' => 'status-pill--success', 'partial' => 'status-pill--warning', 'cancelled' => 'status-pill--danger', default => 'status-pill--muted', }; $canPay = in_array($invoice->status, ['unpaid', 'partial'], true) && (float) $invoice->remaining_amount > 0; $totalAmount = (float) ($invoice->total ?? 0); $paidAmount = (float) ($invoice->paid_amount ?? 0); $remainingAmount = (float) ($invoice->remaining_amount ?? 0); $paidPercent = $totalAmount > 0 ? min(100, ($paidAmount / $totalAmount) * 100) : 0; $purchasePartyType = $invoice->purchase?->party_type ?? null; // 'student', 'staff', 'company', null $purchaseInvoiceType = $invoice->purchase?->invoice_type ?? null; // 'sale' or 'purchase' // A "sales invoice" that should be redirected to the student flow = sale order for a student only. // Staff/company sale orders are paid through the normal purchase payment flow. $isSalesInvoice = $purchaseInvoiceType === 'sale' && $purchasePartyType === 'student'; $isSaleOrder = $purchaseInvoiceType === 'sale'; // any sale order (staff or student) $invoiceTitle = $isSaleOrder ? 'Sales Invoice' : 'Purchase Invoice'; $orderLabel = $isSaleOrder ? 'Sales Order' : 'Purchase Order'; $partyLabel = match(true) { $purchasePartyType === 'staff' => 'Staff', $purchasePartyType === 'student' => 'Student', default => 'Vendor', }; $partyName = match(true) { $purchasePartyType === 'student' => ($invoice->purchase?->student?->full_name ?? $invoice->purchase?->name ?? $invoice->vendor?->name ?? 'Party'), $purchasePartyType === 'staff' => ($invoice->purchase?->name ?? 'Staff'), default => ($invoice->vendor?->name ?? $invoice->purchase?->vendors?->first()?->name ?? 'Vendor'), }; $canPay = !$isSalesInvoice && $canPay; @endphp

Invoice Details

{{ $isSalesInvoice ? 'Sales invoice posting records receivable and revenue.' : 'Invoice posting creates payable only. Payments move treasury balance.' }}

{{ ucwords(str_replace('_', ' ', $invoice->status)) }}
{{ $orderLabel }} {{ $invoice->purchase?->purchase_number ?? '--' }}
{{ $partyLabel }} {{ $partyName ?: '--' }}
{{ $isSalesInvoice ? 'Invoice Reference' : 'Vendor Invoice No.' }} {{ $invoice->vendor_invoice_number ?: '--' }}
Invoice Date {{ optional($invoice->invoice_date)->format('M d, Y') ?: '--' }}
Due Date {{ optional($invoice->due_date)->format('M d, Y') ?: '--' }}
Journal Entry {{ $invoice->invoiceJournalEntry?->reference_no ?? '--' }}
@forelse($invoice->purchase?->details ?? [] as $detail) @empty @endforelse
# Item Qty Price Amount Tax
{{ $loop->iteration }} {{ $detail->product_name ?: '--' }} {{ $detail->quantity }} {{ number_format((float) $detail->price, 2) }} {{ number_format((float) $detail->amount, 2) }} {{ number_format((float) $detail->tax_rate, 2) }}%
No items found.

Payments

{{ $isSalesInvoice ? 'Payments linked to this sales invoice.' : 'Every payment creates a treasury OUT transaction and a payment journal entry.' }}

@php $paymentRows = $invoice->payments->map(fn ($payment) => ['payment' => $payment, 'amount' => $payment->amount]) ->concat($invoice->paymentAllocations->filter(fn ($allocation) => $allocation->payment) ->map(fn ($allocation) => ['payment' => $allocation->payment, 'amount' => ($allocation->is_reversal ? -1 : 1) * (float) $allocation->allocated_amount])); @endphp @forelse($paymentRows as $paymentRow) @php $payment = $paymentRow['payment']; @endphp @empty @endforelse
# Date Treasury Amount Journal Entry Notes
{{ $loop->iteration }} {{ optional($payment->payment_date)->format('M d, Y') ?: '--' }} {{ $payment->treasury?->name ?? '--' }} {{ number_format((float) $paymentRow['amount'], 2) }} {{ $payment->journalEntry?->reference_no ?? '--' }} {{ $payment->notes ?: '--' }}
No payments recorded yet.

Balance

Payment progress for this invoice.

Total {{ number_format($totalAmount, 2) }}
Paid {{ number_format($paidAmount, 2) }}
Remaining {{ number_format($remainingAmount, 2) }}
@if($canPay)

Record Payment

Choose treasury and amount. This is the cash movement step.

@csrf
@error('payment_date')
{{ $message }}
@enderror
@error('treasury_id')
{{ $message }}
@enderror
@error('amount')
{{ $message }}
@enderror
@error('notes')
{{ $message }}
@enderror
@else

{{ $isSalesInvoice ? 'Sales Payment' : 'Payment Complete' }}

{{ $isSalesInvoice ? 'Use the sales payment flow to collect this invoice.' : 'This purchase invoice has no remaining balance.' }}

{{ $isSalesInvoice ? 'Sales invoice' : 'Paid in full' }}
@endif
@endsection