@php use Modules\BookKeeping\Models\DocumentLine; // Excel-derived greens $green1 = '#297983'; $green2 = '#298C9C'; $green3 = '#4AC4D6'; $stampColour = $document->status->getColor(); // Parent lines with children (included) and profit for VAT/discount on pro/misc $parentLines ??= ( $document->relationLoaded('lines') ? $document->lines : $document->lines() ->whereNull(DocumentLine::PARENT_LINE_ID) ->with([ 'children' => fn($query) => $query->where(DocumentLine::INCLUDED, 1), 'profit', ]) ->get() ); $currency = "{$document->currency_symbol} "; $serviceTotals = []; $proMiscTotals = [ 'quantity' => 0, 'subtotal' => 0, 'vat' => 0, 'total' => 0, 'discount' => 0, ]; // Try find the service that represents professional/misc fees (via DB list first, config fallback) $proMiscServiceId = $services->first(function ($service) { $name = strtolower($service->service_name ?? ''); return str_contains($name, 'professional') || str_contains($name, 'misc'); })?->service_id; $proMiscConfigId = collect(config('items_services', [])) ->filter(function ($service) { $name = strtolower($service['name'] ?? ''); return str_contains($name, 'professional') || str_contains($name, 'misc'); }) ->keys() ->first(); $proMiscServiceId ??= $proMiscConfigId; // Build totals: parent rows use child/parent prices only; VAT/discount handled on pro/misc fee foreach ($parentLines as $line) { $serviceId = $line->service_id; $profit = $line->profit; $childrenTotal = $line->children->sum(DocumentLine::PRICE); $parentPrice = $line->price ?? 0; $lineSubtotal = $parentPrice + $childrenTotal; if ($serviceId !== null) { $serviceTotals[$serviceId] ??= [ 'service_id' => $serviceId, 'quantity' => 0, 'subtotal' => 0, 'vat' => 0, 'total' => 0, ]; $serviceTotals[$serviceId]['quantity'] += 1; $serviceTotals[$serviceId]['subtotal'] += $lineSubtotal; $serviceTotals[$serviceId]['total'] += $lineSubtotal; // no VAT on parent items } // Professional + miscellaneous fees live on the parent profit record $proFee = $profit?->pro_fee ?? 0; $miscFee = $profit?->misc_fee ?? 0; $discount = $profit?->discount ?? 0; $taxTotal = $profit?->tax_total ?? 0; $proMiscSubtotal = $proFee + $miscFee - $discount; if ($proMiscSubtotal !== 0.0 || $taxTotal !== 0.0) { //Keep pro/misc quantity as 1 //This prevents the document from displaying a quantity greater than 1 for pro/misc fees $proMiscTotals['quantity'] = 1; $proMiscTotals['subtotal'] += $proMiscSubtotal; $proMiscTotals['discount'] += $discount; $proMiscTotals['vat'] += $taxTotal; $proMiscTotals['total'] += $proMiscSubtotal + $taxTotal; } } $services = $services instanceof \Illuminate\Support\Collection ? $services : collect($services); $serviceItems = []; foreach ($services as $service) { $totals = $serviceTotals[$service->service_id] ?? [ 'service_id' => $service->service_id, 'quantity' => 0, 'subtotal' => 0, 'vat' => 0, 'total' => 0, ]; if ($proMiscServiceId && $service->service_id === $proMiscServiceId) { $totals['quantity'] += $proMiscTotals['quantity']; $totals['subtotal'] += $proMiscTotals['subtotal']; $totals['vat'] += $proMiscTotals['vat']; $totals['total'] += $proMiscTotals['total']; // Clear so we don't append a duplicate fallback row later $proMiscTotals = [ 'quantity' => 0, 'subtotal' => 0, 'vat' => 0, 'total' => 0, 'discount' => 0, ]; } $serviceItems[] = [ 'name' => $service->service_name, 'quantity' => $totals['quantity'], 'subtotal' => $totals['subtotal'], 'vat' => $totals['vat'], 'total' => $totals['total'], ]; } $serviceItems[] = [ 'name' => 'Professional & Miscellaneous Fees', 'quantity' => $proMiscTotals['quantity'], 'subtotal' => $proMiscTotals['subtotal'], 'vat' => $proMiscTotals['vat'], 'total' => $proMiscTotals['total'], ]; @endphp {{ $document->document_type->value . ' ' . $document->number }} {{-- ========================= SCREEN VIEW (single page) ========================= --}}
{{-- Decorative GREEN SHARDS background --}}
{{-- Header --}}
Company Logo
{{ $document->issuer_display_name }}
Licensing & Compliance Services
Cell
{{ $document->user_primary_cell }}
Email
{{ $document->user_primary_email }}
{{-- Stamp centered between left tagline and right document details (screen only) --}} @if($stampColour)
{{ $document->status }}
@endif

{{ $document->document_type->value }}

{{ $document->status }}
{{ $document->number_ui_string }}
{{ $document->number }}
{{ $document->date_ui_string }}
{{ $document?->date ?? 'To Be Set' }}
{{-- Bill To / Business --}}

Bill To

Client
{{ $document->bill_to_trading_as }}
Reg No
{{ $document->bill_to_reg_number ?? '-' }}
Trading As
{{ $document->bill_to_trading_as }}
VAT No
{{ $document->bill_to_vat_number ?? 'To Be Advised' }}
Street
{{ $document->bill_to_street }}
Town/City
{{ $document->bill_to_town }}
Code
{{ $document->bill_to_postal_code }}

Contact

Executive
{{ $document->bill_to_contact_full_name }}
Cell
{{ $document->bill_to_contact_primary_cell }}
Email
{{ $document->bill_to_contact_primary_email }}
{{-- Services--}}
{{-- Description (flex) --}} {{-- Quantity --}} {{-- Sub --}} {{-- VAT 15% --}} {{-- Total --}} @forelse($serviceItems as $line) @php $qty = (float) ($line['quantity'] ?? 1); $sub = $line['subtotal']; $vat = $line['vat']; $total = $line['total']; @endphp @empty @endforelse
Description Quantity Sub VAT {{ config('globalSettings.tax_rate') }}% Total
{{ $line['name'] ?? '' }}
@if(!empty($line['note']))
{{ $line['note'] }}
@endif
{{ number_format($qty, 2) }} {{ $currency . number_format($sub, 2, '.', ' ') }} {{ $currency . number_format($vat, 2, '.', ' ') }} {{ $currency . number_format($total, 2, '.', ' ') }}
No items added.
{{-- Banking + Totals (SCREEN) --}} @php $discount = $document->discount_total; @endphp
{{-- Banking --}}

Banking & Payment Information

Account Name
{{ $document->issuer_bank_account_holder}}
Bank
{{ $document->issuer_bank_name }}
Account No
{{ $document->issuer_bank_account_number ?? '' }}
Branch Code
{{ $document->issuer_bank_branch_code }}
Type
{{ $document->issuer_bank_account_type }}
Terms
{{ $document->term_content }}
{{-- Totals --}}
Sub-Total{{ $currency . number_format($document->subtotal, 2, '.', ' ') }}
Discount{{ $currency . number_format($document->discount_total, 2, '.', ' ') }}
VAT {{ config('globalSettings.tax_rate') * 100 }} %{{ $currency . number_format($document->tax_total, 2, '.', ' ') }}
@if ($document->document_type === \Modules\BookKeeping\Enums\DocumentType::INVOICE)
Paid {{ $currency . number_format($document->paid, 2, '.', ' ') }}
@endif
@if ($document->document_type === \Modules\BookKeeping\Enums\DocumentType::INVOICE)
Total Due {{ $currency . number_format($document->balance - $document->paid, 2, '.', ' ') }}
@elseif ($document->document_type === \Modules\BookKeeping\Enums\DocumentType::QUOTE)
Total Due {{ $currency . number_format($document->total, 2, '.', ' ') }}
@endif @if(!empty($document->date_due))
{{$document->date_ui_string . ' ' . $document->date_due }}
@endif
{{-- Screen footer / print button --}}