<?php
    $isNonTax = $sale->invoice_type === 'non_tax_invoice';
    $invoiceTitle = $isNonTax ? 'INVOICE' : ($sale->invoice_type === 'simplified_tax_invoice' ? 'SIMPLIFIED TAX INVOICE' : 'TAX INVOICE');
    $invoiceTitleAr = $isNonTax ? 'فاتورة' : ($sale->invoice_type === 'simplified_tax_invoice' ? 'فاتورة ضريبية مبسطة' : 'فاتورة ضريبية');
    $blankRows = max(0, 10 - $sale->items->count());
?>
<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"><title><?php echo e($sale->invoice_no); ?></title><style>
*{box-sizing:border-box}body{margin:0;background:#e9ede9;color:#17251c;font-family:"DejaVu Sans",Arial,sans-serif;font-size:8px}.toolbar{width:196mm;margin:10px auto 0;text-align:right}.toolbar a,.toolbar button{border:0;border-radius:4px;padding:7px 12px;margin-left:5px;background:#1d6842;color:#fff;text-decoration:none;font:600 11px Arial}.toolbar a{background:#fff;color:#1d6842}.sheet{width:196mm;min-height:283mm;margin:7px auto;background:#fff;border:1.5px solid #1d6842;padding:3mm;box-shadow:0 7px 24px rgba(20,55,34,.16)}table{width:100%;border-collapse:collapse}.top td{background:#1d6842;color:#fff;padding:7px 10px;vertical-align:middle}.company-en{width:52%}.company-en h1{margin:0;font-size:21px;line-height:1.05;letter-spacing:.02em}.company-en div{margin-top:2px;font-size:8.5px;letter-spacing:.05em}.company-ar{width:48%;direction:rtl;text-align:right}.company-ar h2{margin:0;font-size:19px;line-height:1.2}.company-ar div{margin-top:2px;font-size:8.5px}.header{margin-top:2px}.header>tbody>tr>td{border:1px solid #347451;vertical-align:top;padding:4px 6px}.header .side{width:36%}.header .document{width:28%;padding:0;text-align:center}.kv{min-height:18px;padding:3px 1px;border-bottom:1px solid #b3c6b9}.kv:last-child{border-bottom:0}.kv b{color:#245e3e;margin-right:4px}.kv span{font-weight:600;margin-right:4px}.kv em{direction:rtl;color:#245e3e;font-style:normal;font-weight:bold}.invoice-title{padding:6px 3px;border-bottom:1px solid #7a9c86;color:#1d6842}.invoice-title strong{display:block;font-size:12px;letter-spacing:.04em}.invoice-title b{display:block;direction:rtl;margin-top:2px;font-size:10px}.docline{padding:4px 6px;border-bottom:1px solid #b3c6b9;text-align:left}.docline:last-child{border-bottom:0}.docline b{color:#245e3e;margin-right:6px}.docline span{font-weight:bold}.customer-name{margin-top:2px}.customer-name td{border:1px solid #347451;padding:5px 7px;font-size:9.5px}.customer-name b{color:#245e3e}.customer-name .ar{direction:rtl;text-align:right}.customer{margin-top:-1px}.customer td{border:1px solid #71977f;padding:3px 5px;height:20px}.customer .label{width:12%;color:#245e3e;font-weight:bold}.customer .value{width:28%;font-weight:600}.customer .ar-label{width:10%;direction:rtl;text-align:right;color:#245e3e;font-weight:bold}.items{margin-top:2px;page-break-inside:auto}.items thead{display:table-header-group}.items th{background:#1d6842;color:#fff;border:1px solid #174e33;padding:4px 2px;text-align:center;font-size:7px;line-height:1.15}.items th small{display:block;direction:rtl;margin-top:2px;font-size:6.7px}.items td{border:1px solid #71977f;height:28px;padding:3px;vertical-align:middle}.items tr{page-break-inside:avoid}.items .center{text-align:center}.items .num{text-align:right;white-space:nowrap}.description strong{display:block;font-size:8px}.description small{display:block;direction:rtl;text-align:right;color:#355c44;margin-top:2px;font-size:7px}.blank td{height:25px;color:transparent}.summary td{border:1px solid #347451;border-top:0;padding:4px 6px;font-weight:bold}.summary .qty-label{background:#e2eee6;color:#245e3e;text-align:right}.summary .num{text-align:right}.settlement{margin-top:2px}.settlement>tbody>tr>td{border:1px solid #347451;vertical-align:top}.words-cell{width:72%;padding:0!important}.words{min-height:43px;padding:5px 7px;border-bottom:1px solid #7a9c86;line-height:1.45}.words b{color:#245e3e}.words .ar{direction:rtl;text-align:right}.signature-strip{height:42px;padding:8px 5px 3px;text-align:center;color:#245e3e;word-spacing:15px}.qr-standalone{text-align:center;padding:4px}.qr-standalone svg{width:23mm;height:23mm}.totals-cell{width:28%;padding:0!important}.total{padding:4px 5px;border-bottom:1px solid #71977f;min-height:20px}.total:last-child{border-bottom:0}.total b{color:#245e3e;margin-right:5px}.total span{font-weight:bold}.total.grand{background:#1d6842;color:#fff;font-size:10px}.total.grand b{color:#fff}.terms{margin-top:2px;border:1px solid #347451;padding:4px 7px;text-align:center;color:#315a42;font-size:7px}.terms .ar{direction:rtl}.bank{margin-top:2px}.bank td{width:50%;background:#1d6842;color:#fff;padding:5px 8px;border-right:1px solid rgba(255,255,255,.5)}.bank td:last-child{border-right:0}.bank strong{display:block}.contact{background:#174e33;color:#fff;padding:3px 7px;text-align:center;font-size:7px}@media print{body{background:#fff}.toolbar{display:none}.sheet{width:auto;min-height:auto;margin:0;padding:0;border:0;box-shadow:none}}
.header td{height:22px}.header .label{width:14%;background:#e2eee6;color:#245e3e;font-weight:bold;text-align:center}.header .document{width:28%;text-align:center;line-height:1.65;color:#245e3e}.header .document strong{font-size:12px}.header .document hr{border:0;border-top:1px solid #7a9c86;margin:4px 0}
.settlement td{border:1px solid #347451;padding:4px 5px}.settlement .words-flat{width:72%;vertical-align:top;line-height:1.65}.settlement .words-flat b{color:#245e3e}.settlement .tlabel{width:17%;background:#e2eee6;color:#245e3e;font-weight:bold}.settlement .tvalue{width:11%;text-align:right;font-weight:bold}.settlement .grand-row td{background:#1d6842;color:#fff;font-size:10px}
.toolbar{display:none}.sheet{width:auto;min-height:0;margin:0;padding:0;border:0;box-shadow:none}@media screen{body{background:#e9ede9}.toolbar{display:block;width:196mm}.sheet{width:196mm;min-height:283mm;margin:7px auto;background:#fff;border:1.5px solid #1d6842;padding:3mm;box-shadow:0 7px 24px rgba(20,55,34,.16)}}
</style></head><body>
<div class="toolbar"><a href="<?php echo e(route('sales.show',$sale)); ?>">Back</a><button onclick="window.print()">Print invoice</button></div>
<main class="sheet">
<table class="top"><tr><td class="company-en"><h1><?php echo e(strtoupper($company->company_name_en)); ?></h1><div><?php echo e(strtoupper($company->trade_name ?: 'TRADING COMPANY')); ?></div><?php if($company->email): ?><div><?php echo e($company->email); ?></div><?php endif; ?></td><td class="company-ar"><h2><?php echo e($company->company_name_ar); ?></h2><?php if($company->email): ?><div><?php echo e($company->email); ?></div><?php endif; ?></td></tr></table>
<table class="header">
<tr><?php if(!$isNonTax): ?><td class="label">VAT No.<br>الرقم الضريبي</td><td><?php echo e($company->vat_number); ?></td><?php else: ?><td class="label">C.R No.<br>رقم السجل</td><td><?php echo e($company->cr_number?:'—'); ?></td><?php endif; ?><td class="document" rowspan="4"><strong><?php echo e($invoiceTitle); ?></strong><br><?php echo e($invoiceTitleAr); ?><hr><b>Inv. No.</b> <?php echo e($sale->invoice_no); ?><br><b>Date</b> <?php echo e($sale->invoice_date->format('d-m-Y')); ?><br><b>Payment</b> <?php echo e(strtoupper($sale->payment_type)); ?></td><td class="label">City<br>المدينة</td><td><?php echo e($company->city_en?:'—'); ?></td></tr>
<tr><td class="label">C.R No.<br>رقم السجل</td><td><?php echo e($company->cr_number?:'—'); ?></td><td class="label">Street<br>الشارع</td><td><?php echo e($company->street_name_en?:'—'); ?></td></tr>
<tr><td class="label">Building No.<br>رقم المبنى</td><td><?php echo e($company->building_number?:'—'); ?></td><td class="label">District<br>الحي</td><td><?php echo e($company->district_en?:'—'); ?></td></tr>
<tr><td class="label">Phone<br>الهاتف</td><td><?php echo e($company->phone?:$company->mobile?:'—'); ?></td><td class="label">Country<br>الدولة</td><td><?php echo e($company->country?:'Saudi Arabia'); ?></td></tr>
</table>
<table class="customer-name"><tr><td><b>Customer Name:</b> <?php echo e($sale->customer->name_en); ?></td><td class="ar"><b>اسم العميل:</b> <?php echo e($sale->customer->name_ar); ?></td></tr></table>
<table class="customer">
<tr><td class="label">City</td><td class="value"><?php echo e($sale->customer->city_en?:'—'); ?></td><td class="ar-label">المدينة</td><?php if(!$isNonTax): ?><td class="label">VAT No.</td><td class="value"><?php echo e($sale->customer->vat_number?:'—'); ?></td><td class="ar-label">الرقم الضريبي</td><?php else: ?><td class="label">Customer ID</td><td class="value"><?php echo e($sale->customer->customer_ref?:$sale->customer->customer_code); ?></td><td class="ar-label">رقم العميل</td><?php endif; ?></tr>
<tr><td class="label">Country</td><td class="value"><?php echo e($sale->customer->country?:'Saudi Arabia'); ?></td><td class="ar-label">الدولة</td><td class="label">C.R No.</td><td class="value"><?php echo e($sale->customer->cr_number?:'—'); ?></td><td class="ar-label">رقم السجل</td></tr>
<tr><td class="label">Contact No.</td><td class="value"><?php echo e($sale->customer->mobile?:$sale->customer->phone?:'—'); ?></td><td class="ar-label">رقم الاتصال</td><td class="label">Address</td><td class="value"><?php echo e(trim(($sale->customer->building_number?$sale->customer->building_number.' ':'').($sale->customer->street_en?:'').($sale->customer->district_en?', '.$sale->customer->district_en:''))?:'—'); ?></td><td class="ar-label">العنوان</td></tr>
</table>
<table class="items"><thead><tr><th style="width:4%">S.No.<small>الرقم</small></th><th style="width:7%">SKU<small>رمز الصنف</small></th><th style="width:<?php echo e($isNonTax?'44%':'29%'); ?>">Description<small>البيان</small></th><th style="width:6%">Qty<small>الكمية</small></th><th style="width:6%">Unit<small>الوحدة</small></th><th style="width:10%">Unit Price<small>سعر الوحدة</small></th><th style="width:10%">Amount<small>المبلغ</small></th><?php if(!$isNonTax): ?><th style="width:5%">VAT %<small>الضريبة</small></th><th style="width:8%">VAT<small>قيمة الضريبة</small></th><?php endif; ?><th style="width:10%">Total<small>الإجمالي</small></th></tr></thead><tbody>
<?php $__currentLoopData = $sale->items; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $i=>$item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><tr><td class="center"><?php echo e($i+1); ?></td><td class="center"><?php echo e($item->product?->product_code?:$item->product_id); ?></td><td class="description"><strong><?php echo e($item->product_name_en_snapshot); ?></strong><small><?php echo e($item->product_name_ar_snapshot); ?></small></td><td class="num"><?php echo e(number_format((float)$item->quantity,2)); ?></td><td class="center"><?php echo e($item->unit_snapshot); ?></td><td class="num"><?php echo e(number_format((float)$item->unit_price,2)); ?></td><td class="num"><?php echo e(number_format((float)$item->taxable_amount,2)); ?></td><?php if(!$isNonTax): ?><td class="num"><?php echo e(number_format((float)$item->vat_rate_snapshot,0)); ?></td><td class="num"><?php echo e(number_format((float)$item->vat_amount,2)); ?></td><?php endif; ?><td class="num"><b><?php echo e(number_format((float)$item->line_total,2)); ?></b></td></tr><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
<?php for($i=0;$i<$blankRows;$i++): ?><tr class="blank"><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><?php if(!$isNonTax): ?><td>&nbsp;</td><td>&nbsp;</td><?php endif; ?><td>&nbsp;</td></tr><?php endfor; ?>
</tbody></table>
<table class="summary"><tr><td style="width:52%">Outstanding Balance: <?php echo e(number_format((float)$sale->balance_total,2)); ?></td><td class="qty-label" style="width:30%">Total Qty. / الكمية الإجمالية</td><td class="num" style="width:18%"><?php echo e(number_format((float)$sale->items->sum('quantity'),2)); ?></td></tr></table>
<table class="settlement">
<tr><td class="words-flat" rowspan="<?php echo e($isNonTax ? 5 : 6); ?>"><b>Amount in words:</b> <?php echo e($amountInWordsEn); ?><br><span dir="rtl"><?php echo e($amountInWordsAr); ?></span><br><br><b>Received By / المستلم</b>&nbsp;&nbsp;&nbsp;&nbsp; <b>Prepared By / أعد بواسطة</b>&nbsp;&nbsp;&nbsp;&nbsp; <b>Authorized Signature / التوقيع</b></td><td class="tlabel">Total Amount</td><td class="tvalue"><?php echo e(number_format((float)$sale->gross_total,2)); ?></td></tr>
<tr><td class="tlabel">Discount</td><td class="tvalue"><?php echo e(number_format((float)$sale->discount_total,2)); ?></td></tr>
<?php if(!$isNonTax): ?><tr><td class="tlabel">VAT Amount</td><td class="tvalue"><?php echo e(number_format((float)$sale->vat_total,2)); ?></td></tr><?php endif; ?>
<tr class="grand-row"><td class="tlabel">Grand Total</td><td class="tvalue"><?php echo e(number_format((float)$sale->grand_total,2)); ?></td></tr>
<tr><td class="tlabel">Paid</td><td class="tvalue"><?php echo e(number_format((float)$sale->paid_total,2)); ?></td></tr>
<tr><td class="tlabel">Balance</td><td class="tvalue"><?php echo e(number_format((float)$sale->balance_total,2)); ?></td></tr>
</table><?php if(!$isNonTax): ?><div class="qr-standalone"><?php echo $qrSvg; ?></div><?php endif; ?>
<div class="terms"><b>Note:</b> <?php echo e($company->terms_conditions_en?:'Goods once sold cannot be returned after 7 days.'); ?><div class="ar"><?php echo e($company->terms_conditions_ar); ?></div></div>
<table class="bank"><tr><td><strong><?php echo e($company->bank_name?:'Bank Details'); ?></strong>A/C: <?php echo e($company->account_number?:'—'); ?> &nbsp; | &nbsp; IBAN: <?php echo e($company->iban?:'—'); ?></td><td><strong><?php echo e($company->bank_account_name?:$company->company_name_en); ?></strong><?php echo e($company->city_en); ?>, Saudi Arabia</td></tr></table><div class="contact"><?php echo e($company->district_en); ?> · <?php echo e($company->city_en); ?> · K.S.A. <?php if($company->mobile): ?> · Mob: <?php echo e($company->mobile); ?> <?php endif; ?> <?php if($company->phone): ?> · Tel: <?php echo e($company->phone); ?> <?php endif; ?></div>
</main></body></html>
<?php /**PATH C:\xampp\htdocs\saudi\resources\views/invoices/sale_a4.blade.php ENDPATH**/ ?>