After ending a work period then starting a new one the order numbers are continuing instead of starting from 1

Alright, we’ll have to change the Event in the rule for Tagging Ticket:

2020-06-26_23;21_1593235278_Samba.Presentation

Event Name: Before Ticket Closing

Custom Constraints:
{TICKET TAG:orderNumber} [IS NULL]
[=TN('{ORDER COUNT}')] [GREATER] 0

THis will now only tag a ticket when there is an order on it and the ticket has not been tagged yet for an order number.

EDIT: updated with recommended Event Name

Thank you a lot! You are great man. :smiley:

Anytime. I seem to have a lot more fee time these days lol

Not sure ticket closing will be 100%, isnt printing on before ticket closing?

It is.

Do you think the “Before Closing” event would be better?

Since the tag is generally going to be used on prints i would imagine it would make sence.

1 Like

Gotcha and makes sense.

@ozgurekin87 go and change the rule event for tagging the ticket from “Ticket Closing” to “Before Ticket Closing”

And check rule sort order to make sure it is before any print related rules.

1 Like

Great point! That still trips me up from time to time.

Before Ticket Closing event did the trick. Thanks

Hey since its working now would you create a tutorial :slight_smile:

1 Like

@Memo I have followed this instruction, however everything work except printing the Tag on the Ticket.
I am using the “ACM Auto Print Rule for Cash” any recommendations?

Like JTR suggested, check the rules’ sort order and make sure the order # rule is before the print rule. The rules engine executes automation based on that order.

Also, there’s a better way of doing what I had originally posted that will save the round trips to the database.

I’ll whip something up a bit later and share it.

@Memo thanks for your response. I have placed the AMC prints rules as the last items in the sort list, however the tag does not print. Only if I reprint the bill it shows.

Looking forward to the update method. Thank you

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:
image
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:
image
Action Name: ON Execute Script
Action Type: Execute Script
Function: [:Function x.y()]

image
Action Name: ON Update Ticket Tag
Action Type: Update Ticket Tag
Tag Name: [:Tag Name]
Tag Value: [:Tag Value]

image
Action Name: ON Execute Automation Command
Automation Command Name: [:Command Name]
Command Value: [:Command Value]

Rules:

image
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()}

image
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()

image
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:
image
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

@Memo I will definetly use this method, I have tried so many other method but always seems to leave a duplicated.

My only other question is MC3 will this work or should I just setup for PCs. Thank you again this will be of great help for me.

For the reset method I am using Virtual Work period. I know the management users wont click the reset button every morning. Lol.

So I was planning to do a CRON job in the database to reset the number based on the same time set for the virtual workperiod.

It might work with mc3 but you should try it and see. MC3 can trigger rule events but it’s not the same. Not all events are available to it.

@Jesse Thank you I will test and provide feedback.

Think

IS NOT NULL

should be

IS NULL
1 Like

@memo thank you for this solution, however I am still not getting the TAG printed out on my tickets.
I am using the Fast Cash button. When I reprint the ticket I am seeing the number.

Update: Samba is great, I was able to use the Rule Debugger to see the flow of my rules and the “BeforeTicketClosing Rules” are running after “Payment Processed” rules which contain the print bill command.

How can I correct this, all auto print buttons are in Payment Processed rules?