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

COMMIT;
SET DEFINE OFF
/////////////////////////////////////////////////////////////////////////////////////////// YKPN#79783
// Deze als eerste, want op de oorspronkelijke plek was de nodige fac-package al invalid
// en zo kunnen we een geforceerde compile vermijden
INSERT INTO prs_perslid (prs_perslid_module,
                         prs_srtperslid_key,
                         prs_afdeling_key,
                         prs_perslid_naam,
                         prs_perslid_oslogin,
                         prs_perslid_apikey,
                         prs_perslid_flags,
                         prs_perslid_opmerking)
   SELECT prs_perslid_module,
          prs_srtperslid_key,
          prs_afdeling_key,
          'Sensor',
          '_SENSOR',
          DBMS_RANDOM.string ('a', 32),
          36,
          'System account for sensor-triggered-events.'||CHR(13)||'For Facilitor internal use'
     FROM prs_perslid
    WHERE prs_perslid_oslogin = '_FACILITOR'
      AND NOT EXISTS (SELECT 1 FROM prs_perslid WHERE prs_perslid_oslogin = '_SENSOR');

/////////////////////////////////////////////////////////////////////////////////////////// PZEE#79764
// Deze idem, ook vooraan
UPDATE prs_perslid
   SET prs_perslid_inactief = SYSDATE
 WHERE prs_perslid_naam LIKE 'INACTIEF:%' AND prs_perslid_inactief IS NULL;

/////////////////////////////////////////////////////////////////////////////////////////// AAIT#77483
ALTER TABLE fac_gebruikersgroep ADD fac_gebruikersgroep_vervaldatum DATE;

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#79092
ALTER TABLE fac_usrrap_cols  MODIFY fac_usrrap_cols_column_name VARCHAR2(128);

/////////////////////////////////////////////////////////////////////////////////////////// PCHW#79057
ALTER TABLE fac_usrrap_cols DROP CONSTRAINT fac_u_fac_usrrap_cols1 DROP INDEX;

UPDATE fac_usrrap_cols
   SET fac_usrrap_cols_column_name =
              fac_usrrap_cols_column_name
           || '_DUPLICATE_COLUMN_NAME_'
           || fac_usrrap_cols_key
 WHERE fac_usrrap_cols_key IN
           (SELECT fac_usrrap_cols_key
              FROM fac_usrrap_cols
             WHERE ROWID NOT IN
                       (  SELECT MIN (ROWID)
                            FROM fac_usrrap_cols
                        GROUP BY fac_usrrap_key,
                                 UPPER (fac_usrrap_cols_column_name)));

CREATE UNIQUE INDEX fac_i_fac_usrrap_cols1 ON fac_usrrap_cols(fac_usrrap_key, UPPER(fac_usrrap_cols_column_name));

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#78649
ALTER TABLE fac_version ADD fac_version_cleanup_date DATE;

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#79033
UPDATE fac_import_app
   SET fac_functie_key = (SELECT fac_functie_key FROM fac_functie WHERE fac_functie_code = 'WEB_FACFAC')
 WHERE fac_import_app_key IN
     ( SELECT i.fac_import_app_key
         FROM fac_import_app i
            , fac_functie f
        WHERE i.fac_import_app_code IN ( 'ONRGOED1_DEL'
                                       , 'ONRGOED2_DEL'
                                       , 'ORGANISATIE_DEL'
                                       , 'PERSLID_DEL'
                                       , 'MLD_DEL'
                                       , 'FAC_GROEP_DEL'
                                       , 'FAC_VAKGROEP_DEL'
                                       )
          AND i.fac_functie_key = f.fac_functie_key
          AND f.fac_functie_code = 'WEB_FACTAB'
     );

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#77754
ALTER TABLE fac_imp_onrgoed1 MODIFY
    ( alg_kenmerkwaarde1  VARCHAR2(4000)
    , alg_kenmerkwaarde2  VARCHAR2(4000)
    , alg_kenmerkwaarde3  VARCHAR2(4000)
    , alg_kenmerkwaarde4  VARCHAR2(4000)
    , alg_kenmerkwaarde5  VARCHAR2(4000)
    , alg_kenmerkwaarde6  VARCHAR2(4000)
    , alg_kenmerkwaarde7  VARCHAR2(4000)
    , alg_kenmerkwaarde8  VARCHAR2(4000)
    , alg_kenmerkwaarde9  VARCHAR2(4000)
    , alg_kenmerkwaarde10 VARCHAR2(4000)
    );
ALTER TABLE fac_imp_onrgoed2 MODIFY
    ( alg_kenmerkwaarde1  VARCHAR2(4000)
    , alg_kenmerkwaarde2  VARCHAR2(4000)
    , alg_kenmerkwaarde3  VARCHAR2(4000)
    , alg_kenmerkwaarde4  VARCHAR2(4000)
    , alg_kenmerkwaarde5  VARCHAR2(4000)
    );
ALTER TABLE fac_imp_perslid MODIFY
    ( prs_kenmerk1  VARCHAR2(4000)
    , prs_kenmerk2  VARCHAR2(4000)
    , prs_kenmerk3  VARCHAR2(4000)
    , prs_kenmerk4  VARCHAR2(4000)
    , prs_kenmerk5  VARCHAR2(4000)
    , prs_kenmerk6  VARCHAR2(4000)
    , prs_kenmerk7  VARCHAR2(4000)
    , prs_kenmerk8  VARCHAR2(4000)
    , prs_kenmerk9  VARCHAR2(4000)
    , prs_kenmerk10 VARCHAR2(4000)
    , prs_kenmerk11 VARCHAR2(4000)
    , prs_kenmerk12 VARCHAR2(4000)
    , prs_kenmerk13 VARCHAR2(4000)
    , prs_kenmerk14 VARCHAR2(4000)
    , prs_kenmerk15 VARCHAR2(4000)
    , prs_kenmerk16 VARCHAR2(4000)
    , prs_kenmerk17 VARCHAR2(4000)
    , prs_kenmerk18 VARCHAR2(4000)
    , prs_kenmerk19 VARCHAR2(4000)
    , prs_kenmerk20 VARCHAR2(4000)
    );
ALTER TABLE fac_imp_sle MODIFY
    ( ins_kenmerkwaarde1 VARCHAR2(4000)
    , ins_kenmerkwaarde2 VARCHAR2(4000)
    , ins_kenmerkwaarde3 VARCHAR2(4000)
    , ins_kenmerkwaarde4 VARCHAR2(4000)
    , ins_kenmerkwaarde5 VARCHAR2(4000)
    );
ALTER TABLE fac_imp_factuur MODIFY
    ( kenmerk1  VARCHAR2(4000)
    , kenmerk2  VARCHAR2(4000)
    , kenmerk3  VARCHAR2(4000)
    , kenmerk4  VARCHAR2(4000)
    , kenmerk5  VARCHAR2(4000)
    , kenmerk6  VARCHAR2(4000)
    , kenmerk7  VARCHAR2(4000)
    , kenmerk8  VARCHAR2(4000)
    , kenmerk9  VARCHAR2(4000)
    , kenmerk10 VARCHAR2(4000)
    , kenmerk11 VARCHAR2(4000)
    , kenmerk12 VARCHAR2(4000)
    , kenmerk13 VARCHAR2(4000)
    , kenmerk14 VARCHAR2(4000)
    , kenmerk15 VARCHAR2(4000)
    , kenmerk16 VARCHAR2(4000)
    , kenmerk17 VARCHAR2(4000)
    , kenmerk18 VARCHAR2(4000)
    , kenmerk19 VARCHAR2(4000)
    , kenmerk20 VARCHAR2(4000)
    );

/////////////////////////////////////////////////////////////////////////////////////////// AAIT#78892
ALTER TABLE fac_nieuws ADD fac_nieuws_parentkey NUMBER(10) REFERENCES fac_nieuws(fac_nieuws_key) ON DELETE CASCADE;
DEF_FAC_FUNCTIE3('WEB_MSGUSE', 'MSG', 3, 0,    0);

/////////////////////////////////////////////////////////////////////////////////////////// PROR#75652
CREATE_TABLE(mld_stdmelding_srtruimte, 0)
(
    mld_stdmelding_srtruimte_key
        NUMBER(10)
        PRIMARY KEY,
    mld_stdmelding_key
        NUMBER(10)
        NOT_NULL(mld_stdmelding_key, mld_c_mld_stdmelding_key3)
        CONSTRAINT mld_r_mld_stdmelding_key11 REFERENCES mld_stdmelding(mld_stdmelding_key) ON DELETE CASCADE,
    alg_srtruimte_key
        NUMBER(10)
        NOT_NULL(alg_srtruimte_key, ins_c_ins_srtruimte_key)
        CONSTRAINT mld_r_ins_srtruimte_key REFERENCES alg_srtruimte(alg_srtruimte_key) ON DELETE CASCADE,
    CONSTRAINT mld_u_mld_stdmelding_srtruimte UNIQUE(mld_stdmelding_key, alg_srtruimte_key)
);

CREATE SEQUENCE mld_s_mld_stdm_srtruimte_key MINVALUE 1;

/////////////////////////////////////////////////////////////////////////////////////////// MARX#79430
UPDATE fac_functie
   SET fac_functie_module = 'FIN'
 WHERE fac_functie_code IN ('WEB_MLDFIN', 'WEB_BESFIN', 'WEB_CNTFIN');

/////////////////////////////////////////////////////////////////////////////////////////// MARX#78859
ALTER TABLE mld_melding_note ADD fac_tracking_key NUMBER (10) REFERENCES fac_tracking (fac_tracking_key) ON DELETE SET NULL;
ALTER TABLE mld_opdr_note ADD fac_tracking_key NUMBER (10) REFERENCES fac_tracking (fac_tracking_key) ON DELETE SET NULL;
CREATE INDEX mld_i_mld_melding_note2 ON mld_melding_note(fac_tracking_key);
CREATE INDEX mld_i_mld_opdr_note2 ON mld_opdr_note(fac_tracking_key);

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#79830
DELETE FROM fac_usrrap WHERE LOWER (fac_usrrap_view_name) = 'mld_v_udr_meldingkenmerk_cnd' AND fac_usrrap_functie = 32;
DELETE FROM fac_usrrap WHERE LOWER (fac_usrrap_view_name) = 'mld_v_udr_opdrachtkenmerk_cnd' AND fac_usrrap_functie = 32;
DELETE FROM fac_usrrap WHERE LOWER (fac_usrrap_view_name) = 'ins_v_udr_deelkenmerk_cnd' AND fac_usrrap_functie = 32;
DELETE FROM fac_locale WHERE fac_locale_tekst = 'UDR: Calls plus';
DELETE FROM fac_locale WHERE fac_locale_tekst = 'Basic report for call+property reports';
DELETE FROM fac_locale WHERE fac_locale_tekst = 'UDR: Order plus';
DELETE FROM fac_locale WHERE fac_locale_tekst = 'Basic report for order+property reports';
DELETE FROM fac_locale WHERE fac_locale_tekst = 'UDR: Objects plus';
DELETE FROM fac_locale WHERE fac_locale_tekst = 'Basic report for object+property reports';

/////////////////////////////////////////////////////////////////////////////////////////// SGGR#79416
CREATE VIEW ins_v_udr_sensorhistorie AS SELECT * FROM DUAL;
DEF_RAPPORT('WEB_UDRMAN', 32, 'ins_v_udr_sensorhistorie',      'UDR: Sensor recente historie',     'Basisrapport voor recente meetwaarden van sensoren',
                                                               'UDR: Sensors recent history',      'Basic report for recent values of sensors');
CREATE VIEW ins_v_udr_sensorhistogram AS SELECT * FROM DUAL;
DEF_RAPPORT('WEB_UDRMAN', 32, 'ins_v_udr_sensorhistogram',     'UDR: Sensor gearchiveerde historie','Basisrapport voor historische gecomprimeerde meetwaarden van status-sensoren',
                                                               'UDR: Sensors archived history',    'Basic report for historic compressed values of state-sensors');

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#79851
UPDATE fac_setting
   SET fac_setting_pvalue = CASE WHEN fac_setting_pvalue = 'false' THEN 0 ELSE 2 END
 WHERE fac_setting_pvalue IS NOT NULL AND fac_setting_name = 'fac_gui_counter_menu_daily';

ALTER TABLE fac_gui_counter
    DROP CONSTRAINT fac_u_fac_gui_counter DROP INDEX;

ALTER TABLE fac_gui_counter
    ADD CONSTRAINT fac_u_fac_gui_counter UNIQUE (fac_gui_counter_group,
                                                 fac_gui_counter_date,
                                                 fac_gui_counter_refkey,
                                                 prs_perslid_key,
                                                 fac_gui_counter_info);

/////////////////////////////////////////////////////////////////////////////////////////// YKPN#79783
ALTER TABLE ins_srtdeel ADD ins_srtdeel_statethreshold VARCHAR2(128);
ALTER TABLE ins_srtdeel ADD mld_stdmelding_key NUMBER(10) REFERENCES mld_stdmelding(mld_stdmelding_key);
INSERT INTO mld_meldbron(mld_meldbron_key, mld_meldbron_omschrijving) VALUES (11, 'Sensor');
// bijbehorende aanmaak van sensor-perslid is bovenin al gedaan.

/////////////////////////////////////////////////////////////////////////////////////////// AAIT#78162
DEF_FAC_SRTNOT('BESLIK', 0, 'lcl_noti_BESLIK', 'bes/bes_bestelling.asp?bes_key=',         0, 'bestelling',  10);
DEF_FAC_SRTNOT('MLDLIK', 0, 'lcl_noti_MLDLIK', 'mld/mld_melding.asp?mld_key=',            0, 'melding',     10);
DEF_FAC_SRTNOT('RESLIK', 0, 'lcl_noti_RESLIK', 'res/res_reservering.asp?rsv_ruimte_key=', 0, 'reservering', 10);

///////////////////////////////////////////////////////////////////////////////////////////  FCLT#79939
DEF_MENUITEM(2, 'lcl_menu_fmstats'              , '', 'FAC', 'appl/alg/alg_fm_stats.asp', 0, 0, 'WEB_PRSSYS', 'W', '1', '');
MENU_INS_AFTER('lcl_menu_fmstats', 'lcl_menu_gdprstats', 1);

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#79462
DROP TRIGGER exc_t_exc_import_B_IU;

/////////////////////////////////////////////////////////////////////////////////////////// INLO#79906
UPDATE fac_setting
   SET fac_setting_pvalue = 10
 WHERE fac_setting_pvalue = 2
   AND fac_setting_name IN ('res_planbord_options','res_forselect_options');

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#72820
BEGIN
    adm.trydrop ('TRIGGER prs_t_prs_staffel_B_IU');
    adm.trydrop ('SEQUENCE prs_s_prs_staffel_key');
    adm.trydrop ('TABLE prs_staffel');
END;
/
DELETE FROM fac_menuitems WHERE fac_menuitems_label = 'lcl_menu_prs_staffels';
DELETE FROM fac_menu WHERE fac_menu_alturl like 'appl/mgt/prs_staffel.asp%';


/////////////////////////////////////////////////////////////////////////////////////////// FCLT#80161
ALTER PACKAGE fac COMPILE; // anders (nog) invalid en lukt het volgende niet niet
ALTER PACKAGE fac COMPILE BODY;
UPDATE fac_setting
   SET fac_setting_pvalue = NULL
 WHERE fac_setting_pvalue is not null
   AND fac.safe_to_number(fac_setting_pvalue) > 10
   AND fac_setting_name ='facilitiespast_mld';

UPDATE fac_setting
   SET fac_setting_pvalue = (SELECT MAX(fac.safe_to_number(fac_setting_pvalue))
                               FROM fac_setting
                              WHERE fac_setting_name IN ('facilitiesfuture_mld','facilitiesfuture_bes','facilitiesfuture_bez','facilitiesfuture_res'))
 WHERE fac_setting_pvalue is not null
   AND fac_setting_name ='facilitiesfuture';

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#76740
#define PRESERVESETTINGVAL(setting, newdefault) \
UPDATE fac_setting \
   SET fac_setting_default = newdefault, fac_setting_pvalue = fac_setting_default \
 WHERE fac_setting_name = setting AND fac_setting_pvalue IS NULL; ~\
UPDATE fac_setting \
   SET fac_setting_default = newdefault, fac_setting_pvalue = NULL \
 WHERE     fac_setting_pvalue = newdefault \
       AND fac_setting_name = setting \
       AND fac_setting_default <> newdefault;

PRESERVESETTINGVAL('fg_ins_symbols', 'true')
PRESERVESETTINGVAL('menu_tab_toggles_portalmenu', '0')
PRESERVESETTINGVAL('mjb_inflation', '0')
PRESERVESETTINGVAL('mld_accept_confirm', 'true')
PRESERVESETTINGVAL('mld_order_autoprint', '3')
PRESERVESETTINGVAL('mld_use_order_approval', '0')
PRESERVESETTINGVAL('prs_werkplek_implicit', '1')
PRESERVESETTINGVAL('puo_usesenderasreplyto', 'true')
PRESERVESETTINGVAL('sys_ip_lockmode', '0')
PRESERVESETTINGVAL('wgt_enabled', '1')

#define DROP_MENUITEM(label, uriwc) \
DELETE FROM fac_menuitems WHERE fac_menuitems_label = label; ~\
DELETE FROM fac_menu WHERE fac_menu_alturl like uriwc;

DROP_MENUITEM('lcl_menu_bes_prodgroepen'      , 'appl/mgt/bes_srtprodgroep.asp%')
DROP_MENUITEM('lcl_menu_bes_proditems'        , 'appl/bes/bes_srtprod_search.asp%')
DROP_MENUITEM('lcl_menu_bes_prodcatalogi'     , 'appl/mgt/bes_disciplineprod.asp%')
DROP_MENUITEM('lcl_menu_bes_prodmonitor'      , 'appl/bes/bes_assmon_search.asp%')
DROP_MENUITEM('lcl_menu_bes_bedrijfprod'      , 'appl/bes/bes_bedrijfproduct_search.asp%')
DROP_MENUITEM('lcl_menu_bes_grootheden'       , 'appl/mgt/bes_grootheid.asp%')
// opvegen, onderstaand statement verwijdert bovenstaande ook, maar voor de zekerheid anders kan de functie niet weg.
DELETE FROM fac_menuitems
      WHERE fac_functie_key = (SELECT fac_functie_key
                                 FROM fac_functie
                                WHERE fac_functie_code = 'WEB_PRDMSU');
DELETE FROM fac_functie WHERE fac_functie_code = 'WEB_PRDMSU';

/////////////////////////////////////////////////////////////////////////////////////////// PROR#79462
BEGIN
    adm.trydrop ('index jgl');
END;
/

CREATE INDEX exc_i_exc_import2
    ON exc_import (APPT_ID || '|' || RECUR_ID, SEQ_NR, FAC_IMPORT_KEY);

/////////////////////////////////////////////////////////////////////////////////////////// BAMG#80325
DROP TRIGGER bes_t_bes_bestelling_item_B_IU;
DROP TRIGGER bes_t_bes_bestelopdr_item_B_IU;
DROP TRIGGER bes_t_bes_bestellevr_B_IU;
DROP TRIGGER bes_t_bes_bestellevr_item_B_IU;

/////////////////////////////////////////////////////////////////////////////////////////// EIND#80371
BEGIN
    adm.trydrop ('index jgl');
END;
/
CREATE INDEX mld_i_mld_melding_note3 ON mld_melding_note(mld_melding_note_parent_key);
CREATE INDEX mld_i_mld_melding_note4 ON mld_melding_note(prs_perslid_key);
CREATE INDEX mld_i_mld_opdr_note3 ON mld_opdr_note(mld_opdr_note_parent_key);
CREATE INDEX mld_i_mld_opdr_note4 ON mld_opdr_note(prs_perslid_key);

/////////////////////////////////////////////////////////////////////////////////////////// AAIT#78892
DEF_MENUITEM(3, 'lcl_mobile_msg'                , '', 'MSG', 'appl/pda/berichten.asp', 0, 0, 'WEB_MSGUSE', 'R', '1',              'fa-circle-y');

-- Op deze nieuwe LCL kunnen we wel veilig 1 menuitem matchen
INSERT INTO fac_menu (fac_menuitems_key,
                      fac_menu_volgnr,
                      fac_menu_altlabel,
                      fac_menu_alturl,
                      fac_menu_altgroep,
                      fac_menu_image,
                      fac_menu_level,
                      fac_menu_info)
    SELECT fac_menuitems_key,
           fac_menuitems_key * 100,
           fac_menuitems_label,
           fac_menuitems_url,
           fac_menuitems_groep,
           fac_menuitems_image,
           CASE
               WHEN fac_menuitems_groep IN (0, 1, 2) THEN 2
               WHEN fac_menuitems_groep IN (3, 7) THEN 1
               ELSE 0
           END    fac_menu_level,
           fac_menuitems_oms
      FROM fac_menuitems i
     WHERE fac_menuitems_label = 'lcl_mobile_msg';

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#00000



REGISTERONCE('$Id$')

#include "epilogue.inc"
