/* DB14TO15.SRC
 * Update script van Facilitor schema
 * $Revision$
 * $Id$
 */
#include "prologue.inc"

COMMIT;
/////////////////////////////////////////////////////////////////////////////////////////// ATCH#21595
CREATE_TABLE(mld_opdruren_dagkosten, 0)
(
    mld_opdruren_dagkosten_key
        NUMBER(10)
        CONSTRAINT mld_k_opdruren_dagkosten_key PRIMARY KEY,
    prs_perslid_key
        NUMBER(10)
        CONSTRAINT mld_r_prs_perslid_key11 REFERENCES prs_perslid(prs_perslid_key),
    mld_opdruren_dagkosten_datum
        DATE
        CONSTRAINT mld_c_opdruren_dagkosten_datum NOT NULL,
    mld_opdruren_dagkosten_aantal
        NUMBER(3),
    mld_opdruren_dagkosten_totaal
        NUMBER(10),
    mld_opdruren_dagkosten_opm
        VARCHAR2(4000),
    mld_opdruren_dagkosten_aanmaak
        DATE
        DEFAULT SYSDATE
);

CREATE SEQUENCE mld_s_mld_opdruren_dagk_key MINVALUE 1;
-- Geen trunc op de datum?
CREATE UNIQUE INDEX mld_i_opdruren_dagkosten1 ON mld_opdruren_dagkosten(prs_perslid_key, mld_opdruren_dagkosten_datum);

CREATE_TRIGGER(mld_t_mld_opdruren_dagk_b_iu)
BEFORE INSERT OR UPDATE ON mld_opdruren_dagkosten
FOR EACH ROW
BEGIN
  UPDATE_PRIMARY_KEY(mld_opdruren_dagkosten_key, mld_s_mld_opdruren_dagk_key);
END;
/

/////////////////////////////////////////////////////////////////////////////////////////// UWVA#18070
UPDATE fac_functie SET fac_functie_min_level=3 WHERE fac_functie_code='WEB_FINMSU' AND fac_functie_min_level=0;

/////////////////////////////////////////////////////////////////////////////////////////// UWVA#18070
ALTER TABLE ins_deel MODIFY ins_deel_dwgx NUMBER(16,3);
ALTER TABLE ins_deel MODIFY ins_deel_dwgy NUMBER(16,3);
ALTER TABLE ins_deel MODIFY ins_deel_dwgz NUMBER(16,3);

/////////////////////////////////////////////////////////////////////////////////////////// FSN#22147
// Voor de zekerheid laatste keer invullen. Zou eigenlijk geen records moeten updaten
UPDATE mld_stdmelding
   SET mld_stdmelding_uitvoertijd = 2, mld_stdmelding_streeftijd = NULL
 WHERE mld_stdmelding_uitvoertijd IS NULL;
ALTER TABLE mld_stdmelding MODIFY mld_stdmelding_uitvoertijd NOT_NULL(mld_stdmelding_uitvoertijd, mld_c_stdmelding_uitvoertijd);
ALTER TABLE mld_stdmelding MODIFY mld_stdmelding_uitvoertijd DEFAULT 2;

/////////////////////////////////////////////////////////////////////////////////////////// FSN#????
ALTER TABLE bes_srtdeel ADD (bes_srtdeel_depotverdeelpct NUMBER(3));
ALTER TABLE bes_bedrijf_srtprod ADD (bes_bedrijf_srtprod_prijs NUMBER(8,2));

/////////////////////////////////////////////////////////////////////////////////////////// UWVA#22038
UPDATE mld_stdmelding
   SET mld_stdmelding_regime = 1
 WHERE mld_stdmelding_regime IS NULL;
ALTER TABLE mld_stdmelding MODIFY mld_stdmelding_regime DEFAULT 1;
ALTER TABLE mld_stdmelding DROP CONSTRAINT mld_c_stdmelding_regime;
ALTER TABLE mld_stdmelding MODIFY mld_stdmelding_regime CONSTRAINT mld_c_stdmelding_regime CHECK (mld_stdmelding_regime IS NOT NULL AND mld_stdmelding_regime IN (1,2,3));

/////////////////////////////////////////////////////////////////////////////////////////// FSN#22214
-- Allerlaatse keer DELETE. FAC_LOCALE_XSL_CUST kan nog niet gevuld zijn!
DELETE FROM fac_locale_xsl;
ALTER TABLE fac_locale_xsl ADD fac_locale_xsl_cust VARCHAR2(2000);

CREATE_TABLE(fac_locale_xref, 0) // TEMP XREF
(
  fac_locale_xref_key        NUMBER(10) PRIMARY KEY,
  fac_locale_xref_filepath   VARCHAR2(100 CHAR) NOT NULL,
  fac_locale_xref_client     NUMBER(1) // 1 voor clientside (anders serverside)
      CONSTRAINT fac_c_fac_locale_xref_client CHECK(fac_locale_xref_client IS NULL
                                                    OR fac_locale_xref_client = 1),
  fac_locale_xsl_key         REFERENCES fac_locale_xsl(fac_locale_xsl_key) ON DELETE CASCADE,
  fac_locale_xref_aanmaak    DATE DEFAULT SYSDATE
);

CREATE SEQUENCE fac_s_fac_locale_xref_key       MINVALUE 1;
CREATE UNIQUE INDEX fac_i_fac_locale_xref1 ON fac_locale_xref (fac_locale_xref_filepath, fac_locale_xsl_key);

-- Cleanup
update fac_menuitems set fac_menuitems_label = 'lcl_menu_mld_bo_close' WHERE fac_menuitems_label = 'lcl_menu_mld_bo_close   ';
update fac_menuitems set fac_menuitems_label = 'lcl_menu_fg_tekeningen' WHERE fac_menuitems_label = 'lcl_menu_fg_tekeningen ';
-- voor de onbekenden
update fac_menuitems set fac_menuitems_label = TRIM(fac_menuitems_label);

-- Eerst dubbelen die zouden ontstaan opruimen
DELETE
  FROM fac_localeitems a
 WHERE fac_localeitems_lcl LIKE '%.%'
       AND EXISTS
             (SELECT *
                FROM fac_localeitems b
               WHERE a.fac_localeitems_dialect_id =
                        b.fac_localeitems_dialect_id
                     AND REPLACE (REPLACE(a.fac_localeitems_lcl, 'LCL', 'lcl'), '.', '_') =
                           b.fac_localeitems_lcl);
UPDATE fac_localeitems
   SET fac_localeitems_lcl = replace(fac_localeitems_lcl, 'LCL.', 'lcl_')
 WHERE fac_localeitems_lcl LIKE 'LCL.%';
UPDATE fac_localeitems
   SET fac_localeitems_lcl = replace(fac_localeitems_lcl, '.', '_')
 WHERE fac_localeitems_lcl LIKE '%.%';

DEF_FAC_FUNCTIE2('WEB_LCLSYS', 'Beheer:Terminologie',        'FAC', 0, 0,2,'Om als beheerder de vaste teksten en vertalingen te kunnen muteren');
DEF_MENUITEM(2, 'lcl_menu_fac_vertalingen'          , '', 'FAC', 'appl/fac/fac_locale_search_std.asp', 0, 0, 'WEB_LCLSYS', 'R', '1');
-- Default optie, dus toevoegen
MENU_INS_AFTER('lcl_menu_fac_vertalingen', 'lcl_menu_fac_menustructuur', 2);

/////////////////////////////////////////////////////////////////////////////////////////// AAEN#22151
-- de verbeterde bes.getsrtdeelprijs() is nog niet beschikbaar tijdens de upgrade.
-- Zet de opdrachtprijzen gelijk aan de bestelprijzen
UPDATE bes_bestelopdr_item boi
   SET bes_bestelopdr_item_prijs =
          (SELECT bes_bestelling_item_prijs
             FROM bes_bestelling_item bi
            WHERE bi.bes_bestelopdr_item_key = boi.bes_bestelopdr_item_key)
 WHERE bes_bestelopdr_item_prijs IS NULL
   AND EXISTS
          (SELECT bes_bestelopdr_item_key
             FROM bes_bestelling_item bi
            WHERE bi.bes_bestelopdr_item_key = boi.bes_bestelopdr_item_key
                  AND bes_bestelling_item_prijs <> 0);

/////////////////////////////////////////////////////////////////////////////////////////// ATCH#22012
CREATE_TABLE(prs_perslid_inzetbaar, 0)
(
    prs_perslid_inzetbaar_key
        NUMBER(10)
        CONSTRAINT prs_k_prs_inzetbaar_key PRIMARY KEY,
    prs_perslid_key
        NUMBER(10)
        NOT_NULL(prs_perslid_key, prs_c_prs_perslid_key6)
        CONSTRAINT prs_r_prs_perslid_key17 REFERENCES prs_perslid(prs_perslid_key),
    prs_perslid_inzetbaar_van
        NUMBER(4)
        NOT_NULL(prs_perslid_inzetbaar_van, prs_c_prs_perslid_inzet_van)
        CONSTRAINT prs_c_prs_perslid_inzet_van2 CHECK(prs_perslid_inzetbaar_van>=0 AND prs_perslid_inzetbaar_van<1440),
    prs_perslid_inzetbaar_tot
        NUMBER(4)
        NOT_NULL(prs_perslid_inzetbaar_tot, prs_c_prs_perslid_inzet_tot)
        CONSTRAINT prs_c_prs_perslid_inzet_tot2 CHECK(prs_perslid_inzetbaar_tot>=0 AND prs_perslid_inzetbaar_tot<1440),
    prs_perslid_inzetbaar_dag  -- 0=zondag, 1=maandag, 2=dinsdag, 3=woensdag, 4=donderdag, 5=vrijdag, 6=zaterdag
        NUMBER(1)
        NOT_NULL(prs_perslid_inzetbaar_dag, prs_c_prs_perslid_inzet_dag)
);

CREATE_TABLE(mld_opdr_uitvoeren, 0)
(
    mld_opdr_uitvoeren_key
        NUMBER(10)
        CONSTRAINT mld_k_mld_opdr_uitvoeren_key PRIMARY KEY,
    mld_opdr_key
        NUMBER(10)
        NOT_NULL(mld_opdr_key, mld_c_mld_opdr_key)
        CONSTRAINT mld_r_mld_opdr_key2 REFERENCES mld_opdr(mld_opdr_key),
    mld_opdr_uitvoeren_van
        NUMBER(4)
        NOT_NULL(mld_opdr_uitvoeren_van, mld_c_opdr_inzetbaar_van)
        CONSTRAINT mld_c_opdr_inzetbaar_van2 CHECK(mld_opdr_uitvoeren_van>=0 AND mld_opdr_uitvoeren_van<1440),
    mld_opdr_uitvoeren_tot
        NUMBER(4)
        NOT_NULL(mld_opdr_uitvoeren_tot, mld_c_opdr_inzetbaar_tot)
        CONSTRAINT mld_c_opdr_inzetbaar_tot2 CHECK(mld_opdr_uitvoeren_tot>=0 AND mld_opdr_uitvoeren_tot<1440),
    mld_opdr_uitvoeren_dag  -- 0=zondag, 1=maandag, 2=dinsdag, 3=woensdag, 4=donderdag, 5=vrijdag, 6=zaterdag
        NUMBER(1)
        NOT_NULL(mld_opdr_uitvoeren_dag, mld_c_mld_opdr_uitvoeren_dag),
    mld_opdr_uitvoeren_infrequent
        NUMBER(1)
);

CREATE SEQUENCE prs_s_prs_perslid_inzet_key    MINVALUE 1;
CREATE SEQUENCE mld_s_mld_opdr_uitvoeren_key   MINVALUE 1;

CREATE UNIQUE INDEX prs_i_prs_perslid_inzetbaar1 ON prs_perslid_inzetbaar(prs_perslid_key, prs_perslid_inzetbaar_dag);
CREATE UNIQUE INDEX mld_i_mld_opdr_uitvoeren1 ON mld_opdr_uitvoeren(mld_opdr_key, mld_opdr_uitvoeren_dag);

/////////////////////////////////////////////////////////////////////////////////////////// FSN#21989
CREATE_TABLE(mld_opdr_materiaal, 0)
(
    mld_opdr_materiaal_key
        NUMBER(10)
        CONSTRAINT mld_k_mld_opdr_materiaal_key PRIMARY KEY,
    mld_opdr_key
        NUMBER(10)
        NOT_NULL(mld_opdr_key, mld_c_mld_opdr_key2)
        CONSTRAINT mld_r_mld_opdr_key3 REFERENCES mld_opdr(mld_opdr_key),
    fac_usrdata_key
        NUMBER(10)
        NOT_NULL(fac_usrdata_key, fac_c_fac_usrdata_key)
        CONSTRAINT fac_r_fac_usrdata_key REFERENCES fac_usrdata(fac_usrdata_key),
    mld_opdr_materiaal_aantal
        NUMBER(6)
        NOT_NULL(mld_opdr_materiaal_aantal, mld_c_mld_opdr_mat_aantal),
    mld_opdr_materiaal_prijs
       NUMBER(9,2)
       CONSTRAINT mld_c_mld_opdr_materiaal_prijs CHECK((mld_opdr_materiaal_prijs >= 0) or (mld_opdr_materiaal_prijs IS NULL))
);

CREATE SEQUENCE mld_s_mld_opdr_materiaal_key   MINVALUE 1;

CREATE UNIQUE INDEX mld_i_mld_opdr_materiaal1 ON mld_opdr_materiaal(mld_opdr_key, fac_usrdata_key);

ALTER TABLE mld_typeopdr DROP CONSTRAINT mld_c_mld_typeopdr_match;
ALTER TABLE mld_typeopdr MODIFY mld_typeopdr_matchtype CONSTRAINT mld_c_mld_typeopdr_match CHECK(mld_typeopdr_matchtype IN (0,1,2,3,4,5)); /*0=altijd ok, 1=mld_opdr_totaal, 2=termijnen, 3=mld_opdr_uren, 4=geen kosten 5=aantal x prijs */

/////////////////////////////////////////////////////////////////////////////////////////// GVBA#22321
ALTER TABLE res_srtactiviteit ADD res_srtactiviteit_anonym
    NUMBER(1) DEFAULT(0)
    CONSTRAINT res_c_srtactiviteit_anonym CHECK(res_srtactiviteit_anonym IN (0,1));
-- Veilige instelling voor als db14to15.asp nog niet is gedraaid
UPDATE res_srtactiviteit SET res_srtactiviteit_anonym = 1;

/////////////////////////////////////////////////////////////////////////////////////////// LOGI#22311
ALTER TABLE res_srtactiviteit DROP CONSTRAINT res_c_srtactiviteit_kpn;
ALTER TABLE res_srtactiviteit ADD CONSTRAINT res_c_srtactiviteit_kpn CHECK(res_srtactiviteit_kpnverplicht IN (0,1,2));

ALTER TABLE res_srtactiviteit ADD res_srtactiviteit_metomschr
    NUMBER(1) DEFAULT(1) CONSTRAINT res_c_srtactiviteit_metomschr CHECK(res_srtactiviteit_metomschr IN (0,1));
ALTER TABLE res_srtactiviteit ADD res_srtactiviteit_metopmerk
    NUMBER(1) DEFAULT(1) CONSTRAINT res_c_srtactiviteit_metopmerk CHECK(res_srtactiviteit_metopmerk IN (0,1));
ALTER TABLE res_srtactiviteit ADD res_srtactiviteit_metaantal
    NUMBER(1) DEFAULT(1) CONSTRAINT res_c_srtactiviteit_metaantal CHECK(res_srtactiviteit_metaantal IN (0,1));

ALTER TABLE res_activiteit ADD res_activiteit_aantalverplicht  /* is res_rsv_ruimte_bezoekers verplicht */
    NUMBER(1) DEFAULT(0) CONSTRAINT res_c_activiteit_aantal CHECK(res_activiteit_aantalverplicht IN (0,1));


/////////////////////////////////////////////////////////////////////////////////////////// FSN#22433
ALTER TABLE alg_onrgoedkenmerk DROP CONSTRAINT alg_u_alg_onrgoedkenmerk_key DROP INDEX;

ALTER TABLE alg_onrgoedkenmerk ADD CONSTRAINT alg_u_alg_onrgoedkenmerk_key
     UNIQUE(alg_onrgoed_key, alg_kenmerk_key, alg_onrgoedkenmerk_verwijder);

ALTER TABLE prs_kenmerklink DROP CONSTRAINT prs_u_prs_kenmerklink_key DROP INDEX;

ALTER TABLE prs_kenmerklink ADD CONSTRAINT prs_u_prs_kenmerklink_key
     UNIQUE(prs_link_key, prs_kenmerk_key,prs_kenmerklink_verwijder);

/////////////////////////////////////////////////////////////////////////////////////////// FSN#22325
ALTER TABLE prs_werkplek ADD prs_werkplek_type
        NUMBER(1) DEFAULT 0
        CONSTRAINT prs_c_prs_werkplek_type CHECK(prs_werkplek_type IN (0,1,2));

/////////////////////////////////////////////////////////////////////////////////////////// FSN#22249
ALTER TABLE FAC_IMPORT ADD (adm_tracking_date DATE);
ALTER TABLE FAC_IMPORT ADD (fac_import_filenaam VARCHAR2 (256));

ALTER TABLE fac_imp_file ADD (fac_import_key NUMBER);
ALTER TABLE fac_imp_file ADD (
  CONSTRAINT fac_r_fac_import_key1
 FOREIGN KEY (fac_import_key)
 REFERENCES fac_import (fac_import_key)
 ON DELETE CASCADE);

-- Opruimen
DELETE FROM imp_log
    WHERE imp_log_datum < sysdate - 60;

ALTER TABLE imp_log ADD (fac_import_key NUMBER);
ALTER TABLE imp_log ADD (
      CONSTRAINT fac_r_fac_import_key2
         FOREIGN KEY (fac_import_key)
         REFERENCES fac_import (fac_import_key)
         ON DELETE CASCADE);

ALTER TABLE imp_log ADD imp_log_key NUMBER(10);
CREATE SEQUENCE imp_s_imp_log_key MINVALUE 1;
DECLARE
   i_key   NUMBER;
   CURSOR c IS SELECT *
                   FROM imp_log
             ORDER BY IMP_LOG_APPLICATIE, IMP_LOG_RUN, IMP_LOG_DATUM
             FOR UPDATE OF imp_log_key;
BEGIN
   FOR rec IN c
   LOOP
      SELECT imp_s_imp_log_key.NEXTVAL INTO i_key FROM DUAL;
      UPDATE imp_log
         SET imp_log_key = i_key
       WHERE CURRENT OF c;
   END LOOP;
END;
/
CREATE UNIQUE INDEX imp_k_imp_log_key ON imp_log (imp_log_key);
ALTER TABLE imp_log ADD CONSTRAINT imp_k_imp_log PRIMARY KEY(imp_log_key);
ALTER TABLE imp_log ADD CONSTRAINT fac_c_imp_log_appl CHECK(fac_import_key IS NOT NULL OR imp_log_applicatie IS NOT NULL);

--ALTER TABLE IMP_LOG DROP COLUMN(IMP_LOG_APPLICATIE)-->neen, laten voor putorders
ALTER TABLE IMP_LOG MODIFY IMP_LOG_DATUM DEFAULT SYSDATE;


ALTER TABLE fac_imp_catalogus ADD (fac_import_key NUMBER);
ALTER TABLE fac_imp_catalogus ADD (
      CONSTRAINT fac_r_fac_import_key3
         FOREIGN KEY (fac_import_key)
         REFERENCES fac_import (fac_import_key)
         ON DELETE CASCADE);

ALTER TABLE fac_import_app ADD  (fac_import_app_charset VARCHAR(32));
-- NOT NULL DEFAULT 'Windows-1252';?

ALTER TABLE fac_import_app ADD (fac_import_app_folder VARCHAR(320));
ALTER TABLE fac_import_app ADD (fac_import_app_files VARCHAR(32));
ALTER TABLE fac_import_app ADD (fac_import_app_prefix VARCHAR(4));

UPDATE fac_import_app
   SET fac_import_app_prefix = SUBSTR(USER,1,4) -- Noot: niet per definitie customerId, db14to15.asp corrigeert dat
 WHERE fac_import_app_systeem IS NULL;

-- De volgende index was soms nog niet aanwezig
DROP INDEX fac_i_fac_import_app1;
-- Daardoor was 'FIP: 6-Aanvullen ruimtegebonden objecten' soms dubbel aanwezig
-- Zet bestaande imports om naar de app met laagste key
UPDATE fac_import
   SET fac_import_app_key =
          (SELECT MIN (fac_import_app_key)
             FROM fac_import_app
            WHERE fac_import_app_oms =
                     'FIP: 6-Aanvullen ruimtegebonden objecten')
 WHERE fac_import_app_key IN
             (SELECT fac_import_app_key
                FROM fac_import_app
               WHERE fac_import_app_oms =
                        'FIP: 6-Aanvullen ruimtegebonden objecten');
-- Verwijder nu de app met de hoogste key
DELETE FROM fac_import_app fia1
      WHERE fia1.fac_import_app_oms =
               'FIP: 6-Aanvullen ruimtegebonden objecten'
            AND EXISTS
                  (SELECT *
                     FROM fac_import_app fia2
                    WHERE fia2.fac_import_app_oms =
                             'FIP: 6-Aanvullen ruimtegebonden objecten'
                          AND fia1.fac_import_app_key >
                                fia2.fac_import_app_key);
-- Nu kan eindelijk de index er weer op
CREATE UNIQUE INDEX fac_i_fac_import_app1
   ON fac_import_app (fac_import_app_code,
                      ins_discipline_key,
                      prs_bedrijf_key,
                      fac_import_app_prefix);

/////////////////////////////////////////////////////////////////////////////////////////// AALB#21864
CREATE_TABLE(fin_factuur_note, 0)
(
   fin_factuur_note_key             NUMBER (10) PRIMARY KEY,
   fin_factuur_key                  NUMBER (10) NOT NULL REFERENCES fin_factuur (fin_factuur_key) ON DELETE CASCADE,
   fin_factuur_note_aanmaak         DATE        DEFAULT SYSDATE,
   prs_perslid_key                  NUMBER (10) REFERENCES prs_perslid (prs_perslid_key) ON DELETE SET NULL,
   fin_factuur_note_omschrijving    VARCHAR2 (4000 CHAR)
);

CREATE SEQUENCE fin_s_fin_factuur_note_key MINVALUE 1;

CREATE_TRIGGER(fin_t_fin_factuur_note_b_iu)
BEFORE INSERT OR UPDATE ON fin_factuur_note
FOR EACH ROW
BEGIN
  UPDATE_PRIMARY_KEY(fin_factuur_note_key, fin_s_fin_factuur_note_key);
END;
/

/////////////////////////////////////////////////////////////////////////////////////////// FSN#21553
CREATE_TABLE(fac_code2label, 0)
(
   fac_code2label_key     NUMBER (10) PRIMARY KEY,
   fac_code2label_domein  VARCHAR2 (30) NOT NULL,
   fac_code2label_code    NUMBER (3) NOT NULL,
   fac_code2label_label   VARCHAR2 (30) NOT NULL
);
CREATE SEQUENCE fac_s_fac_code2label_key        MINVALUE 1;

CREATE_TRIGGER(fac_t_fac_code2label_B_IU)
BEFORE INSERT OR UPDATE ON fac_code2label
FOR EACH ROW
BEGIN
   UPDATE_PRIMARY_KEY(fac_code2label_key, fac_s_fac_code2label_key);
END;
/
CREATE UNIQUE INDEX fac_i_fac_code2label ON fac_code2label (fac_code2label_domein, fac_code2label_code);
-- EN DAN NU DE DIVERSE INI's:
DEF_STATUS('opdracht', 1, 'lcl_mld_ord_afgewzen');
DEF_STATUS('opdracht', 2, 'lcl_mld_ord_niet_akkoord');
DEF_STATUS('opdracht', 3, 'lcl_mld_ord_ter_fiattering');
DEF_STATUS('opdracht', 4, 'lcl_mld_ord_gefiatteerd');
DEF_STATUS('opdracht', 5, 'lcl_mld_ord_uitgegeven');
DEF_STATUS('opdracht', 6, 'lcl_mld_ord_afgemeld');
DEF_STATUS('opdracht', 7, 'lcl_mld_ord_verwerkt');
DEF_STATUS('opdracht', 8, 'lcl_mld_ord_geaccepteerd');
DEF_STATUS('opdracht', 9, 'lcl_mld_ord_afgerond');
DEF_STATUS('opdracht', 10, 'lcl_mld_ord_ter_goedkeuring');

DEF_STATUS('melding', 0, 'lcl_mld_pending');
DEF_STATUS('melding', 1, 'lcl_mld_afgewezen');
DEF_STATUS('melding', 2, 'lcl_mld_ingevoerd');
DEF_STATUS('melding', 3, 'lcl_mld_ingezien');
DEF_STATUS('melding', 4, 'lcl_mld_geaccepteerd');
DEF_STATUS('melding', 5, 'lcl_mld_afgemeld');
DEF_STATUS('melding', 6, 'lcl_mld_verwerkt');
DEF_STATUS('melding', 7, 'lcl_mld_uitgegeven');
DEF_STATUS('melding', 99, 'lcl_mld_not_solved');

DEF_STATUS('factuur', 1, 'lcl_fin_afgewezen');
DEF_STATUS('factuur', 2, 'lcl_fin_ingevoerd');
DEF_STATUS('factuur', 3, 'lcl_fin_incompleet');
DEF_STATUS('factuur', 6, 'lcl_fin_akkoord');
DEF_STATUS('factuur', 7, 'lcl_fin_verwerkt');

DEF_STATUS('bestelling', 1, 'lcl_bes_afgewezen');
DEF_STATUS('bestelling', 2, 'lcl_bes_new');
DEF_STATUS('bestelling', 3, 'lcl_bes_gefiatteerd');
DEF_STATUS('bestelling', 4, 'lcl_bes_geaccepteerd');
DEF_STATUS('bestelling', 5, 'lcl_bes_besteld');
DEF_STATUS('bestelling', 6, 'lcl_bes_geleverd');
DEF_STATUS('bestelling', 7, 'lcl_bes_verwerkt');
DEF_STATUS('bestelling', 8, 'lcl_bes_is_bescan');

DEF_STATUS('bestelopdr', 1, 'lcl_bes_ord_afgewezen');
DEF_STATUS('bestelopdr', 2, 'lcl_bes_ord_new');
DEF_STATUS('bestelopdr', 3, 'lcl_bes_ord_inbehandeling');
DEF_STATUS('bestelopdr', 4, 'lcl_bes_ord_inbestelling');
DEF_STATUS('bestelopdr', 5, 'lcl_bes_ord_onbevestigd');
DEF_STATUS('bestelopdr', 6, 'lcl_bes_ord_geleverd');
DEF_STATUS('bestelopdr', 7, 'lcl_bes_ord_verwerkt');
DEF_STATUS('bestelopdr', 8, 'lcl_bes_ord_geannuleerd');

DEF_STATUS('reservering', 2, 'lcl_res_resnew');
DEF_STATUS('reservering', 5, 'lcl_res_resafm');
DEF_STATUS('reservering', 6, 'lcl_res_resver');

DEF_STATUS('resfostatus', 1, 'lcl_optie');
DEF_STATUS('resfostatus', 2, 'lcl_def');
DEF_STATUS('resfostatus', 3, 'lcl_blokkade');
DEF_STATUS('resfostatus', 4, 'lcl_vervallen');

-- De identieke/compatible views komen vanzelf met de recreate.

-- Wijigingen:
ALTER TABLE mld_melding DROP CONSTRAINT mld_r_mld_statuses_key;
ALTER TABLE mld_melding ADD CONSTRAINT mld_c_mld_status1 CHECK(mld_melding_status IN (0,1,2,3,4,5,6,7,99));
ALTER TABLE mld_opdr DROP CONSTRAINT mld_r_mld_statusopdr_key2;
ALTER TABLE mld_opdr ADD CONSTRAINT mld_c_mld_statusopdr_key2 CHECK (mld_statusopdr_key BETWEEN 1 AND 10);
ALTER TABLE fin_factuur DROP CONSTRAINT fin_r_fin_factuur6;
ALTER TABLE fin_factuur ADD CONSTRAINT fin_c_fin_status1 CHECK(fin_factuur_statuses_key IN (1,2,3,6,7));
ALTER TABLE res_rsv_ruimte DROP CONSTRAINT res_c_res_status_bo_key1;
ALTER TABLE res_rsv_ruimte ADD CONSTRAINT res_c_res_status_bo1 CHECK(res_status_bo_key IN (2,5,6));
ALTER TABLE res_rsv_deel DROP CONSTRAINT res_c_res_status_bo_key2;
ALTER TABLE res_rsv_deel ADD CONSTRAINT res_c_res_status_bo2 CHECK(res_status_bo_key IN (2,5,6));
ALTER TABLE res_rsv_artikel DROP CONSTRAINT res_c_res_status_bo_key3;
ALTER TABLE res_rsv_artikel ADD CONSTRAINT res_c_res_status_bo3 CHECK(res_status_bo_key IN (2,5,6));
ALTER TABLE res_rsv_ruimte DROP CONSTRAINT res_c_res_status_fo_key1;
ALTER TABLE res_rsv_ruimte ADD CONSTRAINT res_c_res_status_fo1 CHECK(res_status_fo_key IN (1,2,3,4));

/////////////////////////////////////////////////////////////////////////////////////////// AAIT#22177
ALTER TABLE mld_typeopdr ADD mld_typeopdr_offertelimiet NUMBER(8);
ALTER TABLE mld_typeopdr ADD mld_typeopdr_isofferte NUMBER(1) DEFAULT (0)
      CONSTRAINT mld_c_mld_typeopdr_isofferte CHECK(mld_typeopdr_isofferte IN (0,1));
ALTER TABLE mld_typeopdr ADD mld_typeopdr_typeopdr_key NUMBER(10)
      CONSTRAINT mld_r_typeopdr_key1 REFERENCES mld_typeopdr(mld_typeopdr_key);

DEF_FAC_SRTNOT('ORDOOK', 0, 'Offerte ##OPDRKEY## is goedgekeurd',                                           'mld/mld_opdr.asp?opdr_key=', '0', 'opdracht');
DEF_FAC_SRTNOT('ORDONO', 0, 'Offerte ##OPDRKEY## is afgewezen',                                             'mld/mld_opdr.asp?opdr_key=', '0', 'opdracht');

/////////////////////////////////////////////////////////////////////////////////////////// AAIT#22746
DEF_FAC_FUNCTIE2('WEB_ORDOAP', 'Backoffice:Offertes-accept',  'MLD', 0, 0, 0, 'Om offertes te kunnen accepteren/afwijzen');

/////////////////////////////////////////////////////////////////////////////////////////// FSN#22491
UPDATE ins_srtdeel SET ins_srtdeel_binding = 1 WHERE ins_srtdeel_binding = 0 AND ins_srtdeel_module='INS';
ALTER TABLE ins_srtdeel DROP CONSTRAINT ins_c_ins_srtdeel_binding;
ALTER TABLE ins_srtdeel ADD CONSTRAINT ins_c_ins_srtdeel_binding
  CHECK ((ins_srtdeel_module ='INS' AND ins_srtdeel_binding > 0) OR ins_srtdeel_module <> 'INS');

/////////////////////////////////////////////////////////////////////////////////////////// FSN#22523
ALTER TABLE mld_melding ADD prs_project_key NUMBER(10)
 CONSTRAINT mld_r_prs_project_key1 REFERENCES prs_project(prs_project_key);

ALTER TABLE prs_project ADD prs_project_datum DATE;

/////////////////////////////////////////////////////////////////////////////////////////// FSN#22159
INSERT INTO fac_setting (fac_setting_module,
                         fac_setting_name,
                         fac_setting_description,
                         fac_setting_default)
    VALUES ('FAC',
            'fac_logfilename',
            'Naam van de logfile',
            'temp/log_' || USER || '_' || DBMS_RANDOM.string ('a', 16));
              -- Noot: USER is niet per definitie customerId, db14to15.asp corrigeert dat

/////////////////////////////////////////////////////////////////////////////////////////// FSN#21129

ALTER TABLE mld_melding  DROP CONSTRAINT fac_r_fac_activiteit1;
ALTER TABLE mld_melding ADD (
  CONSTRAINT fac_r_fac_activiteit1
 FOREIGN KEY (fac_activiteit_key)
 REFERENCES fac_activiteit (fac_activiteit_key) ON DELETE CASCADE);

ALTER TABLE mld_opdr  DROP CONSTRAINT fac_r_fac_activiteit2;
ALTER TABLE mld_opdr ADD (
  CONSTRAINT fac_r_fac_activiteit2
 FOREIGN KEY (fac_activiteit_key)
 REFERENCES fac_activiteit (fac_activiteit_key) ON DELETE CASCADE);

ALTER TABLE bes_bestelling  DROP CONSTRAINT fac_r_fac_activiteit3;
ALTER TABLE bes_bestelling ADD (
  CONSTRAINT fac_r_fac_activiteit3
 FOREIGN KEY (fac_activiteit_key)
 REFERENCES fac_activiteit (fac_activiteit_key) ON DELETE CASCADE);

DELETE FROM fac_activiteit WHERE fac_activiteit_verwijder IS NOT NULL;

/////////////////////////////////////////////////////////////////////////////////////////// AAEN#20529
ALTER TABLE prs_ruimteafdeling MODIFY prs_ruimteafdeling_bezetting  NUMBER(8,5);

/////////////////////////////////////////////////////////////////////////////////////////// AAIT#22750
UPDATE fac_kenmerkdomein SET fac_kenmerkdomein_xmlnode = LOWER(fac_kenmerkdomein_xmlnode) WHERE fac_kenmerkdomein_xmlnode IS NOT NULL;

/////////////////////////////////////////////////////////////////////////////////////////// RWSN#22742
UPDATE res_rsv_ruimte r
   SET res_rsv_ruimte_cvab_mode =
          (SELECT res_rsv_ruimte_cvab_mode
             FROM res_rsv_ruimte rr
            WHERE     r.res_reservering_key = rr.res_reservering_key
                  AND res_rsv_ruimte_volgnr = 1)
 WHERE     res_rsv_ruimte_cvab_mode IS NULL
       AND res_rsv_ruimte_volgnr > 1
       AND res_ruimte_opstel_key IS NULL
       AND res_rsv_ruimte_verwijder IS NULL
       AND EXISTS (
             SELECT res_rsv_ruimte_cvab_mode
                FROM res_rsv_ruimte rr
            WHERE     r.res_reservering_key = rr.res_reservering_key
                  AND res_rsv_ruimte_volgnr = 1
                  AND rr.res_rsv_ruimte_cvab_mode IS NOT NULL);

/////////////////////////////////////////////////////////////////////////////////////////// GENERAL
-- Correctie bij fac_ini.src;506
UPDATE   fac_srtnotificatie
   SET   fac_srtnotificatie_oms = REPLACE (fac_srtnotificatie_oms, 'Contract', 'Persoon')
 WHERE   fac_srtnotificatie_code LIKE 'PRS%';

/////////////////////////////////////////////////////////////////////////////////////////// CLEANUP
-- Drop PRA stuff
DELETE FROM fac_menuitems WHERE fac_menuitems_label='lcl_menu_prs_aanmeld';
DELETE FROM fac_menuitems WHERE fac_menuitems_label='lcl_menu_prs_mutaties';

-- Tijdelijk nu nodig, volgende keer in ADM package beschikbaar
CREATE OR REPLACE PROCEDURE tmp_trydrop (n IN VARCHAR2)
AS
   stmt VARCHAR2 (1000);
BEGIN
   stmt := 'DROP ' || n;

   EXECUTE IMMEDIATE stmt;
EXCEPTION
   WHEN OTHERS
   THEN
      IF SQLCODE IN (-942, -1418, -2289, -4043)
      THEN
         NULL;
      ELSE
         raise_application_error (-20001, 'Error trying to DROP ' || n || ': ' || SQLERRM);
      END IF;
END;
/

BEGIN
    tmp_trydrop('PROCEDURE PRS_set_aanmeld_workplace');
    tmp_trydrop('SEQUENCE prs_s_prs_aanmeld_key');
    tmp_trydrop('SEQUENCE prs_s_prs_perslid_wijzig_key');
    tmp_trydrop('TABLE prs_aanmeld PURGE');
    tmp_trydrop('TABLE prs_perslid_wijziging PURGE');
END;
/
DROP PROCEDURE tmp_trydrop;

DROP TABLE bes_bestellingstatuses PURGE;
DROP TABLE bes_bestelopdrstatuses PURGE;
DROP TABLE fin_factuur_statuses PURGE;
DROP TABLE mld_statuses PURGE;
DROP TABLE mld_statusopdr PURGE;
-- Beide indexen mld_i_mld_statusopdr1 en mld_i_mld_statusopdr2 zijn dan ook gedropt.
DROP TABLE res_status_bo PURGE;
DROP TABLE res_status_fo PURGE;

ALTER TABLE fac_imp_file DROP COLUMN ins_discipline_key;
ALTER TABLE fac_imp_file DROP COLUMN fac_imp_file_applicatie;
ALTER TABLE IMP_LOG DROP COLUMN IMP_LOG_RUN;
ALTER TABLE fac_imp_catalogus DROP COLUMN fac_imp_catalogus_id;
ALTER TABLE fac_import_app DROP COLUMN fac_import_app_systeem;

ALTER TABLE res_srtactiviteit DROP COLUMN res_srtactiviteit_aantalreq;
ALTER TABLE res_disc_params DROP COLUMN res_disc_params_aantalreq;
ALTER TABLE fac_kenmerkdomein DROP COLUMN fac_kenmerkdomein_key_org;

DROP TABLE fac_entity_name PURGE;

///////////////////////////////////////////////////////////////////////////////////////////
REGISTERONCE('$Workfile: DB14to15.src $','$Revision$')
#include "epilogue.inc"
