0) { $spName = $qOrder['Name'] ?? ''; if ($orderTypeID == 1) { $tableName = strlen($spName) ? $spName : 'Table'; $taskTitle = "Deliver Order #{$OrderID} to {$tableName}"; $taskCategoryID = 3; } elseif ($orderTypeID == 2) { $taskTitle = "Order #{$OrderID} Ready for Pickup"; $taskCategoryID = 4; } elseif ($orderTypeID == 3) { $taskTitle = "Deliver Order #{$OrderID} to Address"; $taskCategoryID = 5; } else { $taskTitle = ''; $taskCategoryID = 0; } if ($taskTitle !== '') { $spID = (int) ($qOrder['ServicePointID'] ?? 0); queryTimed( "INSERT INTO Tasks (BusinessID, OrderID, ServicePointID, TaskTypeID, CategoryID, Title, ClaimedByUserID, CreatedOn) VALUES (?, ?, ?, ?, ?, ?, 0, NOW())", [ $qOrder['BusinessID'], $OrderID, $spID > 0 ? $spID : null, $taskTypeID, $taskCategoryID, $taskTitle ] ); $taskCreated = true; } } // Check for pending cash payment and create "Pay With Cash" task $qCashPayment = queryOne( "SELECT p.PaymentPaidInCash, o.PaymentStatus, o.ServicePointID, sp.Name AS ServicePointName FROM Orders o LEFT JOIN Payments p ON p.PaymentID = o.PaymentID LEFT JOIN ServicePoints sp ON sp.ID = o.ServicePointID WHERE o.ID = ?", [$OrderID] ); if ($qCashPayment && (float) ($qCashPayment['PaymentPaidInCash'] ?? 0) > 0 && ($qCashPayment['PaymentStatus'] ?? '') === 'pending' ) { // Check if there's already an active cash task $qExistingCash = queryOne( "SELECT t.ID FROM Tasks t INNER JOIN tt_TaskTypes tt ON tt.ID = t.TaskTypeID WHERE t.OrderID = ? AND tt.Name LIKE '%Cash%' AND t.CompletedOn IS NULL LIMIT 1", [$OrderID] ); if (!$qExistingCash) { $qCashType = queryOne( "SELECT ID FROM tt_TaskTypes WHERE BusinessID = ? AND Name LIKE '%Cash%' LIMIT 1", [$qOrder['BusinessID']] ); $cashTaskTypeID = $qCashType ? (int) $qCashType['ID'] : 0; $cashTitle = "Pay With Cash - Order #{$OrderID}"; $spName2 = $qCashPayment['ServicePointName'] ?? ''; if (strlen($spName2)) { $cashTitle .= " ({$spName2})"; } $cashSPID = (int) ($qCashPayment['ServicePointID'] ?? 0); queryTimed( "INSERT INTO Tasks (BusinessID, OrderID, TaskTypeID, Title, ClaimedByUserID, CreatedOn, ServicePointID) VALUES (?, ?, ?, ?, 0, NOW(), ?)", [ $qOrder['BusinessID'], $OrderID, $cashTaskTypeID, $cashTitle, $cashSPID > 0 ? $cashSPID : null ] ); $cashTaskCreated = true; } } } catch (Exception $e) { // Task creation failed, but don't fail the status update } }