/** * Get pending orders for a user at a specific business * Used when a beacon is detected to check if user has an order to pick up * * Query params: * UserID - the user's ID * BusinessID - the business ID * * Returns orders with status 1-3 (Submitted, Preparing, Ready) */ response = { "OK": false }; try { UserID = val(url.UserID ?: 0); BusinessID = val(url.BusinessID ?: 0); if (UserID LTE 0) { response["ERROR"] = "missing_user"; response["MESSAGE"] = "UserID is required"; writeOutput(serializeJSON(response)); abort; } if (BusinessID LTE 0) { response["ERROR"] = "missing_business"; response["MESSAGE"] = "BusinessID is required"; writeOutput(serializeJSON(response)); abort; } // Get orders with status 1 (Submitted), 2 (Preparing), or 3 (Ready) // These are orders that are paid but not yet completed/picked up qOrders = queryExecute(" SELECT o.ID, o.UUID, o.OrderTypeID, o.StatusID, o.SubmittedOn, o.ServicePointID, sp.Name AS Name, b.Name AS BizName, (SELECT COALESCE(SUM(oli.Price * oli.Quantity), 0) FROM OrderLineItems oli WHERE oli.OrderID = o.ID AND oli.IsDeleted = 0 AND oli.ParentOrderLineItemID = 0) as Subtotal FROM Orders o LEFT JOIN ServicePoints sp ON sp.ID = o.ServicePointID LEFT JOIN Businesses b ON b.ID = o.BusinessID WHERE o.UserID = :userId AND o.BusinessID = :businessId AND o.StatusID IN (1, 2, 3) ORDER BY o.SubmittedOn DESC LIMIT 5 ", { userId: { value: UserID, cfsqltype: "cf_sql_integer" }, businessId: { value: BusinessID, cfsqltype: "cf_sql_integer" } }, { datasource: "payfrit" }); orders = []; for (row in qOrders) { statusName = ""; switch (row.StatusID) { case 1: statusName = "Submitted"; break; case 2: statusName = "Preparing"; break; case 3: statusName = "Ready for Pickup"; break; } orderTypeName = ""; switch (row.OrderTypeID) { case 1: orderTypeName = "Dine-In"; break; case 2: orderTypeName = "Takeaway"; break; case 3: orderTypeName = "Delivery"; break; } arrayAppend(orders, { "OrderID": row.ID, "UUID": row.UUID, "OrderTypeID": row.OrderTypeID, "OrderTypeName": orderTypeName, "StatusID": row.StatusID, "StatusName": statusName, "SubmittedOn": dateTimeFormat(row.SubmittedOn, "yyyy-mm-dd HH:nn:ss"), "ServicePointID": row.ServicePointID, "Name": len(trim(row.Name)) ? row.Name : "", "Name": len(trim(row.Name)) ? row.Name : "", "Subtotal": row.Subtotal }); } response["OK"] = true; response["ORDERS"] = orders; response["HAS_PENDING"] = arrayLen(orders) GT 0; } catch (any e) { response["ERROR"] = "server_error"; response["MESSAGE"] = e.message; } writeOutput(serializeJSON(response));