@php
// Get sub_total directly from order (already refreshed in mount)
// Use getOriginal to get database value if order was modified
$displaySubTotal = (float)($order->getOriginal('sub_total') ?? $order->sub_total ?? 0);
@endphp
{{ currency_format($displaySubTotal, $restaurant->currency_id) }}
{{-- @if(function_exists('module_enabled') && module_enabled('Loyalty')) --}}
@php
// Always prioritize order data from database
$displayLoyaltyPointsRedeemed = (float)($order->loyalty_points_redeemed ?? 0);
$displayLoyaltyDiscountAmount = (float)($order->loyalty_discount_amount ?? 0);
// Fallback to component variables if order values are 0
if ($displayLoyaltyPointsRedeemed == 0) {
$displayLoyaltyPointsRedeemed = (float)($loyaltyPointsRedeemed ?? 0);
}
if ($displayLoyaltyDiscountAmount == 0) {
$displayLoyaltyDiscountAmount = (float)($loyaltyDiscountAmount ?? 0);
}
@endphp
@if($displayLoyaltyPointsRedeemed > 0 && $displayLoyaltyDiscountAmount > 0)
@endif
{{-- @endif --}}
@php
// Use order data if component variables are not set (for discount check)
$checkLoyaltyPointsRedeemed = $loyaltyPointsRedeemed ?? $order->loyalty_points_redeemed ?? 0;
@endphp
@if (!is_null($order->discount_amount) && $checkLoyaltyPointsRedeemed == 0)
@php
// Check if order is paid - use both status fields to be safe
$isPaid = ($order->status === 'paid') || ($order->order_status->value ?? null) === 'paid';
@endphp
@if(!$isPaid)
{{ __('loyalty::app.remove') }}...
@endif
@php
// Ensure order has taxes loaded for calculation
if (!$order->relationLoaded('taxes')) {
$order->load('taxes.tax');
}
if (!$order->relationLoaded('items')) {
$order->load('items');
}
// Calculate preview total correctly:
// 1. Start with subtotal (use order->sub_total to match order detail page calculation)
// This ensures consistency with how taxes are calculated elsewhere
$previewSubTotal = (float)($order->sub_total ?? 0);
// 2. Apply regular discount (if any)
$previewSubTotal -= (float)($order->discount_amount ?? 0);
// 3. Apply loyalty discount (only to subtotal)
$previewDiscount = (float)($loyaltyDiscountAmount ?? 0);
$newSubTotal = $previewSubTotal - $previewDiscount;
// 4. Calculate taxes - FIRST calculate current tax, THEN calculate tax after loyalty discount
// Match the calculation method used in order detail page (line 438)
$currentTaxAmount = 0.0; // Tax on current subtotal (before loyalty discount)
$previewTaxAmount = 0.0; // Tax on new subtotal (after loyalty discount)
$newTaxAmountAfterDiscount = 0.0; // Tax amount after loyalty discount (for new total calculation)
if ($order->tax_mode === 'order' && $order->taxes && $order->taxes->count() > 0) {
// IMPORTANT: Don't round individual tax amounts, only round the final sum
// Ensure we process ALL taxes (no deduplication)
// Make sure taxes relationship is loaded
if (!$order->relationLoaded('taxes')) {
$order->load('taxes.tax');
}
// Get the base for CURRENT tax calculation (subtotal - regular discount, BEFORE loyalty discount)
// This matches the order detail page calculation: (tax_percent / 100) * (sub_total - discount_amount)
$currentTaxBase = (float)($order->sub_total ?? 0) - (float)($order->discount_amount ?? 0);
// Process ALL taxes - iterate through ALL order taxes without any filtering
// Get all taxes directly from the relationship to ensure no filtering
$allOrderTaxes = $order->taxes()->with('tax')->get();
foreach ($allOrderTaxes as $orderTax) {
$tax = $orderTax->tax ?? null;
if ($tax && isset($tax->tax_percent) && $tax->tax_percent > 0) {
$taxPercent = (float)$tax->tax_percent;
// Calculate CURRENT tax amount (on current subtotal before loyalty discount)
// This is what the order currently has: 7.50 (3.75 + 3.75)
$currentTax = ($taxPercent / 100.0) * (float)$currentTaxBase;
$currentTaxAmount += $currentTax;
// Calculate NEW tax amount (on new subtotal after loyalty discount)
// This is what the tax will be after loyalty discount is applied
$newTax = ($taxPercent / 100.0) * (float)$newSubTotal;
$previewTaxAmount += $newTax;
}
}
// Round ONLY the final sums to 2 decimal places for display
$currentTaxAmount = round($currentTaxAmount, 2);
$previewTaxAmount = round($previewTaxAmount, 2);
// Store the new tax amount (after discount) for reference
// Tax should be calculated on discounted subtotal (after loyalty discount)
$newTaxAmountAfterDiscount = $previewTaxAmount;
// For display in modal, show the NEW tax (after loyalty discount)
// $previewTaxAmount already contains the correct tax calculated on newSubTotal
} else {
// Item-level taxes - sum from order items (already calculated with precision)
$previewTaxAmount = (float)($order->items->sum('tax_amount') ?? 0);
// For item-level taxes, tax amount doesn't change with subtotal discount (it's per item)
// So the new tax amount is the same as current tax
$newTaxAmountAfterDiscount = $previewTaxAmount;
// Order doesn't have direct restaurant relationship, use $restaurant from component
$isInclusive = ($restaurant->tax_inclusive ?? false);
// For inclusive taxes, tax is already in item prices, so don't add again
// For exclusive taxes, we'll add it below
}
// 5. Calculate charges on discounted subtotal (ensure float precision)
$previewCharges = 0.0;
if ($order->charges && $order->charges->count() > 0) {
foreach ($order->charges as $chargeRelation) {
if ($chargeRelation->charge) {
$chargeAmount = $chargeRelation->charge->getAmount((float)$newSubTotal);
$previewCharges += (float)$chargeAmount;
}
}
// Round to 2 decimal places
$previewCharges = round($previewCharges, 2);
}
// 6. Build new total: new subtotal + taxes (if exclusive) + charges + tip + delivery
// Ensure all values are floats for proper calculation
// Order doesn't have direct restaurant relationship, use $restaurant from component
$isInclusive = ($restaurant->tax_inclusive ?? false);
$newTotal = (float)$newSubTotal;
// Use the NEW tax amount (after loyalty discount) for the new total
// $newTaxAmountAfterDiscount is always set (either from order-level or item-level calculation)
if (!$isInclusive && $order->tax_mode !== 'order') {
// Add item-level exclusive taxes (use new tax amount after discount)
$newTotal += (float)$newTaxAmountAfterDiscount;
} elseif ($order->tax_mode === 'order') {
// Add order-level taxes (always exclusive) - use new tax amount after discount
$newTotal += (float)$newTaxAmountAfterDiscount;
}
$newTotal += (float)$previewCharges;
$newTotal += (float)($order->tip_amount ?? 0);
$newTotal += (float)($order->delivery_fee ?? 0);
// Round final total to 2 decimal places
$newTotal = round($newTotal, 2);
// Current total for display
$currentTotal = $order->total ?? 0;
@endphp