/** * Get the user's active cart (status=0) if one exists * Optionally filter by BusinessID to check for cart at specific business */ param name="url.UserID" type="numeric" default=0; param name="url.BusinessID" type="numeric" default=0; if (url.UserID == 0) { writeOutput(serializeJSON({ "OK": false, "ERROR": "UserID is required" })); abort; } // Build query with optional business filter 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 = :userId AND o.StatusID = 0 "; params = [{ name: "userId", value: url.UserID, cfsqltype: "integer" }]; if (url.BusinessID > 0) { sql &= " AND o.BusinessID = :businessId"; params.append({ name: "businessId", value: url.BusinessID, cfsqltype: "integer" }); } sql &= " ORDER BY o.AddedOn DESC LIMIT 1"; query name="qCart" datasource="#request.ds#" { writeOutput(sql); for (p in params) { cfqueryparam(value=p.value, cfsqltype=p.cfsqltype); } } if (qCart.recordCount == 0) { writeOutput(serializeJSON({ "OK": true, "HAS_CART": false, "CART": javacast("null", "") })); abort; } writeOutput(serializeJSON({ "OK": true, "HAS_CART": true, "CART": { "OrderID": qCart.OrderID, "OrderUUID": qCart.OrderUUID, "BusinessID": qCart.BusinessID, "BusinessName": qCart.BusinessName, "OrderTypeID": qCart.OrderTypeID, "OrderTypeName": qCart.OrderTypeName, "ServicePointID": qCart.ServicePointID, "ServicePointName": qCart.ServicePointName, "ItemCount": qCart.ItemCount } }));