/* DELETEr for Facilitor P-ATO (reverse OTAP) chain
 *
 * $Id$
 *
 */

DEFINE thisfile = 'anon_delete.SQL'
DEFINE dbuser   = '*'

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

SET FEEDBACK ON
SET TIME ON

-- De volgorde is gevoelig. Primair getest tegen HPJI
-- Er zijn TRUNCATE's die op zich ook wel opgelost worden door een latere
-- CASCADE maar expliciet verwijderen is duidelijker

TRUNCATE TABLE mld_opdr_note CASCADE;
TRUNCATE TABLE mld_melding_note CASCADE;
TRUNCATE TABLE fac_tracking CASCADE;

TRUNCATE TABLE FAC_AUDIT CASCADE;

TRUNCATE TABLE AUT_CLIENT_PERSLID CASCADE;

ALTER TABLE ins_deel DISABLE CONSTRAINT INS_R_RES_RSV_DEEL_KEY;
DELETE res_rsv_deel WHERE bez_bezoekers_key IS NOT NULL;
UPDATE ins_deel SET res_rsv_deel_key = NULL WHERE res_rsv_deel_key IS NOT NULL;
TRUNCATE TABLE BEZ_BEZOEKERS CASCADE;
TRUNCATE TABLE BEZ_AFSPRAAK CASCADE;
TRUNCATE TABLE BEZ_KENMERKWAARDE CASCADE;
TRUNCATE TABLE BGT_BUDGETMUTATIE CASCADE;
TRUNCATE TABLE BGT_BUDGET CASCADE;
TRUNCATE TABLE EXC_IMPORT CASCADE;
TRUNCATE TABLE FAC_BIJLAGEN CASCADE;
TRUNCATE TABLE FAC_GUI_COUNTER CASCADE;

UPDATE mld_disc_params SET fac_activiteit_key = NULL WHERE fac_activiteit_key IS NOT NULL;
ALTER TABLE mld_disc_params DISABLE CONSTRAINT MLD_R_FAC_ACTIVITEIT_KEY1;
TRUNCATE TABLE FAC_IMPORT_APP CASCADE;
TRUNCATE TABLE FAC_IMP_ADRES CASCADE;
TRUNCATE TABLE FAC_IMP_BEDRIJFADRES CASCADE;
TRUNCATE TABLE FAC_IMP_CATALOGUS CASCADE;
TRUNCATE TABLE FAC_IMP_CNT CASCADE;
TRUNCATE TABLE FAC_IMP_CONTACTPERSOON CASCADE;
TRUNCATE TABLE FAC_IMP_CSV CASCADE;
TRUNCATE TABLE FAC_IMP_EXT_BEDRIJF CASCADE;
TRUNCATE TABLE FAC_IMP_FACTUUR CASCADE;
TRUNCATE TABLE FAC_IMP_FAC_GEBRUIKERSGROEP CASCADE;
TRUNCATE TABLE FAC_IMP_FAC_GROEP CASCADE;
TRUNCATE TABLE FAC_IMP_FAQ CASCADE;
TRUNCATE TABLE FAC_IMP_FILE CASCADE;
TRUNCATE TABLE FAC_IMP_FLEX CASCADE;
TRUNCATE TABLE FAC_IMP_INS CASCADE;
TRUNCATE TABLE FAC_IMP_INSPECTIE CASCADE;
TRUNCATE TABLE FAC_IMP_KPN CASCADE;
TRUNCATE TABLE FAC_IMP_MENU CASCADE;
TRUNCATE TABLE FAC_IMP_MLD CASCADE;
TRUNCATE TABLE FAC_IMP_MLDAFMTXT CASCADE;
TRUNCATE TABLE FAC_IMP_ONRGOED1 CASCADE;
TRUNCATE TABLE FAC_IMP_ONRGOED2 CASCADE;
TRUNCATE TABLE FAC_IMP_OPROGNOSE CASCADE;
TRUNCATE TABLE FAC_IMP_ORGANISATIE CASCADE;
TRUNCATE TABLE FAC_IMP_PERSLID CASCADE;
TRUNCATE TABLE FAC_IMP_RES_ARTIKEL_SYNC CASCADE;
TRUNCATE TABLE FAC_IMP_SLE CASCADE;
TRUNCATE TABLE FAC_IMP_TRANSLATION CASCADE;
TRUNCATE TABLE FAC_IMP_USRDATA CASCADE;
TRUNCATE TABLE FAC_MESSAGE CASCADE;
TRUNCATE TABLE FAC_NOTIFICATIE CASCADE;
TRUNCATE TABLE FAC_PERFMON CASCADE;
TRUNCATE TABLE FAC_PERFMON_SAMPLE CASCADE;
TRUNCATE TABLE FAC_RESULT CASCADE;
TRUNCATE TABLE FAC_SELECTIE CASCADE;
TRUNCATE TABLE FAC_SESSION CASCADE;

TRUNCATE TABLE FIN_FACTUUR_NOTE CASCADE;
TRUNCATE TABLE FIN_KENMERKFACTREGEL CASCADE;
TRUNCATE TABLE FIN_FACTUURREGEL CASCADE;
TRUNCATE TABLE FIN_KENMERKFACTUUR CASCADE;
TRUNCATE TABLE FIN_FACTUUR CASCADE;
TRUNCATE TABLE FIN_VERKOOPFACTUUR CASCADE;
TRUNCATE TABLE FIN_VERKOOPFACTUURKOP CASCADE;
TRUNCATE TABLE IMP_LOG CASCADE;
TRUNCATE TABLE INS_IMPORT CASCADE;

TRUNCATE TABLE MLD_DEELWERKMATERIAAL CASCADE;
TRUNCATE TABLE MLD_DEELWERK CASCADE;
TRUNCATE TABLE MLD_OPDRUREN_DAGKOSTEN CASCADE;
TRUNCATE TABLE MLD_OPDRUREN_KOSTEN CASCADE;
TRUNCATE TABLE MLD_OPDR_UREN CASCADE;
TRUNCATE TABLE MLD_IMPROPDR CASCADE;
TRUNCATE TABLE MLD_KENMERKOPDR CASCADE;
TRUNCATE TABLE MLD_OPDR_UITVOEREN CASCADE;
TRUNCATE TABLE MLD_OPDR_MATERIAAL CASCADE;

TRUNCATE TABLE RES_KENMERKWAARDE CASCADE;
TRUNCATE TABLE RES_RSV_ARTIKEL CASCADE;
TRUNCATE TABLE RES_RSV_DEEL CASCADE;
UPDATE mld_melding SET res_rsv_ruimte_key = NULL WHERE res_rsv_ruimte_key IS NOT NULL;
ALTER TABLE mld_melding DISABLE CONSTRAINT MLD_R_RES_RSV_RUIMTE_KEY;
TRUNCATE TABLE RES_RSV_RUIMTE CASCADE;
TRUNCATE TABLE RES_RESERVERING CASCADE;

TRUNCATE TABLE MLD_OPDR CASCADE;



TRUNCATE TABLE MLD_KENMERKMELDING CASCADE;
TRUNCATE TABLE MLD_MELDING_NOTE CASCADE;
TRUNCATE TABLE MLD_MELDING_OBJECT CASCADE;
UPDATE bes_bestelling SET mld_melding_key = NULL WHERE mld_melding_key IS NOT NULL;
ALTER TABLE bes_bestelling DISABLE CONSTRAINT BES_R_MLD_MELDING1;
TRUNCATE TABLE MLD_MELDING CASCADE;

-- Bestelling kan naar een contract verwijzen, daarom contract eerst
-- Opdracht kan naar contract of dienst verwijzen. Na opdracht dus
TRUNCATE TABLE CNT_KENMERKCONTRACT CASCADE;
TRUNCATE TABLE CNT_CONTRACT_NOTE CASCADE;
TRUNCATE TABLE CNT_CONTRACT_DIENST CASCADE;
TRUNCATE TABLE CNT_CONTRACT_OBJECT CASCADE;
TRUNCATE TABLE CNT_CONTRACT_PLAATS CASCADE;
TRUNCATE TABLE CNT_CONTRACT CASCADE;
ALTER TABLE ins_deel DISABLE CONSTRAINT BES_R_BES_BESTELOPDR_KEY3;


TRUNCATE TABLE BES_KENMERKBESTELI CASCADE;
TRUNCATE TABLE BES_KENMERKBESTELL CASCADE;
TRUNCATE TABLE BES_BESTELLEVR_ITEM CASCADE;
TRUNCATE TABLE BES_BESTELLEVR CASCADE;
TRUNCATE TABLE BES_BESTELLING_ITEM CASCADE;
TRUNCATE TABLE BES_BESTELOPDR_ITEM CASCADE;
DELETE ins_deel WHERE bes_bestelopdr_key IS NOT NULL;
TRUNCATE TABLE BES_BESTELOPDR CASCADE;
TRUNCATE TABLE BES_BESTELLING CASCADE;
TRUNCATE TABLE BES_FAVORIET CASCADE;


TRUNCATE TABLE MSG_MESSAGE CASCADE;
TRUNCATE TABLE PRJ_DEEL CASCADE;
TRUNCATE TABLE PRJ_PERSLIDWERKPLEK CASCADE;
TRUNCATE TABLE PRJ_RUIMTE CASCADE;
TRUNCATE TABLE PRJ_RUIMTEAFDELING CASCADE;
TRUNCATE TABLE PRJ_SCENARIO CASCADE;
TRUNCATE TABLE PRJ_SCENARIO_NOTE CASCADE;
TRUNCATE TABLE PRJ_SELECTIE CASCADE;
TRUNCATE TABLE PRJ_WERKPLEK CASCADE;

TRUNCATE TABLE PRS_CONTACTPERSOON_LOCATIE CASCADE;
TRUNCATE TABLE PRS_CONTACTPERSOON CASCADE;
TRUNCATE TABLE PRS_KENMERKLINK  CASCADE;

TRUNCATE TABLE WEB_USER_MESSAGES CASCADE;
TRUNCATE TABLE FAC_ACTIVITEIT CASCADE;
TRUNCATE TABLE FAC_AUDITFAIL CASCADE;

ALTER TRIGGER aud_t_fac_gebruikersgroep DISABLE;
ALTER TRIGGER aud_t_prs_kostenplaats DISABLE;
ALTER TRIGGER aud_t_prs_perslidkostenplaats DISABLE;
UPDATE alg_gebouw SET prs_perslid_key_verantw = NULL WHERE prs_perslid_key_verantw IS NOT NULL;
UPDATE alg_gebouw SET prs_perslid_key_verantw2 = NULL WHERE prs_perslid_key_verantw2 IS NOT NULL;
UPDATE alg_terreinsector SET prs_perslid_key_verantw = NULL WHERE prs_perslid_key_verantw IS NOT NULL;
UPDATE alg_terreinsector SET prs_perslid_key_verantw2 = NULL WHERE prs_perslid_key_verantw2 IS NOT NULL;
UPDATE ins_deel SET prs_perslid_key_beh = NULL WHERE prs_perslid_key_beh IS NOT NULL;
UPDATE prs_kostenplaats SET prs_perslid_key = NULL WHERE prs_perslid_key IS NOT NULL;
UPDATE prs_kostenplaatsgrp SET prs_perslid_key = NULL WHERE prs_perslid_key IS NOT NULL;
UPDATE prs_kostenplaatsgrp SET prs_perslid_key = NULL WHERE prs_perslid_key IS NOT NULL;

BEGIN
  DBMS_STATS.gather_schema_stats (
    ownname          => USER,
    cascade          => TRUE,
    options          => 'GATHER AUTO');
END;
/

-- faclikedeeplink, PRS password reset, PRS QR code identificatie
DELETE
  FROM fac_bookmark
 WHERE fac_bookmark_expire IS NOT NULL;

DELETE
  FROM prs_perslid p
 WHERE (prs_perslid_oslogin IS NULL OR SUBSTR (prs_perslid_oslogin, 1, 1) <> '_')
       AND BITAND(prs_perslid_flags, 32) = 0 -- undeletable
       AND prs_perslid_key NOT IN
               (SELECT prs_perslid_key_auth
                  FROM fac_bookmark
                 WHERE prs_perslid_key_auth IS NOT NULL)
       AND prs_perslid_key NOT IN
               (SELECT fw.prs_perslid_key
                  FROM fac_v_webgebruiker fw, fac_functie f
                 WHERE fw.fac_functie_key = f.fac_functie_key
                   AND fac_functie_code = 'WEB_PRSSYS');

ALTER TRIGGER aud_t_fac_gebruikersgroep ENABLE;
ALTER TRIGGER aud_t_prs_kostenplaats ENABLE;
ALTER TRIGGER aud_t_prs_perslidkostenplaats ENABLE;
ALTER TRIGGER aud_t_fac_gebruikersgroep ENABLE;
ALTER TRIGGER aud_t_prs_kostenplaats ENABLE;
ALTER TRIGGER aud_t_prs_perslidkostenplaats ENABLE;

ALTER TABLE mld_melding ENABLE CONSTRAINT MLD_R_RES_RSV_RUIMTE_KEY;
ALTER TABLE ins_deel ENABLE CONSTRAINT INS_R_RES_RSV_DEEL_KEY;
ALTER TABLE ins_deel ENABLE CONSTRAINT BES_R_BES_BESTELOPDR_KEY3;
ALTER TABLE bes_bestelling ENABLE CONSTRAINT BES_R_MLD_MELDING1;
ALTER TABLE mld_disc_params ENABLE CONSTRAINT MLD_R_FAC_ACTIVITEIT_KEY1;

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