-- ═══════════════════════════════════════════════════════════════════════════
-- Seed cash-out approval chains + controllers/actions + role perms
-- ═══════════════════════════════════════════════════════════════════════════

-- New approval chain for petty cash / imprest / any simple cash-out --------
INSERT INTO fin_chain_defs (c0d, chain_key, title, description, status, created_by, edited_by)
VALUES (10, 'PETTY_CASH_APPROVAL', 'Petty Cash / Cash-Out Approval',
        'Simple 2-step approval for any small cash request (petty cash, imprest draws, small advances). PM checks purpose + amount; FM signs off before Accountant prepares the PV.',
        'ACTIVE', 0, 0)
ON DUPLICATE KEY UPDATE title = VALUES(title), description = VALUES(description);

INSERT INTO fin_chain_steps (c0d, chain_id, step_no, step_key, step_label, role_required, is_terminal, description, sort_order, created_by, edited_by)
VALUES
  (30, 10, 1, 'PM_CHECK',   'PM Check',   'PROJECT MANAGER', 0, 'PM confirms purpose is legitimate and amount reasonable', 1, 0, 0),
  (31, 10, 2, 'FM_APPROVE', 'FM Approve', 'FINANCE MANAGER', 1, 'Finance Manager signs off; Accountant can then prepare PV',   2, 0, 0)
ON DUPLICATE KEY UPDATE step_label = VALUES(step_label), role_required = VALUES(role_required);

-- Register new controllers + actions --------------------------------------

INSERT IGNORE INTO controllers_actions (controller_name, action_name) VALUES
  ('Lpos','index'), ('Lpos','view'), ('Lpos','submitAj'),
  ('PettyCash','raise'), ('PettyCash','save'),
  ('Payables','index'), ('Payables','prepQueue'),
  ('PaymentVouchers','index'), ('PaymentVouchers','view'),
  ('PaymentVouchers','prepAj'), ('PaymentVouchers','scheduleAj'),
  ('Payments','index'), ('Payments','record');

-- Perms: who can do what ---------------------------------------------------

-- Everyone who touches a project can VIEW Lpos + raise Petty Cash
INSERT IGNORE INTO user_groups_perms (user_group_id, controller_allowed, action_allowed)
SELECT ug.c0d, x.c, x.a
  FROM user_groups ug
  CROSS JOIN (
    SELECT 'Lpos' c, 'index' a UNION
    SELECT 'Lpos','view' UNION
    SELECT 'PettyCash','raise' UNION
    SELECT 'PettyCash','save' UNION
    SELECT 'Lpos','submitAj'
  ) x
 WHERE ug.user_group_name IN ('Projects Coordinator','Project Manager','Field Supervisor','Procurement','Stores','CRM','Administrator');

-- Accountant + FM + Executive + Administrator see Payables queue + PV admin
INSERT IGNORE INTO user_groups_perms (user_group_id, controller_allowed, action_allowed)
SELECT ug.c0d, x.c, x.a
  FROM user_groups ug
  CROSS JOIN (
    SELECT 'Lpos' c, 'index' a UNION
    SELECT 'Lpos','view' UNION
    SELECT 'Payables','index' UNION
    SELECT 'Payables','prepQueue' UNION
    SELECT 'PaymentVouchers','index' UNION
    SELECT 'PaymentVouchers','view' UNION
    SELECT 'PaymentVouchers','prepAj' UNION
    SELECT 'PaymentVouchers','scheduleAj' UNION
    SELECT 'Payments','index' UNION
    SELECT 'Payments','record'
  ) x
 WHERE ug.user_group_name IN ('Accountant','Finance Manager','Executive','Administrator');
