-- =====================================================================
-- Section 4: role permissions + ticket print tracking.
-- Run once in phpMyAdmin against wbcorp_webapp.
-- =====================================================================

-- Per-company overrides of what Supervisor / Operator may do (set on the Role Permissions
-- screen). Anything without a row here uses the built-in defaults.
CREATE TABLE tblCompany_Role_Permission (
  company_id     INT NOT NULL,
  role_id        INT NOT NULL,
  permission_key VARCHAR(100) NOT NULL,
  allowed        TINYINT(1) NOT NULL,
  updated_by     INT NULL,
  updated_at     DATETIME DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (company_id, role_id, permission_key),
  FOREIGN KEY (company_id) REFERENCES tblCompany(company_id) ON DELETE CASCADE,
  FOREIGN KEY (role_id) REFERENCES tblRole(role_id)
) ENGINE=InnoDB;

-- Print tracking: 0 = never printed. The first print right after completion is the original;
-- every later print is a logged Reprint and the ticket is labelled "REPRINT - Copy #n".
ALTER TABLE tblTransaction_Job
  ADD COLUMN print_count INT NOT NULL DEFAULT 0 AFTER void_reason,
  ADD COLUMN first_printed_at DATETIME NULL AFTER print_count;
