--
-- $Id$
--
-- Opruimscript om alle Heerlen-data uit database voor BLCC te halen.

DEFINE thisfile = 'BLCC#53955.sql'
DEFINE dbuser   = '^BLCC'

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 ------
/*
UPDATE prs_perslid p
   SET p.prs_perslid_nr = SUBSTR (COALESCE (p.prs_perslid_oslogin, p.prs_perslid_oslogin2, ''), 1, 16)
 WHERE p.prs_perslid_key != 10681                             -- Ron Smeets
       AND prs_perslid_naam NOT LIKE 'INACTIEF:%'
       AND EXISTS
          (SELECT 1
             FROM prs_v_afdeling_boom
            WHERE prs_bedrijf_key IN (2441, 2442)                   -- BSSC
                  AND prs_afdeling_key = p.prs_afdeling_key);

UPDATE prs_perslid p
   SET p.prs_perslid_naam = SUBSTR ('INACTIEF:' || prs_perslid_naam, 1, 60),
       p.prs_perslid_oslogin = NULL,
       p.prs_perslid_oslogin2 = NULL
 WHERE p.prs_perslid_key != 10681                             -- Ron Smeets
       AND prs_perslid_naam NOT LIKE 'INACTIEF:%'
       AND EXISTS
          (SELECT 1
             FROM prs_v_afdeling_boom
            WHERE prs_bedrijf_key IN (2441, 2442)                   -- BSSC
                  AND prs_afdeling_key = p.prs_afdeling_key);
*/

-- Verhuizen _FACILITOR, _GAST, _HMAIL en _PUTORDERS naar 1052-afdeling én naar
-- BCC-werkplek (of geen werkplek)!

-- Schonen notificaties ouder dan 30 dagen!
DELETE FROM fac_notificatie
      WHERE fac_notificatie_datum < SYSDATE - 30;
DELETE FROM web_user_messages
      WHERE web_user_mess_action_datum < SYSDATE - 30;

-- Schonen BSSC-import met key=61 (incl. historie) of door BSSC-personen!
DELETE FROM imp_log;
DELETE FROM fac_import i
      WHERE i.fac_import_app_key = 61
            OR EXISTS
                  (SELECT 1
                     FROM prs_perslid p, prs_v_afdeling_boom ab
                    WHERE p.prs_afdeling_key = ab.prs_afdeling_key
                          AND ab.prs_bedrijf_key IN (2441, 2442)
                          AND p.prs_perslid_key = i.prs_perslid_key);
DELETE FROM fac_import_app
      WHERE fac_import_app_key = 61;

-- Schonen BSSC-rapportages ('bssc_v_rap_ssc_cat_looplijst' en 'bssc_v_rap_ssc_cat_factlijst')!
DELETE FROM fac_usrrap
      WHERE fac_usrrap_key IN (681, 682);

-- BEZ - Bezoekers
DELETE FROM bez_kenmerkwaarde kw
      WHERE EXISTS
               (SELECT 1
                  FROM bez_kenmerk
                 WHERE bez_kenmerk_verwijder IS NOT NULL
                       AND bez_kenmerk_key = kw.bez_kenmerk_key)
            OR EXISTS
               (SELECT 1
                  FROM bez_bezoekers b, bez_afspraak a
                 WHERE b.bez_afspraak_key = a.bez_afspraak_key
                       AND a.alg_locatie_key = 21                      -- SSCH
                       AND b.bez_bezoekers_key = kw.bez_bezoekers_key);

DELETE FROM bez_kenmerk
      WHERE bez_kenmerk_verwijder IS NOT NULL;

DELETE FROM bez_bezoekers b
      WHERE EXISTS
               (SELECT 1
                  FROM bez_afspraak
                 WHERE alg_locatie_key = 21                            -- SSCH
                       AND bez_afspraak_key = b.bez_afspraak_key);

DELETE FROM bez_afspraak
      WHERE alg_locatie_key = 21;

DELETE FROM fac_tracking t
      WHERE EXISTS
               (SELECT 1
                  FROM fac_srtnotificatie
                 WHERE fac_srtnotificatie_xmlnode IN ('bezoeker')
                       AND fac_srtnotificatie_key = t.fac_srtnotificatie_key)
            AND NOT EXISTS (SELECT 1
                              FROM bez_bezoekers
                             WHERE bez_bezoekers_key = t.fac_tracking_refkey);

DELETE FROM fac_tracking t
      WHERE EXISTS
               (SELECT 1
                  FROM fac_srtnotificatie
                 WHERE fac_srtnotificatie_xmlnode IN ('afspraak')
                       AND fac_srtnotificatie_key = t.fac_srtnotificatie_key)
            AND NOT EXISTS (SELECT 1
                              FROM bez_afspraak
                             WHERE bez_afspraak_key = t.fac_tracking_refkey);

-- RES - Reserveringen
DELETE FROM res_kenmerkwaarde kw
      WHERE EXISTS
               (SELECT 1
                  FROM res_rsv_ruimte rrr,
                       res_ruimte_opstelling rro,
                       (  SELECT res_ruimte_key, MAX (alg_ruimte_key) alg_ruimte_key
                            FROM res_alg_ruimte
                           WHERE res_alg_ruimte_verwijder IS NULL
                        GROUP BY res_ruimte_key) rar1,
                       (  SELECT res_ruimte_key, MAX (alg_ruimte_key) alg_ruimte_key
                            FROM res_alg_ruimte
                           WHERE res_alg_ruimte_verwijder IS NOT NULL
                        GROUP BY res_ruimte_key) rar2,
                       alg_v_allonroerendgoed aog
                 WHERE rrr.res_ruimte_opstel_key = rro.res_ruimte_opstel_key(+)
                       AND rro.res_ruimte_key = rar1.res_ruimte_key(+)
                       AND rro.res_ruimte_key = rar2.res_ruimte_key(+)
                       AND COALESCE (rar1.alg_ruimte_key, rar2.alg_ruimte_key, rrr.alg_ruimte_key) = aog.alg_ruimte_key
                       AND aog.alg_locatie_key = 21                    -- SSCH
                       AND rrr.res_rsv_ruimte_key = kw.res_rsv_ruimte_key);

DELETE FROM res_kenmerk k
      WHERE k.res_kenmerk_verwijder IS NOT NULL
            OR EXISTS
                 (SELECT 1
                    FROM res_activiteitdiscipline
                   WHERE res_discipline_key IN (161, 181, 201, 202, 203, 204, 602, 603, 841, 861, 862, 881, 901, 921, 1041, 1042, 1061, 1361, 1561, 1721, 1741, 1742, 1761)
                         AND res_activiteit_key = k.res_activiteit_key);

DELETE FROM res_srtkenmerk sk
      WHERE NOT EXISTS
               (SELECT 1
                  FROM res_kenmerk
                 WHERE res_srtkenmerk_key = sk.res_srtkenmerk_key);

DELETE FROM res_rsv_deel rrd
      WHERE EXISTS
               (SELECT 1
                  FROM res_rsv_ruimte rrr,
                       res_ruimte_opstelling rro,
                       (  SELECT res_ruimte_key, MAX (alg_ruimte_key) alg_ruimte_key
                            FROM res_alg_ruimte
                           WHERE res_alg_ruimte_verwijder IS NULL
                        GROUP BY res_ruimte_key) rar1,
                       (  SELECT res_ruimte_key, MAX (alg_ruimte_key) alg_ruimte_key
                            FROM res_alg_ruimte
                           WHERE res_alg_ruimte_verwijder IS NOT NULL
                        GROUP BY res_ruimte_key) rar2,
                       alg_v_allonroerendgoed aog
                 WHERE rrr.res_ruimte_opstel_key = rro.res_ruimte_opstel_key(+)
                       AND rro.res_ruimte_key = rar1.res_ruimte_key(+)
                       AND rro.res_ruimte_key = rar2.res_ruimte_key(+)
                       AND COALESCE (rar1.alg_ruimte_key, rar2.alg_ruimte_key, rrr.alg_ruimte_key) = aog.alg_ruimte_key
                       AND aog.alg_locatie_key = 21                    -- SSCH
                       AND rrr.res_rsv_ruimte_key = rrd.res_rsv_ruimte_key);

DELETE FROM res_rsv_artikel rra
      WHERE EXISTS
               (SELECT 1
                  FROM res_rsv_ruimte rrr,
                       res_ruimte_opstelling rro,
                       (  SELECT res_ruimte_key, MAX (alg_ruimte_key) alg_ruimte_key
                            FROM res_alg_ruimte
                           WHERE res_alg_ruimte_verwijder IS NULL
                        GROUP BY res_ruimte_key) rar1,
                       (  SELECT res_ruimte_key, MAX (alg_ruimte_key) alg_ruimte_key
                            FROM res_alg_ruimte
                           WHERE res_alg_ruimte_verwijder IS NOT NULL
                        GROUP BY res_ruimte_key) rar2,
                       alg_v_allonroerendgoed aog
                 WHERE rrr.res_ruimte_opstel_key = rro.res_ruimte_opstel_key(+)
                       AND rro.res_ruimte_key = rar1.res_ruimte_key(+)
                       AND rro.res_ruimte_key = rar2.res_ruimte_key(+)
                       AND COALESCE (rar1.alg_ruimte_key, rar2.alg_ruimte_key, rrr.alg_ruimte_key) = aog.alg_ruimte_key
                       AND aog.alg_locatie_key = 21                    -- SSCH
                       AND rrr.res_rsv_ruimte_key = rra.res_rsv_ruimte_key);

DELETE FROM res_rsv_ruimte rrr1 -- Afhankelijkheid met bezoekers en meldingen op reserveringen!
      WHERE EXISTS
               (SELECT 1
                  FROM res_rsv_ruimte rrr2,
                       res_ruimte_opstelling rro,
                       (  SELECT res_ruimte_key, MAX (alg_ruimte_key) alg_ruimte_key
                            FROM res_alg_ruimte
                           WHERE res_alg_ruimte_verwijder IS NULL
                        GROUP BY res_ruimte_key) rar1,
                       (  SELECT res_ruimte_key, MAX (alg_ruimte_key) alg_ruimte_key
                            FROM res_alg_ruimte
                           WHERE res_alg_ruimte_verwijder IS NOT NULL
                        GROUP BY res_ruimte_key) rar2,
                       alg_v_allonroerendgoed aog
                 WHERE rrr2.res_ruimte_opstel_key = rro.res_ruimte_opstel_key(+)
                       AND rro.res_ruimte_key = rar1.res_ruimte_key(+)
                       AND rro.res_ruimte_key = rar2.res_ruimte_key(+)
                       AND COALESCE (rar1.alg_ruimte_key, rar2.alg_ruimte_key, rrr2.alg_ruimte_key) = aog.alg_ruimte_key
                       AND aog.alg_locatie_key = 21                    -- SSCH
                       AND rrr1.res_rsv_ruimte_key = rrr2.res_rsv_ruimte_key);

DELETE FROM res_reservering rr
      WHERE NOT EXISTS
               (SELECT 1
                  FROM res_rsv_ruimte
                 WHERE res_reservering_key = rr.res_reservering_key);

DELETE FROM res_deel
      WHERE res_discipline_key IN (161, 181, 201, 202, 203, 204, 602, 603, 841, 861, 862, 881, 901, 921, 1041, 1042, 1061, 1361, 1561, 1721, 1741, 1742, 1761);

DELETE FROM res_artikel
      WHERE res_discipline_key IN (161, 181, 201, 202, 203, 204, 602, 603, 841, 861, 862, 881, 901, 921, 1041, 1042, 1061, 1361, 1561, 1721, 1741, 1742, 1761);

DELETE FROM res_alg_ruimte rar
      WHERE EXISTS
               (SELECT 1
                  FROM res_ruimte
                 WHERE res_discipline_key IN (161, 181, 201, 202, 203, 204, 602, 603, 841, 861, 862, 881, 901, 921, 1041, 1042, 1061, 1361, 1561, 1721, 1741, 1742, 1761)
                       AND res_ruimte_key = rar.res_ruimte_key);

DELETE FROM res_ruimte_opstelling rro
      WHERE EXISTS
               (SELECT 1
                  FROM res_ruimte
                 WHERE res_discipline_key IN (161, 181, 201, 202, 203, 204, 602, 603, 841, 861, 862, 881, 901, 921, 1041, 1042, 1061, 1361, 1561, 1721, 1741, 1742, 1761)
                       AND res_ruimte_key = rro.res_ruimte_key);

DELETE FROM res_ruimte
      WHERE res_discipline_key IN (161, 181, 201, 202, 203, 204, 602, 603, 841, 861, 862, 881, 901, 921, 1041, 1042, 1061, 1361, 1561, 1721, 1741, 1742, 1761);

DELETE FROM res_srtartikel_onrgoed
      WHERE res_discipline_key IN (161, 181, 201, 202, 203, 204, 602, 603, 841, 861, 862, 881, 901, 921, 1041, 1042, 1061, 1361, 1561, 1721, 1741, 1742, 1761);

DELETE FROM res_activiteitdiscipline
      WHERE res_discipline_key IN (161, 181, 201, 202, 203, 204, 602, 603, 841, 861, 862, 881, 901, 921, 1041, 1042, 1061, 1361, 1561, 1721, 1741, 1742, 1761);

DELETE FROM res_disc_params
      WHERE res_ins_discipline_key IN (161, 181, 201, 202, 203, 204, 602, 603, 841, 861, 862, 881, 901, 921, 1041, 1042, 1061, 1361, 1561, 1721, 1741, 1742, 1761);

DELETE FROM ins_tab_discipline
      WHERE ins_discipline_module = 'RES'
            AND ins_discipline_key IN (161, 181, 201, 202, 203, 204, 602, 603, 841, 861, 862, 881, 901, 921, 1041, 1042, 1061, 1361, 1561, 1721, 1741, 1742, 1761);

DELETE FROM res_activiteit ra
      WHERE NOT EXISTS
               (SELECT 1
                  FROM res_rsv_ruimte
                 WHERE res_activiteit_key = ra.res_activiteit_key);

DELETE FROM res_srtactiviteit rsa
      WHERE NOT EXISTS
               (SELECT 1
                  FROM res_activiteit
                 WHERE res_srtactiviteit_key = rsa.res_srtactiviteit_key);

DELETE FROM fac_tracking t
      WHERE EXISTS
               (SELECT 1
                  FROM fac_srtnotificatie
                 WHERE fac_srtnotificatie_xmlnode IN ('reservering', 'xreservering')
                       AND fac_srtnotificatie_key = t.fac_srtnotificatie_key)
            AND NOT EXISTS (SELECT 1
                              FROM res_rsv_ruimte
                             WHERE res_rsv_ruimte_key = t.fac_tracking_refkey);

-- Schonen CNT - Contracten
DELETE FROM cnt_contract_note cn
      WHERE EXISTS
               (SELECT 1
                  FROM cnt_contract
                 WHERE ins_discipline_key IN (581, 1688, 1701)
                       AND cnt_contract_key = cn.cnt_contract_key);

DELETE FROM cnt_contract_plaats cp
      WHERE EXISTS
               (SELECT 1
                  FROM cnt_contract
                 WHERE ins_discipline_key IN (581, 1688, 1701)
                       AND cnt_contract_key = cp.cnt_contract_key);

DELETE FROM cnt_kenmerkcontract kc
      WHERE EXISTS
               (SELECT 1
                  FROM cnt_contract
                 WHERE ins_discipline_key IN (581, 1688, 1701)
                       AND cnt_contract_key = kc.cnt_contract_key);

DELETE FROM cnt_kenmerk k
      WHERE EXISTS
               (SELECT 1
                  FROM cnt_contract
                 WHERE ins_discipline_key IN (581, 1688, 1701)
                       AND ins_discipline_key = k.cnt_srtcontract_key);

DELETE FROM cnt_srtkenmerk sk
      WHERE NOT EXISTS
               (SELECT 1
                  FROM cnt_kenmerk
                 WHERE cnt_srtkenmerk_key = sk.cnt_srtkenmerk_key);

DELETE FROM cnt_contract
      WHERE ins_discipline_key IN (581, 1688, 1701);

DELETE FROM cnt_disc_params
      WHERE cnt_ins_discipline_key IN (581, 1688, 1701);

DELETE FROM ins_tab_discipline
      WHERE ins_discipline_module = 'CNT'
            AND ins_discipline_key IN (581, 1688, 1701);

DELETE FROM fac_tracking t
      WHERE EXISTS
               (SELECT 1
                  FROM fac_srtnotificatie
                 WHERE fac_srtnotificatie_xmlnode = 'contract'
                       AND fac_srtnotificatie_key = t.fac_srtnotificatie_key)
            AND NOT EXISTS (SELECT 1
                              FROM cnt_contract
                             WHERE cnt_contract_key = t.fac_tracking_refkey);

-- Schonen INS - Objecten
DELETE FROM ins_kenmerkdeel kd
      WHERE EXISTS
               (SELECT 1
                  FROM ins_deel
                 WHERE ins_discipline_key IN (1021)
                       AND ins_deel_key = kd.ins_deel_key);

DELETE FROM ins_kenmerk k
      WHERE EXISTS
               (SELECT 1
                  FROM ins_deel
                 WHERE ins_discipline_key IN (1021)
                       AND (ins_deel_key = k.ins_srtinstallatie_key OR ins_srtdeel_key = k.ins_srtinstallatie_key));

DELETE FROM ins_srtkenmerk sk
      WHERE NOT EXISTS
               (SELECT 1
                  FROM ins_kenmerk
                 WHERE ins_srtkenmerk_key = sk.ins_srtkenmerk_key);

--DELETE FROM ins_deel
--      WHERE ins_discipline_key IN (1021);
DELETE FROM ins_deel d       -- Afhankelijkheid met reserveringen op objecten!
      WHERE EXISTS
               (SELECT 1
                  FROM ins_srtdeel sd, ins_srtgroep sg
                 WHERE sd.ins_srtgroep_key = sg.ins_srtgroep_key
                       AND sg.ins_discipline_key IN (1021)
                       AND sd.ins_srtdeel_key = d.ins_srtdeel_key);

DELETE FROM ins_srtdeel sd
      WHERE EXISTS
               (SELECT 1
                  FROM ins_srtgroep
                 WHERE ins_discipline_key IN (1021)
                       AND ins_srtgroep_key = sd.ins_srtgroep_key);

DELETE FROM ins_srtgroep
      WHERE ins_discipline_key IN (1021);

DELETE FROM ins_disc_params
      WHERE ins_discipline_key IN (1021);

DELETE FROM ins_tab_discipline
      WHERE ins_discipline_module = 'INS'
            AND ins_discipline_key IN (1021);

DELETE FROM fac_tracking t
      WHERE EXISTS
               (SELECT 1
                  FROM fac_srtnotificatie
                 WHERE fac_srtnotificatie_xmlnode = 'deel'
                       AND fac_srtnotificatie_key = t.fac_srtnotificatie_key)
            AND NOT EXISTS (SELECT 1
                              FROM ins_deel
                             WHERE ins_deel_key = t.fac_tracking_refkey);

DELETE FROM fac_tracking t
      WHERE EXISTS
               (SELECT 1
                  FROM fac_srtnotificatie
                 WHERE fac_srtnotificatie_xmlnode = 'srtdeel'
                       AND fac_srtnotificatie_key = t.fac_srtnotificatie_key)
            AND NOT EXISTS (SELECT 1
                              FROM ins_srtdeel
                             WHERE ins_srtdeel_key = t.fac_tracking_refkey);

-- PRS - Organisatie/Personen
DELETE FROM prs_kenmerklink kl
      WHERE kl.prs_kenmerklink_niveau = 'P'
            AND EXISTS
                  (SELECT 1
                     FROM prs_perslid p, prs_v_afdeling_boom ab
                    WHERE p.prs_afdeling_key = ab.prs_afdeling_key
                          AND ab.prs_bedrijf_key IN (2441, 2442)
                          AND p.prs_perslid_key = kl.prs_link_key);

DELETE FROM prs_kenmerklink
      WHERE prs_kenmerk_key = 1040;

DELETE FROM prs_kenmerk
      WHERE prs_kenmerk_key = 1040;

DELETE FROM prs_ruimteafdeling ra
      WHERE EXISTS
                  (SELECT 1
                     FROM prs_v_afdeling_boom
                    WHERE prs_bedrijf_key IN (2441, 2442)
                          AND prs_afdeling_key = ra.prs_afdeling_key);

DELETE FROM prs_ruimteafdeling ra
      WHERE EXISTS
                  (SELECT 1
                     FROM alg_v_allonroerendgoed
                    WHERE alg_locatie_key = 21                         -- BSSC
                          AND alg_type = 'R'
                          AND alg_ruimte_key = ra.alg_ruimte_key);

DELETE FROM prs_perslidwerkplek pw
      WHERE EXISTS
                  (SELECT 1
                     FROM prs_perslid p, prs_v_afdeling_boom ab
                    WHERE p.prs_afdeling_key = ab.prs_afdeling_key
                          AND ab.prs_bedrijf_key IN (2441, 2442)
                          AND p.prs_perslid_key = pw.prs_perslid_key);

DELETE FROM prs_perslidwerkplek pw
      WHERE EXISTS
               (SELECT 1
                  FROM alg_v_allonroerendgoed aog, prs_werkplek wp
                 WHERE aog.alg_locatie_key = 21                        -- BSSC
                       AND aog.alg_type = 'R'
                       AND aog.alg_ruimte_key = wp.prs_alg_ruimte_key
                       AND wp.prs_werkplek_key = pw.prs_werkplek_key);

DELETE FROM prs_werkplek wp
      WHERE NOT EXISTS
                  (SELECT 1
                     FROM prs_perslidwerkplek
                    WHERE prs_werkplek_key = wp.prs_werkplek_key);

DELETE FROM prs_collega c
      WHERE EXISTS
                  (SELECT 1
                     FROM prs_perslid p, prs_v_afdeling_boom ab
                    WHERE p.prs_afdeling_key = ab.prs_afdeling_key
                          AND ab.prs_bedrijf_key IN (2441, 2442)
                          AND p.prs_perslid_key = c.prs_perslid_key);

UPDATE alg_gebouw g
   SET g.prs_perslid_key_verantw = NULL
 WHERE EXISTS
          (SELECT 1
             FROM prs_perslid p, prs_v_afdeling_boom ab
            WHERE p.prs_afdeling_key = ab.prs_afdeling_key
                  AND ab.prs_bedrijf_key IN (2441, 2442)
                  AND p.prs_perslid_key = g.prs_perslid_key_verantw);

UPDATE prs_kostenplaats kp
   SET kp.prs_perslid_key = NULL
 WHERE EXISTS
          (SELECT 1
             FROM prs_perslid p, prs_v_afdeling_boom ab
            WHERE p.prs_afdeling_key = ab.prs_afdeling_key
                  AND ab.prs_bedrijf_key IN (2441, 2442)
                  AND p.prs_perslid_key = kp.prs_perslid_key);

UPDATE prs_kostenplaatsgrp kpg
   SET kpg.prs_perslid_key = NULL
 WHERE EXISTS
          (SELECT 1
             FROM prs_perslid p, prs_v_afdeling_boom ab
            WHERE p.prs_afdeling_key = ab.prs_afdeling_key
                  AND ab.prs_bedrijf_key IN (2441, 2442)
                  AND p.prs_perslid_key = kpg.prs_perslid_key);

DELETE FROM prs_perslidkostenplaats pkp
      WHERE pkp.prs_perslid_key != 4
            AND EXISTS
                  (SELECT 1
                     FROM prs_perslid p, prs_v_afdeling_boom ab
                    WHERE p.prs_afdeling_key = ab.prs_afdeling_key
                          AND ab.prs_bedrijf_key IN (2441, 2442)
                          AND p.prs_perslid_key = pkp.prs_perslid_key);

DELETE FROM fac_gebruikersgroep gg
      WHERE gg.prs_perslid_key != 4
            AND EXISTS
                  (SELECT 1
                     FROM prs_perslid p, prs_v_afdeling_boom ab
                    WHERE p.prs_afdeling_key = ab.prs_afdeling_key
                          AND ab.prs_bedrijf_key IN (2441, 2442)
                          AND p.prs_perslid_key = gg.prs_perslid_key);

-- ? BSSC-contacts voor afspraak bij BLCC!?
UPDATE bez_afspraak a
   SET a.bez_afspraak_contact_key = 4                            -- _FACILITOR
 WHERE EXISTS
          (SELECT 1
             FROM prs_perslid p, prs_v_afdeling_boom ab
            WHERE p.prs_afdeling_key = ab.prs_afdeling_key
                  AND ab.prs_bedrijf_key IN (2441, 2442)
                  AND p.prs_perslid_key = a.bez_afspraak_contact_key);

-- ? BSSC-hosts voor afspraak bij BLCC!?
UPDATE bez_afspraak a
   SET a.bez_afspraak_host_key = 4                               -- _FACILITOR
 WHERE EXISTS
          (SELECT 1
             FROM prs_perslid p, prs_v_afdeling_boom ab
            WHERE p.prs_afdeling_key = ab.prs_afdeling_key
                  AND ab.prs_bedrijf_key IN (2441, 2442)
                  AND p.prs_perslid_key = a.bez_afspraak_host_key);

-- ? BSSC-ers contact voor reservering bij BLCC!?
UPDATE res_rsv_ruimte r
   SET r.res_rsv_ruimte_contact_key = 4                          -- _FACILITOR
 WHERE EXISTS
          (SELECT 1
             FROM prs_perslid p, prs_v_afdeling_boom ab
            WHERE p.prs_afdeling_key = ab.prs_afdeling_key
                  AND ab.prs_bedrijf_key IN (2441, 2442)
                  AND p.prs_perslid_key = r.res_rsv_ruimte_contact_key);

-- ? BSSC-ers host voor reservering bij BLCC!?
UPDATE res_rsv_ruimte r
   SET r.res_rsv_ruimte_host_key = 4                             -- _FACILITOR
 WHERE r.res_status_bo_key != 6
       AND EXISTS
             (SELECT 1
                FROM prs_perslid p, prs_v_afdeling_boom ab
               WHERE     p.prs_afdeling_key = ab.prs_afdeling_key
                     AND ab.prs_bedrijf_key IN (2441, 2442)
                     AND p.prs_perslid_key = r.res_rsv_ruimte_host_key);

-- ? BSSC-ers wijziger van settings (Smeets)!
UPDATE fac_setting s
   SET s.prs_perslid_key = 4                                     -- _FACILITOR
 WHERE EXISTS
          (SELECT 1
             FROM prs_perslid p, prs_v_afdeling_boom ab
            WHERE p.prs_afdeling_key = ab.prs_afdeling_key
                  AND ab.prs_bedrijf_key IN (2441, 2442)
                  AND p.prs_perslid_key = s.prs_perslid_key);
/*
DELETE FROM prs_contactpersoon_locatie cpl
      WHERE NOT EXISTS
                  (SELECT 1
                     FROM prs_contactpersoon cp, prs_bedrijf b
                    WHERE cp.prs_bedrijf_key = b.prs_bedrijf_key
                          AND b.prs_bedrijf_huurder = 1
                          AND cp.prs_contactpersoon_key = cpl.prs_contactpersoon_key);

DELETE FROM prs_contactpersoon cp
      WHERE NOT EXISTS
                  (SELECT 1
                     FROM prs_bedrijf
                    WHERE prs_bedrijf_huurder = 1
                          AND prs_bedrijf_key = cp.prs_bedrijf_key);
*/
DELETE FROM prs_perslid p
      WHERE p.prs_perslid_key != 4
            --AND p.prs_perslid_verwijder IS NULL
            AND EXISTS
                  (SELECT 1
                     FROM prs_v_afdeling_boom
                    WHERE prs_bedrijf_key IN (2441, 2442)
                          AND prs_afdeling_key = p.prs_afdeling_key)
            AND NOT EXISTS
                  (SELECT 1
                     FROM mld_melding
                    WHERE prs_perslid_key = p.prs_perslid_key)
            AND NOT EXISTS
                  (SELECT 1
                     FROM res_rsv_ruimte
                    WHERE res_rsv_ruimte_host_key = p.prs_perslid_key);

DELETE FROM prs_srtperslid sp
      WHERE NOT EXISTS
                  (SELECT 1
                     FROM prs_perslid
                    WHERE prs_srtperslid_key = sp.prs_srtperslid_key);
/*
DELETE FROM prs_afdeling a
      WHERE EXISTS
                  (SELECT 1
                     FROM prs_v_afdeling_boom
                    WHERE prs_bedrijf_key IN (2441, 2442)
                          AND prs_afdeling_key = a.prs_afdeling_key);

DELETE FROM prs_bedrijf
      WHERE prs_bedrijf_key IN (2441, 2442);
*/
UPDATE prs_afdeling a
   SET a.prs_kostenplaats_key = NULL
 WHERE a.prs_kostenplaats_key IS NOT NULL
       AND EXISTS
             (SELECT 1
                FROM prs_v_afdeling_boom
               WHERE prs_bedrijf_key IN (2441, 2442)
                     AND prs_afdeling_key = a.prs_afdeling_key);

DELETE FROM prs_bedrijfdienstlocatie bdl
      WHERE EXISTS
                  (SELECT 1
                     FROM prs_bedrijf
                    WHERE prs_bedrijf_huurder = 1
                          AND prs_bedrijf_key = bdl.prs_bedrijf_key);

DELETE FROM prs_bedrijfadres ba
      WHERE EXISTS
                  (SELECT 1
                     FROM prs_bedrijf
                    WHERE prs_bedrijf_huurder = 1
                          AND prs_bedrijf_key = ba.prs_bedrijf_key);

DELETE FROM prs_bedrijf
      WHERE prs_bedrijf_key != 11205                -- Universiteit Maastricht
            AND COALESCE (prs_bedrijf_intern, 0) != 1
            AND COALESCE (prs_bedrijf_huurder, 0) = 1;

DELETE FROM fac_tracking t
      WHERE EXISTS
               (SELECT 1
                  FROM fac_srtnotificatie
                 WHERE fac_srtnotificatie_xmlnode IN ('perslid')
                       AND fac_srtnotificatie_key = t.fac_srtnotificatie_key)
            AND NOT EXISTS (SELECT 1
                              FROM prs_perslid
                             WHERE prs_perslid_key = t.fac_tracking_refkey);

DELETE FROM fac_tracking t
      WHERE EXISTS
               (SELECT 1
                  FROM fac_srtnotificatie
                 WHERE fac_srtnotificatie_xmlnode IN ('bedrijfadres')
                       AND fac_srtnotificatie_key = t.fac_srtnotificatie_key)
            AND NOT EXISTS (SELECT 1
                              FROM prs_bedrijfadres
                             WHERE prs_bedrijfadres_key = t.fac_tracking_refkey);

DELETE FROM fac_tracking t
      WHERE EXISTS
               (SELECT 1
                  FROM fac_srtnotificatie
                 WHERE fac_srtnotificatie_xmlnode IN ('bedrijf')
                       AND fac_srtnotificatie_key = t.fac_srtnotificatie_key)
            AND NOT EXISTS (SELECT 1
                              FROM prs_bedrijf
                             WHERE prs_bedrijf_key = t.fac_tracking_refkey);

-- ALG - Vastgoed
DELETE FROM alg_onrgoedkenmerk ok
      WHERE ok.alg_onrgoed_niveau = 'R'
            AND NOT EXISTS
                  (SELECT 1
                     FROM alg_v_allonroerendgoed
                    WHERE alg_locatie_key = 1                          -- BLCC
                          AND alg_type = 'R'
                          AND alg_ruimte_key = ok.alg_onrgoed_key);

DELETE FROM alg_onrgoedkenmerk ok
      WHERE ok.alg_onrgoed_niveau = 'V'
            AND NOT EXISTS
                  (SELECT 1
                     FROM alg_v_allonroerendgoed
                    WHERE alg_locatie_key = 1                          -- BLCC
                          AND alg_type = 'V'
                          AND alg_verdieping_key = ok.alg_onrgoed_key);

DELETE FROM alg_onrgoedkenmerk ok
      WHERE ok.alg_onrgoed_niveau = 'G'
            AND NOT EXISTS
                  (SELECT 1
                     FROM alg_v_allonroerendgoed
                    WHERE alg_locatie_key = 1                          -- BLCC
                          AND alg_type = 'G'
                          AND alg_gebouw_key = ok.alg_onrgoed_key);

DELETE FROM alg_onrgoedkenmerk ok
      WHERE ok.alg_onrgoed_niveau = 'T'
            AND NOT EXISTS
                  (SELECT 1
                     FROM alg_v_allonroerendgoed
                    WHERE alg_locatie_key = 1                          -- BLCC
                          AND alg_type = 'T'
                          AND alg_terreinsector_key = ok.alg_onrgoed_key);

DELETE FROM alg_kenmerk
      WHERE alg_kenmerk_key BETWEEN 1000 AND 1002;

DELETE FROM alg_ruimte r
      WHERE EXISTS
               (SELECT 1
                  FROM alg_v_allonroerendgoed
                 WHERE alg_locatie_key = 21                            -- BSSC
                       AND alg_type = 'R'
                       AND alg_ruimte_key = r.alg_ruimte_key);

DELETE FROM alg_verdieping v
      WHERE EXISTS
               (SELECT 1
                  FROM alg_v_allonroerendgoed
                 WHERE alg_locatie_key = 21                            -- BSSC
                       AND alg_type = 'V'
                       AND alg_verdieping_key = v.alg_verdieping_key);

DELETE FROM alg_gebouw g
      WHERE EXISTS
               (SELECT 1
                  FROM alg_v_allonroerendgoed
                 WHERE alg_locatie_key = 21                            -- BSSC
                       AND alg_type = 'G'
                       AND alg_gebouw_key = g.alg_gebouw_key);

DELETE FROM mld_adres
      WHERE alg_locatie_key = 21;

DELETE FROM alg_locatie
      WHERE alg_locatie_key = 21;

DELETE FROM alg_district
      WHERE alg_district_key = 21;

-- FIN - Financieel/Verkoopfacturen
--DELETE FROM fin_verkoopfactuur;
--DELETE FROM fin_verkoopfactuurkop;

--DELETE FROM prs_kostencombinatie;

--DELETE FROM prs_kostensoort;

DELETE FROM prs_kostenplaats kp
      WHERE (prs_kostenplaats_key = 341
             OR prs_kostenplaats_key BETWEEN 722 AND 733
             OR EXISTS
                  (SELECT 1
                     FROM prs_afdeling a, prs_v_afdeling_boom ab
                    WHERE a.prs_afdeling_key = ab.prs_afdeling_key
                          AND ab.prs_bedrijf_key IN (2441, 2442)       -- BSSC
                          AND a.prs_kostenplaats_key = kp.prs_kostenplaats_key))
            AND NOT EXISTS
                  (SELECT 1
                     FROM res_rsv_ruimte
                    WHERE prs_kostenplaats_key = kp.prs_kostenplaats_key);

DELETE FROM prs_kostenplaatsgrp
      WHERE prs_kostenplaatsgrp_key IN (121, 122);

DELETE FROM fac_tracking t
      WHERE EXISTS
               (SELECT 1
                  FROM fac_srtnotificatie
                 WHERE fac_srtnotificatie_xmlnode IN ('kostenplaats')
                       AND fac_srtnotificatie_key = t.fac_srtnotificatie_key)
            AND NOT EXISTS (SELECT 1
                              FROM prs_kostenplaats
                             WHERE prs_kostenplaats_key = t.fac_tracking_refkey);

-- Overig
-- Schonen ongebruikte kenmerkdomeinen!
DELETE FROM fac_kenmerkdomein kd
      WHERE fac_kenmerkdomein_key IN (141, 421, 422, 441, 442, 461, 462);

-- Schonen eigen tabellen van BLCC!
DELETE FROM fac_usrdata WHERE fac_usrtab_key BETWEEN 401 AND 462;
DELETE FROM fac_usrtab WHERE fac_usrtab_key BETWEEN 401 AND 462;

-- Eerst _FACILITOR-gebruiker onder BLCC-bedrijf hangen!
DELETE FROM fac_groeprechten gr
      WHERE gr.fac_groep_key NOT IN (1, 2, 301, 501) -- _Default/_Admin/_FACILITOR/_API
            AND NOT EXISTS
                  (SELECT 1
                     FROM fac_gebruikersgroep gg, prs_perslid p, prs_v_afdeling_boom ab
                    WHERE gg.prs_perslid_key = p.prs_perslid_key
                          AND p.prs_afdeling_key = ab.prs_afdeling_key
                          AND ab.prs_bedrijf_key NOT IN (2441, 2442)   -- BLCC
                          AND gg.fac_groep_key = gr.fac_groep_key);

DELETE FROM fac_groep g
      WHERE NOT EXISTS
                  (SELECT 1
                     FROM fac_gebruikersgroep
                    WHERE fac_groep_key = g.fac_groep_key);

-- Droppen BSSC-procedures/views!
--'bssc_v_rap_ssc_cat_looplijst'
--'bssc_v_rap_ssc_cat_factlijst'

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