@if (count($orderItemList) > 0 && user_can('Update Order'))
@if (user_can('Add Discount on POS'))
@lang('modules.order.addDiscount')
@endif
{{-- Loyalty redemption is disabled on KOT detail page --}}
@endif
@lang('modules.order.totalItem')
@php
// Count only items that are actually displayed (belong to visible KOTs)
$displayedItemCount = 0;
if (isset($kotList) && is_array($kotList)) {
foreach ($kotList as $kotKey => $kot) {
$kotPrefix = 'kot_' . $kot->id;
foreach ($orderItemList as $key => $item) {
if (strpos($key, $kotPrefix) !== false) {
$displayedItemCount++;
}
}
}
} else {
// Fallback: count all items if kotList is not available
$displayedItemCount = count($orderItemList);
}
@endphp
{{ $displayedItemCount }}
@lang('modules.order.subTotal')
@php
$stampDiscountAmount = 0;
$hasFreeStampItems = false;
if (isset($orderID) && $orderID && isset($orderDetail) && $orderDetail) {
$stampDiscountAmount = (float)($orderDetail->stamp_discount_amount ?? 0);
$hasFreeStampItems = $orderDetail->items()->where('is_free_item_from_stamp', true)->exists();
}
@endphp
@if($stampDiscountAmount > 0 || $hasFreeStampItems)
@lang('app.stampDiscount')
@if($stampDiscountAmount > 0)
(-{{ currency_format($stampDiscountAmount, restaurant()->currency_id) }})
@elseif($hasFreeStampItems)
(@lang('app.freeItem'))
@endif
@endif
@php
// For existing orders, always use database value to avoid mismatches
// For new orders, use component variable
$displaySubTotal = isset($orderID) && $orderID && isset($orderDetail) && $orderDetail
? ($orderDetail->sub_total ?? $subTotal)
: $subTotal;
@endphp
{{ currency_format($displaySubTotal, restaurant()->currency_id) }}
@if ($orderDetail->loyalty_points_redeemed > 0 && $orderDetail->loyalty_discount_amount > 0)
@lang('app.loyaltyDiscount') ({{ number_format($orderDetail->loyalty_points_redeemed) }} @lang('app.points'))
{{-- Edit loyalty redemption disabled on KOT detail page --}}
-{{ currency_format($orderDetail->loyalty_discount_amount, restaurant()->currency_id) }}
@endif
@php
$displayDiscountAmount = isset($orderDetail) && $orderDetail
? (float)($orderDetail->discount_amount ?? 0)
: (float)($discountAmount ?? 0);
$displayDiscountType = isset($orderDetail) && $orderDetail
? ($orderDetail->discount_type ?? $discountType)
: $discountType;
$displayDiscountValue = isset($orderDetail) && $orderDetail
? ($orderDetail->discount_value ?? $discountValue)
: $discountValue;
$useLiveTotals = false;
@endphp
@if ($displayDiscountAmount > 0 && ($orderDetail->loyalty_points_redeemed ?? 0) == 0)
@lang('modules.order.discount') @if ($displayDiscountType == 'percent')
({{ $displayDiscountValue }}%)
@endif
-{{ currency_format($displayDiscountAmount, restaurant()->currency_id) }}
@endif
@php
$applicableExtraCharges = collect($extraCharges ?? [])->filter(function ($charge) use ($orderType) {
$allowedTypes = $charge->order_types ?? [];
return empty($allowedTypes) || in_array($orderType, $allowedTypes);
});
@endphp
@foreach ($applicableExtraCharges as $charge)
{{ $charge->charge_name }}
@if ($charge->charge_type == 'percent')
({{ $charge->charge_value }}%)
@endif
@if (user_can('Update Order'))
@endif
@php
// Calculate discounted subtotal for charges (after regular + loyalty discounts)
$hasOrderDetail = isset($orderID) && $orderID && isset($orderDetail) && $orderDetail;
$baseSubTotal = $useLiveTotals ? $subTotal : ($hasOrderDetail ? ($orderDetail->sub_total ?? $subTotal) : $subTotal);
$regularDiscount = $useLiveTotals ? (float)($discountAmount ?? 0) : ($hasOrderDetail ? (float)($orderDetail->discount_amount ?? 0) : (float)($discountAmount ?? 0));
$loyaltyDiscount = $useLiveTotals ? (float)($loyaltyDiscountAmount ?? 0) : ($hasOrderDetail ? (float)($orderDetail->loyalty_discount_amount ?? 0) : (float)($loyaltyDiscountAmount ?? 0));
$chargeBase = $baseSubTotal - $regularDiscount - $loyaltyDiscount;
@endphp
{{ currency_format($charge->getAmount($chargeBase), restaurant()->currency_id) }}
@endforeach
@if ($tipAmount > 0)
@lang('modules.order.tip')
{{ currency_format($tipAmount, restaurant()->currency_id) }}
@endif
@if ($orderType === 'delivery' && !is_null($deliveryFee))
@lang('modules.delivery.deliveryFee')
@if ($deliveryFee > 0)
{{ currency_format($deliveryFee, restaurant()->currency_id) }}
@else
@lang('modules.delivery.freeDelivery')
@endif
@endif
@if ($taxMode == 'order')
@php
// For existing orders, use order's taxes; otherwise use all restaurant taxes
// Prevent duplicate taxes by tracking tax IDs and tax names
$taxesToDisplay = [];
$seenTaxIds = [];
$seenTaxNames = [];
if (isset($orderID) && $orderID && isset($orderDetail) && $orderDetail && $orderDetail->taxes && $orderDetail->taxes->count() > 0) {
// Use order's specific taxes (avoid duplicates by both ID and name)
// Use unique() to remove duplicates at collection level first
$uniqueOrderTaxes = $orderDetail->taxes->unique(function ($orderTax) {
$tax = $orderTax->tax ?? null;
return $tax ? ($tax->id ?? $tax->tax_name ?? '') : '';
});
foreach ($uniqueOrderTaxes as $orderTax) {
$tax = $orderTax->tax ?? null;
if ($tax) {
$taxId = $tax->id ?? null;
$taxName = strtolower(trim($tax->tax_name ?? ''));
// Check both ID and name to prevent duplicates
$isDuplicate = false;
if ($taxId && in_array($taxId, $seenTaxIds)) {
$isDuplicate = true;
} elseif ($taxName && in_array($taxName, $seenTaxNames)) {
$isDuplicate = true;
}
if (!$isDuplicate) {
if ($taxId) $seenTaxIds[] = $taxId;
if ($taxName) $seenTaxNames[] = $taxName;
$taxesToDisplay[] = (object)[
'tax_name' => $tax->tax_name ?? '',
'tax_percent' => $tax->tax_percent ?? 0
];
}
}
}
} else {
// Use all restaurant taxes for new orders (avoid duplicates)
foreach ($taxes ?? [] as $tax) {
if ($tax) {
$taxId = $tax->id ?? null;
$taxName = strtolower(trim($tax->tax_name ?? ''));
// Check both ID and name to prevent duplicates
$isDuplicate = false;
if ($taxId && in_array($taxId, $seenTaxIds)) {
$isDuplicate = true;
} elseif ($taxName && in_array($taxName, $seenTaxNames)) {
$isDuplicate = true;
}
if (!$isDuplicate) {
if ($taxId) $seenTaxIds[] = $taxId;
if ($taxName) $seenTaxNames[] = $taxName;
$taxesToDisplay[] = (object)[
'tax_name' => $tax->tax_name ?? '',
'tax_percent' => $tax->tax_percent ?? 0
];
}
}
}
}
// Step 1: Calculate discounted subtotal for tax calculation (after both regular and loyalty discounts)
// Loyalty points are always removed from subtotal before calculating tax
// Use database values for existing orders
$hasOrderDetail = isset($orderID) && $orderID && isset($orderDetail) && $orderDetail;
$baseSubTotal = $useLiveTotals ? $subTotal : ($hasOrderDetail ? ($orderDetail->sub_total ?? $subTotal) : $subTotal);
$regularDiscount = $useLiveTotals ? (float)($discountAmount ?? 0) : ($hasOrderDetail ? (float)($orderDetail->discount_amount ?? 0) : (float)($discountAmount ?? 0));
$loyaltyDiscount = $useLiveTotals ? (float)($loyaltyDiscountAmount ?? 0) : ($hasOrderDetail ? (float)($orderDetail->loyalty_discount_amount ?? 0) : (float)($loyaltyDiscountAmount ?? 0));
$discountedSubtotal = $baseSubTotal - $regularDiscount - $loyaltyDiscount;
// Step 2: Calculate service charges on discounted subtotal
$serviceTotal = 0;
// Ensure applicableExtraCharges is available (it's defined earlier in the template)
if (!isset($applicableExtraCharges)) {
$applicableExtraCharges = collect($extraCharges ?? [])->filter(function ($charge) use ($orderType) {
$allowedTypes = $charge->order_types ?? [];
return empty($allowedTypes) || in_array($orderType, $allowedTypes);
});
}
if ($applicableExtraCharges && $applicableExtraCharges->count() > 0) {
foreach ($applicableExtraCharges as $charge) {
if ($charge && method_exists($charge, 'getAmount')) {
$chargeAmount = $charge->getAmount((float)$discountedSubtotal);
$serviceTotal += (float)$chargeAmount;
}
}
}
// Step 3: Calculate tax_base based on Tax Calculation Base setting
// Check if service charges should be included in tax base
$restaurant = restaurant();
$includeChargesInTaxBase = false;
if ($restaurant && isset($restaurant->include_charges_in_tax_base)) {
$includeChargesInTaxBase = (bool)$restaurant->include_charges_in_tax_base;
}
// Tax base = (subtotal - discounts) + service charges (if enabled)
$taxBase = $includeChargesInTaxBase
? ($discountedSubtotal + $serviceTotal)
: $discountedSubtotal;
$taxBase = max(0, (float)$taxBase);
@endphp
@php
$orderLevelTotalTax = 0;
@endphp
@foreach ($taxesToDisplay as $item)
{{ $item->tax_name }} ({{ $item->tax_percent }}%)
@php
// Step 4: Calculate tax on tax_base
$taxAmount = ($item->tax_percent / 100) * $taxBase;
$orderLevelTotalTax += $taxAmount;
@endphp
{{ currency_format($taxAmount, restaurant()->currency_id) }}
@endforeach
{{-- @if(count($taxesToDisplay) > 0)
@lang('modules.order.totalTax')
@php
// For existing orders, use database value (which is correct after loyalty discount)
// For new orders, use calculated total from individual taxes
$displayTaxAmount = isset($orderID) && $orderID && isset($orderDetail) && $orderDetail
? ($orderDetail->total_tax_amount ?? $orderLevelTotalTax)
: $orderLevelTotalTax;
@endphp
{{ currency_format($displayTaxAmount, restaurant()->currency_id) }}
@endif --}}
@else
@php
$taxTotals = [];
foreach ($orderItemTaxDetails as $item) {
$qty = $item['qty'] ?? 1;
if (!empty($item['tax_breakup'])) {
foreach ($item['tax_breakup'] as $taxName => $taxInfo) {
if (!isset($taxTotals[$taxName])) {
$taxTotals[$taxName] = [
'percent' => $taxInfo['percent'],
'amount' => 0
];
}
$taxTotals[$taxName]['amount'] += $taxInfo['amount'] * $qty;
}
}
}
@endphp
@foreach ($taxTotals as $taxName => $taxInfo)
{{ $taxName }} ({{ $taxInfo['percent'] }}%)
{{ currency_format($taxInfo['amount'], restaurant()->currency_id) }}
@endforeach
@lang('modules.order.totalTax')
@php
// For existing orders, use database value (which is correct after loyalty discount)
// For new orders, use component variable
$displayTaxAmount = $useLiveTotals
? $totalTaxAmount
: (isset($orderID) && $orderID && isset($orderDetail) && $orderDetail
? ($orderDetail->total_tax_amount ?? $totalTaxAmount)
: $totalTaxAmount);
@endphp
{{ currency_format($displayTaxAmount, restaurant()->currency_id) }}
@endif
@lang('modules.order.total')
@php
// For existing orders, use database value (which is correct after loyalty discount)
// For new orders, use component variable
$displayTotal = $useLiveTotals
? $total
: (isset($orderID) && $orderID && isset($orderDetail) && $orderDetail
? ($orderDetail->total ?? $total)
: $total);
// dd($displayTotal, $total);
@endphp
{{ currency_format($displayTotal, restaurant()->currency_id) }}