Alright, so instead of program setting value we’re going to use a sequence store in the database. With the way I originally posted has three round trips to the db and it leaves one open to a race condition: imagine two terminals select the value from the db at the same time or one selects before another updates, there could be duplicate order numbers. Not so with a sequence as the db engine handles locking.
SQL
IF OBJECT_ID('dbo.seq_OrderNumber') IS NULL
BEGIN
CREATE SEQUENCE dbo.seq_OrderNumber
START WITH 1
INCREMENT BY 1
CYCLE
NO CACHE;
END;
GO
Execute this in SSMS or use this database task file
place in %USERPROFILE%\Documents\SambaPOS5\Database Tasks
then navigate to Management → Settings → Database Tools and select the corresponding task and click “Execute Task”. A backup will happen automatically if you need to rollback for whatever reason.
[CB]Order_Number_Sequence.zip (318 Bytes)
Script:
Name: ON Order Number
Handler: orderNumber
Script:
function get()
{
var q = "SELECT NEXT VALUE FOR dbo.seq_OrderNumber;";
var r = sql.Query(q).First;
return Helper.Format(r,'0000');
}
function reset()
{
q = "ALTER SEQUENCE dbo.seq_OrderNumber RESTART;";
sql.Exec(q);
}
Actions:
Action Name: ON Execute Script
Action Type: Execute Script
Function: [:Function x.y()]
Action Name: ON Update Ticket Tag
Action Type: Update Ticket Tag
Tag Name: [:Tag Name]
Tag Value: [:Tag Value]
Action Name: ON Execute Automation Command
Automation Command Name: [:Command Name]
Command Value: [:Command Value]
Rules:
Rule Name: ON Set Custom Order Number
Event Name: Before Ticket Closing
Execute rule if: Matches All
Custom Constraints:
'{TICKET TAG:Order Number}'
[EQUALS] ''
[=TN('{ORDER COUNT}')]
[GREATER] 0
Actions:
ON Update Ticket Tag
Tag Name: Order Number
Tag Value: {CALL:orderNumber.get()}
Rule Name: ON Reset Order Number
Event Name: Automation Command Executed
Constrains:
Automation Command Name
[EQUALS] ON Reset Order Number
Actions:
ON Execute Script
Function x.y(): orderNumber.reset()
Rule Name: ON Reset Order Number on Work Period Closing
Event Name: Work Period Ended
Constraints:
1
[EQUALS] 0
Actions:
ON Execute Automation Command
Command Name: ON Reset Order Number
Automation Commands:
Name: ON Reset Order Number
Category: Navigation
Button Header: Reset\rOrder Number
Confirmation: Admin Pin
Mappings: None (more on this below)
There are a couple of things to note: the constraint for ON Reset Order Number on Work Period Closing
of 1 == 0
effectively disables that rule. You could use the mappings to enable/disable, but I like to do it this was as with a quick glance I can tell I disabled the rule. Remove the constraint if you want to automatically reset order numbers when the work period is closed.
For the automation command I specified “None” for mappings. Add a mapping if you want to have a navigation button to manually reset the order number. Be sure to have it admin pin confirmed so a regular user can’t reset it themselves.
Here’s an import you can use:
on_order_number.zip (1.3 KB)
Example database:
DefaultInstall_202301181111SQM.zip (524.5 KB)
EDIT: fixed SQL