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

COMMIT;

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#55775
// Nog uit DB35toDB35a
ALTER TABLE fac_imp_factuur
    MODIFY locatie VARCHAR2(30);

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#55024
// Aanpassen indexen voor zover deze niet goed zijn
// Eerst wat tijdelijke procedures definieren voor het zonder errors toevoegen/hernoemen van indexen
CREATE OR REPLACE PROCEDURE tmp_trycreate (n IN VARCHAR2)
    AS
        stmt VARCHAR2 (1000);
    BEGIN
        stmt := 'CREATE ' || n;

        EXECUTE IMMEDIATE stmt;
        DBMS_OUTPUT.put_line ('Successfully created: ' || n);
    EXCEPTION
        WHEN OTHERS
        THEN
            IF SQLCODE IN (-901, -904, -942, -1418, -1452, -1918, -2158, -2425, -2427, -4043)
            THEN
                -- cannot create, voor identifier (column) table, view, index, sequence, object and trigger, constraint
                DBMS_OUTPUT.put_line ('Not found, not created: ' || n);
                NULL;
            ELSIF SQLCODE IN (-955, -1408, -1442, -2261)
            THEN
                -- object or not-null property already exist
                DBMS_OUTPUT.put_line ('Already exists, not created: ' || n);
                NULL;
            ELSE
                raise_application_error (-20001, 'Error trying to CREATE ' || n || ': ' || SQLERRM);
            END IF;
    END;
/

CREATE OR REPLACE PROCEDURE tmp_tryrename(n_from IN VARCHAR2, n_to IN VARCHAR2)
    AS
        exec_dml VARCHAR2(500);
    BEGIN
        exec_dml := 'ALTER '|| n_from ||' RENAME TO '|| n_to;
        EXECUTE IMMEDIATE exec_dml;
        DBMS_OUTPUT.put_line ('Successfully renamed index to: ' || n_to);
    EXCEPTION
        WHEN OTHERS
        THEN
            IF SQLCODE IN (-1418)
            THEN
                DBMS_OUTPUT.put_line ('Index does not exist: ' || n_from);
                NULL;
            ELSE
                raise_application_error (-20001, 'Error trying to RENAME INDEX ' || n_from || ': ' || SQLERRM);
            END IF;
    END;
/

CREATE OR REPLACE PROCEDURE tmp_tryrename_pk (tab IN VARCHAR2, name IN VARCHAR2)
    AS
        old_name VARCHAR2(100);
        exec_dml VARCHAR2(500);
   BEGIN
        -- Zoek de constaint-name van de primary-key op. Deze is dan meestal van type sys_c00....
        SELECT constraint_name
          INTO old_name
          FROM user_constraints
         WHERE table_name = UPPER(tab)
           AND constraint_type = 'P';  -- constraint_type : Unique, Reference, Check, Primary key
        --
        IF (UPPER(old_name) = UPPER(name))
        THEN
            DBMS_OUTPUT.put_line ('Already exists, not created PK: ' || name);
        ELSE
            exec_dml := 'ALTER TABLE '|| tab ||' RENAME CONSTRAINT '|| old_name ||' TO '|| name;
            EXECUTE IMMEDIATE exec_dml;
            exec_dml := 'ALTER INDEX '|| old_name ||' RENAME TO '|| name;
            EXECUTE IMMEDIATE exec_dml;
            DBMS_OUTPUT.put_line ('Successfully renamed to: ' || name);
        END IF;
    EXCEPTION
        WHEN NO_DATA_FOUND
        THEN
                DBMS_OUTPUT.put_line ('Table does not exist: ' || tab);
                NULL;
        WHEN OTHERS
        THEN
            IF SQLCODE IN (-1403)
            THEN
                DBMS_OUTPUT.put_line ('Index not found: ' || old_name);
                NULL;
            ELSIF SQLCODE IN (-2264, -1418)
            THEN
                DBMS_OUTPUT.put_line ('Index already exists: ' || name);
                NULL;
            ELSE
                raise_application_error (-20001, 'Error trying to RENAME TO CONSTRAINT ' || name || ': ' || SQLERRM);
            END IF;
    END;
/

CREATE OR REPLACE PROCEDURE tmp_tryalter (n IN VARCHAR2)
   AS
       exec_dml VARCHAR2 (1000);
   BEGIN
       exec_dml := 'ALTER ' || n;

       EXECUTE IMMEDIATE exec_dml;
       DBMS_OUTPUT.put_line ('Found, successfully altered: ' || n);
   EXCEPTION
       WHEN OTHERS
       THEN
          IF SQLCODE IN (-904, -942, -1418, -2289, -4043, -4080, -2443)
          THEN
             -- did not exist, voor identifier (column) table, view, index, sequence, object and trigger, constraint
             DBMS_OUTPUT.put_line ('Not found, not altered: ' || n);
             NULL;
          ELSIF SQLCODE IN (-955, -1442, -2261)
          THEN
             -- object or not-null property already exist
             DBMS_OUTPUT.put_line ('Already exists, not altered: ' || n);
             NULL;
          ELSE
             raise_application_error (-20001, 'Error trying to ALTER ' || n || ': ' || SQLERRM);
          END IF;
   END;
/

BEGIN
    adm.trydrop('INDEX prs_i_prs_kostenplaats1');

    tmp_trycreate('INDEX aut_i_cp_accesstoken ON aut_client_perslid(aut_client_perslid_accesstoken)');
    tmp_trycreate('INDEX aut_i_cp_refreshtkn  ON aut_client_perslid(aut_client_perslid_refreshtkn)');
    tmp_trycreate('UNIQUE INDEX fac_i_fac_menuitems1 ON fac_menuitems (fac_menuitems_label)');
    tmp_trycreate('UNIQUE INDEX fac_i_srtnotificatie_code2 on fac_srtnotificatie(fac_srtnotificatie_key, fac_srtnotificatie_xmlnode)');
    tmp_trycreate('UNIQUE INDEX mld_i_opdruren_dagkosten1 ON mld_opdruren_dagkosten(prs_perslid_key, mld_opdruren_dagkosten_datum)');
    tmp_trycreate('INDEX bgt_i_bgt_budget ON bgt_budget(ins_discipline_key, bgt_project_key, bgt_kostenrubriek_key, prs_kostensoortgrp_key, prs_kostensoort_key)');
    tmp_trycreate('INDEX bgt_i_bgt_disc_params ON bgt_disc_params(bgt_ins_discipline_key)');
    tmp_trycreate('INDEX ins_i_ins_deel_note1 ON ins_deel_note(ins_deel_key)');
    tmp_trycreate('INDEX mld_i_mld_opdr17 ON mld_opdr(mld_opdr_teverzenden)');
    tmp_tryalter('TABLE ins_tab_discipline ADD CONSTRAINT ins_u_ins_discipline_code UNIQUE (ins_discipline_code)');
    tmp_tryalter('TABLE mld_srtkenmerk ADD CONSTRAINT mld_u_mld_srtkenmerk_code UNIQUE (mld_srtkenmerk_code)');
    tmp_tryalter('TABLE mld_kenmerk ADD CONSTRAINT mld_u_mld_kenmerk_code UNIQUE (mld_kenmerk_code)');
    tmp_tryrename('INDEX bgt_bgt_budget_key_van',  'bgt_i_bgt_budget_key_van');
    tmp_tryrename('INDEX bgt_bgt_budget_key_naar', 'bgt_i_bgt_budget_key_naar');

    tmp_tryrename_pk('imp_log', 'imp_k_imp_log_key');
END;
/

// nu mogen de tijdelijke procedures weer verwijderd worden
DROP PROCEDURE tmp_tryrename_pk;
DROP PROCEDURE tmp_tryrename;
DROP PROCEDURE tmp_trycreate;
DROP PROCEDURE tmp_tryalter;

/////////////////////////////////////////////////////////////////////////////////////////// NYBU#55433
CREATE TABLE res_arrangement
(
    res_arrangement_key
        NUMBER(10)
        PRIMARY KEY
,   res_arrangement_omschrijving
        VARCHAR2(60)
        CONSTRAINT res_c_res_arrangement_omschr CHECK(res_arrangement_omschrijving IS NOT NULL)
,   res_arrangement_opmerking
        VARCHAR2(4000)
);
CREATE SEQUENCE res_s_res_arrangement_key MINVALUE 1;

CREATE TABLE res_arrangement_artikel
(  res_arrangement_artikel_key
      NUMBER(10)
      PRIMARY KEY
,  res_arrangement_key
      NUMBER(10)
      CONSTRAINT res_c_res_arrangement_key CHECK(res_arrangement_key IS NOT NULL)
      CONSTRAINT res_c_res_arrangement REFERENCES res_arrangement(res_arrangement_key)
,  res_artikel_key
      NUMBER(10)
      CONSTRAINT res_c_res_arrangement_art_key CHECK(res_artikel_key IS NOT NULL)
      CONSTRAINT res_c_res_arrangement_artikel REFERENCES res_artikel(res_artikel_key)
);
CREATE SEQUENCE res_s_res_arrangement_art_key MINVALUE 1;
create index res_i_res_arrangement_art1 on res_arrangement_artikel(res_arrangement_key, res_arrangement_artikel_key);

DEF_MENUITEM(2, 'lcl_menu_res_arrangementen', '', 'RES', 'appl/mgt/res_arrangement.asp', 0, 0, 'WEB_RESMSU', 'W', '1', '');
MENU_INS_AFTER('lcl_menu_res_arrangementen', 'lcl_menu_res_verbruiksartikele', 2);

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#53571
ALTER TABLE mld_disc_params
  ADD mld_disc_params_kanpubliek
        NUMBER(1)       -- valid are 0, 1
        DEFAULT 0
      NOT NULL;
DEF_MENUITEM(0, 'lcl_menu_mld_fe_common_wishes' , '', 'MLD', 'appl/mld/mld_search.asp?urole=fe&public=1&autosearch=1', 0, 0, 'WEB_MLDUSE', 'R', '0', '');

/////////////////////////////////////////////////////////////////////////////////////////// PCHD#54648
ALTER TABLE mld_typeopdr
  ADD mld_typeopdr_accept
        NUMBER(1)       -- valid are 0, 1
        DEFAULT 1
      NOT NULL;

/////////////////////////////////////////////////////////////////////////////////////////// DJIN#55036
ALTER TABLE alg_locatie ADD alg_locatie_vervaldatum DATE;
ALTER TABLE alg_terreinsector ADD alg_terreinsector_vervaldatum DATE;

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#56301
ALTER TABLE res_disc_params ADD res_disc_params_min_duur NUMBER(4,2) DEFAULT(0) NOT NULL;
ALTER TABLE res_disc_params ADD res_disc_params_min_deelnmrs NUMBER(5) DEFAULT(0) NOT NULL;

/////////////////////////////////////////////////////////////////////////////////////////// TWYN#55483
ALTER TABLE fac_usrrap
  ADD fac_usrrap_volgnr
        NUMBER(10);
create index fac_i_fac_usrrap_volgnr on fac_usrrap(fac_usrrap_volgnr, fac_usrrap_omschrijving);

/////////////////////////////////////////////////////////////////////////////////////////// YKPN#55141
ALTER TABLE bez_bezoekers ADD bez_bezoekers_aanmaak DATE DEFAULT SYSDATE NOT NULL;
ALTER TABLE bez_afspraak  ADD bez_afspraak_aanmaak DATE DEFAULT SYSDATE NOT NULL;

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#55575
ALTER TABLE fac_imp_ins ADD ins_deel_aanmaak DATE;
ALTER TABLE fac_imp_ins ADD ins_deel_vervaldatum DATE;
ALTER TABLE fac_imp_ins ADD ins_deel_aantal NUMBER(8,2) DEFAULT(0) NOT NULL;

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#55831
ALTER TABLE mld_melding_note ADD mld_melding_note_wijzigdatum DATE;
ALTER TABLE mld_opdr_note ADD mld_opdr_note_wijzigdatum DATE;

/////////////////////////////////////////////////////////////////////////////////////////// YKPN#55846
ALTER TABLE mld_typeopdr ADD mld_typeopdr_annuleermelding NUMBER(1) DEFAULT(0) NOT NULL
CONSTRAINT mld_c_mld_typeopdr_annulm CHECK(mld_typeopdr_annuleermelding IN (0,1,2)); /*0=niet, 1=vragen, 2=vanzelf*/

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#55795
ALTER TABLE fac_locale MODIFY fac_locale_tekst VARCHAR2(4000);

/////////////////////////////////////////////////////////////////////////////////////////// PLAT#55511
INSERT INTO fac_setting (fac_setting_module,
                         fac_setting_name,
                         fac_setting_description,
                         fac_setting_pvalue)
     VALUES('INS',
           'ins_element_description',
           'SQL voor de objectomschrijving achter de "heeft betrekking op" knop voor het selecteren van elementen',
            '');
UPDATE fac_setting
   SET fac_setting_pvalue = (SELECT fac_setting_pvalue FROM fac_setting WHERE fac_setting_name = 'fac_reserved_string1')
 WHERE fac_setting_name = 'ins_element_description';
UPDATE fac_setting
   SET fac_setting_pvalue = ''
 WHERE fac_setting_name = 'fac_reserved_string1';

 /////////////////////////////////////////////////////////////////////////////////////////// AAFM#52969
 UPDATE fac_setting
    SET fac_setting_pvalue = ''
  WHERE fac_setting_name = 'mld_note_sort_ascending';

  /////////////////////////////////////////////////////////////////////////////////////////// FCLT#56591
 INSERT INTO fac_setting (fac_setting_module,
                          fac_setting_name,
                          fac_setting_description,
                          fac_setting_pvalue)
      VALUES('FAC',
            'fac_xml_max_factuur',
            'Maximum number of lines for invoice in sales-invoice node',
             '');
 UPDATE fac_setting
    SET fac_setting_pvalue = (SELECT fac_setting_pvalue FROM fac_setting WHERE fac_setting_name = 'res_xml_max_rsv_ruimte')
  WHERE fac_setting_name = 'fac_xml_max_factuur';

/////////////////////////////////////////////////////////////////////////////////////////// NYBU#56537
ALTER TABLE cnt_disc_params ADD cnt_disc_params_binding NUMBER(3) DEFAULT (3) NOT NULL;

/////////////////////////////////////////////////////////////////////////////////////////// YKPN#55980
ALTER TABLE FIN_FACTUUR
  ADD cnt_contract_plaats_key
      NUMBER(10)
      CONSTRAINT fin_r_fin_factuur8 REFERENCES cnt_contract_plaats(cnt_contract_plaats_key);

/////////////////////////////////////////////////////////////////////////////////////////// RWSN#55076
ALTER TABLE fac_nieuws
  ADD fac_groep_key
      NUMBER(10)
      REFERENCES fac_groep(fac_groep_key);

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#54360
UPDATE fac_menuitems
   SET fac_menuitems_url = 'appl/mgt/mld_kenmerk.asp?niveau=M'
 WHERE fac_menuitems_label = 'lcl_menu_mld_kenmerken_m';

UPDATE fac_menuitems
   SET fac_menuitems_url = 'appl/mgt/mld_kenmerk.asp?niveau=O'
 WHERE fac_menuitems_label = 'lcl_menu_mld_kenmerken';

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#55853
CREATE INDEX mld_i_mld_opdr_uren2 ON mld_opdr_uren(mld_opdr_key);

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#54957
UPDATE fac_setting
   SET fac_setting_pvalue =
              'xsl/'
           || (SELECT MAX (fac_version_cust) FROM fac_version)
           || '.xsl'
 WHERE     fac_setting_name = 'puo_notificationxsl'
       AND (   LOWER (fac_setting_pvalue) = 'xsl/cust.xsl'
            OR LOWER (fac_setting_pvalue) = '../xsl/cust.xsl');

UPDATE prs_bedrijfadres
   SET prs_bedrijfadres_xsl =
              'xsl/'
           || (SELECT MAX (fac_version_cust) FROM fac_version)
           || '.xsl'
 WHERE LOWER (prs_bedrijfadres_xsl) = 'xsl/cust.xsl';

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#56028
UPDATE fac_srtnotificatie
   SET fac_srtnotificatie_url = 'mgt/prs_kostenplaats.asp?mode=show&id='
 WHERE fac_srtnotificatie_code = 'PRSKPU';

/////////////////////////////////////////////////////////////////////////////////////////// MDUX#31137
UPDATE fac_menu
   SET fac_menu_alturl = REPLACE(fac_menu_alturl, 'exp_export.asp', 'exp_export_confirm.asp')
 WHERE fac_menu_alturl LIKE '%exp_export%';

/////////////////////////////////////////////////////////////////////////////////////////// FNS#37089
ALTER TABLE ins_srtdiscipline
 DROP CONSTRAINT ins_c_ins_srtdiscipline_kk;

ALTER TABLE ins_srtdiscipline MODIFY ins_srtdiscipline_kostenklant NUMBER(2) DEFAULT (0);

ALTER TABLE ins_srtdiscipline
  ADD CONSTRAINT ins_c_ins_srtdiscipline_kk CHECK(ins_srtdiscipline_kostenklant IN (0,1,2,4,5,6,8,9,10));

UPDATE ins_srtdiscipline
   SET ins_srtdiscipline_kostenklant = 5
 WHERE ins_srtdiscipline_kostenklant = 1;

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#54494
ALTER TABLE fac_version
  ADD fac_version_group VARCHAR2(4);

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#56269
DROP INDEX prs_i_prs_perslid3;

ALTER TABLE prs_perslid
 DROP COLUMN prs_perslid_init_upper;

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#53978
DROP INDEX fac_i_fac_xml1;

DROP TABLE fac_xml PURGE;

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#56327
UPDATE fac_message
   SET fac_message_text = 'Copyright (c) 1996-2019, Aareon Nederland B.V.'
 WHERE fac_message_code = 'FAC_M005';

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#56379
ALTER TABLE fac_kenmwaarden
  ADD ( fac_kenmwaarden_kenmerklen NUMBER(4)
      , fac_kenmwaarden_kenmerkdec NUMBER(3)
      );

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#56461
UPDATE mld_disc_params
   SET mld_disc_params_srtgroep =
           CASE
               WHEN mld_disc_params_srtgroep = 1 THEN 5
               WHEN mld_disc_params_srtgroep = 2 THEN 3
               ELSE   -- mld_disc_params_srtgroep IN (NULL, 0, 3+)
                    1 -- New default
           END;

ALTER TABLE mld_disc_params
    MODIFY mld_disc_params_srtgroep NUMBER (1)
           DEFAULT (1)
           NOT NULL
           CONSTRAINT mld_c_mld_disc_params_srtgroep CHECK
               (mld_disc_params_srtgroep IN (1,5,2,3,7));

UPDATE fac_locale_xsl
   SET fac_locale_xsl_cust = NULL
 WHERE fac_locale_xsl_label = 'mld_discipline_srtgroepLOV';

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#56358
ALTER TABLE prs_bedrijfadres
  ADD ( prs_bedrijfadres_sender VARCHAR2(512)
      , prs_bedrijfadres_replyaddress VARCHAR2(512)
      );

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#56458
CREATE OR REPLACE TYPE km_record AS OBJECT (tbl VARCHAR2(50), cst VARCHAR2(50));
/

DECLARE
    TYPE km_tables  IS VARRAY(11) OF km_record;
    rec             km_tables;
    opdracht        VARCHAR2(1000);
    sql_type        VARCHAR2(1000);
BEGIN
    rec := km_tables( km_record('alg_kenmerk', 'alg_c_alg_kenmerk_type1')
                    , km_record('bez_kenmerk', 'bez_c_bez_kenmerk_type2')
                    , km_record('faq_kenmerk', 'faq_c_faq_kenmerk_type1')
                    , km_record('fin_kenmerk', 'fin_c_fin_kenmerk_type1')
                    , km_record('prs_kenmerk', 'prs_c_prs_kenmerk_type2')
                    , km_record('bes_srtkenmerk', 'bes_c_bes_srtkenmerk_kenmtype')
                    , km_record('cnt_srtkenmerk', 'cnt_c_cnt_srtkenmerk_kenmtype')
                    , km_record('ins_srtkenmerk', 'ins_c_ins_srtkenmerk_kenmtype')
                    , km_record('mld_srtkenmerk', 'mld_c_mld_srtkenmerk_kenmtype')
                    , km_record('res_srtkenmerk', 'res_c_res_srtkenmerk_kenmtype')
                    , km_record('fac_kenmwaarden', 'fac_c_fac_kenmwaarden_kenmtype')
                    );
    FOR i IN 1 .. 11 LOOP
        --
        -- huidige constraint verwijderen
        opdracht := 'TABLE '|| rec(i).tbl ||' DROP CONSTRAINT '|| rec(i).cst;
        --DBMS_OUTPUT.put_line('ALTER '|| opdracht);
        ADM.tryalter(opdracht);
        --
        -- nieuwe constraint zetten
        opdracht := 'TABLE '|| rec(i).tbl ||' ADD CONSTRAINT '|| rec(i).cst
                 ||' CHECK('|| rec(i).tbl ||'_kenmerktype'
                 ||' IN (''C'',''N'',''D'',''T'',''R'',''X'',''L'',''Q'',''F'',''M'',''E'',''S'',''l'',''B'',''V''))'
                 ||' ENABLE NOVALIDATE';
        --DBMS_OUTPUT.put_line('ALTER '|| opdracht);
        ADM.tryalter(opdracht);
        --
        -- waarden voor checkbox omzetten
        IF (rec(i).tbl = 'fac_kenmwaarden')
        THEN
            DELETE FROM fac_kenmwaarden;
        ELSE
            opdracht := 'UPDATE '|| rec(i).tbl
                     || '   SET '|| rec(i).tbl ||'_kenmerktype = ''V'''
                     || '     , '|| rec(i).tbl ||'_lengte = NULL'
                     || '     , '|| rec(i).tbl ||'_nmin = NULL'
                     || '     , '|| rec(i).tbl ||'_nmax = NULL'
                     || ' WHERE '|| rec(i).tbl ||'_kenmerktype = ''N'''
                     || '   AND '|| rec(i).tbl ||'_lengte = 1'
                     || '   AND '|| rec(i).tbl ||'_nmin = 0'
                     || '   AND '|| rec(i).tbl ||'_nmax = 1';
            --DBMS_OUTPUT.put_line(opdracht);
            EXECUTE IMMEDIATE opdracht;
        END IF;
    END LOOP;
END;
/

DROP TYPE km_record;

/////////////////////////////////////////////////////////////////////////////////////////// DJIN#56347
ALTER TABLE aut_idp_map
    ADD aut_idp_map_autocreate -- unused/future use
        NUMBER(1)
        DEFAULT 0 -- +1: create; +2: update
        NOT NULL;

/////////////////////////////////////////////////////////////////////////////////////////// MARX#53135
ALTER TABLE prs_bedrijf_bedrijf
        ADD fin_btwtabelwaarde_key -- default btw
            NUMBER(10)
 CONSTRAINT prs_r_fin_btwtabelwaarde_key2 REFERENCES fin_btwtabelwaarde(fin_btwtabelwaarde_key);
ALTER TABLE prs_bedrijf_bedrijf
        ADD prs_bedrijf_bedrijf_g_pct -- G-percentage
            NUMBER(3)
 CONSTRAINT prs_c_bedrijf_bedrijf_g_pct CHECK(prs_bedrijf_bedrijf_g_pct BETWEEN 0 AND 100);

/////////////////////////////////////////////////////////////////////////////////////////// DJIN#51899
ALTER TABLE res_ruimte_opstelling
        ADD res_ruimte_opstel_tijd
            NUMBER(5,2)
            DEFAULT 0
            NOT NULL;

/////////////////////////////////////////////////////////////////////////////////////////// XXXX#43129

ALTER TABLE mld_melding MODIFY mld_melding_actiecode NUMBER(3);

ALTER TRIGGER mld_t_mld_melding_b_iu DISABLE;
ALTER PACKAGE fac COMPILE; // anders (nog) invalid en lukt fac.getSetting
ALTER PACKAGE fac COMPILE BODY;  // nodig?

UPDATE mld_melding
   SET mld_melding_actiecode = 2, -- (actie ligt bij melder)
       mld_melding_flag = 0 -- reset flag
 WHERE mld_melding_flag = fac.getSetting('mld_flag_on_fenote')
   AND fac.getSetting('mld_flag_on_fenote') > 0;

UPDATE mld_melding
   SET mld_melding_actiecode = 1 + 128, -- (actie bij BO+attentie)
       mld_melding_flag = 0 -- reset flag
 WHERE mld_melding_flag = fac.getSetting('mld_flag_on_bonote')
   AND fac.getSetting('mld_flag_on_bonote') > 0;

ALTER TRIGGER mld_t_mld_melding_B_IU ENABLE;

INSERT INTO fac_setting (fac_setting_module,
                         fac_setting_name,
                         fac_setting_description,
                         fac_setting_pvalue)
     VALUES('MLD',
           'mld_melding_actiecode',
           'Dummy',
            CASE WHEN fac.getSetting('mld_flag_on_fenote') > 0 OR fac.getSetting('mld_flag_on_fenote') > 0 THEN 1 ELSE NULL END);

// Proberen flags te resetten
DECLARE
    newflag   NUMBER (1);
BEGIN
    newflag := fac.getSetting ('mld_melding_flags');
    DBMS_OUTPUT.PUT_LINE('mld_melding_flags was: ' || newflag);
    DBMS_OUTPUT.PUT_LINE('mld_flag_on_fenote was: ' || fac.getSetting ('mld_flag_on_fenote'));
    DBMS_OUTPUT.PUT_LINE('mld_flag_on_bonote was: ' || fac.getSetting ('mld_flag_on_bonote'));

    IF fac.getSetting ('mld_flag_on_fenote') = newflag - 1
    THEN
        newflag := newflag - 1;
        UPDATE fac_setting
           SET fac_setting_pvalue = NULL
         WHERE fac_setting_name = 'mld_flag_on_fenote';
    END IF;
    IF fac.getSetting ('mld_flag_on_bonote') = newflag - 1
    THEN
        newflag := newflag - 1;
        UPDATE fac_setting
           SET fac_setting_pvalue = NULL
         WHERE fac_setting_name = 'mld_flag_on_bonote';
    END IF;
    IF fac.getSetting ('mld_flag_on_fenote') = newflag - 1
    THEN
        newflag := newflag - 1;
        UPDATE fac_setting
           SET fac_setting_pvalue = NULL
         WHERE fac_setting_name = 'mld_flag_on_fenote';
    END IF;
    IF newflag <= 1 THEN
        newflag := 0;
    END IF;
    DBMS_OUTPUT.PUT_LINE('mld_melding_flags is now: ' || newflag);
    IF newflag <> fac.getSetting ('mld_melding_flags') THEN
        UPDATE fac_setting
           SET fac_setting_pvalue = NULL
         WHERE fac_setting_name = 'mld_melding_flags';
    END IF;
END;
/

DEF_FAC_SRTNOT('MLDA2B', 0, 'lcl_noti_MLDA2B', 'mld/mld_melding.asp?urole=bo&mld_key=', '0', 'melding', 10);
DEF_FAC_SRTNOT('MLDA2M', 0, 'lcl_noti_MLDA2M', 'mld/mld_melding.asp?urole=fe&mld_key=', '0', 'melding', 10);

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#56113
ALTER TABLE mld_melding
   ADD mld_melding_externsyncdate    /* voor koppelingen met externe systemen */
        DATE;

/////////////////////////////////////////////////////////////////////////////////////////// NYBU#55434
ALTER TABLE res_kenmerk
 DROP CONSTRAINT res_c_res_kenmerk_niveau2;

ALTER TABLE res_kenmerk
  ADD CONSTRAINT res_c_res_kenmerk_niveau2 CHECK(res_kenmerk_niveau IN ('A', 'D')); /* Activiteit of Discipline/Catalogus */

ALTER TABLE res_kenmerk
  MODIFY res_kenmerk_niveau DEFAULT 'A';

ALTER TABLE res_kenmerk
  ADD res_artikel_key
      NUMBER(10)
  CONSTRAINT res_r_res_artikel_key REFERENCES res_artikel(res_artikel_key);

ALTER TABLE res_kenmerk
 DROP CONSTRAINT res_u_res_kenmerk DROP INDEX;

ALTER TABLE res_kenmerk
  ADD CONSTRAINT res_u_res_kenmerk UNIQUE(res_artikel_key, res_activiteit_key, res_srtkenmerk_key, res_kenmerk_groep, res_kenmerk_verwijder);

CREATE SEQUENCE res_s_res_kenmerkartikel_key    MINVALUE 1;

CREATE_TABLE(res_kenmerkartikel, 0)
(  res_kenmerkartikel_key
      NUMBER(10)
      CONSTRAINT res_k_res_kenmerkartikel_key PRIMARY KEY
,  res_rsv_artikel_key
      NUMBER(10)
,  res_kenmerk_key
      NUMBER(10)
,  res_kenmerkartikel_waarde
      VARCHAR2(4000)
,  res_kenmerkartikel_aanmaak
      DATE
      DEFAULT SYSDATE
,  res_kenmerkartikel_verwijder
      DATE
      DEFAULT NULLDATUM
,  CONSTRAINT res_r_res_rsv_artikel_key          CHECK(res_rsv_artikel_key IS NOT NULL)
,  CONSTRAINT res_r_res_kenmerk_key2             CHECK(res_kenmerk_key IS NOT NULL)
,  CONSTRAINT res_r_res_rsv_artikel_key2         FOREIGN KEY(res_rsv_artikel_key) REFERENCES res_rsv_artikel(res_rsv_artikel_key) ON DELETE CASCADE
,  CONSTRAINT res_c_res_kenmerkartikel_aanm      CHECK(res_kenmerkartikel_aanmaak IS NOT NULL)
,  CONSTRAINT res_u_res_kenmerkartikel           UNIQUE(res_rsv_artikel_key, res_kenmerk_key, res_kenmerkartikel_verwijder)
);

/////////////////////////////////////////////////////////////////////////////////////////// MNNL#55706
ALTER TABLE mld_workflowstep ADD
  mld_workflowstep_eindtype
      NUMBER(2)
      DEFAULT 0
      NOT_NULL(mld_workflowstep_eindtype, mld_c_mld_workflowstep_eindtyp);

/////////////////////////////////////////////////////////////////////////////////////////// MARX#56743
ALTER TABLE mld_opdr DROP CONSTRAINT mld_c_mld_opdr_materiaal;

/////////////////////////////////////////////////////////////////////////////////////////// DIAN#55467
ALTER TABLE ins_tab_discipline
 DROP CONSTRAINT ins_c_ins_discipline_kpn;

ALTER TABLE ins_tab_discipline
  ADD CONSTRAINT ins_c_ins_discipline_kpn CHECK(ins_discipline_kpnverplicht IN (0,1,2));

/////////////////////////////////////////////////////////////////////////////////////////// PCHD#54382
ALTER TABLE prs_dienst ADD prs_dienst_hint VARCHAR2(1000);

/////////////////////////////////////////////////////////////////////////////////////////// AAVG#56809
ALTER TABLE fac_usrrap_cols MODIFY fac_usrrap_cols_filterdefault VARCHAR2(500);

/////////////////////////////////////////////////////////////////////////////////////////// XXXX#nnnnn

///////////////////////////////////////////////////////////////////////////////////////////

REGISTERONCE('$Id$')

#include "epilogue.inc"
