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

COMMIT;

/////////////////////////////////////////////////////////////////////////////////////////// MDUX#31775
INSERT INTO fac_usrrap (fac_usrrap_omschrijving, fac_usrrap_view_name,fac_usrrap_info, fac_functie_key, fac_usrrap_functie)
 SELECT 'UDR: Kostenoverzicht','FIN_V_UDR_KOSTENOVERZICHT','Basisrapport voor kostenrapportages', fac_functie_key, 32
   FROM fac_functie WHERE fac_functie_code='WEB_PRSSYS'
    AND NOT EXISTS (SELECT '' FROM fac_usrrap WHERE UPPER(fac_usrrap_view_name)='FIN_V_UDR_KOSTENOVERZICHT');

/////////////////////////////////////////////////////////////////////////////////////////// FSN#34749
ALTER TABLE fac_usrrap ADD fac_usrrap_graph_type
    NUMBER(1) DEFAULT 0 NOT NULL;

/////////////////////////////////////////////////////////////////////////////////////////// FSN#34679
DROP SEQUENCE alg_s_alg_srtterrein_key;
DECLARE
   stmt   VARCHAR2 (200);
BEGIN
   SELECT 'CREATE SEQUENCE alg_s_alg_srtterrein_key START WITH '
          || COALESCE(MAX (alg_srtterreinsector_key + 1), 1)
     INTO stmt
     FROM alg_srtterreinsector;

   EXECUTE IMMEDIATE stmt;
END;
/

/////////////////////////////////////////////////////////////////////////////////////////// FSN#34763
ALTER TABLE res_ruimte_opstelling DROP CONSTRAINT res_u_res_ruimte_opstel_key DROP INDEX;
ALTER TABLE res_ruimte_opstelling ADD CONSTRAINT res_u_res_ruimte_opstel_key UNIQUE(res_ruimte_key, res_opstelling_key, res_ruimte_opstel_verwijder);

/////////////////////////////////////////////////////////////////////////////////////////// FSN#34440
ALTER TABLE cnt_contract ADD cnt_contract_termijntermijn
    NUMBER(10) CONSTRAINT cnt_r_cnt_termijn_key4 REFERENCES cnt_termijn(cnt_termijn_key);

/////////////////////////////////////////////////////////////////////////////////////////// FSN#35197
UPDATE fac_menu
   SET fac_menu_alturl = 'appl/mgt/fac_widget.asp'
 WHERE LOWER(fac_menu_alturl) LIKE '%fac_widget_search.asp%';

/////////////////////////////////////////////////////////////////////////////////////////// FSN#35119
DEF_FAC_FUNCTIE2('WEB_RESAFR', 'Backoffice:Reserv-afronden',     'RES',  0, 0,    1, 'Om goedgekeurde reserveringen te kunnen wijzigen');

/////////////////////////////////////////////////////////////////////////////////////////// AAGN#34728
ALTER TABLE alg_gebouw ADD alg_gebouw_email VARCHAR2(200);
ALTER TABLE fac_srtnotificatie ADD fac_srtnotificatie_gebouw
    NUMBER(1) DEFAULT 0 NOT_NULL(fac_srtnotificatie_gebouw, fac_c_fac_srtnotificatie_geb);

/////////////////////////////////////////////////////////////////////////////////////////// FSN#35352
DELETE FROM fac_result WHERE fac_result_sessionid = 'hMailServer';

/////////////////////////////////////////////////////////////////////////////////////////// FSN#35185
UPDATE cnt_contract
   SET cnt_contract_mantel_key = NULL
 WHERE cnt_contract_key IN (SELECT c.cnt_contract_key
                              FROM cnt_contract c,
                                   cnt_disc_params dp
                             WHERE c.ins_discipline_key = dp.cnt_ins_discipline_key
                               AND dp.cnt_srtcontract_type = 6
                               AND c.cnt_contract_mantel_key IS NOT NULL);

/////////////////////////////////////////////////////////////////////////////////////////// FSN#35360
ALTER TABLE fac_imp_factuur MODIFY ordernr VARCHAR2 (31);

/////////////////////////////////////////////////////////////////////////////////////////// FSN#35474
ALTER TABLE ins_srtcontrole MODIFY ins_srtcontrole_info VARCHAR2(4000);

/////////////////////////////////////////////////////////////////////////////////////////// MARX#35457
ALTER TABLE mld_opdr_materiaal ADD mld_opdr_materiaal_eenheid VARCHAR2(30);
ALTER TABLE mld_opdr_materiaal ADD mld_opdr_materiaal_code VARCHAR2(30);
ALTER TABLE mld_opdr_materiaal ADD mld_opdr_materiaal_info VARCHAR2(4000);
ALTER TABLE mld_opdr_materiaal ADD     fin_btwtabelwaarde_key NUMBER(10)
 CONSTRAINT mld_r_fin_btwtabelwaarde1 REFERENCES fin_btwtabelwaarde(fin_btwtabelwaarde_key);

DROP INDEX mld_i_mld_opdr_materiaal1;
CREATE INDEX mld_i_mld_opdr_materiaal1 ON mld_opdr_materiaal(mld_opdr_key, fac_usrdata_key, mld_opdr_materiaal_omschr);

/////////////////////////////////////////////////////////////////////////////////////////// MARX#35787
ALTER TABLE mld_opdr_materiaal ADD mld_opdr_materiaal_eenheidcode VARCHAR2(10);

/////////////////////////////////////////////////////////////////////////////////////////// FSN#33999
ALTER TABLE ins_srtcontrole ADD ins_srtcontrole_level NUMBER(3)
 DEFAULT(10) NOT_NULL(ins_srtcontrole_level, ins_c_ins_srtcontrole_level);

ALTER TABLE ins_srtcontrole ADD ins_srtcontrole_uren      NUMBER(8,2);
ALTER TABLE ins_srtcontrole ADD ins_srtcontrole_materiaal NUMBER(9,2);
ALTER TABLE ins_srtcontrole ADD ins_srtcontrole_kosten2   NUMBER(11,2);
ALTER TABLE ins_srtcontrole ADD ins_srtcontrole_kosten3   NUMBER(11,2);
ALTER TABLE ins_srtcontrole ADD ins_srtcontrole_percentage NUMBER(3) DEFAULT (100)
 CONSTRAINT ins_c_srtcontrole_percentage CHECK(ins_srtcontrole_percentage BETWEEN 0 AND 100);

ALTER TABLE ins_srtcontroledl_xcp ADD ins_srtcontroledl_xcp_uren      NUMBER(8,2);
ALTER TABLE ins_srtcontroledl_xcp ADD ins_srtcontroledl_xcp_materia   NUMBER(9,2);
ALTER TABLE ins_srtcontroledl_xcp ADD ins_srtcontroledl_xcp_kosten2   NUMBER(11,2);
ALTER TABLE ins_srtcontroledl_xcp ADD ins_srtcontroledl_xcp_kosten3   NUMBER(11,2);
ALTER TABLE ins_srtcontroledl_xcp ADD ins_srtcontroledl_xcp_perc NUMBER(3) DEFAULT (100)
 CONSTRAINT ins_c_srtcontroledl_xcp_perc CHECK(ins_srtcontroledl_xcp_perc BETWEEN 0 AND 100);

CREATE_TABLE(ins_scenario, 0)
(
    ins_scenario_key
        NUMBER(10)
        CONSTRAINT insk_ins_scenario_key PRIMARY KEY,
    ins_scenario_omschrijving
        VARCHAR2(30),
        NOT_NULL(ins_scenario_omschrijving, ins_r_ins_scenario_omschr),
    ins_scenario_status
        NUMBER(3),
    ins_scenario_opmerking
        VARCHAR2(4000),
    prs_perslid_key
        NUMBER(10)
        CONSTRAINT ins_r_prs_perslid_key0 REFERENCES prs_perslid(prs_perslid_key),
    ins_scenario_aanmaak
        DATE
        DEFAULT SYSDATE,
    ins_scenario_verwerkt
        DATE
);

CREATE SEQUENCE ins_s_ins_scenario_key MINVALUE 1;

ALTER TABLE ins_srtcontrole
  ADD ins_scenario_key NUMBER(10) CONSTRAINT ins_r_ins_scenario_key1 REFERENCES ins_scenario(ins_scenario_key) ON DELETE CASCADE;
ALTER TABLE ins_srtcontroledl_xcp
  ADD ins_scenario_key NUMBER(10) CONSTRAINT ins_r_ins_scenario_key2 REFERENCES ins_scenario(ins_scenario_key) ON DELETE CASCADE;

/////////////////////////////////////////////////////////////////////////////////////////// FSN#35371
CREATE INDEX prs_i_prs_afdeling2 ON prs_afdeling(prs_kostenplaats_key);
CREATE INDEX bez_i_bez_afspraak_bez7 ON bez_afspraak(alg_locatie_key);
CREATE INDEX cnt_i_cnt_contract_object2 ON cnt_contract_object(cnt_ins_deel_key);
ALTER TABLE cnt_kenmerkcontract DROP CONSTRAINT cnt_u_cnt_kenmerkcontract DROP INDEX;
ALTER TABLE cnt_kenmerkcontract ADD CONSTRAINT cnt_u_cnt_kenmerkcontract UNIQUE(cnt_contract_key, cnt_kenmerk_key, cnt_kenmerkcontract_verwijder);
ALTER TABLE fac_usrdata DROP CONSTRAINT fac_u_fac_usrdata_upper DROP INDEX;
ALTER TABLE fac_usrdata ADD CONSTRAINT fac_u_fac_usrdata_upper UNIQUE(fac_usrtab_key, fac_usrdata_upper, fac_usrdata_verwijder);
ALTER TABLE fac_usrrap_cols ADD CONSTRAINT fac_u_fac_usrrap_cols1 UNIQUE(fac_usrrap_key, fac_usrrap_cols_column_name);
CREATE INDEX mld_i_mld_melding22 ON mld_melding(mld_alg_locatie_key);

/////////////////////////////////////////////////////////////////////////////////////////// FSN#35498
ALTER TABLE fac_usrrap_cols ADD fac_usrrap_cols_expression VARCHAR2(4000);

/////////////////////////////////////////////////////////////////////////////////////////// XXX#nnnnn
ALTER TABLE mld_melding ADD mld_melding_onderwerp VARCHAR2(60);

/////////////////////////////////////////////////////////////////////////////////////////// ASFC#34371
-- Notificatiesoort tbv ad hoc mail (putorders needs to know xmlnode)
DECLARE
   aantal   NUMBER;
BEGIN
  SELECT COUNT(*)
    INTO aantal
    FROM fac_srtnotificatie
   WHERE fac_srtnotificatie_code = 'INSCMA';
  IF (aantal = 0)
  THEN
     INSERT INTO fac_srtnotificatie (fac_srtnotificatie_code,
                                     fac_srtnotificatie_mode,
                                     fac_srtnotificatie_oms,
                                     fac_srtnotificatie_url,
                                     fac_srtnotificatie_groep,
                                     fac_srtnotificatie_xmlnode,
                                     fac_srtnotificatie_delay
              )
       VALUES ('INSCMA',
               0,
               'Controle ##KEY##',
               '',
               '0',
               'controle',
               10);
  END IF;
END;
/

ALTER TABLE ins_deelsrtcontrole DROP CONSTRAINT ins_c_ins_deelsrtcontrole_org;
ALTER TABLE ins_deelsrtcontrole MODIFY ins_controlemode_key NULL; // Bij eerdere statussen mag die nu leeg zijn.

-- In de ins_deelsrtcontrole tabel vervalt het veld ins_deelsrtcontrole_datum_xcp. Deze waarden kopieren naar het veld ins_deelsrtcontrole_plandatum.
UPDATE ins_deelsrtcontrole
   SET ins_deelsrtcontrole_plandatum = ins_deelsrtcontrole_datum_xcp
 WHERE ins_deelsrtcontrole_datum_xcp IS NOT NULL;

ALTER PACKAGE fac COMPILE; // anders (nog) invalid en lukt het volgende niet niet
ALTER PACKAGE fac COMPILE BODY;

-- Huidige respijtdatum in ins_srtcontroledl_xcp tabel (ins_srtcontroledl_xcp_respijt) overzetten naar de plandatum in de ins_deelsrtcontrole tabel (ins_deelsrtcontrole_plandatum).
DECLARE
   nextdate   ins_deelsrtcontrole.ins_deelsrtcontrole_datum_org%TYPE;
BEGIN
   FOR c IN ((SELECT xcp.ins_deel_key,
                     xcp.ins_srtcontrole_key,
                     xcp.ins_srtcontroledl_xcp_respijt,
                     0,
                     ins.nextcyclusdate (xcp.ins_deel_key, xcp.ins_srtcontrole_key, 1) orgdate
                FROM ins_srtcontroledl_xcp xcp
               WHERE xcp.ins_srtcontroledl_xcp_respijt IS NOT NULL))
   LOOP
     INSERT INTO ins_deelsrtcontrole (ins_deel_key,
                                      ins_srtcontrole_key,
                                      ins_deelsrtcontrole_plandatum,
                                      ins_deelsrtcontrole_status,
                                      ins_deelsrtcontrole_datum_org)
                               VALUES(c.ins_deel_key,
                                      c.ins_srtcontrole_key,
                                      c.ins_srtcontroledl_xcp_respijt,
                                      0,
                                      c.orgdate);
   END LOOP;
END;
/

--Echte DROPs altijd pas in de volgende release
--ALTER TABLE ins_srtcontroledl_xcp DROP COLUMN ins_srtcontroledl_xcp_respijt;
--ALTER TABLE ins_deelsrtcontrole DROP COLUMN ins_deelsrtcontrole_datum_xcp;


/////////////////////////////////////////////////////////////////////////////////////////// AAIT#33506
DROP SEQUENCE mld_s_mld_meldbron_key;
CREATE SEQUENCE mld_s_mld_meldbron_key MINVALUE 50;
INSERT INTO mld_meldbron(mld_meldbron_key, mld_meldbron_omschrijving) VALUES (10, 'Inspectie');


/////////////////////////////////////////////////////////////////////////////////////////// FSN#34157
CREATE_VIEW(mld_v_udr_meldingkenmerk_cnd, 0) AS SELECT '' x FROM DUAL;
CREATE_VIEW(mld_v_udr_opdrachtkenmerk_cnd, 0) AS SELECT '' x FROM DUAL;
-- NB: Zij die de oude al gebruikten moeten nu de juiste kolom gaan gebruiken afh type. Handmatig.
-- Dat hier nog signaleren?
UPDATE fac_usrrap
   SET fac_usrrap_view_name = 'MLD_V_UDR_MELDINGKENMERK_CND'
 WHERE fac_usrrap_view_name = 'MLD_V_UDR_MELDINGKENMERK';

UPDATE fac_usrrap
   SET fac_usrrap_view_name = 'MLD_V_UDR_OPDRACHTKENMERK_CND'
 WHERE fac_usrrap_view_name = 'MLD_V_UDR_OPDRACHTKENMERK';

/////////////////////////////////////////////////////////////////////////////////////////// FSN#35623
// Nieuwste triggers opdat ze niet in de _default groep komen
#include "..\prs\prs_tri.src"

INSERT INTO fac_api (fac_api_name, fac_api_filepath, fac_api_loglevel, fac_api_viewmapping_json) VALUES
   ('GETFLEX', 'appl/api/api_flex.asp', 0, NULL);

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)
   SELECT prs_perslid_module,
          prs_srtperslid_key,
          prs_afdeling_key,
          'API-user tbv. Putorders-koppeling',
          '_PUTORDERS',
          DBMS_RANDOM.string ('a', 32),
          4
     FROM prs_perslid
    WHERE prs_perslid_oslogin = '_FACILITOR';

INSERT INTO fac_setting (fac_setting_module,
                         fac_setting_name,
                         fac_setting_description,
                         fac_setting_pvalue)
    SELECT 'PUO',
           'puo_fclt_web_apikey',
           'Putorders APIKEY to the FACILITOR software',
            prs_perslid_apikey
       FROM prs_perslid
      WHERE prs_perslid_oslogin = '_PUTORDERS';

UPDATE fac_functie
   SET fac_functie_omschrijving = 'Beheer:Applicatiebeheer',
       fac_functie_info = 'Functioneel applicatie beheer'
 WHERE fac_functie_code = 'WEB_PRSSYS';

/////////////////////////////////////////////////////////////////////////////////////////// FSN#35693
INSERT INTO fac_api (fac_api_name, fac_api_filepath, fac_api_loglevel, fac_api_viewmapping_json)
   SELECT 'HMAIL', 'appl/api/api_hmail.asp', 0, NULL FROM DUAL
   WHERE 'HMAIL' NOT IN (SELECT fac_api_name FROM fac_api);

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)
   SELECT prs_perslid_module,
          prs_srtperslid_key,
          prs_afdeling_key,
          'API-user tbv. hMail-koppeling',
          '_HMAIL',
          DBMS_RANDOM.string ('a', 32),
          4
     FROM prs_perslid
    WHERE prs_perslid_oslogin = '_FACILITOR'
          AND NOT EXISTS (SELECT prs_perslid_oslogin
                            FROM prs_perslid
                           WHERE prs_perslid_oslogin = '_HMAIL');

/////////////////////////////////////////////////////////////////////////////////////////// PNBR#34571
ALTER TABLE res_ruimte ADD res_ruimte_volgnummer NUMBER(3);

/////////////////////////////////////////////////////////////////////////////////////////// FSN#35338
ALTER TABLE cad_label
  ADD cad_label_visible NUMBER(1) DEFAULT 1 NOT NULL
  CONSTRAINT cad_c_cad_label_visible CHECK (cad_label_visible IN (0,1));
UPDATE cad_label
   SET cad_label_visible = 0
     , cad_label_size = -1 * cad_label_size
 WHERE cad_label_size < 0;

//CNT
UPDATE fac_menuitems SET fac_menuitems_url = 'appl/mgt/cnt_discipline.asp' WHERE fac_menuitems_label = 'lcl_menu_cnt_contractsoorten';
UPDATE fac_menuitems SET fac_menuitems_url = 'appl/mgt/cnt_termijn.asp' WHERE fac_menuitems_label = 'lcl_menu_cnt_termijnen';
UPDATE fac_menuitems SET fac_menuitems_url = 'appl/mgt/cnt_kenmerk.asp' WHERE fac_menuitems_label = 'lcl_menu_cnt_kenmerken';
UPDATE fac_menuitems SET fac_menuitems_url = 'appl/mgt/cnt_srtkenmerk.asp' WHERE fac_menuitems_label = 'lcl_menu_cnt_kenmerksoorten';
//FIN
UPDATE fac_menuitems SET fac_menuitems_url = 'appl/mgt/prs_kostensoort.asp' WHERE fac_menuitems_label = 'lcl_menu_fin_kostensoorten';
UPDATE fac_menuitems SET fac_menuitems_url = 'appl/mgt/prs_kostensoortgrp.asp' WHERE fac_menuitems_label = 'lcl_menu_fin_kostensoortgroepe';
UPDATE fac_menuitems SET fac_menuitems_url = 'appl/mgt/prs_kostencombinatie.asp' WHERE fac_menuitems_label = 'lcl_menu_fin_combinatievalidat';
UPDATE fac_menuitems SET fac_menuitems_url = 'appl/mgt/fac_profiel.asp' WHERE fac_menuitems_label = 'lcl_menu_fin_profielen';
UPDATE fac_menuitems SET fac_menuitems_url = 'appl/mgt/fac_profielwaarde.asp' WHERE fac_menuitems_label = 'lcl_menu_fin_profiel_limieten';
UPDATE fac_menuitems SET fac_menuitems_url = 'appl/mgt/fin_btwtabel.asp' WHERE fac_menuitems_label = 'lcl_menu_fin_btwtabellen';
UPDATE fac_menuitems SET fac_menuitems_url = 'appl/mgt/fin_kenmerk.asp' WHERE fac_menuitems_label = 'lcl_menu_fin_kenmerken';
DELETE FROM fac_menu WHERE fac_menuitems_key IN (SELECT fac_menuitems_key FROM fac_menuitems WHERE fac_menuitems_label = 'lcl_menu_fin_btwtabelwaarden');
DELETE FROM fac_menuitems WHERE fac_menuitems_label = 'lcl_menu_fin_btwtabelwaarden';
//MRK
UPDATE fac_menuitems SET fac_menuitems_url = 'appl/mgt/mrk_discipline.asp' WHERE fac_menuitems_label = 'lcl_menu_mrk_categorieen';
//FAQ
UPDATE fac_menuitems SET fac_menuitems_url = 'appl/mgt/faq_kenmerk.asp' WHERE fac_menuitems_label = 'lcl_menu_faq_kenmerk';
//Overig
UPDATE fac_menuitems SET fac_menuitems_url = 'appl/mgt/fac_usergraph.asp' WHERE fac_menuitems_label = 'lcl_menu_fac_graphs';
UPDATE fac_menuitems SET fac_menuitems_url = 'appl/mgt/fac_notificatiejob.asp' WHERE fac_menuitems_label = 'lcl_menu_fac_notificatiejobs';
UPDATE fac_menuitems SET fac_menuitems_url = 'appl/mgt/mld_vrije_dagen.asp' WHERE fac_menuitems_label = 'lcl_menu_fac_vrije_dagen';
UPDATE fac_menuitems SET fac_menuitems_url = 'appl/mgt/fac_usrtab.asp' WHERE fac_menuitems_label = 'lcl_menu_fac_eigen_tabellen';
UPDATE fac_menuitems SET fac_menuitems_url = 'appl/mgt/fac_srtnotificatie.asp' WHERE fac_menuitems_label = 'lcl_menu_fac_notificaties';
//CAD
UPDATE fac_menuitems SET fac_menuitems_url = 'appl/mgt/cad_label.asp' WHERE fac_menuitems_label = 'lcl_menu_cad_labels';
UPDATE fac_menuitems SET fac_menuitems_url = 'appl/mgt/cad_thema.asp' WHERE fac_menuitems_label = 'lcl_menu_cad_themas';
UPDATE fac_menuitems SET fac_menuitems_url = 'appl/mgt/cad_legenda.asp' WHERE fac_menuitems_label = 'lcl_menu_cad_legendas';
UPDATE fac_menuitems SET fac_menuitems_url = 'appl/mgt/cad_legendawaarde.asp' WHERE fac_menuitems_label = 'lcl_menu_cad_legendawaarden';
//INS
UPDATE fac_menuitems SET fac_menuitems_url = 'appl/mgt/ins_discipline.asp' WHERE fac_menuitems_label = 'lcl_menu_ins_disciplines';
UPDATE fac_menuitems SET fac_menuitems_url = 'appl/mgt/ins_srtgroep.asp' WHERE fac_menuitems_label = 'lcl_menu_ins_groepen';
//MLD
UPDATE fac_menuitems SET fac_menuitems_url = 'appl/mgt/mld_srtdiscipline.asp' WHERE fac_menuitems_label = 'lcl_menu_mld_vakgroeptypes';
UPDATE fac_menuitems SET fac_menuitems_url = 'appl/mgt/mld_discipline.asp' WHERE fac_menuitems_label = 'lcl_menu_mld_vakgroepen';
UPDATE fac_menuitems SET fac_menuitems_url = 'appl/mgt/mld_stdmelding_groep.asp' WHERE fac_menuitems_label = 'lcl_menu_mld_stdmeldinggroep';
UPDATE fac_menuitems SET fac_menuitems_url = 'appl/mgt/mld_behandelgroep.asp' WHERE fac_menuitems_label = 'lcl_menu_mld_behandelgroep';
UPDATE fac_menuitems SET fac_menuitems_url = 'appl/mgt/mld_dienstniveau.asp' WHERE fac_menuitems_label = 'lcl_menu_mld_dienst_niveaus';
UPDATE fac_menuitems SET fac_menuitems_url = 'appl/mgt/mld_typeopdr.asp' WHERE fac_menuitems_label = 'lcl_menu_mld_opdrachttypes';
UPDATE fac_menuitems SET fac_menuitems_url = 'appl/mgt/mld_impropdr.asp' WHERE fac_menuitems_label = 'lcl_menu_mld_improdopdr';
UPDATE fac_menuitems SET fac_menuitems_url = 'appl/mgt/mld_kosten.asp' WHERE fac_menuitems_label = 'lcl_menu_mld_opdrkosten';
UPDATE fac_menuitems SET fac_menuitems_url = 'appl/mgt/mld_workflow.asp' WHERE fac_menuitems_label = 'lcl_menu_mld_workflows';
UPDATE fac_menuitems SET fac_menuitems_url = 'appl/mgt/mld_kenmerk.asp' WHERE fac_menuitems_label = 'lcl_menu_mld_kenmerken';
UPDATE fac_menuitems SET fac_menuitems_url = 'appl/mgt/mld_srtkenmerk.asp' WHERE fac_menuitems_label = 'lcl_menu_mld_kenmerksoorten';
//AUT
DEF_MENUITEM(2, 'lcl_menu_fac_aut_edit_functie' , '', 'FAC', 'appl/mgt/fac_functie.asp?editmode=1', 0, 0, 'WEB_PRSMSU', 'W', '1', '');
DEF_MENUITEM(2, 'lcl_menu_fac_aut_edit_groep'   , '', 'FAC', 'appl/mgt/fac_groep.asp?editmode=1',   0, 0, 'WEB_FACMSU', 'W', '1', '');
DEF_MENUITEM(2, 'lcl_menu_fac_aut_functie'      , '', 'FAC', 'appl/mgt/fac_functie.asp',            0, 0, 'WEB_FACMSU', 'W', '1', '');
DEF_MENUITEM(2, 'lcl_menu_fac_aut_groep'        , '', 'FAC', 'appl/mgt/fac_groep.asp',              0, 0, 'WEB_FACMSU', 'W', '1', '');
DEF_MENUITEM(2, 'lcl_menu_fac_aut_perslid'      , '', 'FAC', 'appl/mgt/prs_perslid.asp',            0, 0, 'WEB_FACMSU', 'W', '1', '');

MENU_INS_AFTER( 'lcl_menu_fac_aut_edit_functie' , 'lcl_menu_fac_autorisatiefuncti', 2);
MENU_INS_AFTER( 'lcl_menu_fac_aut_edit_groep'   , 'lcl_menu_fac_aut_edit_functie', 2);
MENU_INS_AFTER( 'lcl_menu_fac_aut_functie'      ,   'lcl_menu_fac_aut_edit_groep', 2);
MENU_INS_AFTER( 'lcl_menu_fac_aut_groep'        , 'lcl_menu_fac_aut_functie', 2);
MENU_INS_AFTER( 'lcl_menu_fac_aut_perslid'      , 'lcl_menu_fac_aut_groep', 2);
// Nu kunnen de oude weg
DELETE FROM fac_menuitems
    WHERE fac_menuitems_label IN ('lcl_menu_fac_autorisatiefuncti', 'lcl_menu_fac_autorisaties', 'lcl_menu_fac_autorisatiegroepe');

/////////////////////////////////////////////////////////////////////////////////////////// FSN#35702
DELETE FROM fac_imp_file WHERE fac_import_key IS NULL;
-- TODO alter table fac_imp_file constraint fac_import_key not null o.i.d.

/////////////////////////////////////////////////////////////////////////////////////////// FSN#34822
DEF_FAC_FUNCTIE2('WEB_FACTAB', 'Beheer:Technisch Appl. beheer',  'FAC',  0, 0,    2, 'Technisch applicatie beheer (server instellingen)');
DEF_FAC_FUNCTIE2('WEB_FACFAC', 'Beheer:FACILITOR intern beheer', 'FAC',  0, 0,    2, 'FACILITOR internal use only');
DEF_FAC_FUNCTIE2('WEB_APIDOC', 'Beheer:API documentatie',        'FAC',  0, 0, NULL, 'Kan online API documentatie opvragen');
DEF_FAC_FUNCTIE2('WEB_UDRMAN', 'Beheer:Rapportages',             'FAC',  0, 0,    2, 'Om rapportages te kunnen definiëren.');

ALTER TABLE fac_setting ADD fac_functie_key NUMBER(10)
      CONSTRAINT fac_r_setting_functie REFERENCES fac_functie(fac_functie_key);

ALTER PACKAGE fac COMPILE; // anders (nog) invalid en lukt insert niet
ALTER PACKAGE fac COMPILE BODY;

INSERT INTO fac_groeprechten
          ( fac_groep_key
          , fac_functie_key
          , fac_gebruiker_prs_level_read
          , fac_gebruiker_alg_level_read
          , fac_gebruiker_prs_level_write
          , fac_gebruiker_alg_level_write
          )
   SELECT g.fac_groep_key
        , (select fac_functie_key from fac_functie where fac_functie_code = 'WEB_UDRMAN')
        , r.fac_gebruiker_prs_level_read
        , r.fac_gebruiker_alg_level_read
        , r.fac_gebruiker_prs_level_write
        , r.fac_gebruiker_alg_level_write
     FROM fac_groeprechten r
        , fac_functie f
        , fac_groep g
    WHERE r.fac_functie_key = f.fac_functie_key
      AND r.fac_groep_key = g.fac_groep_key
      AND f.fac_functie_code = 'WEB_PRSSYS';

INSERT INTO fac_groep
            (fac_groep_omschrijving)
     VALUES ('_FACILITOR');

-- Stop gebruiker _FACILITOR in groep _FACILITOR
INSERT INTO fac_gebruikersgroep
            (fac_groep_key, prs_perslid_key)
     SELECT fac_groep_key, prs_perslid_key
       FROM fac_groep, prs_perslid
      WHERE fac_groep_upper = '_FACILITOR'
        AND prs_perslid_oslogin = '_FACILITOR';

-- De _FACILITOR groep krijgt PRS_FACFAC/PRS_FACTAB
INSERT INTO fac_groeprechten
            (fac_groep_key,
             fac_functie_key,
             fac_gebruiker_prs_level_read,
             fac_gebruiker_alg_level_read,
             fac_gebruiker_prs_level_write,
             fac_gebruiker_alg_level_write
            )
   SELECT (SELECT fac_groep_key
             FROM fac_groep
            WHERE fac_groep_upper = '_FACILITOR'), fac_functie_key, -1, -1, -1, -1
     FROM fac_functie
    WHERE fac_functie_code = 'WEB_FACFAC'
      OR  fac_functie_code = 'WEB_FACTAB';

UPDATE fac_menuitems
   SET fac_functie_key =
          (SELECT fac_functie_key
             FROM fac_functie
            WHERE fac_functie_code = 'WEB_UDRMAN')
 WHERE fac_menuitems_label = 'lcl_menu_fac_rapportages';

UPDATE fac_usrrap
   SET fac_functie_key =
          (SELECT fac_functie_key
             FROM fac_functie
            WHERE fac_functie_code = 'WEB_UDRMAN')
 WHERE fac_usrrap_view_name LIKE '%UDR%'
     AND fac_functie_key =
              (SELECT fac_functie_key
                 FROM fac_functie
                WHERE fac_functie_code = 'WEB_PRSSYS');

/*
_XXXX gebruikers 'locked' bitje zetten zodat alleen _FACFAC ze kan verwijderen?
_UDR_ rapportages omzetten naar WEB_UDRMAN
*/

/////////////////////////////////////////////////////////////////////////////////////////// AAIT#35647
DEF_FAC_SRTNOT('PRSLOG', 0, 'Persoon ingelogd',        'prs/prs_perslid.asp?prs_key='   , '0', 'perslid',      10);

/////////////////////////////////////////////////////////////////////////////////////////// FSN#35804
ALTER TABLE fac_groep ADD fac_groep_opmerking
        VARCHAR2(320);

ALTER TABLE fac_functie MODIFY fac_functie_info
        VARCHAR2(320);

/////////////////////////////////////////////////////////////////////////////////////////// AAIT#34601
ALTER TABLE mld_opdr_uren ADD mld_opdr_uren_opmerking VARCHAR2(100);

/////////////////////////////////////////////////////////////////////////////////////////// AKZA#34314
ALTER TABLE prs_contactpersoon ADD prs_contactpersoon_uurloon NUMBER(6,2);

/////////////////////////////////////////////////////////////////////////////////////////// FSN#35816
ALTER TABLE fac_setting MODIFY fac_setting_default VARCHAR2(4000);
ALTER TABLE fac_setting MODIFY fac_setting_pvalue VARCHAR2(4000);

/////////////////////////////////////////////////////////////////////////////////////////// FSN#35799
ALTER TABLE fac_usrrap ADD fac_usrrap_cond
        NUMBER(1)
        DEFAULT 0
        NOT NULL
        CONSTRAINT fac_c_fac_usrrap_cond CHECK (fac_usrrap_cond IN (0,1,2,3));

/////////////////////////////////////////////////////////////////////////////////////////// FSN#35895
INSERT INTO fac_message (fac_message_code, fac_message_text)
    VALUES ('PRS_C_PRS_PERSLID_OSLOGIN', 'Het veld ''Login'' is niet uniek.');

/////////////////////////////////////////////////////////////////////////////////////////// FSN#35755
DEF_MENUKOPV(2, 99000, 'lcl_facmgt_setup'); // Facilitor-use only
DEF_MENUENTRY(2, 99010, 'lcl_menu_fac_exporteren'      , '', 'FAC', 'appl/mgt/fac_export_app.asp', 0, 0, 'WEB_FACFAC');
DEF_MENUENTRY(2, 99020, 'lcl_menu_fac_importeren'      , '', 'FAC', 'appl/mgt/fac_import_app.asp?gen=1', 0, 0, 'WEB_FACFAC');
DEF_MENUENTRY(2, 99030, 'lcl_menu_admtracking_search'  , '', 'FAC', 'appl/fac/fac_admtracking_search.asp', 0, 0, 'WEB_FACTAB');
DEF_MENUENTRY(2, 99040, 'lcl_menu_api_list'            , '', 'FAC', 'appl/fac/fac_api_list.asp', 0, 0, 'WEB_FACFAC');
DEF_MENUENTRY(2, 99050, 'lcl_menu_locale_search'       , '', 'FAC', 'appl/fac/fac_locale_search.asp', 0, 0, 'WEB_LCLSYS');
DEF_MENUENTRY(2, 99060, 'lcl_menu_session'             , '', 'FAC', 'appl/fac/fac_session.asp', 0, 0, 'WEB_FACTAB');
DEF_MENUENTRY(2, 99050, 'lcl_menu_mytracking'          , '', 'FAC', 'appl/fac/fac_mytracking.asp', 0, 0, 'WEB_FACTAB');
DEF_MENUENTRY(2, 99070, 'lcl_menu_fac_verify'          , '', 'FAC', 'appl/fac/fac_verify.asp', 0, 0, 'WEB_FACTAB');
DEF_MENUENTRY(2, 99080, 'lcl_menu_cad_verify'          , '', 'FAC', 'appl/cad/cad_verify.asp', 0, 0, 'WEB_FACTAB');
DEF_MENUENTRY(2, 99090, 'lcl_menu_fac_verify_data'     , '', 'FAC', 'appl/fac/fac_verify_data.asp', 0, 0, 'WEB_FACTAB');

DEF_MENUITEM(2, 'lcl_menu_fac_widgets'          , '', 'FAC', 'appl/fac/fac_widget_search.asp', 0, 0, 'WEB_PRSSYS', 'W', '1', '');
// hebben de meesten al wel MENU_INS_AFTER('lcl_menu_fac_widgets', 'lcl_menu_fac_styling', 2);

/////////////////////////////////////////////////////////////////////////////////////////// KFNS#34161
ALTER TABLE mld_disc_params ADD mld_disc_params_offhours NUMBER(1) DEFAULT 0 NOT NULL;

/////////////////////////////////////////////////////////////////////////////////////////// MARX#35947
CREATE_VIEW(mld_v_udr_opdrachtmateriaal, 0) AS SELECT '' x FROM DUAL;
INSERT INTO fac_usrrap (fac_usrrap_omschrijving, fac_usrrap_view_name,fac_usrrap_info, fac_functie_key, fac_usrrap_functie)
 SELECT 'UDR: Opdrachten-materialen','MLD_V_UDR_OPDRACHTMATERIAAL','Basisrapport voor opdracht+materiaal rapportages', fac_functie_key, 32
   FROM fac_functie WHERE fac_functie_code='WEB_UDRMAN'
    AND NOT EXISTS (SELECT '' FROM fac_usrrap WHERE UPPER(fac_usrrap_view_name)='MLD_V_UDR_OPDRACHTMATERIAAL');

/////////////////////////////////////////////////////////////////////////////////////////// STPH#35077
ALTER TABLE bes_bestelling_item DROP CONSTRAINT bes_c_bes_bestelling_item;
ALTER TABLE bes_bestelling_item ADD
    CONSTRAINT bes_c_bes_bestelling_item
        CHECK ((bes_bestelling_item_aantal>=0 AND bes_bestelling_item_aantalontv<=bes_bestelling_item_aantal)
               OR (bes_bestelling_item_aantal<0 AND bes_bestelling_item_aantalontv>=bes_bestelling_item_aantal));

/////////////////////////////////////////////////////////////////////////////////////////// FSN#36148
ALTER TABLE mld_typeopdr_srtdiscipline
    RENAME COLUMN mld_typeopdr_srtdiscipline_key TO mld_typeopdr_srtdisc_key;

/////////////////////////////////////////////////////////////////////////////////////////// MARX#36208
ALTER TABLE mld_opdr_materiaal ADD mld_opdr_materiaal_groep VARCHAR2(60);
ALTER TABLE mld_opdr_materiaal ADD mld_opdr_materiaal_extra1 VARCHAR2(60);
ALTER TABLE mld_opdr_materiaal ADD mld_opdr_materiaal_extra2 VARCHAR2(60);
ALTER TABLE mld_opdr_materiaal ADD mld_opdr_materiaal_extra3 VARCHAR2(4000);

//////////////////////////////////////////////////////////////////////////////// UWVA#35128 HCAS#36268
DEF_FAC_SRTNOT('BEZUNI', 9, 'Uw bezoeker ##NAAM## is nog niet gearriveerd', 'bez/bez_afspraak.asp?bez_key=', '0', 'bezoeker', 0);

/////////////////////////////////////////////////////////////////////////////////////////// GENERAL

/////////////////////////////////////////////////////////////////////////////////////////// CLEANUP
DROP VIEW MLD_V_UDR_MELDINGKENMERK;
DROP VIEW MLD_V_UDR_OPDRACHTKENMERK;

///////////////////////////////////////////////////////////////////////////////////////////
REGISTERONCE('$Id$')
#include "epilogue.inc"
