@extends('layouts.portal') @section('title', 'Invoices') @section('page_title', 'Invoices') @section('page_subtitle', 'Manage client invoices and payments') @section('sidebar') @include('admin.partials.sidebar', ['active' => 'invoices']) @endsection @section('content')

Ready for Invoice

@php $invoicedWillIds = \App\Models\Invoice::whereNotNull('will_id')->pluck('will_id')->toArray(); $willsReadyForInvoice = \App\Models\Will::where('status', 'completed') ->whereNotNull('package_id') ->whereNotIn('id', $invoicedWillIds) ->count(); @endphp
{{ $willsReadyForInvoice }}

Completed wills pending invoicing

Total Invoices

{{ \App\Models\Invoice::count() }}

All invoices

Pending Payment

{{ \App\Models\Invoice::where('status', 'pending')->count() }}

Awaiting payment

Paid

{{ \App\Models\Invoice::where('status', 'paid')->count() }}

Completed

Total Revenue

@php $paidAmount = \App\Models\Invoice::where('status', 'paid')->sum('total_amount'); @endphp
RM{{ number_format($paidAmount, 2) }}

From paid invoices


Invoice Records

View and manage invoice payments

Create Invoice for Will or Trust

@php // Get invoiced will/trust IDs $invoicedWillIds = \App\Models\Invoice::whereNotNull('will_id')->pluck('will_id')->toArray(); // Get wills that don't have invoices yet AND are completed with package_id $wills = \App\Models\Will::with('client.kycDocuments', 'package') ->where('status', 'completed') ->where('will_type', '!=', 'trust') // Exclude trusts from will type ->whereNotNull('package_id') ->whereNotIn('id', $invoicedWillIds) ->latest() ->get(); // Get trusts from the TRUSTS table (not from wills table with will_type=trust) // Trusts can be invoiced at any status (drafting, completed, approved) as long as they have a package $trusts = \App\Models\Trust::with('client', 'package') ->whereNotNull('package_id') ->whereNotIn('id', $invoicedWillIds) // Exclude already-invoiced trusts ->latest() ->get(); // Get trusts WITHOUT packages (need package assignment first) $trustsNeedingPackage = \App\Models\Trust::with('client', 'package') ->whereNull('package_id') ->whereNotIn('id', $invoicedWillIds) ->latest() ->get(); // Combine both into one array with type indicator $documents = []; foreach ($wills as $will) { $documents[] = (object)[ 'id' => $will->id, 'type' => 'will', 'document_type' => 'Will', 'client' => $will->client, 'package' => $will->package, 'status' => $will->status ]; } foreach ($trusts as $trust) { $documents[] = (object)[ 'id' => $trust->id, 'type' => 'trust', 'document_type' => 'Trust', 'client' => $trust->client, 'package' => $trust->package, 'status' => $trust->status ]; } @endphp @if(count($trustsNeedingPackage) > 0)
Trusts Pending Package Assignment

The following trusts need a package assigned before they can be invoiced:

@foreach($trustsNeedingPackage as $trust)

{{ $trust->client ? $trust->client->full_name : 'Unknown Client' }}

Trust ID: {{ $trust->id }} | Status: {{ ucfirst($trust->status) }}

@endforeach
@endif @if(count($documents) > 0)
@foreach($documents as $doc) @endforeach
Document # Type Client Status Package Price Action
#{{ $doc->id }} {{ $doc->document_type }}

{{ $doc->client ? $doc->client->full_name : 'Unknown Client' }}

{{ $doc->client ? $doc->client->email : 'No email' }}

{{ ucfirst($doc->status) }} {{ $doc->package?->name ?? 'N/A' }} @if($doc->package) RM{{ number_format($doc->package->price, 2) }} @else N/A @endif @if($doc->type === 'will') Create Invoice @elseif($doc->type === 'trust') Create Invoice @endif
@else

All wills and trusts with packages are invoiced or pending approval.

Ready to invoice count: {{ $willsReadyForInvoice }}

@endif

Existing Invoices

@if($invoices->count() > 0)
@foreach($invoices as $invoice) @endforeach
Invoice # Client Type Amount Status Due Date Action
{{ $invoice->invoice_number }}

{{ $invoice->client ? $invoice->client->full_name : 'Unknown Client' }}

{{ $invoice->client ? $invoice->client->email : 'No email' }}

{{ App\Models\Invoice::getTypes()[$invoice->type] }} RM{{ number_format($invoice->total_amount, 2) }} @if($invoice->discount > 0)

Disc: -RM{{ number_format($invoice->discount, 2) }}

@endif
{{ App\Models\Invoice::getStatuses()[$invoice->status] }} {{ $invoice->due_date->format('d M Y') }}
@if($invoice->status === 'pending') @else Paid @endif
{{ $invoices->count() }} invoice(s) displayed
Pending: {{ $invoices->where('status', 'pending')->count() }}
Paid: {{ $invoices->where('status', 'paid')->count() }}
Total: RM{{ number_format($invoices->sum('total_amount'), 2) }}
@if($invoices->count() > 0)
Showing {{ $invoices->firstItem() ?? 0 }} to {{ $invoices->lastItem() ?? 0 }} of {{ $invoices->total() }} results
@if($invoices->hasPages())
{{-- Previous Page Link --}} @if($invoices->onFirstPage()) @else Previous @endif {{-- Page Numbers --}} @php $start = max(1, $invoices->currentPage() - 2); $end = min($invoices->lastPage(), $invoices->currentPage() + 2); @endphp @if($start > 1) ... @endif @for($i = $start; $i <= $end; $i++) @if($i == $invoices->currentPage()) {{ $i }} @else {{ $i }} @endif @endfor @if($end < $invoices->lastPage()) ... @endif {{-- Next Page Link --}} @if($invoices->hasMorePages()) Next @else @endif
@endif
@endif @else

No invoices found.

@endif
@endsection