--
-- $Id$
--
-- Pre-exportscript ter "medium" anonimisering van de DJIN-database zoals
-- aangevraagd met DJIN#37953.

DEFINE thisfile = 'AnonimiseerFMS_med.SQL'
DEFINE dbuser   = '^FMS'

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 ------

CREATE OR REPLACE PROCEDURE djin_select_nachtwerk (p_applname   IN VARCHAR2,
                                                   p_applrun    IN VARCHAR2)
AS
   CURSOR c_afspraak
   IS
        SELECT bez_afspraak_key
          FROM bez_afspraak
         --WHERE bez_afspraak_datum < TRUNC (SYSDATE - 180)
      ORDER BY bez_afspraak_key;

   -- DJIN#39650: Anonimiseren=verwijderen kenmerk=141 van melding=82.
   CURSOR c_bezorgadres
   IS
        SELECT km.mld_kenmerkmelding_key
          FROM mld_melding m, mld_kenmerkmelding km
         WHERE m.mld_stdmelding_key = 82         -- Aanvraag bloemen/boeketten
           AND m.mld_melding_key = km.mld_melding_key
           AND km.mld_kenmerk_key = 141                         -- Bezorgadres
           --AND EXISTS (SELECT 1
           --              FROM mld_v_aanwezigkenmerkmelding
           --             WHERE mld_melding_key = m.mld_melding_key
           --               AND mld_kenmerk_key = 21      -- Gewenste leverdatum
           --               AND ADD_MONTHS (fac.safe_to_date (km.mld_kenmerkmelding_waarde, 'dd-mm-yyyy'), 1) < SYSDATE)
      ORDER BY km.mld_kenmerkmelding_key;

   c_oud_medewerker   VARCHAR2 (100) := 'Oud medewerker';
   v_errormsg         VARCHAR2 (1000);
   oracle_err_num     NUMBER;
   oracle_err_mes     VARCHAR2 (200);
   v_count_del        NUMBER (10);

   -- SUBPROC
   PROCEDURE prs_anonimiseer (p_applname IN VARCHAR2)
   AS
      CURSOR c1
      IS
           SELECT '[' || TO_CHAR (p.prs_perslid_key) || '|' || p.prs_perslid_naam || '|' || p.prs_perslid_email || '] ' aanduiding,
                  p.prs_perslid_key
             FROM prs_perslid p
            WHERE (p.prs_perslid_oslogin IS NULL OR p.prs_perslid_oslogin NOT LIKE '\_%' ESCAPE '\') -- ' tbv. opmaak
                  AND p.prs_perslid_oslogin2 IS NULL
                  AND p.prs_perslid_apikey IS NULL       -- Voor de zekerheid!
                  AND ADD_MONTHS (COALESCE (p.prs_perslid_login, p.prs_perslid_aanmaak), 12) < SYSDATE -- Een jaar niet ingelogd!
                  --AND p.prs_perslid_upper != UPPER ('Oud medewerker')
                  AND p.prs_perslid_upper != UPPER (c_oud_medewerker)
                  AND NOT EXISTS (SELECT 1     -- Geen actuele verplichtingen!
                                    FROM prs_v_verplichting_keys
                                   WHERE prs_perslid_key = p.prs_perslid_key)
         ORDER BY 2;

      CURSOR c2
      IS
           SELECT t.fac_tracking_key,
                  t.fac_tracking_oms,
                  LENGTH (REPLACE (t.fac_tracking_oms || 'x', ' ', '')) - LENGTH (REPLACE (TRANSLATE (t.fac_tracking_oms || 'x', '@', ' '), ' ', '')) aant_email
             FROM fac_tracking t, fac_srtnotificatie sn
            WHERE ADD_MONTHS (t.fac_tracking_datum, 12) < SYSDATE
                  AND INSTR (t.fac_tracking_oms, '@') > 0
                  AND t.fac_srtnotificatie_key = sn.fac_srtnotificatie_key
                  AND (sn.fac_srtnotificatie_code LIKE '%MAI' OR sn.fac_srtnotificatie_code LIKE '%MA')
         ORDER BY 1;

      v_count_upd      NUMBER (10);
      v_tracking_oms   VARCHAR2 (1000);
   BEGIN
      v_count_upd := 0;
      FOR rec IN c1
      LOOP
         BEGIN
            v_errormsg := 'Fout anonimiseren persoon';

            UPDATE prs_perslid
               SET prs_perslid_naam = c_oud_medewerker,
                   prs_perslid_tussenvoegsel = NULL,
                   prs_perslid_voorletters = NULL,
                   prs_perslid_voornaam = NULL,
                   prs_perslid_initialen = NULL,
                   prs_perslid_geslacht = NULL,
                   prs_perslid_nr = NULL,
                   prs_perslid_telefoonnr = NULL,
                   prs_perslid_mobiel = NULL,
                   prs_perslid_email = NULL,
                   prs_perslid_oslogin = NULL, -- prs_perslid_oslogin2 = NULL
                   prs_perslid_verwijder = SYSDATE
             WHERE prs_perslid_key = rec.prs_perslid_key;
            COMMIT;

            v_count_upd := v_count_upd + 1;
         EXCEPTION
            WHEN OTHERS
            THEN
               oracle_err_num := SQLCODE;
               oracle_err_mes := SUBSTR (SQLERRM, 1, 200);
               v_errormsg := v_errormsg || ' ORACLE (error ' || oracle_err_num || '/' || oracle_err_mes || ')';
               fac.writelog (p_applname, 'W', rec.aanduiding || v_errormsg, '');
               COMMIT;
         END;
      END LOOP;

      fac.writelog (p_applname, 'S', 'Personen/#geanonimiseerd: ' || TO_CHAR (v_count_upd), '');
      COMMIT;

      v_errormsg := 'Fout anonimiseren tracking';

      SELECT COUNT ( * )
        INTO v_count_upd
        FROM fac_tracking
       WHERE fac_srtnotificatie_key IN (50, 67, 68, 98) -- MLDBEH/ORDSNT/ORDSNX/INSOUT
             AND ADD_MONTHS (fac_tracking_datum, 12) < SYSDATE;

      UPDATE fac_tracking
         SET fac_tracking_oms = 'Behandelaar is toegewezen'
       WHERE fac_srtnotificatie_key = 50                             -- MLDBEH
             AND ADD_MONTHS (fac_tracking_datum, 12) < SYSDATE;
      COMMIT;

      UPDATE fac_tracking
         SET fac_tracking_oms = 'Opdracht is verstuurd'
       WHERE fac_srtnotificatie_key IN (67, 68)               -- ORDSNT/ORDSNX
             AND ADD_MONTHS (fac_tracking_datum, 12) < SYSDATE;
      COMMIT;

      UPDATE fac_tracking
         SET fac_tracking_oms = 'Object is uitgegeven'
       WHERE fac_srtnotificatie_key = 98                             -- INSOUT
             AND ADD_MONTHS (fac_tracking_datum, 12) < SYSDATE;
      COMMIT;

      FOR rec IN c2
      LOOP
         BEGIN
            v_tracking_oms := rec.fac_tracking_oms;
            WHILE v_count_upd < rec.aant_email
            LOOP
               v_tracking_oms := SUBSTR (v_tracking_oms, 1, INSTR (SUBSTR (v_tracking_oms, 1, INSTR (v_tracking_oms, '@')), ' ', -1))
                                 || SUBSTR (v_tracking_oms, INSTR (v_tracking_oms, '@') + 1);

               v_count_upd := v_count_upd + 1;
            END LOOP;

            UPDATE fac_tracking
               SET fac_tracking_oms = v_tracking_oms
             WHERE fac_tracking_key = rec.fac_tracking_key;
            COMMIT;
         EXCEPTION
            WHEN OTHERS
            THEN
               oracle_err_num := SQLCODE;
               oracle_err_mes := SUBSTR (SQLERRM, 1, 200);
               v_errormsg := v_errormsg || ' ORACLE (error ' || oracle_err_num || '/' || oracle_err_mes || ')';
               fac.writelog (p_applname, 'W', v_errormsg, '');
               COMMIT;
         END;
      END LOOP;

      fac.writelog (p_applname, 'S', 'Tracking/#geanonimiseerd: ' || TO_CHAR (v_count_upd), '');
      COMMIT;
   END;
BEGIN
   v_count_del := 0;

   FOR rec IN c_afspraak
   LOOP
      -- Delete alle kenmerkwaarden van bezoekers bij te verwijderen afspraken.
      DELETE FROM bez_kenmerkwaarde kw
            WHERE EXISTS
                     (SELECT 1
                        FROM bez_bezoekers
                       WHERE bez_bezoekers_key = kw.bez_bezoekers_key
                             AND bez_afspraak_key = rec.bez_afspraak_key);

      -- Delete alle bezoekers-tracking bij te verwijderen afspraken.
      DELETE FROM fac_tracking t
            WHERE t.fac_srtnotificatie_key IN (28, 29, 30, 31, 32) -- BEZBAD/BEZDON/BEZDO2/BEZOUT/BEZOU2
                  AND EXISTS
                        (SELECT 1
                           FROM bez_bezoekers
                          WHERE bez_bezoekers_key = t.fac_tracking_refkey
                                AND bez_afspraak_key = rec.bez_afspraak_key);

      -- Delete alle bezoekers bij te verwijderen afspraken.
      DELETE FROM bez_bezoekers
            WHERE bez_afspraak_key = rec.bez_afspraak_key;

      -- Delete alle afspraak-tracking bij te verwijderen afspraken.
      DELETE FROM fac_tracking
            WHERE fac_srtnotificatie_key IN (26, 27, 128) -- BEZMUT/BEZUPD/BEZMAI
                  AND fac_tracking_refkey = rec.bez_afspraak_key;

      -- Delete alle te verwijderen afspraken.
      DELETE FROM bez_afspraak
            WHERE bez_afspraak_key = rec.bez_afspraak_key;

      v_count_del := v_count_del + 1;
   --fac.trackaction ('#BEZUPD', rec.bez_afspraak_key, NULL, v_nu, v_txt_anoniem);
   END LOOP;

   fac.writelog (p_applname, 'S', 'Afspraken/#verwijderd: ' || TO_CHAR (v_count_del), '');
   COMMIT;

   v_count_del := 0;

   -- Delete bezorgadressen van 'Aanvraag bloemen/boeketten'-meldingen 1 maand
   -- na leverdatum.
   FOR rec IN c_bezorgadres
   LOOP
      DELETE FROM mld_kenmerkmelding
            WHERE mld_kenmerkmelding_key = rec.mld_kenmerkmelding_key;

      v_count_del := v_count_del + 1;
   --fac.trackaction ('#MLDUPD', rec.mld_melding_key, NULL, v_nu, v_txt_anoniem);
   END LOOP;

   fac.writelog (p_applname, 'S', 'Bezorgadressen/#verwijderd: ' || TO_CHAR (v_count_del), '');
   COMMIT;

   prs_anonimiseer (p_applname);
EXCEPTION
   WHEN OTHERS
   THEN
      oracle_err_num := SQLCODE;
      oracle_err_mes := SUBSTR (SQLERRM, 1, 200);
      v_errormsg := 'ORACLE (error ' || oracle_err_num || '/' || oracle_err_mes || ')';
      fac.writelog (p_applname, 'E', 'Proces NACHTWERK afgebroken!', v_errormsg);
END djin_select_nachtwerk;
/

-- Anonimisering zoals normaal via NACHTWERK gebeurt, maar dan direct muv. de
-- medewerkers (dat gebeurt afhankelijk van de versie van dit script) én de
-- tracking!
BEGIN
   djin_select_nachtwerk ('NACHTWERK_MED', -1);
END;
/

-- Naam=X/Voornaam=-/Tussenvoegsel=NULL/Telefoonnr=NULL/Mobiel=NULL
UPDATE prs_perslid
   SET prs_perslid_naam = '[Geanonimiseerd]',
       prs_perslid_tussenvoegsel = NULL,
       prs_perslid_voorletters = NULL,
       prs_perslid_voornaam = NULL,
       prs_perslid_telefoonnr = NULL,
       prs_perslid_mobiel = NULL;
COMMIT;

-- Email=NULL/Oslogin=NULL behalve van de gebruikers in '_Admin'-groepen
UPDATE prs_perslid p
   SET p.prs_perslid_email = NULL,
       p.prs_perslid_oslogin = NULL,
       p.prs_perslid_oslogin2 = NULL
 WHERE NOT EXISTS
          (SELECT 1
             FROM fac_gebruikersgroep gg, fac_groep g
            WHERE gg.prs_perslid_key = p.prs_perslid_key
                  AND gg.fac_groep_key = g.fac_groep_key
                  AND g.fac_groep_omschrijving LIKE '_Admin%');
COMMIT;

------ 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
quit
