@php $branch = $purchase->branch ?? null; $company = $branch->company ?? null; /* ---------- COMPANY / BRANCH ---------- */ $companyName = $settings->company_name ?? ($company->name ?? ''); $companyAddress = $settings->address ?? ($company->address ?? ''); $companyPhone = $settings->phone ?? ($company->phone ?? ''); $companyGstin = $settings->gst_no ?? ''; $bankName = $settings->bank_name ?? ''; $accountNo = $settings->account_no ?? ''; $ifscCode = $settings->ifsc_code ?? ''; /* ---------- SUPPLIER (the party this bill is raised against) ---------- */ $supplier = $purchase->supplier ?? null; $supplierName = $supplier->supplier_name ?? ''; $supplierAddress = $supplier->address ?? ''; $supplierGstin = $supplier->gstin ?? ''; /* Legacy records store placeholders like "N/A" where a value was unknown. Nothing missing should print on a bill, so blank them out. */ $blank = function ($value) { $v = trim((string) $value); return in_array(strtoupper($v), ['N/A', 'NA', '-', 'NULL']) ? '' : $v; }; $supplierName = $blank($supplierName); $supplierAddress = $blank($supplierAddress); $supplierGstin = $blank($supplierGstin); $billTo = trim($supplierName . ($supplierAddress ? ', ' . $supplierAddress : '')); /* ---------- BILL HEAD ---------- */ $billNo = $purchase->purchase_code ?? ''; $billDate = !empty($purchase->purchase_date) ? \Carbon\Carbon::parse($purchase->purchase_date)->format('d-m-Y') : ''; /* ---------- NUMBER FORMATTER (drops trailing zeros, like the printed book) ---------- */ $num = function ($value, $decimals = 2) { $formatted = rtrim(rtrim(number_format((float) $value, $decimals, '.', ''), '0'), '.'); return $formatted === '' || $formatted === '-' ? '0' : $formatted; }; /* Money values carry the rupee symbol and always show 2 decimals (200 -> 200.00). Quantities and tax rates keep using $num, which trims trailing zeros. */ $money = function ($value, $decimals = 2) { return '₹' . number_format((float) $value, $decimals, '.', ''); }; /* ---------- LINE ITEMS + TAX BUCKETS ---------- */ $items = $purchase->productDetails ?? collect(); $itemCount = count($items); $rows = []; $subTotal = 0; $totalTax = 0; $sgst = ['rate' => 0, 'amount' => 0]; $cgst = ['rate' => 0, 'amount' => 0]; $igst = ['rate' => 0, 'amount' => 0]; foreach ($items as $item) { $qty = (float) ($item->qty ?? 0); $rate = (float) ($item->margin_price ?? 0); $lineTotal = $item->total_amount !== null ? (float) $item->total_amount : $qty * $rate; $rows[] = [ 'name' => isset($settings->native_name_flag) && $settings->native_name_flag == 'A' ? $item->native_name ?? $item->name : $item->name ?? '', 'hsn' => $item->product->hsn_code ?? '', 'qty' => $qty, 'rate' => $rate, 'amount' => $lineTotal, ]; $subTotal += $lineTotal; } $billAmount = $purchase->total_amount ?? $subTotal + $totalTax; /* Paid comes from the payment ledger so later payments show here too; refunds and losses are netted off. Falls back to the stored column. */ $ledger = \App\Models\ProductPaymentModel::where('purchase_id', $purchase->id)->get(); $paidTillNow = $ledger->isNotEmpty() ? $ledger->whereNotIn('type', ['refund', 'loss'])->sum('paid_amount') - $ledger->whereIn('type', ['refund', 'loss'])->sum('paid_amount') : (float) ($purchase->paid_amount ?? 0); $balanceDue = max($billAmount - $paidTillNow, 0); $amountWords = ucfirst(amountToWords($billAmount)) . ' only'; /* Filler keeps the body the same depth as the printed stationery */ $fillerHeight = max(240 - $itemCount * 30, 30); @endphp {{-- ACTION BUTTONS (screen only) --}}
Back
{{-- HEADER --}}
GSTIN : {{ $companyGstin }}
PURCHASE BILL
CELL : {{ $companyPhone }}
{{ strtoupper($companyName) }}
{{ $companyAddress }}
{{-- BILL NUMBER AND DATE --}}
No : {{ $billNo }} Date : {{ $billDate }}
{{-- SUPPLIER DETAILS --}}
From
{{ $billTo }}
GSTIN . {{ $supplierGstin }}
{{-- PRODUCT TABLE --}} @foreach ($rows as $index => $row) @endforeach {{-- Empty Rows --}}
S.
NO.
PARTICULARS HSN
CODE
QTY RATE AMOUNT
{{ $index + 1 }} {{ $row['name'] }} {{ $row['hsn'] }} {{ $num($row['qty'], 3) }} {{ $money($row['rate']) }} {{ $money($row['amount']) }}
{{-- AMOUNT AND TOTAL --}}
Rupees : {{ $amountWords }}
 
RECEIVER'S SIGNATURE
Total {{ $money($subTotal) }}
SGST - {{ $sgst['rate'] > 0 ? $num($sgst['rate']) : '' }}% {{ $sgst['amount'] > 0 ? $money($sgst['amount']) : '' }}
CGST - {{ $cgst['rate'] > 0 ? $num($cgst['rate']) : '' }}% {{ $cgst['amount'] > 0 ? $money($cgst['amount']) : '' }}
IGST - {{ $igst['rate'] > 0 ? $num($igst['rate']) : '' }}% {{ $igst['amount'] > 0 ? $money($igst['amount']) : '' }}
Bill Amount {{ $money($billAmount) }}
Paid {{ $money($paidTillNow) }}
Balance {{ $money($balanceDue) }}
{{-- BANK AND SIGNATURE --}}