@php // Safe data extraction with defaults $store = $data['store'] ?? [ 'name' => config('app.name', 'Kenyan Supermarket'), 'address' => config('app.address', 'Nairobi CBD'), 'phone' => config('app.phone', '0700 000 000'), 'pin' => config('app.pin', 'P051234567N'), 'vat_number' => config('app.vat_number', 'VAT001234567'), ]; $customer = $data['customer'] ?? []; $items = $data['items'] ?? []; $totals = $data['totals'] ?? []; $payment = $data['payment'] ?? []; $vatInfo = $data['vat_info'] ?? []; // Customer VAT status handling $customerName = $customer['name'] ?? 'Walk-in Customer'; $customerStatus = $customer['vat_status'] ?? 'vatable'; $isExempted = $customerStatus === 'exempted'; $isZeroRated = $customerStatus === 'zero_rated'; $isVatable = !$isExempted && !$isZeroRated; // Safe totals $subtotal = $totals['subtotal'] ?? 0; $discount = $totals['discount'] ?? 0; $vatAmount = $totals['vat_amount'] ?? $totals['tax_total'] ?? 0; $grandTotal = $totals['grand_total'] ?? 0; // VAT breakdown and summary amounts - IMPORTANT: Using original structure $vatBreakdown = $vatInfo['breakdown'] ?? []; if (!is_array($vatBreakdown)) { $vatBreakdown = []; } // Original VAT summary fields from first receipt $taxableAmount = $totals['taxable_amount'] ?? 0; $zeroRatedAmount = $totals['zero_rated_amount'] ?? 0; $exemptedAmount = $totals['exempted_amount'] ?? 0; $nonTaxableAmount = $totals['non_taxable_amount'] ?? 0; // Payment data $paymentMethod = $payment['method'] ?? 'cash'; $amountPaid = $payment['amount_paid'] ?? $grandTotal; $change = $payment['change'] ?? 0; $transactionId = $payment['transaction_id'] ?? null; $mpesaDetails = $payment['mpesa_details'] ?? null; // Notes $notes = $data['notes'] ?? [ 'Thank you for your business!', 'Items sold are not returnable unless defective', 'Please check goods before leaving the counter', 'Valid for 7 days from date of purchase', 'Keep this receipt for warranty claims', 'VAT Invoice - Valid for Tax Purposes' ]; @endphp
{{ strtoupper($store['name']) }}
{{ $store['address'] ?? '' }}
Tel: {{ $store['phone'] ?? '' }}
@if(!empty($store['pin']))
PIN: {{ $store['pin'] }}
@endif @if(!empty($store['vat_number']))
VAT: {{ $store['vat_number'] }}
@endif
Receipt No: {{ $data['invoice_no'] ?? $data['receipt_no'] ?? 'N/A' }}
Date: {{ $data['date'] ?? date('Y-m-d') }}
Time: {{ $data['time'] ?? date('H:i:s') }}
Cashier: {{ $data['cashier'] ?? 'Cashier' }}
Customer: {{ $customerName }}
@if(!empty($customer['phone']))
Phone: {{ $customer['phone'] }}
@endif @if(!empty($customer['vat_number']))
VAT No: {{ $customer['vat_number'] }}
@endif
VAT Status: {{ $isExempted ? 'EXEMPTED' : ($isZeroRated ? 'ZERO-RATED' : 'VATABLE') }}
@if($customerName === 'Walk-in Customer')
Customer: Walk-in Customer
@endif
ITEMS
ITEM
QTY
PRICE
TOTAL
@if(count($items) > 0) @foreach($items as $item) @php $quantity = $item['quantity'] ?? 1; $unitPrice = $item['price'] ?? $item['unit_price'] ?? 0; $itemTotal = $unitPrice * $quantity; $taxRate = $item['tax_rate'] ?? 0; $isItemVatable = $item['is_vatable'] ?? ($taxRate > 0); @endphp
{{ $item['name'] ?? 'Product' }}
{{ $quantity }}
{{ number_format($unitPrice, 2) }}
{{ number_format($itemTotal, 2) }}
@if($isItemVatable && $taxRate > 0 && ($item['tax_amount'] ?? 0) > 0)
VAT {{ $taxRate }}% KES {{ number_format($item['tax_amount'] ?? 0, 2) }}
@endif @endforeach @else
No items
0.00
@endif
TOTALS
Subtotal: KES {{ number_format($subtotal, 2) }}
@if($discount > 0)
Discount: -KES {{ number_format($discount, 2) }}
@endif @if(!$isExempted && !empty($vatBreakdown))
VAT BREAKDOWN Amount
@foreach($vatBreakdown as $details) @php // Safely get values with defaults $rate = $details['rate'] ?? 0; $taxableAmt = $details['taxable_amount'] ?? $details['taxableAmount'] ?? 0; $vatAmt = $details['vat_amount'] ?? $details['amount'] ?? 0; $type = $details['type'] ?? 'standard'; @endphp @if($taxableAmt > 0 && $rate >= 0)
@if($type === 'zero_rated') Zero-Rated (0%) @else VAT {{ $rate }}% @endif on KES {{ number_format($taxableAmt, 2) }}: KES {{ number_format($vatAmt, 2) }}
@endif @endforeach @if($vatAmount > 0)
Total VAT: KES {{ number_format($vatAmount, 2) }}
@endif
@endif @if($taxableAmount > 0 || $zeroRatedAmount > 0 || $exemptedAmount > 0 || $nonTaxableAmount > 0)
@if($taxableAmount > 0)
Taxable Amount: KES {{ number_format($taxableAmount, 2) }}
@endif @if($zeroRatedAmount > 0)
Zero-Rated Amount: KES {{ number_format($zeroRatedAmount, 2) }}
@endif @if($exemptedAmount > 0)
Exempted Amount: KES {{ number_format($exemptedAmount, 2) }}
@endif @if($nonTaxableAmount > 0)
Non-Taxable Amount: KES {{ number_format($nonTaxableAmount, 2) }}
@endif
@endif @if($vatAmount > 0 && !$isExempted)
VAT Amount: KES {{ number_format($vatAmount, 2) }}
@endif
TOTAL: KES {{ number_format($grandTotal, 2) }}
@if(isset($data['payment']))
PAYMENT
{{ strtoupper($paymentMethod) }}
Amount Paid: KES {{ number_format($amountPaid, 2) }}
@if($paymentMethod === 'cash' && isset($payment['cash_received']))
Cash Received: KES {{ number_format($payment['cash_received'], 2) }}
@endif @if($change > 0)
Change: KES {{ number_format($change, 2) }}
@endif @if($transactionId)
Transaction ID: {{ $transactionId }}
@endif @if($mpesaDetails)
Phone: {{ $mpesaDetails['phone'] ?? '' }}
@if(!empty($mpesaDetails['receipt_no']))
M-Pesa Receipt: {{ $mpesaDetails['receipt_no'] }}
@endif @endif
@endif @if($isExempted || $isZeroRated)
VAT INFORMATION
@if($isExempted)
VAT EXEMPTED CUSTOMER
No VAT Applied
@elseif($isZeroRated)
ZERO-RATED CUSTOMER
0% VAT Rate Applied
@endif
@endif
@if($data['invoice_no'] ?? false)
Receipt No: {{ $data['invoice_no'] }}
@endif