Looks up actual ShardUUID, Major, Minor from BeaconHardware instead of deriving from Business/ServicePoint tables. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
214 lines
7.6 KiB
Text
214 lines
7.6 KiB
Text
<cfsetting showdebugoutput="false">
|
|
<cfsetting enablecfoutputonly="true">
|
|
|
|
<cffunction name="apiAbort" access="public" returntype="void" output="true">
|
|
<cfargument name="payload" type="struct" required="true">
|
|
<cfcontent type="application/json; charset=utf-8">
|
|
<cfoutput>#serializeJSON(arguments.payload)#</cfoutput>
|
|
<cfabort>
|
|
</cffunction>
|
|
|
|
<cffunction name="readJsonBody" access="public" returntype="struct" output="false">
|
|
<cfset var raw = getHttpRequestData().content>
|
|
<cfif isNull(raw) OR len(trim(raw)) EQ 0>
|
|
<cfreturn {}>
|
|
</cfif>
|
|
<cftry>
|
|
<cfset var data = deserializeJSON(raw)>
|
|
<cfif isStruct(data)>
|
|
<cfreturn data>
|
|
<cfelse>
|
|
<cfreturn {}>
|
|
</cfif>
|
|
<cfcatch>
|
|
<cfreturn {}>
|
|
</cfcatch>
|
|
</cftry>
|
|
</cffunction>
|
|
|
|
<cfset data = readJsonBody()>
|
|
<cfset TaskID = val( structKeyExists(data,"TaskID") ? data.TaskID : 0 )>
|
|
|
|
<cfif TaskID LTE 0>
|
|
<cfset apiAbort({ "OK": false, "ERROR": "missing_params", "MESSAGE": "TaskID is required." })>
|
|
</cfif>
|
|
|
|
<cftry>
|
|
<!--- Get the task and linked order details (no categories - using task types only) --->
|
|
<cfset qTask = queryExecute("
|
|
SELECT
|
|
t.ID AS TaskID,
|
|
t.BusinessID,
|
|
t.OrderID,
|
|
t.TaskTypeID,
|
|
t.CreatedOn,
|
|
t.ClaimedByUserID,
|
|
t.ServicePointID AS TaskServicePointID,
|
|
tt.Name AS TaskTypeName,
|
|
tt.Color AS TaskTypeColor,
|
|
o.ID AS OID,
|
|
o.UUID AS OrderUUID,
|
|
o.UserID AS OrderUserID,
|
|
o.OrderTypeID,
|
|
o.StatusID AS OrderStatusID,
|
|
o.ServicePointID AS OrderServicePointID,
|
|
o.Remarks,
|
|
o.SubmittedOn,
|
|
o.TipAmount,
|
|
o.DeliveryFee,
|
|
b.TaxRate,
|
|
COALESCE(sp.Name, tsp.Name) AS ServicePointName,
|
|
COALESCE(sp.TypeID, tsp.TypeID) AS ServicePointTypeID,
|
|
COALESCE(sp.ID, tsp.ID) AS ServicePointID,
|
|
u.ID AS CustomerUserID,
|
|
u.FirstName,
|
|
u.LastName,
|
|
u.ContactNumber,
|
|
u.ImageExtension AS CustomerImageExtension
|
|
FROM Tasks t
|
|
LEFT JOIN tt_TaskTypes tt ON tt.ID = t.TaskTypeID
|
|
LEFT JOIN Orders o ON o.ID = t.OrderID
|
|
LEFT JOIN Businesses b ON b.ID = t.BusinessID
|
|
LEFT JOIN ServicePoints sp ON sp.ID = o.ServicePointID
|
|
LEFT JOIN ServicePoints tsp ON tsp.ID = t.ServicePointID
|
|
LEFT JOIN Users u ON u.ID = COALESCE(NULLIF(o.UserID, 0), NULLIF(t.UserID, 0))
|
|
WHERE t.ID = ?
|
|
", [ { value = TaskID, cfsqltype = "cf_sql_integer" } ], { datasource = "payfrit" })>
|
|
|
|
<cfif qTask.recordCount EQ 0>
|
|
<cfset apiAbort({ "OK": false, "ERROR": "not_found", "MESSAGE": "Task not found." })>
|
|
</cfif>
|
|
|
|
<!--- Build basic task info --->
|
|
<cfset taskTitle = "Task ##" & qTask.TaskID>
|
|
<cfif qTask.OrderID GT 0>
|
|
<cfset taskTitle = "Order ##" & qTask.OrderID>
|
|
</cfif>
|
|
|
|
<!--- Check if user photo file exists (try both .jpg and .png) --->
|
|
<cfset customerPhotoUrl = "">
|
|
<cfif qTask.CustomerUserID GT 0>
|
|
<cfset uploadDir = expandPath("/uploads/users/")>
|
|
<cfset jpgPath = uploadDir & qTask.CustomerUserID & ".jpg">
|
|
<cfset pngPath = uploadDir & qTask.CustomerUserID & ".png">
|
|
<cfset pngPathUpper = uploadDir & qTask.CustomerUserID & ".PNG">
|
|
<cfif fileExists(jpgPath)>
|
|
<cfset customerPhotoUrl = "https://biz.payfrit.com/uploads/users/" & qTask.CustomerUserID & ".jpg">
|
|
<cfelseif fileExists(pngPath)>
|
|
<cfset customerPhotoUrl = "https://biz.payfrit.com/uploads/users/" & qTask.CustomerUserID & ".png">
|
|
<cfelseif fileExists(pngPathUpper)>
|
|
<cfset customerPhotoUrl = "https://biz.payfrit.com/uploads/users/" & qTask.CustomerUserID & ".PNG">
|
|
</cfif>
|
|
</cfif>
|
|
|
|
<cfset result = {
|
|
"TaskID": qTask.TaskID,
|
|
"TaskBusinessID": qTask.BusinessID,
|
|
"TaskTypeID": qTask.TaskTypeID ?: 1,
|
|
"TaskTypeName": qTask.TaskTypeName ?: "",
|
|
"TaskTypeColor": len(trim(qTask.TaskTypeColor)) ? qTask.TaskTypeColor : "##9C27B0",
|
|
"TaskTitle": taskTitle,
|
|
"TaskCreatedOn": dateFormat(qTask.CreatedOn, "yyyy-mm-dd") & "T" & timeFormat(qTask.CreatedOn, "HH:mm:ss"),
|
|
"TaskStatusID": qTask.ClaimedByUserID GT 0 ? 1 : 0,
|
|
"OrderID": qTask.OrderID ?: 0,
|
|
"OrderRemarks": qTask.Remarks ?: "",
|
|
"OrderSubmittedOn": isDate(qTask.SubmittedOn) ? (dateFormat(qTask.SubmittedOn, "yyyy-mm-dd") & "T" & timeFormat(qTask.SubmittedOn, "HH:mm:ss")) : "",
|
|
"OrderTotal": 0,
|
|
"OrderTotalCents": 0,
|
|
"ServicePointID": qTask.ServicePointID ?: 0,
|
|
"ServicePointName": qTask.ServicePointName ?: "",
|
|
"ServicePointTypeID": qTask.ServicePointTypeID ?: 0,
|
|
"DeliveryAddress": "",
|
|
"DeliveryLat": 0,
|
|
"DeliveryLng": 0,
|
|
"CustomerUserID": qTask.CustomerUserID ?: 0,
|
|
"CustomerFirstName": qTask.FirstName ?: "",
|
|
"CustomerLastName": qTask.LastName ?: "",
|
|
"CustomerPhone": qTask.ContactNumber ?: "",
|
|
"CustomerPhotoUrl": customerPhotoUrl,
|
|
"BeaconUUID": "",
|
|
"BeaconMajor": 0,
|
|
"BeaconMinor": 0,
|
|
"LineItems": [],
|
|
"TableMembers": []
|
|
}>
|
|
|
|
<!--- Get beacon sharding info from BeaconHardware (for auto-completion on Works app) --->
|
|
<!--- BeaconHardware stores: ShardUUID, Major, Minor for each registered beacon --->
|
|
<cfif val(qTask.ServicePointID) GT 0>
|
|
<cfset qBeacon = queryExecute("
|
|
SELECT bh.ShardUUID, bh.Major, bh.Minor
|
|
FROM BeaconHardware bh
|
|
WHERE bh.ServicePointID = ?
|
|
AND bh.Status IN ('assigned', 'verified')
|
|
LIMIT 1
|
|
", [ { value = qTask.ServicePointID, cfsqltype = "cf_sql_integer" } ], { datasource = "payfrit" })>
|
|
<cfif qBeacon.recordCount GT 0>
|
|
<cfset result.BeaconUUID = qBeacon.ShardUUID>
|
|
<cfset result.BeaconMajor = val(qBeacon.Major)>
|
|
<cfset result.BeaconMinor = val(qBeacon.Minor)>
|
|
</cfif>
|
|
</cfif>
|
|
|
|
<!--- Get order line items if there's an order --->
|
|
<cfif qTask.OrderID GT 0>
|
|
<cfset qLineItems = queryExecute("
|
|
SELECT
|
|
oli.ID AS OrderLineItemID,
|
|
oli.ParentOrderLineItemID,
|
|
oli.ItemID,
|
|
oli.Price AS LineItemPrice,
|
|
oli.Quantity,
|
|
oli.Remark,
|
|
i.ID AS IID,
|
|
i.Name AS ItemName,
|
|
i.ParentItemID,
|
|
i.Price AS ItemPrice,
|
|
i.IsCheckedByDefault
|
|
FROM OrderLineItems oli
|
|
INNER JOIN Items i ON i.ID = oli.ItemID
|
|
WHERE oli.OrderID = ?
|
|
AND oli.IsDeleted = b'0'
|
|
ORDER BY oli.ID
|
|
", [ { value = qTask.OrderID, cfsqltype = "cf_sql_integer" } ], { datasource = "payfrit" })>
|
|
|
|
<cfset subtotal = 0>
|
|
<cfloop query="qLineItems">
|
|
<cfset subtotal = subtotal + (qLineItems.LineItemPrice * qLineItems.Quantity)>
|
|
<cfset arrayAppend(result.LineItems, {
|
|
"LineItemID": qLineItems.OrderLineItemID,
|
|
"ParentLineItemID": qLineItems.ParentOrderLineItemID,
|
|
"ItemID": qLineItems.ItemID,
|
|
"ItemName": qLineItems.ItemName,
|
|
"ItemPrice": qLineItems.LineItemPrice,
|
|
"Quantity": qLineItems.Quantity,
|
|
"Remark": qLineItems.Remark,
|
|
"IsModifier": qLineItems.ParentOrderLineItemID GT 0
|
|
})>
|
|
</cfloop>
|
|
|
|
<!--- Calculate order total: subtotal + tax + tip + delivery (if delivery) + platform fee --->
|
|
<cfset taxAmount = subtotal * val(qTask.TaxRate)>
|
|
<cfset tipAmount = val(qTask.TipAmount)>
|
|
<cfset deliveryFee = (val(qTask.OrderTypeID) EQ 3) ? val(qTask.DeliveryFee) : 0>
|
|
<cfset platformFee = subtotal * 0.05>
|
|
<cfset totalAmount = subtotal + taxAmount + tipAmount + deliveryFee + platformFee>
|
|
<cfset result.OrderTotal = numberFormat(totalAmount, "0.00")>
|
|
<cfset result.OrderTotalCents = round(totalAmount * 100)>
|
|
</cfif>
|
|
|
|
<cfset apiAbort({
|
|
"OK": true,
|
|
"ERROR": "",
|
|
"TASK": result
|
|
})>
|
|
|
|
<cfcatch>
|
|
<cfset apiAbort({
|
|
"OK": false,
|
|
"ERROR": "server_error",
|
|
"MESSAGE": "Error loading task details",
|
|
"DETAIL": cfcatch.message
|
|
})>
|
|
</cfcatch>
|
|
</cftry>
|