{{-- resources/views/admin/invoices/show.blade.php --}} @extends('admin.adminLayout') @section('admin_page_title', 'Invoice '.$invoice->invoice_no) @section('admin_content') @php $currency = $invoice->currency ?? 'USD'; // ---------- Helpers ---------- $fnum = function($v, $def = 0.0) { if ($v === null || $v === '') return (float)$def; return is_numeric($v) ? (float)$v : (float)$def; }; $fint = function($v, $def = 0) { if ($v === null || $v === '') return (int)$def; return is_numeric($v) ? (int)$v : (int)$def; }; $trim2 = function($n) { return rtrim(rtrim(number_format((float)$n, 2, '.', ''), '0'), '.'); }; $computeDiscount = function(float $base, string $type, float $value) { $type = strtolower(trim($type ?: 'fixed')); $value = max(0, $value); if ($base <= 0) return 0.0; if ($type === 'percent') { $value = min(100, $value); $amt = $base * ($value / 100); } else { $amt = $value; } return min($base, max(0, $amt)); }; // ---------- Company / Lead ---------- $company = $invoice->company; $lead = $invoice->lead; $leadName = $lead ? trim(($lead->first_name ?? '').' '.($lead->last_name ?? '')) : '-'; $companyEmail = $company->company_email ?? $company->email ?? ''; // ---------- Status badge ---------- $badge = match(strtolower((string)$invoice->status)) { 'paid' => 'text-bg-success', 'canceled','cancel','cancelled' => 'text-bg-danger', default => 'text-bg-warning', }; // ---------- Invoice-level discount config ---------- $invDiscountTypeRaw = $invoice->discount_type ?? null; // fixed|percent|null $invDiscountValueRaw = $invoice->discount_value ?? null; // numeric|null $invDiscountType = strtolower((string)($invDiscountTypeRaw ?? 'fixed')); $invDiscountValue = $fnum($invDiscountValueRaw ?? 0, 0); // Stored money amount (legacy column "discount" = computed invoice discount amount) $invDiscountAmountStored = $fnum($invoice->discount ?? 0, 0); // ---------- Items: compute gross, row discount, net ---------- $items = $invoice->items ?? collect(); $rows = []; $grossSubtotal = 0.0; // SUM(unit*qty) ✅ PDF SUBTOTAL $rowDiscountTotal = 0.0; // SUM(row discount money) $netSubtotalAfterRow = 0.0; // SUM(net line totals (line_total)) $appliedTypes = []; // for Fixed/Percent/Mixed label foreach ($items as $it) { $unit = $fnum($it->unit_price ?? 0, 0); $qty = max(1, $fint($it->qty ?? 1, 1)); $gross = $unit * $qty; // row config $rowTypeRaw = $it->discount_type ?? null; $rowValueRaw = $it->discount_value ?? null; $rowType = strtolower((string)($rowTypeRaw ?? 'fixed')); $rowValue = $fnum($rowValueRaw ?? 0, 0); // stored line_total is NET after row discount (your DB) $lineTotalStored = ($it->line_total ?? null); $netStored = ($lineTotalStored !== null && $lineTotalStored !== '') ? $fnum($lineTotalStored, 0) : null; // explicit config? $hasRowConfig = ($rowTypeRaw !== null) || ($rowValueRaw !== null && $rowValueRaw !== ''); // compute row discount amount $rowDiscountAmount = 0.0; if ($hasRowConfig) { $rowDiscountAmount = $computeDiscount($gross, $rowType, $rowValue); } else { // infer from stored net if possible if ($netStored !== null) { $rowDiscountAmount = max(0, min($gross, $gross - $netStored)); // inferred => treat as fixed for label $rowType = 'fixed'; $rowValue = $rowDiscountAmount; } } // compute net $net = ($netStored !== null) ? $netStored : max(0, $gross - $rowDiscountAmount); $net = min($gross, max(0, $net)); // labels exactly like PDF cells $rowLabelType = ($rowType === 'percent') ? 'Percent' : 'Fixed'; $rowLabelValue = ($rowType === 'percent') ? ($trim2($rowValue).'%') : (number_format($rowValue, 2).' '.$currency); if ($rowDiscountAmount > 0.0000001) { $appliedTypes[] = in_array($rowType, ['fixed','percent']) ? $rowType : 'mixed'; } $rows[] = [ 'product_name' => $it->product_name ?? ($it->product->name ?? 'Item'), 'description' => $it->description ?? '', 'unit' => $unit, 'qty' => $qty, 'gross' => $gross, 'disc_label_type' => $rowLabelType, 'disc_label_value' => $rowLabelValue, 'disc_amount' => $rowDiscountAmount, 'net' => $net, ]; $grossSubtotal += $gross; $rowDiscountTotal += $rowDiscountAmount; $netSubtotalAfterRow += $net; } // subtotal AFTER row discounts (base for invoice-level discount) $subtotalAfterRow = max(0, $netSubtotalAfterRow); // invoice-level discount amount: $invDiscountAmount = $invDiscountAmountStored; if ($invDiscountAmount <= 0 && (($invDiscountTypeRaw !== null) || ($invDiscountValueRaw !== null && $invDiscountValue > 0))) { $invDiscountAmount = $computeDiscount($subtotalAfterRow, $invDiscountType, $invDiscountValue); } $invDiscountAmount = min($subtotalAfterRow, max(0, $invDiscountAmount)); if ($invDiscountAmount > 0.0000001) { $appliedTypes[] = in_array($invDiscountType, ['fixed','percent']) ? $invDiscountType : 'mixed'; } $totalDiscount = $rowDiscountTotal + $invDiscountAmount; // VAT (match PDF: show only if >0) $vat = max(0, $fnum($invoice->vat ?? 0, 0)); // GRAND TOTAL = (subtotalAfterRow - invoiceDiscount) + VAT $computedTotal = max(0, ($subtotalAfterRow - $invDiscountAmount + $vat)); $grandTotal = ($invoice->total !== null && $invoice->total !== '') ? $fnum($invoice->total, $computedTotal) : $computedTotal; // Overall discount type: FIXED / PERCENT / MIXED $uniqueTypes = array_values(array_unique($appliedTypes)); if (count($uniqueTypes) === 0) { $overallDiscountTypeLabel = 'None'; } elseif (count($uniqueTypes) === 1 && $uniqueTypes[0] === 'fixed') { $overallDiscountTypeLabel = 'Fixed'; } elseif (count($uniqueTypes) === 1 && $uniqueTypes[0] === 'percent') { $overallDiscountTypeLabel = 'Percent'; } else { $overallDiscountTypeLabel = 'Mixed'; } @endphp

Invoice #{{ $invoice->invoice_no }}

{{ \Carbon\Carbon::parse($invoice->issue_date ?? $invoice->created_at)->format('d/m/Y') }} | Currency: {{ $currency }}
Status: {{ ucfirst((string)$invoice->status) }}
{{-- Top info (3 blocks) --}}
{{-- INVOICE TO (Company) --}}
Invoice To
{{ $company->company_name ?? $company->name ?? '-' }}
@if(!empty($company->company_number))
Company No: {{ $company->company_number }}
@endif @if(!empty($companyEmail))
Email: {{ $companyEmail }}
@endif
{{-- BILL TO (Lead) --}}
Bill To
{{ $leadName }}
@if(!empty($lead?->email))
Email: {{ $lead->email }}
@endif @if(!empty($lead?->phone))
Phone: {{ \App\Support\AdminPhoneVisibility::lead($lead->phone, null, $lead->country) }}
@endif
{{-- PAYMENT METHODS --}}
Payment Methods
Company No
{{ $company->company_number ?? '-' }}
Bank Account
{{ $company->bank_account ?? '-' }}
Email
{{ $companyEmail ?: '-' }}
SWIFT
{{ $company->company_swift ?? '-' }}
@if(!empty($qrBase64 ?? null))
PayPal
PayPal QR
@endif
{{-- Items (✅ add DISCOUNT column like PDF) --}}
@forelse($rows as $i => $r) @empty @endforelse
# Item Description Unit Cost Qty Discount Total
{{ $i+1 }} {{ $r['product_name'] }} {{ $r['description'] }} {{ number_format($r['unit'], 2) }} {{ $currency }} {{ (int)$r['qty'] }}
{{ $r['disc_label_type'] }}: {{ $r['disc_label_value'] }}
Amount: {{ number_format($r['disc_amount'], 2) }} {{ $currency }}
{{ number_format($r['net'], 2) }} {{ $currency }}
No items
{{-- Totals (✅ match PDF: SUBTOTAL / DISCOUNT (TYPE) / GRAND TOTAL + (ROWS / INVOICE)) --}}
@if($vat > 0) @endif
Subtotal {{ number_format($grossSubtotal, 2) }} {{ $currency }}
Discount
({{ strtoupper($overallDiscountTypeLabel) }})
-{{ number_format($totalDiscount, 2) }} {{ $currency }}
Tax {{ number_format($vat, 2) }} {{ $currency }}
Grand Total {{ number_format($grandTotal, 2) }} {{ $currency }}
@if($totalDiscount > 0.0000001)
(ROWS / INVOICE) -{{ number_format($rowDiscountTotal, 2) }} / -{{ number_format($invDiscountAmount, 2) }} {{ $currency }}
@endif
{{-- /card-body --}}
{{-- /card --}}
@endsection