-- DentalCare v0.8.0.3
-- Patient lifetime visit ledger and controlled General Staff Receptionist/Cashier role.

UPDATE `{{prefix}}roles`
SET `name`='General Staff',
    `description`='Combined Receptionist and Cashier role for registration, appointments, queue, communications, cashier sessions, payments, receipts, and checkout without clinical, inventory, approval, or system authority'
WHERE `code`='staff';

INSERT INTO `{{prefix}}permissions` (`code`,`name`,`module_group`,`description`) VALUES
('reports.frontdesk','View front-desk operational reports','Reports','View front-desk appointment, queue, and service reports'),
('appointment.service_disposition','Defer or move appointment services','Patient Care','Defer, move, or preserve unfinished appointment services with an audited reason'),
('cashier.statement.print','Print cashier invoices, receipts, and statements','Finance','Print authorized invoices, receipts, statements, and patient ledgers'),
('cashier.session.manage','Open and close own cashier session','Finance','Open, operate, reconcile, and close only the signed-in user cashier session'),
('records.print','Print and export authorized records','Reports','Print patient, finance, schedule, and other records within the user assigned scope')
ON DUPLICATE KEY UPDATE `name`=VALUES(`name`),`module_group`=VALUES(`module_group`),`description`=VALUES(`description`);

INSERT INTO `{{prefix}}role_permissions` (`role_id`,`permission_id`,`allowed`)
SELECT r.id,p.id,1
FROM `{{prefix}}roles` r
JOIN `{{prefix}}permissions` p
WHERE r.code='staff'
  AND p.code IN (
    'patients.view','patients.manage','appointments.manage','queue.manage','communications.manage',
    'reports.view','reports.frontdesk','records.print','appointment.service_disposition',
    'billing.manage','cashier.statement.print','cashier.session.manage'
  )
ON DUPLICATE KEY UPDATE allowed=VALUES(allowed);
