@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
| 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. | ||||