payfrit-api/api/orders/_createOrderTasks.php
John Mizerek 1f81d98c52 Initial PHP API migration from CFML
Complete port of all 163 API endpoints from Lucee/CFML to PHP 8.3.
Shared helpers in api/helpers.php (DB, auth, request/response, security).
PDO prepared statements throughout. Same JSON response shapes as CFML.
2026-03-14 14:26:59 -07:00

137 lines
5.1 KiB
PHP

<?php
/**
* Shared task creation logic for orders moving to status 3 (Ready).
*
* Expects these variables in calling scope:
* $OrderID (int)
* $qOrder (array with BusinessID, ServicePointID, Name keys)
* $NewStatusID (int)
* $oldStatusID (int)
*
* Sets:
* $taskCreated (bool)
* $cashTaskCreated (bool)
*/
$taskCreated = false;
$cashTaskCreated = false;
if ($NewStatusID == 3 && $oldStatusID != 3) {
try {
// Get order type
$qOrderDetails = queryOne(
"SELECT OrderTypeID FROM Orders WHERE ID = ?",
[$OrderID]
);
$orderTypeID = $qOrderDetails ? (int) $qOrderDetails['OrderTypeID'] : 1;
// Map order type to task type name
$taskTypeName = '';
if ($orderTypeID == 1) $taskTypeName = 'Deliver to Table';
elseif ($orderTypeID == 2) $taskTypeName = 'Order Ready for Pickup';
elseif ($orderTypeID == 3) $taskTypeName = 'Deliver to Address';
$taskTypeID = 0;
if ($taskTypeName !== '') {
$qTaskType = queryOne(
"SELECT ID FROM tt_TaskTypes WHERE BusinessID = ? AND Name = ? LIMIT 1",
[$qOrder['BusinessID'], $taskTypeName]
);
$taskTypeID = $qTaskType ? (int) $qTaskType['ID'] : 0;
}
// Prevent duplicate tasks
$qExisting = queryOne(
"SELECT ID FROM Tasks WHERE OrderID = ? AND TaskTypeID = ? LIMIT 1",
[$OrderID, $taskTypeID]
);
if (!$qExisting && $taskTypeID > 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
}
}