Prevent duplicate cash tasks for same order

Check for existing active (uncompleted) cash task before creating
a new one. Prevents duplicate "Pay With Cash" tasks if order status
changes are triggered multiple times.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
John Mizerek 2026-02-17 09:41:58 -08:00
parent 7f4af8b910
commit 43afe9ae8c

View file

@ -192,19 +192,28 @@
", [ { value = OrderID, cfsqltype = "cf_sql_integer" } ], { datasource = "payfrit" })>
<cfif qCashPayment.recordCount GT 0 AND val(qCashPayment.PaymentPaidInCash) GT 0 AND qCashPayment.PaymentStatus EQ "pending">
<!--- Get "Pay With Cash" task type ID for this business --->
<cfset qCashTaskType = queryExecute("
SELECT ID FROM tt_TaskTypes WHERE BusinessID = ? AND Name LIKE '%Cash%' LIMIT 1
", [ { value = qOrder.BusinessID, cfsqltype = "cf_sql_integer" } ], { datasource = "payfrit" })>
<!--- Check if there's already an active cash task for this order --->
<cfset qExistingCashTask = queryExecute("
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
", [ { value = OrderID, cfsqltype = "cf_sql_integer" } ], { datasource = "payfrit" })>
<cfset cashTaskTypeID = qCashTaskType.recordCount GT 0 ? qCashTaskType.ID : 0>
<cfset cashTaskTitle = "Pay With Cash - Order ###OrderID#">
<cfif len(qCashPayment.ServicePointName)>
<cfset cashTaskTitle = cashTaskTitle & " (" & qCashPayment.ServicePointName & ")">
</cfif>
<cfif qExistingCashTask.recordCount EQ 0>
<!--- Get "Pay With Cash" task type ID for this business --->
<cfset qCashTaskType = queryExecute("
SELECT ID FROM tt_TaskTypes WHERE BusinessID = ? AND Name LIKE '%Cash%' LIMIT 1
", [ { value = qOrder.BusinessID, cfsqltype = "cf_sql_integer" } ], { datasource = "payfrit" })>
<cfset queryExecute("
INSERT INTO Tasks (
<cfset cashTaskTypeID = qCashTaskType.recordCount GT 0 ? qCashTaskType.ID : 0>
<cfset cashTaskTitle = "Pay With Cash - Order ###OrderID#">
<cfif len(qCashPayment.ServicePointName)>
<cfset cashTaskTitle = cashTaskTitle & " (" & qCashPayment.ServicePointName & ")">
</cfif>
<cfset queryExecute("
INSERT INTO Tasks (
BusinessID,
OrderID,
TaskTypeID,
@ -228,7 +237,8 @@
{ value = cashTaskTitle, cfsqltype = "cf_sql_varchar" },
{ value = qCashPayment.ServicePointID, cfsqltype = "cf_sql_integer", null = isNull(qCashPayment.ServicePointID) OR val(qCashPayment.ServicePointID) EQ 0 }
], { datasource = "payfrit" })>
<cfset cashTaskCreated = true>
<cfset cashTaskCreated = true>
</cfif><!--- qExistingCashTask.recordCount EQ 0 --->
</cfif>
</cfif>
<cfcatch>