-- Customer specific once-script voor BLOS

-- $Id$
--
-- Oncescript om de werkplekkoppelingen die nu lopen op organisatie_code om te zetten naar de vink-opties in de personenkaart

DEFINE thisfile = 'BLOS#68339_wp.SQL'
DEFINE dbuser   = '^BLOS'

SET ECHO ON
SET DEFINE ON
COLUMN fcltlogfile NEW_VALUE fcltlogfile NOPRINT;
WHENEVER SQLERROR EXIT;
SELECT adm.scriptspoolfile('&dbuser', '&thisfile') AS fcltlogfile FROM DUAL;
WHENEVER SQLERROR CONTINUE;
SPOOL &fcltlogfile
SET DEFINE OFF


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

-- Structuur - Cursor-views
---- Prs_perslid in Locatiemanagers met Organisatie_code => eigen_tabel_CODE - eigentabel_NAAM => Kenmerk_naam (vinkbox prs_kenmerk)
---- 1. Voor alle locatiemanagers waar koppeling op LABEL zit 

DECLARE
    v_errormsg       VARCHAR (200);
    v_errorhint      VARCHAR (200);
    oracle_err_num   NUMBER;
    oracle_err_mes   VARCHAR2 (150);
    currentversion   fac_module.fac_module_version%TYPE;
    v_aanduiding     VARCHAR (100);
    v_count          NUMBER;
   
    CURSOR c
    IS
SELECT p.prs_perslid_key,
       v2.prs_kenmerk_key
  FROM prs_perslid   p,
       prs_afdeling  a,
       (SELECT UPPER (ud.fac_usrdata_omschr) cluster_label,
               ud.fac_usrdata_code           cluster_label_code,
               ud.fac_usrdata_vervaldatum
          FROM fac_usrtab ut, fac_usrdata ud
         WHERE     ut.fac_usrtab_key = ud.fac_usrtab_key
               AND ut.fac_usrtab_key = 1
               AND fac_usrdata_verwijder IS NULL) v1,
       (SELECT prs_kenmerk_key, prs_kenmerk_upper
          FROM prs_kenmerk
         WHERE     prs_kenmerk_niveau = 'P'
               AND prs_kenmerk_kenmerktype = 'V'
               AND prs_kenmerk_verwijder IS NULL) v2
 WHERE     p.prs_afdeling_key = a.prs_afdeling_key
       AND a.prs_afdeling_upper = v1.cluster_label_code
       AND v1.cluster_label = v2.prs_kenmerk_upper
       AND p.prs_perslid_verwijder IS NULL
       -- AND p.prs_perslid_key = 6101 -- Wendy Beem WONDERLAND-locatiemanager
       AND NOT EXISTS (select km.prs_kenmerk_key from prs_kenmerklink km where km.prs_kenmerklink_niveau = 'P' and km.prs_kenmerk_key = v2.prs_kenmerk_key and km.prs_link_key = p.prs_perslid_key and km.prs_kenmerklink_verwijder IS NULL)
;

BEGIN

    FOR rec IN c
    LOOP
        BEGIN

        -- v_errorhint := 'Kenmerkveld wf_conversie vullen met omzetten - melding: ' || to_char(rec.mld_melding_start_key);
        INSERT INTO prs_kenmerklink (prs_link_key,
                                     prs_kenmerk_key,
                                     prs_kenmerklink_waarde,
                                     prs_kenmerklink_niveau)
                   VALUES (rec.prs_perslid_key,
                           rec.prs_kenmerk_key,
                           '1',
                           'P') ;
        
        BEGIN fac.trackaction('PRSUPD', rec.prs_perslid_key, 3, NULL, NULL); END;                           


        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
