payfrit-api/api/orders/getActiveCart.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

68 lines
2 KiB
PHP

<?php
require_once __DIR__ . '/../helpers.php';
runAuth();
/**
* Get user's active cart (status=0) if one exists
* GET: ?UserID=int&BusinessID=int (optional)
*/
$UserID = (int) ($_GET['UserID'] ?? 0);
$BusinessID = (int) ($_GET['BusinessID'] ?? 0);
if ($UserID <= 0) {
jsonResponse(['OK' => false, 'ERROR' => 'UserID is required']);
}
$sql = "
SELECT
o.ID AS OrderID,
o.UUID AS OrderUUID,
o.BusinessID,
b.Name AS BusinessName,
o.OrderTypeID,
COALESCE(ot.Name, 'Undecided') AS OrderTypeName,
o.ServicePointID,
COALESCE(sp.Name, '') AS ServicePointName,
(SELECT COUNT(*) FROM OrderLineItems oli
WHERE oli.OrderID = o.ID AND oli.ParentOrderLineItemID = 0 AND oli.IsDeleted = 0) AS ItemCount
FROM Orders o
INNER JOIN Businesses b ON b.ID = o.BusinessID
LEFT JOIN tt_OrderTypes ot ON ot.ID = o.OrderTypeID
LEFT JOIN ServicePoints sp ON sp.ID = o.ServicePointID
WHERE o.UserID = ?
AND o.StatusID = 0
";
$params = [$UserID];
if ($BusinessID > 0) {
$sql .= " AND o.BusinessID = ?";
$params[] = $BusinessID;
}
$sql .= " ORDER BY o.AddedOn DESC LIMIT 1";
try {
$rows = queryTimed($sql, $params);
$cart = $rows[0] ?? null;
if (!$cart) {
jsonResponse(['OK' => true, 'HAS_CART' => false, 'CART' => null]);
}
jsonResponse(['OK' => true, 'HAS_CART' => true, 'CART' => [
'OrderID' => (int) $cart['OrderID'],
'OrderUUID' => $cart['OrderUUID'],
'BusinessID' => (int) $cart['BusinessID'],
'BusinessName' => $cart['BusinessName'],
'OrderTypeID' => (int) $cart['OrderTypeID'],
'OrderTypeName' => $cart['OrderTypeName'],
'ServicePointID' => (int) $cart['ServicePointID'],
'ServicePointName' => $cart['ServicePointName'],
'ItemCount' => (int) $cart['ItemCount'],
]]);
} catch (Exception $e) {
jsonResponse(['OK' => false, 'ERROR' => $e->getMessage()]);
}