@forelse ($orderItemList as $key => $item)
@php
// Initialize variables - START WITH FALSE
$isFreeItem = false;
$hasStampDiscount = false;
$stampDiscountAmount = 0;
$originalAmount = 0;
$itemFoundInDatabase = false; // CRITICAL: Track if we found item in DB
// For existing orders, check both kot_items and order_items tables for stamp data
// PRIORITY: Database values ALWAYS override key patterns
if (isset($orderID) && $orderID && isset($orderDetail) && $orderDetail) {
// Check if this is a KOT item (key format: "kot_{kot_id}_{item_id}")
if (strpos($key, 'kot_') !== false) {
$keyParts = explode('_', trim($key, '"'));
if (count($keyParts) >= 3 && $keyParts[0] === 'kot') {
$kotItemId = (int)($keyParts[2] ?? 0);
$kotId = (int)($keyParts[1] ?? 0);
if ($kotItemId > 0 && $kotId > 0) {
try {
// Load kot items if not already loaded
if (!$orderDetail->relationLoaded('kot')) {
$orderDetail->load('kot.items');
}
// Find the kot and kot_item
$kot = $orderDetail->kot->firstWhere('id', $kotId);
if ($kot) {
if (!$kot->relationLoaded('items')) {
$kot->load('items');
}
$kotItem = $kot->items->firstWhere('id', $kotItemId);
if ($kotItem) {
$itemFoundInDatabase = true; // Found in database
// 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 in kot_items
$discountAmount = (float)($kotItem->getAttribute('discount_amount') ?? $kotItem->discount_amount ?? 0);
$isDiscounted = (bool)($kotItem->getAttribute('is_discounted') ?? $kotItem->is_discounted ?? false);
$hasStampDiscount = $discountAmount > 0 || $isDiscounted || !is_null($kotItem->stamp_rule_id);
if ($hasStampDiscount && $discountAmount > 0) {
$stampDiscountAmount = $discountAmount;
$originalAmount = (float)($kotItem->amount ?? 0) + $discountAmount;
} elseif ($isFreeItem) {
// For free items, calculate original amount from price
$basePrice = (float)($kotItem->getAttribute('price') ?? $kotItem->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) {
// Silently fail if error
}
}
}
}
// Check if this is an order_item (key format: "order_item_{id}")
// Only check if we haven't found it as a KOT item and it's not already marked as free
if (!$itemFoundInDatabase && strpos($key, 'order_item_') !== false) {
$keyParts = explode('_', trim($key, '"'));
if (count($keyParts) >= 3 && $keyParts[0] === 'order' && $keyParts[1] === 'item') {
$orderItemId = (int)($keyParts[2] ?? 0);
if ($orderItemId > 0) {
try {
// Load order items if not already loaded
if (!$orderDetail->relationLoaded('items')) {
$orderDetail->load('items');
}
// Find the order_item from the loaded relationship
$orderItem = $orderDetail->items->firstWhere('id', $orderItemId);
if ($orderItem) {
$itemFoundInDatabase = true; // Found in database
// 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 = $orderItem->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
// Note: order_items table only has 'stamp_rule_id' and 'is_free_item_from_stamp' columns
if (!$hasStampDiscount) {
$hasStampDiscount = !is_null($orderItem->stamp_rule_id) && !$isFreeItem;
}
// For items with stamp discounts, we can't calculate exact discount per item from order_items
// The discount is already deducted from amount field
if ($hasStampDiscount && $stampDiscountAmount == 0) {
// Try to estimate original amount from price field (if available)
$basePrice = $orderItem->price ?? 0;
$modifierPrice = isset($orderItemModifiersPrice[$key]) ? $orderItemModifiersPrice[$key] : 0;
$qty = $orderItemQty[$key] ?? $orderItem->quantity ?? 1;
$estimatedOriginalAmount = ($basePrice + $modifierPrice) * $qty;
// If current amount is less than estimated, there's a discount
$currentAmount = (float)($orderItem->amount ?? 0);
if ($estimatedOriginalAmount > $currentAmount) {
$stampDiscountAmount = $estimatedOriginalAmount - $currentAmount;
$originalAmount = $estimatedOriginalAmount;
}
} elseif ($isFreeItem && $originalAmount == 0) {
// For free items, calculate original amount from price
$basePrice = $orderItem->price ?? 0;
$modifierPrice = isset($orderItemModifiersPrice[$key]) ? $orderItemModifiersPrice[$key] : 0;
$originalAmount = ($basePrice + $modifierPrice) * ($orderItemQty[$key] ?? 1);
}
}
} catch (\Exception $e) {
// Silently fail if error
}
}
}
}
}
// 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]) && str_contains($itemNotes[$key] ?? '', __('loyalty::app.freeItemFromStamp')));
} 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