@php $k = fn ($v) => number_format((float) $v, 2); $p = $s['pricing']; $b = $s['buyer']; $v = $s['vehicle']; $sel = $s['seller']; $fin = $s['finance'] ?? null; $label = \App\Models\DealDocument::TYPES[$doc->type]; @endphp
{{ $sel['company'] }}
{{ $sel['branch'] }}
{{ $sel['address'] }}
@if ($sel['phone']) Tel {{ $sel['phone'] }} @endif @if ($sel['email']) · {{ $sel['email'] }} @endif @if ($sel['kra_pin'])
KRA PIN: {{ $sel['kra_pin'] }}@endif

{{ $label }}

{{ $doc->number }}
Date: {{ $doc->issued_at->format('d M Y') }}
Deal: {{ $s['deal_no'] }} @if ($doc->valid_until)
{{ $doc->type === 'proforma' ? 'Valid until' : 'Valid until' }}: {{ $doc->valid_until->format('d M Y') }}@endif @if ($doc->etims_invoice_no)
Fiscal invoice no: {{ $doc->etims_invoice_no }}@endif
@if ($doc->is_void)@endif @if ($doc->type === 'proforma' && $doc->is_expired && ! $doc->is_void)@endif @if ($doc->type === 'gate_pass' && $doc->released_at)@endif

{{ $doc->type === 'receipt' ? 'Received from' : ($doc->type === 'gate_pass' ? 'Customer' : 'Buyer') }}

{{ $b['name'] }}@if ($b['company'])
{{ $b['company'] }}@endif
{{ $b['phone'] }}@if ($b['email']) · {{ $b['email'] }}@endif @if ($b['address'])
{{ $b['address'] }}@endif @if ($b['kra_pin'] && in_array($doc->type, ['tax_invoice', 'agreement', 'proforma']))
KRA PIN: {{ $b['kra_pin'] }}@endif @if ($b['id_last4'] && $doc->type === 'agreement')
{{ config('crm.id_types')[$b['id_type']] ?? 'ID' }}: ending {{ $b['id_last4'] }}@endif

Vehicle

{{ $v['name'] }}
Stock {{ $v['stock_id'] }} · VIN/Chassis {{ $v['vin'] }}
@if ($v['registration_no']) Reg. {{ $v['registration_no'] }} · @endif{{ $v['color'] }} · {{ $v['mileage'] }} @if ($v['engine_cc']) · {{ number_format($v['engine_cc']) }}cc @endif
{{-- ===== Price table (pro-forma, agreement, tax invoice) ===== --}} @if (in_array($doc->type, ['proforma', 'agreement', 'tax_invoice']))

{{ $doc->type === 'agreement' ? 'Price and payment' : 'Charges' }}

@if (bccomp($p['discount'], '0', 2) > 0)@endif @foreach ($p['items'] as $i)@endforeach @if (bccomp($p['tax'], '0', 2) > 0)@endif
Vehicle: {{ $v['name'] }}KSh {{ $k($p['list_price']) }}
Discount− {{ $k($p['discount']) }}
{{ $i['description'] }}{{ $i['quantity'] > 1 ? ' × '.$i['quantity'] : '' }}{{ $k($i['line_total']) }}
{{ $p['tax_name'] ?? 'Tax' }} {{ rtrim(rtrim($p['tax_rate'], '0'), '.') }}% {{ $p['tax_inclusive'] ? '(included in the prices above)' : '(added)' }}{{ $k($p['tax']) }}
TOTALKSh {{ $k($p['total']) }}
@if ($s['trade_in'])

Trade-in: {{ $s['trade_in']['description'] }}@if ($s['trade_in']['registration_no']) ({{ $s['trade_in']['registration_no'] }})@endif, grade {{ $s['trade_in']['grade'] }}, valued at KSh {{ $k($s['trade_in']['offered']) }}@if (bccomp($s['trade_in']['payoff'], '0', 2) > 0), less loan KSh {{ $k($s['trade_in']['payoff']) }} to {{ $s['trade_in']['lender'] ?: 'the lender' }}@endif. Credit applied: KSh {{ $k($p['trade_in_credit']) }}.

@endif @if (in_array($doc->type, ['proforma', 'agreement'])) @if (bccomp($p['trade_in_credit'], '0', 2) != 0)@endif @if ($fin) @else @endif
Amount due after trade-inKSh {{ $k($p['due_after_trade_in']) }}
DepositKSh {{ $k($p['deposit']) }}
Amount financed{{ $fin['financier'] ? ' by '.$fin['financier'] : '' }}KSh {{ $k($fin['principal']) }}
Monthly instalment ({{ $fin['term'] }} months, {{ rtrim(rtrim($fin['rate'], '0'), '.') }}% {{ $fin['method'] === 'flat' ? 'flat' : 'reducing balance' }}; true yearly cost {{ $fin['effective_rate'] }}%)KSh {{ $k($fin['installment']) }}
Total interest / total to repay{{ $k($fin['total_interest']) }} / {{ $k($fin['total_repayable']) }}
Balance payable on or before collectionKSh {{ $k(bcsub($p['due_after_trade_in'], $p['deposit'], 2)) }}
@endif @if ($doc->type === 'proforma')

This pro-forma invoice is a quotation, not a tax invoice. The price is held until the date above. The vehicle is released only after full payment and a gate pass.

@endif @if ($doc->type === 'tax_invoice')

Payment received in full. Vehicle details as above. Goods remain the property of the seller until payment has cleared.

@endif @endif {{-- ===== Agreement terms and signatures ===== --}} @if ($doc->type === 'agreement')

{{ $s['agreement']['title'] }}

Version {{ $s['agreement']['version'] }}. Between {{ $sel['company'] }} ("the Seller") and {{ $b['name'] }} ("the Buyer").

    @foreach ($s['agreement']['clauses'] as $c)
  1. {{ $c }}
  2. @endforeach

Signatures

@foreach (['buyer' => 'Buyer', 'seller' => 'For the Seller'] as $role => $rl) @php $sig = $doc->signatures->firstWhere('role', $role); @endphp @endforeach
@if ($sig && isset($images[$role])){{ $rl }} signature@endif
{{ $rl }}: {{ $sig?->signer_name ?? '________________' }}@if ($sig?->signer_title), {{ $sig->signer_title }}@endif
{{ $sig ? 'Signed '.$sig->signed_at->format('d M Y, H:i').' · ref '.substr($sig->hash, 0, 12) : 'Not yet signed' }}
@if ($sig = $doc->signatures->firstWhere('role', 'witness'))

Witness: {{ $sig->signer_name }}, {{ $sig->signed_at->format('d M Y, H:i') }}

@endif @if ($doc->is_executed)

Signed by both parties. Document reference {{ substr($doc->snapshot_hash, 0, 16) }}.

@endif @endif {{-- ===== Receipt ===== --}} @if ($doc->type === 'receipt') @php $pay = $s['payment']; @endphp

Payment received

@if ($pay['reference'])@endif @if (bccomp($pay['vat_portion'], '0', 2) > 0)@endif
Amount receivedKSh {{ $k($pay['amount']) }}
Paid by{{ $pay['method'] }}{{ $pay['payer'] === 'financier' ? ' (loan paid by lender)' : '' }}
Reference{{ $pay['reference'] }}
Date received{{ \Illuminate\Support\Carbon::parse($pay['received_on'])->format('d M Y') }}
Of which taxKSh {{ $k($pay['vat_portion']) }}
Total price of the dealKSh {{ $k($p['total']) }}
Balance after this payment{{ bccomp($pay['balance_after'], '0', 2) <= 0 ? 'Paid in full' : 'KSh '.$k($pay['balance_after']) }}

This receipt confirms payment received. A tax invoice is issued when the deal is paid in full.

@endif {{-- ===== Gate pass ===== --}} @if ($doc->type === 'gate_pass') @php $g = $s['gate_pass']; @endphp

Authority to release vehicle

The vehicle {{ $v['name'] }}, registration {{ $v['registration_no'] ?: 'not yet registered' }}, chassis {{ $v['vin'] }}, may leave the premises with:

{{ $g['collector_name'] }}
ID ending {{ $g['collector_id_last4'] }}. The guard must check the original ID.

Valid until {{ $doc->valid_until->format('d M Y') }}. Payment status: {{ bccomp($g['balance_outstanding'], '0', 2) > 0 ? 'BALANCE OUTSTANDING KSh '.$k($g['balance_outstanding']).' (released on '.($g['override_reason'] ?: 'management authority').')' : 'Paid in full' }}.

@if ($qr){!! $qr !!}@endif
{{ $doc->number }}

Scan to check this pass

Collector's signature
Gate officer ({{ $doc->released_at ? $doc->released_at->format('d M Y, H:i') : 'date and time' }})
@endif @includeIf('documents._fiscal', ['doc' => $doc])

Issued by {{ $doc->issuer?->name ?? 'the dealership' }}{{ $s['salesperson'] ? ' · Salesperson: '.$s['salesperson'] : '' }} · {{ $doc->number }}