@php
// Count items that belong to this KOT
$kotItemCount = 0;
foreach ($orderItemList as $key => $item) {
$kotPrefix = 'kot_' . $kot->id;
if (strpos($key, $kotPrefix) !== false) {
$kotItemCount++;
}
}
@endphp
@forelse ($orderItemList as $key => $item)
@php
// Check if this item belongs to the current KOT
// Keys are formatted as: "kot_{kot_id}_{item_id}" (with quotes)
$kotPrefix = 'kot_' . $kot->id;
$belongsToKot = strpos($key, $kotPrefix) !== false;
@endphp
@continue(!$belongsToKot)
@php
$itemName = $item->item_name;
$itemVariation = (isset($orderItemVariation[$key]) ? $orderItemVariation[$key]->variation : '');
$displayPrice = $this->getItemDisplayPrice($key);
// Get amount and stamp info from kot_items table for KOT orders
$totalAmount = $orderItemAmount[$key] ?? 0; // Default to component property
$isFreeItem = false;
$hasDiscount = false;
$discountAmount = 0;
$originalAmount = 0;
$kotItem = null;
$itemFoundInDatabase = false; // CRITICAL: Track if we found item in DB
if (isset($orderID) && $orderID && isset($orderDetail) && $orderDetail && $orderDetail->status === 'kot') {
// Extract kot_item_id from key (format: "kot_{kot_id}_{item_id}")
$keyParts = explode('_', trim($key, '"'));
if (count($keyParts) >= 3 && $keyParts[0] === 'kot') {
$kotItemId = (int)($keyParts[2] ?? 0);
if ($kotItemId > 0) {
try {
// Load kot items if not already loaded (ensure all fields are loaded)
if (!$kot->relationLoaded('items')) {
$kot->load('items');
}
// Find the kot_item from the loaded relationship
// Try both id and direct access
$kotItem = $kot->items->firstWhere('id', $kotItemId);
// If not found by id, try to find by matching menu_item_id and variation
if (!$kotItem) {
$menuItemId = $item->id ?? null;
$variationId = isset($orderItemVariation[$key]) ? $orderItemVariation[$key]->id : null;
$kotItem = $kot->items->first(function($ki) use ($menuItemId, $variationId) {
return $ki->menu_item_id == $menuItemId
&& ($variationId ? $ki->menu_item_variation_id == $variationId : is_null($ki->menu_item_variation_id));
});
}
if ($kotItem) {
$itemFoundInDatabase = true; // Found in database
// Use amount from kot_items table (preserves discounts and free items)
$totalAmount = (float)($kotItem->amount ?? 0);
// STRICT CHECK: Only true if database value is exactly 1, true, or '1'
// Explicitly check for 0, false, null, '0', '' and set to false
$dbFreeValue = $kotItem->getAttribute('is_free_item_from_stamp') ?? $kotItem->is_free_item_from_stamp ?? null;
// Explicitly check: if value is 0, false, null, '0', or empty string, it's NOT free
if ($dbFreeValue === 0 || $dbFreeValue === false || $dbFreeValue === null || $dbFreeValue === '0' || $dbFreeValue === '') {
$isFreeItem = false;
} else {
// Only mark as free if value is exactly 1, true, or '1'
$isFreeItem = ($dbFreeValue === 1 || $dbFreeValue === true || $dbFreeValue === '1');
}
// Check for discount from stamp - access attributes directly
$discountAmount = (float)($kotItem->getAttribute('discount_amount') ?? $kotItem->discount_amount ?? 0);
$isDiscounted = (bool)($kotItem->getAttribute('is_discounted') ?? $kotItem->is_discounted ?? false);
$hasDiscount = $discountAmount > 0 || $isDiscounted;
// Calculate original amount (amount + discount)
if ($hasDiscount && $discountAmount > 0) {
$originalAmount = $totalAmount + $discountAmount;
} elseif ($isFreeItem) {
// For free items, calculate original amount from price
$basePrice = (float)($kotItem->getAttribute('price') ?? $kotItem->price ?? ($item->price ?? 0));
$modifierPrice = isset($orderItemModifiersPrice[$key]) ? (float)$orderItemModifiersPrice[$key] : 0;
$qty = isset($orderItemQty[$key]) ? (int)$orderItemQty[$key] : 1;
$originalAmount = ($basePrice + $modifierPrice) * $qty;
}
}
} catch (\Exception $e) {
// Fallback to component property if error
// Log error for debugging
\Illuminate\Support\Facades\Log::debug('Error loading kot_item stamp data: ' . $e->getMessage());
}
}
}
}
// FALLBACK: Only use key pattern or notes if item was NOT found in database
// This is ONLY for draft orders or items not yet saved to database
// CRITICAL: Only check fallback if we didn't find the item in database
// IMPORTANT: If item was found in database, NEVER use fallback - database value is final
if (!$itemFoundInDatabase) {
// Only check key pattern for draft orders (when orderID might not be set or order is draft)
$isDraftOrder = !isset($orderID) || !$orderID || (isset($orderDetail) && $orderDetail && $orderDetail->status === 'draft');
if ($isDraftOrder) {
// Only check key pattern if not already set from database
// Reset to false first to ensure clean state
$isFreeItem = false;
$isFreeItem = strpos($key, 'free_stamp_') === 0
|| (isset($itemNotes[$key]) && strpos($itemNotes[$key] ?? '', 'FREE') !== false);
} else {
// For non-draft orders, if item not found in DB, it's definitely NOT free
$isFreeItem = false;
}
}
// FINAL SAFEGUARD: If item was found in database, $isFreeItem is already set correctly above - do NOT override
// If item was NOT found and it's not a draft order, ensure it's false
if ($itemFoundInDatabase && !$isFreeItem) {
// Explicitly ensure it stays false - database said it's not free
$isFreeItem = false;
}
@endphp
@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) }}