@php $allowedTemplates = ['classic', 'modern', 'bold', 'simple', 'formal']; $template = in_array($settings['template'] ?? '', $allowedTemplates, true) ? $settings['template'] : 'classic'; $brandColor = $settings['brand_color'] ?? '#1D4ED8'; $footerText = $settings['footer_text'] ?? null; $showContact = (bool) ($settings['show_footer_contact'] ?? false); $showTerms = (bool) ($settings['show_footer_terms'] ?? false); $hex = ltrim((string) $brandColor, '#'); if (strlen($hex) !== 6 || ! ctype_xdigit($hex)) { $hex = '1D4ED8'; $brandColor = '#1D4ED8'; } else { $brandColor = '#'.strtoupper($hex); } $r = hexdec(substr($hex, 0, 2)); $g = hexdec(substr($hex, 2, 2)); $b = hexdec(substr($hex, 4, 2)); $brandRgb = "{$r}, {$g}, {$b}"; $type = $invoice['type'] ?? 'simplified'; $typeVal = ($type instanceof \BackedEnum) ? $type->value : (string) $type; $typeLabels = [ 'standard' => ['ar' => 'فاتورة ضريبية', 'en' => 'Tax Invoice'], 'simplified' => ['ar' => 'فاتورة ضريبية مبسطة', 'en' => 'Simplified Tax Invoice'], 'credit_note' => ['ar' => 'إشعار دائن', 'en' => 'Credit Note'], 'debit_note' => ['ar' => 'إشعار مدين', 'en' => 'Debit Note'], ]; $typeAr = $typeLabels[$typeVal]['ar'] ?? 'فاتورة ضريبية'; $typeEn = $typeLabels[$typeVal]['en'] ?? 'Tax Invoice'; $formatDate = function ($value) { if (! filled($value)) { return '-'; } try { return \Illuminate\Support\Carbon::parse($value)->format('Y/m/d'); } catch (\Throwable) { return (string) $value; } }; $lines = $invoice['lines'] ?? []; $hasDiscount = collect($lines)->sum(fn ($line) => (float) ($line['discount'] ?? 0)) > 0; $subtotal = 0.0; $totalVat = 0.0; $computedLines = []; foreach ($lines as $i => $line) { $qty = (float) ($line['quantity'] ?? 1); $price = (float) ($line['unit_price'] ?? $line['unitPrice'] ?? 0); $disc = (float) ($line['discount'] ?? 0); $vatRate = (float) ($line['vat_rate'] ?? $line['vatRate'] ?? 15); $lineSub = round(($qty * $price) - $disc, 2); $lineVat = round($lineSub * ($vatRate / 100), 2); $subtotal += $lineSub; $totalVat += $lineVat; $computedLines[] = [ 'index' => $i + 1, 'name' => $line['name'] ?? '-', 'qty' => $qty, 'price' => $price, 'discount' => $disc, 'vat_rate' => $vatRate, 'vat' => $lineVat, 'subtotal' => $lineSub, ]; } $grandTotal = $subtotal + $totalVat; $currency = $invoice['currency'] ?? 'SAR'; $seller = $invoice['seller'] ?? []; $sellerAddr = $seller['address'] ?? []; $sellerAddrParts = array_filter([ $sellerAddr['street'] ?? $sellerAddr['streetName'] ?? null, $sellerAddr['building'] ?? $sellerAddr['buildingNumber'] ?? $sellerAddr['building_number'] ?? null, $sellerAddr['district'] ?? null, $sellerAddr['city'] ?? null, ]); $buyer = $invoice['buyer'] ?? []; $buyerVat = $buyer['vat_number'] ?? $buyer['vatNumber'] ?? null; $buyerCr = $buyer['cr_number'] ?? $buyer['crNumber'] ?? null; $buyerAddr = $buyer['address'] ?? []; $buyerAddrParts = array_filter([ $buyerAddr['street'] ?? $buyerAddr['streetName'] ?? null, $buyerAddr['city'] ?? null, ]); $contactParts = array_filter([ $seller['email'] ?? $tenant?->email, $seller['phone'] ?? $tenant?->phone, ]); $colspan = $hasDiscount ? 5 : 4; @endphp
@if($template === 'bold')
@include('invoices.partials.pdf-header', ['formatDate' => $formatDate, 'typeAr' => $typeAr, 'typeEn' => $typeEn])
@elseif($template === 'modern')
@include('invoices.partials.pdf-header', ['formatDate' => $formatDate, 'typeAr' => $typeAr, 'typeEn' => $typeEn, 'compact' => true])
@else @include('invoices.partials.pdf-header', ['formatDate' => $formatDate, 'typeAr' => $typeAr, 'typeEn' => $typeEn]) @endif
المشتري
{{ $buyer['name'] ?? 'عميل نقدي' }}
@if($buyerVat)
VAT {{ $buyerVat }}
@endif @if($buyerCr)
CR {{ $buyerCr }}
@endif @if($buyerAddrParts)
{{ implode('، ', $buyerAddrParts) }}
@endif
المورد
{{ $seller['name'] ?? 'اسم المنشأة' }}
@if(!empty($seller['vatNumber']) || !empty($seller['crNumber']))
@if(!empty($seller['vatNumber']))VAT {{ $seller['vatNumber'] }}@endif @if(!empty($seller['vatNumber']) && !empty($seller['crNumber'])) - @endif @if(!empty($seller['crNumber']))CR {{ $seller['crNumber'] }}@endif
@endif @if($sellerAddrParts)
{{ implode('، ', $sellerAddrParts) }}
@endif @if(!empty($sellerAddr['postalCode']) || !empty($sellerAddr['postal_code']))
{{ $sellerAddr['postalCode'] ?? $sellerAddr['postal_code'] }} {{ $sellerAddr['city'] ?? '' }} SA
@endif
@if($hasDiscount) @endif @forelse($computedLines as $line) @if($hasDiscount) @endif @empty @endforelse
الإجماليالخصمالسعر الكمية الوصف
{{ number_format($line['subtotal'], 2) }}{{ number_format($line['discount'], 2) }}{{ number_format($line['price'], 2) }} {{ rtrim(rtrim(number_format($line['qty'], 3, '.', ''), '0'), '.') }} {{ $line['name'] }}
لا توجد بنود
@php $vatGroups = collect($computedLines)->groupBy('vat_rate'); @endphp @foreach($vatGroups as $rate => $groupLines) @endforeach
قبل الضريبة {{ number_format($subtotal, 2) }}
الضريبة {{ rtrim(rtrim(number_format((float) $rate, 2, '.', ''), '0'), '.') }}% {{ number_format($groupLines->sum('vat'), 2) }}
الإجمالي {{ number_format($grandTotal, 2) }} {{ $currency }}
@if(!empty($invoice['reason']))

السبب: {{ $invoice['reason'] }}

@endif @if(!empty($invoice['original_invoice_number']))

الفاتورة الأصلية: {{ $invoice['original_invoice_number'] }}

@endif