Stop ticket re-opening after WP Closed

I’ve edited this post to work with current work periods

I’ll be reusing actions/rules from the following tutorial:

With the above tutorial, I did not see anywhere where the ticket number or ID is passed to any actions/rules.

The way around it is to manually enter the ticket number (visible on the top left of the ticket). There may be another way, but I know this one works.

Script:

Script Name: Reopen Check
Handler: reopen

function check(ticketid)
{
  qtd = "SELECT FORMAT(Date, 'yyyy-MM-ddTHH:mm:ss') FROM Tickets WHERE Id = '"+ticketid+"'";
  var rtd = sql.Exec(qtd)[0];
  
  qwp = "SELECT FORMAT(StartDate, 'yyyy-MM-ddTHH:mm:ss') FROM WorkPeriods WHERE Id = (SELECT MAX(Id) FROM WorkPeriods WHERE StartDate = EndDate)";
  var rwp = sql.Exec(qwp)[0];

  if (rtd >= rwp)
    {
      return 1;
    }
  else
    {
      return 0;
    }
}

Actions:

26-04-2020_19;08_1587949710_Samba.Presentation
Action Name: Reopen - Load Ticket
Action Type: Load Ticket
Ticket Number: [:Ticket Number]

26-04-2020_19;08_1587949727_Samba.Presentation
(if you don’t already have one)
Action Name: Execute Automation Command
Action Type: Execute Automation Command
Automation Command Name: [:Command Name]
Command Value: [:Command Value]

26-04-2020_19;32_1587951167_Samba.Presentation
Action Name: MSG TEST
Action Type: Show Message
Message: [:Message]

Rules:
26-04-2020_19;34_1587951291_Samba.Presentation
26-04-2020_21;56_1587959798_Samba.Presentation
Rule Name: Reopen
Event Name: Automation Command Executed
Execute Rule if: [Matches]
Custom Constraint:
**Automation Command [Equals] Reopen
** or set this to the button you currently have visible on the ticket screen

Actions to Add:

Action: Reopen - Load Ticket
Constraint:
Ticket Number: [?Ticket Number;\d*;;OCN]

Action: Execute Automation Command
Constraint: {CALL:reopen.check(‘{TICKET ID}’)} == 1
Command Name: Reopen - Reopen Ticket
Command Value: {TICKET ID}

Action: MSG TEST
Constraint: {CALL:reopen.check(‘{TICKET ID}’)} == 0
Message: Ticket out-of-range\rUnable to reopen

26-04-2020_19;09_1587949770_Samba.Presentation
Change the Custom Constraint for RU Reopen Settled Ticket:

Custom Constraint:
Automation Command Name [Equals] Reopen - Reopen Ticket

Reopening ticket from today:
26-04-2020_19;30_1587951018_%pn

Reopening ticket from not today:
26-04-2020_19;43_1587951818_%pn

EDIT: Updated script