-- Slice 8c-Certificate · WorksIpcs perms

/* ── Authors (PC/PM/Admin): full CRUD + approve ──────────────────── */
INSERT INTO user_groups_perms (user_group_id, controller_allowed, action_allowed, created_by, created_on)
SELECT g.c0d, 'WorksIpcs', a.act, 0, NOW()
FROM (
    SELECT 'index'          AS act UNION ALL
    SELECT 'list'                  UNION ALL
    SELECT 'create'                UNION ALL
    SELECT 'view'                  UNION ALL
    SELECT 'createAj'              UNION ALL
    SELECT 'setLineQtyAj'          UNION ALL
    SELECT 'setAdvanceAj'          UNION ALL
    SELECT 'approveAj'
) a
CROSS JOIN user_groups g
WHERE g.user_group_name IN ('Projects Coordinator','Project Manager','Administrator')
  AND NOT EXISTS (
    SELECT 1 FROM user_groups_perms p
    WHERE p.user_group_id = g.c0d
      AND p.controller_allowed = 'WorksIpcs'
      AND p.action_allowed     = a.act
  );

/* ── Read-only (FS/Procurement/FM/Accountant/Exec): index + list + view ─ */
INSERT INTO user_groups_perms (user_group_id, controller_allowed, action_allowed, created_by, created_on)
SELECT g.c0d, 'WorksIpcs', a.act, 0, NOW()
FROM (SELECT 'index' AS act UNION ALL SELECT 'list' UNION ALL SELECT 'view') a
CROSS JOIN user_groups g
WHERE g.user_group_name IN ('Field Supervisor','Procurement','Finance Manager','Accountant','Executive')
  AND NOT EXISTS (
    SELECT 1 FROM user_groups_perms p
    WHERE p.user_group_id = g.c0d
      AND p.controller_allowed = 'WorksIpcs'
      AND p.action_allowed     = a.act
  );
