--
-- $Id$
--
-- BLST#68640 Inrichten Facilitor Flex voor de Belastingdienst
-- Medewerkers DUO in juiste autorisatiegroep zetten

DEFINE thisfile = 'CFDF#68640.SQL'
DEFINE dbuser   = '^CFDF'

SET ECHO ON
SET DEFINE ON
COLUMN fcltlogfile NEW_VALUE fcltlogfile NOPRINT;
COLUMN fcltcusterr NEW_VALUE fcltcusterr NOPRINT;
WHENEVER SQLERROR CONTINUE;

SELECT adm.getscriptspoolfile ('&thisfile') AS fcltlogfile FROM DUAL;

SPOOL &fcltlogfile
WHENEVER SQLERROR EXIT;

SELECT adm.checkscriptcust ('&dbuser') AS fcltcusterr FROM DUAL;

WHENEVER SQLERROR CONTINUE;
PROMPT &fcltcusterr
SET DEFINE OFF

------ payload begin ------

DECLARE
    --   Onderstaande cursor bevat alle mdw DUO/ fac_groep_key = 21;
    CURSOR c_mdw IS
            SELECT p.prs_perslid_key
          FROM prs_perslid p
         WHERE     p.prs_afdeling_key = 21
               AND p.prs_perslid_verwijder IS NULL
               AND p.prs_perslid_key NOT IN (SELECT g.prs_perslid_key
                                               FROM fac_gebruikersgroep g
                                              WHERE g.fac_groep_key = 21);

BEGIN
    FOR rec IN c_mdw
    LOOP
        BEGIN
            INSERT INTO fac_gebruikersgroep (fac_groep_key, prs_perslid_key)
                 VALUES (21, rec.prs_perslid_key);

            COMMIT;
        END;
    END LOOP;
END;
/

------ payload end   ------

SET DEFINE OFF

BEGIN
    adm.systrackscriptId (
        '$Id$',
        1);
END;
/

COMMIT;
SET ECHO OFF
SPOOL OFF
SET DEFINE ON
PROMPT Logfile of this upgrade is: &fcltlogfile