@php $taxesMap = \App\Models\Tax::pluck('rate', 'id')->toArray(); $taxNamesMap = \App\Models\Tax::pluck('name', 'id')->toArray(); function parseItemTaxIds($taxIds) { if (empty($taxIds)) return []; if (is_array($taxIds)) return array_map('intval', $taxIds); if (is_string($taxIds)) { $decoded = json_decode($taxIds, true); if (is_array($decoded)) return array_map('intval', $decoded); if (strpos($taxIds, ',') !== false) return array_map('intval', explode(',', $taxIds)); return $taxIds ? [intval($taxIds)] : []; } return is_numeric($taxIds) ? [intval($taxIds)] : []; } $taxBreakdown = []; $totalTax = 0; foreach ($invoice->items as $item) { if (($item->item_type ?? 'product') === 'product' && $item->tax_ids) { $itemTaxIds = parseItemTaxIds($item->tax_ids); foreach ($itemTaxIds as $taxId) { $taxRate = $taxesMap[$taxId] ?? 0; $taxName = $taxNamesMap[$taxId] ?? 'Tax'; $baseAmount = $item->quantity * $item->rate; $taxAmount = ($baseAmount * $taxRate) / 100; $totalTax += $taxAmount; $key = $taxName . ' (' . $taxRate . '%)'; if (!isset($taxBreakdown[$key])) $taxBreakdown[$key] = ['rate' => $taxRate, 'amount' => 0, 'name' => $taxName]; $taxBreakdown[$key]['amount'] += $taxAmount; } } } if ($totalTax == 0 && $invoice->tax > 0) $totalTax = $invoice->tax; // Calculate total discounts $totalLineDiscounts = 0; foreach ($invoice->items as $item) { if (($item->item_type ?? 'product') === 'product') { $totalLineDiscounts += floatval($item->discount_amount ?? 0); } } $invoiceDiscount = floatval($invoice->discount_amount ?? $invoice->discount ?? 0); $totalDiscounts = $totalLineDiscounts + $invoiceDiscount; // Grand total = Subtotal - All Discounts + Tax $grandTotal = $invoice->subtotal - $totalDiscounts + $totalTax; @endphp @php // Get selected brand IDs from query string $selectedBrandIds = []; $brandsParam = request()->get('brands', ''); if (!empty($brandsParam)) { $selectedBrandIds = array_filter(array_map('intval', explode(',', $brandsParam))); } // Fetch selected brands $selectedBrands = collect([]); if (!empty($selectedBrandIds)) { try { if (class_exists('\Modules\Brand\Models\Brand')) { $selectedBrands = \Modules\Brand\Models\Brand::whereIn('id', $selectedBrandIds) ->where('is_active', 1) ->orderBy('sort_order') ->get(); } } catch (\Exception $e) { $selectedBrands = collect([]); } } $brandCount = is_object($selectedBrands) && method_exists($selectedBrands, 'count') ? $selectedBrands->count() : count($selectedBrands); @endphp
@if(!empty($company['logo'])) @if(str_starts_with($company['logo'], 'data:image')) @else @endif @endif {{-- Brand Logos Row --}} @if($brandCount > 0)
@foreach($selectedBrands as $brand) @if($brand->logo) @php $brandLogoPath = storage_path('app/public/' . $brand->logo); $brandLogoExists = file_exists($brandLogoPath); @endphp @if($brandLogoExists) @php $brandLogoData = base64_encode(file_get_contents($brandLogoPath)); $brandLogoMime = mime_content_type($brandLogoPath); @endphp @endif @endif @endforeach
@endif
{{ $company['name'] }}
@if($company['address']){{ $company['address'] }}
@endif @if($company['phone'])Phone: {{ $company['phone'] }}
@endif @if($company['email'])Email: {{ $company['email'] }}
@endif @if($company['gst'])GST: {{ $company['gst'] }}@endif
INVOICE
#{{ $invoice->invoice_number }}
Invoice Date:{{ $invoice->date ? $invoice->date->format('d M Y') : '-' }}
Due Date:{{ $invoice->due_date ? $invoice->due_date->format('d M Y') : '-' }}
{{ ucfirst($invoice->status) }}
Bill To
@if($invoice->customer)
{{ $invoice->customer->customer_type === 'company' ? $invoice->customer->company : $invoice->customer->name }}
@if($invoice->customer->customer_type === 'company' && $invoice->customer->name)Attn: {{ $invoice->customer->name }}
@endif @if($invoice->address){{ $invoice->address }}
@endif @if($invoice->city || $invoice->state){{ $invoice->city }}{{ $invoice->city && $invoice->state ? ', ' : '' }}{{ $invoice->state }}
@endif @if($invoice->country){{ $invoice->country }} {{ $invoice->zip_code }}
@endif @if($invoice->email)Email: {{ $invoice->email }}
@endif @if($invoice->phone)Phone: {{ $invoice->phone }}@endif
@else
No customer assigned
@endif
Invoice Details
Currency: {{ $invoice->currency ?? 'INR' }}
Status: {{ ucfirst($invoice->status ?? 'draft') }}
Payment Status: {{ ucfirst(str_replace('_', ' ', $invoice->payment_status ?? 'unpaid')) }}
Amount Due: {{ $invoice->currency ?? 'INR' }} {{ number_format($invoice->amount_due, 2) }}
@if($invoice->subject)
Subject: {{ $invoice->subject }}
@endif @if($invoice->content)
📝 Customer Notes
{!! nl2br(e($invoice->content)) !!}
@endif @php $itemIndex = 0; @endphp @forelse($invoice->items as $item) @if(($item->item_type ?? 'product') === 'section') @elseif(($item->item_type ?? 'product') === 'note') @else @php $itemIndex++; $itemTaxIds = []; if (!empty($item->tax_ids)) { if (is_array($item->tax_ids)) { $itemTaxIds = array_map('intval', $item->tax_ids); } else { $decoded = json_decode($item->tax_ids, true); if (is_array($decoded)) { $itemTaxIds = array_map('intval', $decoded); } elseif (strpos($item->tax_ids, ',') !== false) { $itemTaxIds = array_map('intval', array_filter(explode(',', $item->tax_ids))); } elseif ($item->tax_ids) { $itemTaxIds = [intval($item->tax_ids)]; } } } $product = $item->product; @endphp @endif @empty @endforelse
# DESCRIPTION HSN CODE QTY UNIT PRICE/UNIT GST DISCOUNT AMOUNT
{{ $item->description }}
{{ $item->long_description ?: $item->description }}
{{ $itemIndex }}
{{ $item->description }}
@if($item->long_description)
{{ $item->long_description }}
@endif
@if($product && $product->hsn_code) {{ $product->hsn_code }} @else - @endif {{ number_format($item->quantity, 2) }} @php $unitText = $item->unit ?? ($product && $product->unit ? $product->unit->short_name : null); @endphp @if($unitText) {{ $unitText }} @else - @endif {{ number_format($item->rate, 2) }} @foreach($itemTaxIds as $taxId) {{ $taxRatesMap[$taxId] ?? 0 }}% @endforeach @if(empty($itemTaxIds)) - @endif @if(($item->discount_amount ?? 0) > 0)
-₹{{ number_format($item->discount_amount, 2) }}
({{ $item->discount_type === 'percentage' ? number_format($item->discount_value, 2) . '%' : '₹' . number_format($item->discount_value, 2) }})
@else - @endif
{{ number_format($item->amount, 2) }}
No items
Invoice Summary
Subtotal
{{ $invoice->currency ?? 'INR' }} {{ number_format($invoice->subtotal, 2) }}
Total Discount
@if($totalDiscounts > 0) -{{ $invoice->currency ?? 'INR' }} {{ number_format($totalDiscounts, 2) }} @else {{ $invoice->currency ?? 'INR' }} 0.00 @endif
Total Tax @if(count($taxBreakdown) > 0) @php $firstTax = array_values($taxBreakdown)[0]; @endphp ({{ $firstTax['rate'] }}%) @endif
{{ $invoice->currency ?? 'INR' }} {{ number_format($totalTax, 2) }}
Grand Total
{{ $invoice->currency ?? 'INR' }} {{ number_format($grandTotal, 2) }}
@if($invoice->terms_conditions)
📋 Terms & Conditions
{!! $invoice->terms_conditions !!}
@endif @if($invoice->privacy_policy)
🔒 Privacy Policy
{!! $invoice->privacy_policy !!}
@endif @if($invoice->payment_details)
💳 Payment Details
{!! $invoice->payment_details !!}
@endif @if($invoice->conclusion)
📝 Conclusion
{!! $invoice->conclusion !!}
@endif @if(!empty($company['signature'])) ... (signature code above) ... @endif