-- Slice 8b-4c · Retirement AJAX perms

-- PC (originator): startRetirement + expense CRUD + submitRetirement + Admin gets all
INSERT INTO user_groups_perms (user_group_id, controller_allowed, action_allowed, created_by, created_on)
SELECT g.c0d, 'ServiceRequisitions', a.act, 0, NOW()
FROM (
    SELECT 'startRetirementAj'  AS act UNION ALL
    SELECT 'addExpenseAj'              UNION ALL
    SELECT 'deleteExpenseAj'           UNION ALL
    SELECT 'submitRetirementAj'
) a
CROSS JOIN user_groups g
WHERE g.user_group_name IN ('Projects Coordinator','Administrator')
  AND NOT EXISTS (SELECT 1 FROM user_groups_perms p WHERE p.user_group_id = g.c0d AND p.controller_allowed = 'ServiceRequisitions' AND p.action_allowed = a.act);

-- Accountant/FM/Admin: approve + send back
INSERT INTO user_groups_perms (user_group_id, controller_allowed, action_allowed, created_by, created_on)
SELECT g.c0d, 'ServiceRequisitions', a.act, 0, NOW()
FROM (
    SELECT 'approveRetirementAj'  AS act UNION ALL
    SELECT 'sendBackRetirementAj'
) a
CROSS JOIN user_groups g
WHERE g.user_group_name IN ('Accountant','Finance Manager','Administrator')
  AND NOT EXISTS (SELECT 1 FROM user_groups_perms p WHERE p.user_group_id = g.c0d AND p.controller_allowed = 'ServiceRequisitions' AND p.action_allowed = a.act);
