@extends('layouts.pdf')
@section('title', 'Invoice')
@section('heading', 'Invoice')
@section('content')
@if (isset($invoice->id))
|
{{ optional(optional(optional($orders->first())->orderable)->partner)->organization_name ?? 'N/A' }}
|
|
Invoice #: {{ $invoice->id }}
Invoice Date:
{{ $invoice->closed_at
? \Carbon\Carbon::parse($invoice->closed_at)->format('d M, Y')
: 'Not yet closed'
}}
@php
$allLineItems = [];
$sortedOrders = $orders->sortBy(function ($order) {
$earliestBooking = $order->bookings->min('booking_date');
if ($earliestBooking) return \Carbon\Carbon::parse($earliestBooking)->format('Y-m-d');
if ($order->order_date) return \Carbon\Carbon::parse($order->order_date)->format('Y-m-d');
return $order->created_at?->format('Y-m-d') ?? '9999-12-31';
})->values();
foreach ($sortedOrders as $order) {
$orderDate = optional($order->bookings->first())->booking_date
? \Carbon\Carbon::parse($order->bookings->first()->booking_date)->format('Y-m-d')
: ($order->order_date ? \Carbon\Carbon::parse($order->order_date)->format('Y-m-d') : '');
// Group passengers by tour name and type (like invoice-view)
$tourTypeCounts = [];
$tourTypePrices = [];
$tourTypeDiscounts = [];
$tourTypeTaxes = [];
foreach ($order->bookings as $booking) {
$tourName = $booking->tour->name ?? 'N/A';
foreach ($booking->passengers as $passenger) {
$type = $passenger->type ?? 'N/A';
$price = $passenger->price ?? 0;
$discount = (float) $passenger->discountedPrice();
$tax = $passenger->effectiveTaxTotal();
// Store data grouped by tour and type
if (!isset($tourTypeCounts[$tourName][$type])) {
$tourTypeCounts[$tourName][$type] = 1;
$tourTypePrices[$tourName][$type] = $price;
$tourTypeDiscounts[$tourName][$type] = $discount;
$tourTypeTaxes[$tourName][$type] = $tax;
} else {
$tourTypeCounts[$tourName][$type]++;
$tourTypePrices[$tourName][$type] += $price;
$tourTypeDiscounts[$tourName][$type] += $discount;
$tourTypeTaxes[$tourName][$type] += $tax;
}
}
}
// Add tour/type rows grouped by tour and passenger type
foreach ($tourTypeCounts as $tourName => $types) {
foreach ($types as $type => $count) {
$totalPrice = $tourTypePrices[$tourName][$type] ?? 0;
$totalDiscount = $tourTypeDiscounts[$tourName][$type] ?? 0;
$totalTax = $tourTypeTaxes[$tourName][$type] ?? 0;
// Calculate single price
$singlePrice = $count > 0 ? $totalPrice / $count : 0;
// Calculate subtotal
$subtotal = $totalPrice;
// Calculate taxes (GST)
$lineGST = $totalTax;
// Add tour/type product line
$allLineItems[] = [
'order_id' => $order->id,
'partner_ref_id' => $order->partner_ref_id ?? '',
'date' => $orderDate,
'product' => $tourName,
'rate' => ucfirst($type),
'price' => $singlePrice,
'qty' => $count,
'subtotal' => $subtotal,
'discount' => $totalDiscount,
'gst' => $lineGST,
'total' => $subtotal + $totalDiscount + $lineGST,
];
}
}
// Process line items (order => many lineItems, lineItem => many discounts AND taxes)
foreach ($order->lineItems as $lineItem) {
$qty = $lineItem->qty ?? 1;
$price = $lineItem->price ?? 0;
$lineSubtotal = $price * $qty;
$lineGST = $lineItem->effectiveTaxTotal();
// Calculate discount from lineItem
$lineItemDiscount = 0;
$lineItemDiscounts = $lineItem->discounts ?? [];
if (!empty($lineItemDiscounts)) {
foreach ($lineItemDiscounts as $discount) {
$discountIsPercentage = ($discount['type'] ?? '') === 'percentage';
$discountAmount = $discountIsPercentage
? -1 * ($lineSubtotal * $discount['value']/100)
: $discount['value'];
$lineItemDiscount += $discountAmount;
}
}
// Add line item product
$allLineItems[] = [
'order_id' => $order->id,
'partner_ref_id' => $order->partner_ref_id ?? '',
'date' => $orderDate,
'product' => $lineItem->name ?? 'Line Item',
'rate' => ucfirst($lineItem->type ?? 'Individual'),
'price' => $price,
'qty' => $qty,
'subtotal' => $lineSubtotal,
'discount' => $lineItemDiscount,
'gst' => $lineGST,
'total' => $lineSubtotal + $lineItemDiscount + $lineGST,
];
}
}
// Get totals from database (from updateOrderTotals function)
$subtotal = $orders->sum('total_price');
$totalDiscount = abs($orders->sum('discount')); // discount is stored as negative
$totalGST = $orders->sum('taxes');
$totalPaid = $orders->sum('paid');
$groupedByOrder = [];
foreach ($allLineItems as $item) {
$orderId = $item['order_id'];
if (!isset($groupedByOrder[$orderId])) {
$order = $sortedOrders->firstWhere('id', $orderId);
$orderTotal = ($order->total_price + $order->discount) + ($order->taxes ?? 0);
$groupedByOrder[$orderId] = [
'partner_ref_id' => $item['partner_ref_id'],
'date' => $item['date'],
'items' => [],
'order_total' => $orderTotal,
'order_paid' => $order->paid ?? 0,
];
}
$groupedByOrder[$orderId]['items'][] = $item;
}
// finalTotal = subtotal + discount (negative) + taxes
$finalTotal = $subtotal + $orders->sum('discount') + $totalGST;
$balanceDue = $finalTotal - $totalPaid;
@endphp
| Order # |
Reference # |
Date |
Product |
Rate |
Price |
Qty |
Subtotal |
Discount |
Taxes |
Total |
Paid |
Due |
@foreach ($groupedByOrder as $orderId => $orderData)
@foreach ($orderData['items'] as $index => $item)
@if ($index === 0)
| {{ $orderId }} |
{{ $orderData['partner_ref_id'] ?: 'N/A' }} |
{{ $orderData['date'] }} |
@endif
{{ $item['product'] ?? '' }} |
{{ $item['rate'] ?? '' }} |
{{ ($item['price'] ?? 0) < 0 ? '-' : '' }}${{ number_format(abs($item['price'] ?? 0), 2) }} |
{{ ($item['qty'] ?? 0) < 0 ? '-' : '' }}{{ abs($item['qty'] ?? 0) }} |
{{ ($item['subtotal'] ?? 0) < 0 ? '-' : '' }}${{ number_format(abs($item['subtotal'] ?? 0), 2) }} |
{{ ($item['discount'] ?? 0) < 0 ? '-' : '' }}${{ number_format(abs($item['discount'] ?? 0), 2) }} |
{{ ($item['gst'] ?? 0) < 0 ? '-' : '' }}${{ number_format(abs($item['gst'] ?? 0), 2) }} |
@if ($index === 0)
{{ $orderData['order_total'] < 0 ? '-' : '' }}${{ number_format(abs($orderData['order_total']), 2) }}
|
{{ $orderData['order_paid'] < 0 ? '-' : '' }}${{ number_format(abs($orderData['order_paid']), 2) }}
|
@php
$orderDue = $orderData['order_total'] - $orderData['order_paid'];
@endphp
{{ $orderDue < 0 ? '-' : '' }}${{ number_format(abs($orderDue), 2) }}
|
@endif
@endforeach
@endforeach
| |
| Subtotal: |
{{ $subtotal < 0 ? '-' : '' }}${{ number_format(abs($subtotal), 2) }} |
| Discount: |
-${{ number_format($totalDiscount, 2) }} |
| Taxes: |
{{ $totalGST < 0 ? '-' : '' }}${{ number_format(abs($totalGST), 2) }} |
|
| Total: |
{{ $finalTotal < 0 ? '-' : '' }}${{ number_format(abs($finalTotal), 2) }} |
| Payments: |
-${{ number_format($totalPaid, 2) }} |
|
| Balance Due: |
{{ $balanceDue < 0 ? '-' : '' }}${{ number_format(abs($balanceDue), 2) }} |
|
G.S.T Registration No: 84417 5539 RT0001
Currency: Canadian Dollars
|
|
Please indicate invoice # on all payments
Remit any Balance Owing to:
1295 Industrial Rd Kelowna BC Canada V1Z 1G4
Or send remittance advice to:
gcousins@alpinehelicopters.com
|
@endif
@endsection