--
-- $Id$
--
-- Opruimscript om alle Geleen-data uit database voor BSSC te halen.

DEFINE thisfile = 'BSSC#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 ------

-- Runnen script bssc.sql!
-- Verhuizen _FACILITOR, _GAST, _HMAIL en _PUTORDERS naar CMD-afdeling én naar
-- CMD-werkplek (of geen werkplek)!
-- Wijzigen API-key van _HMAIL en _PUTORDERS en 'puo_fclt_web_apikey'-setting!
-- Genereren 'Nieuwe code' via Authenticator!
-- Wijzigen settings 'csstemplate' en 'puo_fromaddress'!

-- Omhangen FAC_VERSION-entry van BLCC naar BSSC!
UPDATE fac_version
   SET fac_version_cust = 'BSSC'
 WHERE fac_version_cust = 'BLCC';

-- Schonen pending notificaties (ook voor BSSC)!
DELETE FROM fac_notificatie;
DELETE FROM web_user_messages;

-- Omhangen import van BLCC naar BSSC!
UPDATE fac_import_app
   SET fac_import_app_prefix = 'BSSC'
 WHERE fac_import_app_key = 61                               -- PERSLID-import
       AND fac_import_app_prefix = 'BLCC';

-- Schonen imports/exports van BLCC (incl. alle historie)!
DELETE FROM imp_log;
DELETE FROM fac_import;
DELETE FROM fac_import_app WHERE fac_import_app_key = 101;
DELETE FROM fac_export_app;

-- Omhangen rapportages van BLCC naar BSSC!
UPDATE fac_usrrap
   SET fac_usrrap_view_name = 'bssc_v_rap_ssc_cat_looplijst'
 WHERE fac_usrrap_key = 681;

UPDATE fac_usrrap
   SET fac_usrrap_view_name = 'bssc_v_rap_ssc_cat_factlijst'
 WHERE fac_usrrap_key = 682;

-- Schonen rapportages van BLCC!
DELETE FROM fac_usrrap
      WHERE UPPER(fac_usrrap_view_name) LIKE 'BLCC%';

-- Schonen FAQ - Kennisbankitems (ALLES!)
DELETE FROM faq_kenmerkwaarde;
DELETE FROM faq_kenmerk;
DELETE FROM mld_stdmeldingfaq;
DELETE FROM fac_faq;

-- Schonen MLD - Meldingen/Opdrachten (ALLES!)
DELETE FROM mld_kenmerkopdr;
DELETE FROM mld_kenmerkmelding;
DELETE FROM mld_kenmerk;
DELETE FROM mld_srtkenmerk;
DELETE FROM mld_opdr_note;
DELETE FROM mld_opdr;
DELETE FROM mld_typeopdr_srtdiscipline;
DELETE FROM mld_typeopdr;
DELETE FROM mld_melding_note;
DELETE FROM mld_melding_object;
DELETE FROM mld_melding;
DELETE FROM mld_stdmelding_srtinst;
DELETE FROM mld_stdmelding;
DELETE FROM mld_disc_params;
DELETE FROM ins_tab_discipline WHERE ins_discipline_module = 'MLD';
DELETE FROM ins_srtdiscipline;
DELETE FROM mld_behandelgroep;
DELETE FROM mld_stdmeldinggroep;

DELETE FROM fac_tracking t
      WHERE EXISTS
               (SELECT 1
                  FROM fac_srtnotificatie
                 WHERE fac_srtnotificatie_xmlnode = 'melding'
                       AND fac_srtnotificatie_key = t.fac_srtnotificatie_key)
            AND NOT EXISTS (SELECT 1
                              FROM mld_melding
                             WHERE mld_melding_key = t.fac_tracking_refkey);

DELETE FROM fac_tracking t
      WHERE EXISTS
               (SELECT 1
                  FROM fac_srtnotificatie
                 WHERE fac_srtnotificatie_xmlnode = 'opdracht'
                       AND fac_srtnotificatie_key = t.fac_srtnotificatie_key)
            AND NOT EXISTS (SELECT 1
                              FROM mld_opdr
                             WHERE mld_opdr_key = t.fac_tracking_refkey);

-- 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 = 1                     -- CAMPUS
                       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 = 1                           -- CAMPUS
                       AND bez_afspraak_key = b.bez_afspraak_key);

DELETE FROM bez_afspraak
      WHERE alg_locatie_key = 1;

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 = 1                   -- CAMPUS
                       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 NOT 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 = 1                   -- CAMPUS
                       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 = 1                   -- CAMPUS
                       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 = 1                   -- CAMPUS
                       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 NOT 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 NOT 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 NOT 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 NOT 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 NOT 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 NOT 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 NOT 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 NOT 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 NOT 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 NOT EXISTS
               (SELECT 1
                  FROM cnt_contract
                 WHERE ins_discipline_key IN (561, 1688, 1701)
                       AND cnt_contract_key = cn.cnt_contract_key);

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

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

DELETE FROM cnt_kenmerk k
      WHERE NOT EXISTS
               (SELECT 1
                  FROM cnt_contract
                 WHERE ins_discipline_key IN (561, 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 NOT IN (561, 1688, 1701);

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

DELETE FROM ins_tab_discipline
      WHERE ins_discipline_module = 'CNT'
            AND ins_discipline_key NOT IN (561, 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 NOT 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 NOT 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 NOT IN (1021);
DELETE FROM ins_deel d       -- Afhankelijkheid met reserveringen op objecten!
      WHERE NOT 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 NOT 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 NOT IN (1021);

DELETE FROM ins_disc_params
      WHERE ins_discipline_key NOT IN (1021);

DELETE FROM ins_tab_discipline
      WHERE ins_discipline_module = 'INS'
            AND ins_discipline_key NOT 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 (2, 21, 22, 23, 805)
                          AND p.prs_perslid_key = kl.prs_link_key);

DELETE FROM prs_kenmerklink
      WHERE prs_kenmerklink_niveau IN ('A', 'B');

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 (2, 21, 22, 23, 805)
                          AND prs_afdeling_key = ra.prs_afdeling_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 (2, 21, 22, 23, 805)
                          AND p.prs_perslid_key = pw.prs_perslid_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 (2, 21, 22, 23, 805)
                          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 (2, 21, 22, 23, 805)
                  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 (2, 21, 22, 23, 805)
                  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 (2, 21, 22, 23, 805)
                  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 (2, 21, 22, 23, 805)
                          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 (2, 21, 22, 23, 805)
                          AND p.prs_perslid_key = gg.prs_perslid_key);

-- 75 BLCC-contacts voor afspraak bij BSSC!?
UPDATE bez_afspraak a
   SET a.bez_afspraak_contact_key = 4   -- _FACILITOR (of 10681 = Ron Smeets?)
 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 (2, 21, 22, 23, 805)
                  AND p.prs_perslid_key = a.bez_afspraak_contact_key);

-- 1 BLCC-hosts voor afspraak bij BSSC!?
UPDATE bez_afspraak a
   SET a.bez_afspraak_host_key = 4      -- _FACILITOR (of 10681 = Ron Smeets?)
 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 (2, 21, 22, 23, 805)
                  AND p.prs_perslid_key = a.bez_afspraak_host_key);

-- 0 BLCC-ers contact voor reservering bij BSSC!?
UPDATE res_rsv_ruimte r
   SET r.res_rsv_ruimte_contact_key = 4 -- _FACILITOR (of 10681 = Ron Smeets?)
 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 (2, 21, 22, 23, 805)
                  AND p.prs_perslid_key = r.res_rsv_ruimte_contact_key);

-- 6 BLCC-ers host voor reservering bij BSSC!?
UPDATE res_rsv_ruimte r
   SET r.res_rsv_ruimte_host_key = 4    -- _FACILITOR (of 10681 = Ron Smeets?)
 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 (2, 21, 22, 23, 805)
                  AND p.prs_perslid_key = r.res_rsv_ruimte_host_key);

-- 3 BLCC-ers wijziger van settings (Treur, Willems en Witter)!
UPDATE fac_setting s
   SET s.prs_perslid_key = 4            -- _FACILITOR (of 10681 = Ron Smeets?)
 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 (2, 21, 22, 23, 805)
                  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 EXISTS
                  (SELECT 1
                     FROM prs_v_afdeling_boom
                    WHERE prs_bedrijf_key IN (2, 21, 22, 23, 805)
                          AND prs_afdeling_key = p.prs_afdeling_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 (2, 21, 22, 23, 805)
                          AND prs_afdeling_key = a.prs_afdeling_key);

DELETE FROM prs_bedrijf
      WHERE prs_bedrijf_key IN (2, 21, 22, 23, 805);

DELETE FROM prs_bedrijfdienstlocatie bdl
      WHERE NOT 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 NOT EXISTS
                  (SELECT 1
                     FROM prs_bedrijf
                    WHERE prs_bedrijf_huurder = 1
                          AND prs_bedrijf_key = ba.prs_bedrijf_key);

DELETE FROM prs_bedrijf
      WHERE 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 = 21                         -- BSSC
                          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 = 21                         -- BSSC
                          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 = 21                         -- BSSC
                          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 = 21                         -- BSSC
                          AND alg_type = 'T'
                          AND alg_terreinsector_key = ok.alg_onrgoed_key);

DELETE FROM alg_kenmerk
      WHERE alg_kenmerk_key NOT BETWEEN 1000 AND 1002;

DELETE FROM alg_ruimte r
      WHERE EXISTS
               (SELECT 1
                  FROM alg_v_allonroerendgoed
                 WHERE alg_locatie_key = 1                           -- CAMPUS
                       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 = 1                           -- CAMPUS
                       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 = 1                           -- CAMPUS
                       AND alg_type = 'G'
                       AND alg_gebouw_key = g.alg_gebouw_key);

DELETE FROM mld_adres
      WHERE alg_locatie_key = 1;

DELETE FROM alg_locatie
      WHERE alg_locatie_key = 1;

DELETE FROM alg_district
      WHERE alg_district_key = 1;

-- 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
            AND prs_kostenplaats_key NOT BETWEEN 722 AND 733
            AND NOT 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);

DELETE FROM prs_kostenplaatsgrp
      WHERE prs_kostenplaatsgrp_key NOT IN (21, 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 NOT IN (141, 421, 422, 441, 442, 461, 462)
            AND NOT EXISTS
               (SELECT 1
                  FROM cnt_srtkenmerk
                 WHERE fac_kenmerkdomein_key = kd.fac_kenmerkdomein_key);

-- Schonen eigen tabellen van BLCC!
DELETE FROM fac_usrdata WHERE fac_usrtab_key < 400;
DELETE FROM fac_usrtab WHERE fac_usrtab_key < 400;

/*
-- Eerst _FACILITOR-gebruiker onder BSSC-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 IN (2441, 2442)       -- BSSC
                          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 BLCC-procedures/views!

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