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

COMMIT;
SET DEFINE OFF

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#64044
-- Deze even vooraan, anders komen fac_menu-manipulaties zoals MENU_INS_AFTER mogelijk niet door
ALTER TABLE fac_menu
    DROP CONSTRAINT fac_c_menu1 DROP INDEX;

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#63401
ALTER TABLE res_activiteit ADD res_activiteit_limiet NUMBER(3);
ALTER TABLE res_activiteit ADD res_activiteit_limiet_fe NUMBER(3);

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#64834
ALTER TABLE mld_stdmelding ADD
    mld_stdmelding_notes
        NUMBER(1)
        DEFAULT 3
        NOT_NULL(mld_stdmelding_notes, mld_c_mld_stdmelding_notes);

-- Als S(mld_merge_notes_and_tracking) & 2 (= 'laat opdrachten notities zien bij de melding'),
-- stel dan default in dat uitvoerders in de melding notities worden weergegeven (= mld_stdmelding_notes & 4)
DECLARE
    mld_merge_notes   fac_setting.fac_setting_pvalue%TYPE;
BEGIN
    SELECT COALESCE (fac_setting_pvalue, fac_setting_default)
      INTO mld_merge_notes
      FROM fac_setting
     WHERE fac_setting_name = 'mld_merge_notes_and_tracking';

    UPDATE mld_stdmelding
       SET mld_stdmelding_notes =
               (CASE WHEN BITAND (mld_merge_notes, 2) = 2 THEN 7 ELSE 3 END);
END;
/

ALTER TABLE mld_opdr_note ADD
   mld_opdr_note_flag
        NUMBER (2)
        DEFAULT 4;

ALTER TABLE mld_opdr ADD
    mld_opdr_actiecode
        NUMBER(3)
        DEFAULT 1 -- Default BO, zie ook de query hieronder
        NOT NULL;

-- Zet de (initiele) actiecode bij de uitvoerder indien opdracht-status = 'Uitgegeven' (5) of 'Geaccepteerd' (8)

UPDATE mld_opdr
   SET mld_opdr_actiecode = 4
 WHERE mld_statusopdr_key IN (5, 8);

DEF_FAC_SRTNOT('ORDA2U', 0, 'lcl_noti_ORDA2U', 'mld/mld_opdr.asp?opdr_key=', '0', 'opdracht', 10);
DEF_FAC_SRTNOT('ORDNOL', 1, 'lcl_noti_ORDNOL', 'mld/mld_opdr.asp?opdr_key=', '0', 'opdracht', 10);
DEF_FAC_SRTNOT('ORDA2B', 0, 'lcl_noti_ORDA2B', 'mld/mld_opdr.asp?opdr_key=', '0', 'opdracht', 10);
DEF_FAC_SRTNOT('ORDNOB', 1, 'lcl_noti_ORDNOB', 'mld/mld_opdr.asp?opdr_key=', '0', 'opdracht', 10);

ALTER TABLE mld_melding_note ADD
   mld_melding_note_parent_key
        NUMBER (10)
        DEFAULT NULL
        REFERENCES mld_melding_note (mld_melding_note_key)
        ON DELETE CASCADE;

ALTER TABLE mld_opdr_note ADD
   mld_opdr_note_parent_key
        NUMBER (10)
        DEFAULT NULL
        REFERENCES mld_opdr_note (mld_opdr_note_key)
        ON DELETE CASCADE;

ALTER TABLE mld_typeopdr ADD mld_typeopdr_image VARCHAR2(255 CHAR);

-- Kleine bugfix; constraint zou alleen van toepassing moeten zijn op niet vervallen groepen, anders blijft een vervallen groepnaam voor altijd bezet
ALTER TABLE fac_note_group
    DROP CONSTRAINT fac_u_fac_note_group_naam DROP INDEX;

ALTER TABLE fac_note_group
    ADD CONSTRAINT fac_u_fac_note_group_naam UNIQUE
            (fac_note_group_xmlnode,
             fac_note_group_naam,
             fac_note_group_vervaldatum);

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#64017

_FAC_MODULE('WGT', 'lcl_module_WGT')

DEF_FAC_FUNCTIE3('WEB_WGTUSE', 'WGT',  3, 0,    0);
DEF_FAC_FUNCTIE3('WEB_WGTMAN', 'WGT',  3, 0,    2);

DEF_MENUITEM(0, 'lcl_menu_my_wgt_page', '', 'WGT', 'appl/fac/fac_widget_page.asp', 0, 0, 'WEB_WGTUSE', 'W', '1', '');
-- Variatie op MENU_INS_AFTER();
INSERT INTO fac_menu (fac_menuitems_key,
                      fac_menu_altlabel,
                      fac_menu_alturl,
                      fac_menu_altgroep,
                      fac_menu_image,
                      fac_menu_info,
                      fac_menu_volgnr,
                      fac_menu_level)
    SELECT i.fac_menuitems_key,
           i.fac_menuitems_label,
           i.fac_menuitems_url,
           i.fac_menuitems_groep,
           i.fac_menuitems_image,
           i.fac_menuitems_oms,
           m.fac_menu_volgnr + 2,
           2
      FROM fac_menuitems i, fac_menu m
     WHERE     i.fac_menuitems_key =
               (SELECT MAX (mmi.fac_menuitems_key)
                  FROM fac_menuitems mmi
                 WHERE mmi.fac_menuitems_label = 'lcl_menu_my_wgt_page')
           AND (   m.fac_menuitems_key =
                   (SELECT MAX (ii.fac_menuitems_key)
                      FROM fac_menuitems ii
                     WHERE ii.fac_menuitems_label = 'lcl_menu_fe_myinfo')           -- Originele fac_menuitems
                OR (    m.fac_menu_altgroep = 0
                    AND (   (m.fac_menu_alturl LIKE '%appl/fac/fac_user_info.asp%') -- Handmatig ingevoegd met originele url
                         OR UPPER (m.fac_menu_altlabel) = 'MIJN GEGEVENS')))        -- Handmatig ingevoegd met originele label
;

DEF_MENUITEM(2, 'lcl_menu_wgt_pages', '', 'WGT', 'appl/mgt/fac_widget_page.asp', 0, 0, 'WEB_PRSSYS', 'R', '1', '');
-- Variatie op MENU_INS_AFTER();
INSERT INTO fac_menu (fac_menuitems_key,
                      fac_menu_altlabel,
                      fac_menu_alturl,
                      fac_menu_altgroep,
                      fac_menu_image,
                      fac_menu_info,
                      fac_menu_volgnr,
                      fac_menu_level)
    SELECT i.fac_menuitems_key,
           i.fac_menuitems_label,
           i.fac_menuitems_url,
           i.fac_menuitems_groep,
           i.fac_menuitems_image,
           i.fac_menuitems_oms,
           m.fac_menu_volgnr + 2,
           2
      FROM fac_menuitems i, fac_menu m
     WHERE     i.fac_menuitems_key =
               (SELECT MAX (mmi.fac_menuitems_key)
                  FROM fac_menuitems mmi
                 WHERE mmi.fac_menuitems_label = 'lcl_menu_wgt_pages')
           AND (   m.fac_menuitems_key =
                   (SELECT MAX (ii.fac_menuitems_key)
                      FROM fac_menuitems ii
                     WHERE ii.fac_menuitems_label = 'lcl_menu_fac_widgets')     -- Originele fac_menuitems
                OR (    m.fac_menu_altgroep = 2
                    AND (   m.fac_menu_alturl LIKE '%appl/mgt/fac_widget.asp%'  -- Handmatig ingevoegd met originele url
                         OR UPPER (m.fac_menu_altlabel) = 'WIDGETS')))          -- Handmatig ingevoegd met originele label
;

CREATE_TABLE(fac_widget_page, 0)
(
    fac_widget_page_key
        NUMBER(10)
        CONSTRAINT fac_k_fac_widget_page_key PRIMARY KEY,
    fac_widget_page_name
        VARCHAR2(60),
    fac_functie_key                                      -- A la menuitem, indien autorisatie dan tonen
        NUMBER(10)
        CONSTRAINT fac_r_widget_page_functie_key REFERENCES fac_functie(fac_functie_key),
    fac_widget_page_aanmaak
        DATE
        DEFAULT SYSDATE
);
ALTER TABLE fac_widget_page ADD (
  prs_perslid_key
        NUMBER(10)
        CONSTRAINT fac_r_prs_perslid_key11 REFERENCES prs_perslid(prs_perslid_key) ON DELETE CASCADE
);

CREATE SEQUENCE fac_s_fac_widget_page_key MINVALUE 100;

CREATE_TRIGGER(fac_t_fac_widget_page_B_I)
BEFORE INSERT ON fac_widget_page
FOR EACH ROW
BEGIN
  UPDATE_PRIMARY_KEY(fac_widget_page_key, fac_s_fac_widget_page_key);
END;
/

CREATE_TABLE(fac_widget_onpage, 0)                        -- Koppeltabel voor fac_widget en fac_widget_page
(
    fac_widget_onpage_key
        NUMBER(10)
        CONSTRAINT fac_k_fac_widget_onpage_key PRIMARY KEY,
    fac_widget_page_key
        NUMBER(10)
        CONSTRAINT fac_r_fac_widget_page_key REFERENCES fac_widget_page(fac_widget_page_key) ON DELETE CASCADE,
    fac_widget_key
        NUMBER(10)
        CONSTRAINT fac_r_fac_widget_key REFERENCES fac_widget(fac_widget_key) ON DELETE CASCADE,
    fac_widget_onpage_order
        NUMBER(5),
    fac_widget_onpage_height                               -- Optioneel afwijkend van default
        NUMBER(5),
    fac_widget_onpage_width                                -- Optioneel afwijkend van default
        NUMBER(5)
);

CREATE SEQUENCE fac_s_fac_widget_onpage_key MINVALUE 1;

CREATE_TRIGGER(fac_t_fac_widget_onpage_B_I)
BEFORE INSERT ON fac_widget_onpage
FOR EACH ROW
BEGIN
  UPDATE_PRIMARY_KEY(fac_widget_onpage_key, fac_s_fac_widget_onpage_key);
END;
/

-- Moving widget page info from fac_widget to fac_widget_page
-- Insert in new table
-- Multiple-update-compatible
INSERT INTO fac_widget_page (fac_widget_page_name)
    SELECT DISTINCT fac_widget_groep
      FROM fac_widget
     WHERE fac_widget_groep NOT IN (SELECT DISTINCT fac_widget_page_name
                                      FROM fac_widget_page);

-- S(widgetportal) wordt een widget_page_key, ipv 1|0
UPDATE fac_setting
   SET fac_setting_pvalue =
           (SELECT fac_widget_page_key
              FROM fac_widget_page
             WHERE fac_widget_page_name = 'myfacilitor')
 WHERE     EXISTS
               (SELECT fac_widget_page_key
                  FROM fac_widget_page
                 WHERE fac_widget_page_name = 'myfacilitor') -- Dubbelcheck, eigenlijk niet nodig want S(widgetportal) = 1 hier
       AND fac_setting_name = 'widgetportal'
       AND fac_setting_pvalue IS NOT NULL
       AND fac_setting_pvalue <> '0';

ALTER TABLE fac_widget ADD fac_widget_naam VARCHAR2(60);
UPDATE fac_widget SET fac_widget_naam = SUBSTR (fac_widget_groep, 1, 56) || '-' || fac_widget_volgnr;
ALTER TABLE fac_widget MODIFY fac_widget_naam NOT NULL;

ALTER TABLE fac_widget MODIFY fac_widget_height NUMBER(5);
ALTER TABLE fac_widget MODIFY fac_widget_width NUMBER(5);

ALTER TABLE fac_widget ADD fac_widget_type VARCHAR2(3) DEFAULT ('FAC') NOT NULL;
ALTER TABLE fac_widget ADD fac_widget_typerefkey NUMBER(10);
ALTER TABLE fac_widget ADD fac_widget_content VARCHAR2(4000);
ALTER TABLE fac_widget ADD fac_widget_public NUMBER(1) DEFAULT 0 NOT NULL;
ALTER TABLE fac_widget
    ADD CONSTRAINT fac_c_widgettype1 CHECK
            (   (fac_widget_type IN ('FAC', 'URL', 'IMG', 'FLR') AND fac_widget_url IS NOT NULL)
             OR (fac_widget_type IN ('MK', 'MI', 'RAP', 'FAQ') AND fac_widget_typerefkey IS NOT NULL)
             OR (fac_widget_type = 'RAW' AND fac_widget_content IS NOT NULL));

-- Het vullen van de koppeltabel inclusief conversie van fac_widget.fac_widget_volgnr -> fac_widget_onpage.fac_widget_onpage_order
INSERT INTO fac_widget_onpage (fac_widget_page_key,
                               fac_widget_key,
                               fac_widget_onpage_order,
                               fac_widget_onpage_height,
                               fac_widget_onpage_width)
    SELECT page.fac_widget_page_key,
           widget.fac_widget_key,
           widget.onpage_order,
           widget.fac_widget_height,
           widget.fac_widget_width
      FROM (  SELECT fac_widget_page_key, fac_widget_page_name
                FROM fac_widget_page) page,
           (  SELECT fac_widget_key,
                     fac_widget_groep,
                     fac_widget_height,
                     fac_widget_width,
                     ROW_NUMBER ()
                         OVER (PARTITION BY fac_widget_groep
                               ORDER BY fac_widget_volgnr) onpage_order
                FROM fac_widget
            ORDER BY fac_widget_groep, fac_widget_volgnr NULLS LAST) widget
     WHERE widget.fac_widget_groep = page.fac_widget_page_name;

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#63668

-- Hieronder wat default widgets;
DEF_WIDGET('FAC', 'Zoeken in Facilitor', NULL, 'prodsearch2.asp', 1);
DEF_WIDGET('FAC', 'Facilitor nieuws', NULL, 'fac_nieuws.asp', 1);
DEF_WIDGET('FAC', 'Lopende zaken', NULL, 'fac_list.asp', 1);
DEF_WIDGET('FAC', 'Mijn berichten', NULL, 'fac_user_messages.asp', 1);
DEF_WIDGET('URL', 'Facilitor.nl', NULL, 'https://facilitor.nl/', 0);
DEF_WIDGET('IMG', 'Facilitor banner', NULL, '../../appl/Pictures/banner.png', 0);
DEF_WIDGET('RAW', '<Leeg>', '<html><head></head><body></body></html>', NULL, 1);

-- Voorbeeld van rapportage-widget
INSERT INTO fac_widget (fac_widget_type,
                        fac_widget_naam,
                        fac_widget_typerefkey,
                        fac_widget_public)
    SELECT 'RAP',
           'Rapportage: Mislukte inlogpogingen',
           fac_usrrap_key,
           0
      FROM fac_usrrap
     WHERE fac_usrrap_omschrijving = 'BEHEER: Audit fail';

/////////////////////////////////////////////////////////////////////////////////////////// NYBU#62190
ALTER TABLE res_deel ADD res_deel_bulkreserveerbaar NUMBER(1) DEFAULT(NULL)
        CONSTRAINT res_c_res_deel_bulkrsveerbaar CHECK(res_deel_bulkreserveerbaar IN (0,1));

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#63646
ALTER TABLE cad_thema ADD cad_thema_opmerking VARCHAR2(4000);
ALTER TABLE cad_thema ADD cad_thema_aanmaak DATE DEFAULT SYSDATE CONSTRAINT cad_c_cad_thema_aanmaak NOT NULL;
ALTER TABLE cad_label ADD cad_label_opmerking VARCHAR2(4000);
ALTER TABLE cad_label ADD cad_label_aanmaak DATE DEFAULT SYSDATE CONSTRAINT cad_c_cad_label_aanmaak NOT NULL;
ALTER TABLE cad_label ADD cad_label_rotation NUMBER(3);

ALTER TABLE ins_srtdeel ADD ins_srtdeel_acadlabelsize NUMBER(5);
ALTER TABLE ins_srtdeel ADD ins_srtdeel_acadlabelpos NUMBER(1);

UPDATE ins_srtdeel
   SET ins_srtdeel_acadlabelsize = 0
 WHERE ins_srtdeel.ins_srtdeel_key IN
           (SELECT ins_srtdeel_key
              FROM ins_disc_params d, ins_srtgroep g, ins_srtdeel s
             WHERE     g.ins_discipline_key = d.ins_discipline_key
                   AND s.ins_srtgroep_key = g.ins_srtgroep_key
                   AND BITAND (ins_disc_params_type, 16) = 16);

// fac_setting_type en fac_setting_default veranderen wel bij de recreate
UPDATE fac_setting
   SET fac_setting_pvalue = fac_setting_pvalue / 1000000
 WHERE fac_setting_name = 'fg_areathreshold' AND fac_setting_type = 'number';
/////////////////////////////////////////////////////////////////////////////////////////// MARX#68582

ALTER TABLE res_rsv_ruimte DROP CONSTRAINT res_c_alg_ruimte_or_opstel;
ALTER TABLE res_rsv_ruimte
    ADD CONSTRAINT res_c_alg_ruimte_or_opstel
    CHECK (res_ruimte_opstel_key IS NOT NULL OR alg_ruimte_key IS NOT NULL OR mld_opdr_key IS NOT NULL) ENABLE NOVALIDATE;

/////////////////////////////////////////////////////////////////////////////////////////// RWSN#66608
ALTER TABLE fac_imp_usrdata
  ADD ( fac_usrdata_reftabel VARCHAR2(16)
      , fac_usrdata_refcode  VARCHAR2(40)
      );

/////////////////////////////////////////////////////////////////////////////////////////// FMHN#65480
ALTER TABLE fac_faq ADD
    faq_ins_discipline_key
        NUMBER(10);

INSERT INTO ins_tab_discipline (ins_discipline_module, ins_discipline_omschrijving)
     VALUES ('FAQ', 'Standaard');

-- Voor alle bestaande kennisbankitems de discipline op de standaard zetten.
UPDATE fac_faq
   SET faq_ins_discipline_key = (SELECT ins_discipline_key
                                   FROM ins_tab_discipline
                                  WHERE ins_discipline_module LIKE 'FAQ'
                                    AND ins_discipline_omschrijving LIKE 'Standaard');

-- Dan kunnen we nu pas de contraints op de kolom faq_ins_discipline_key zetten.
ALTER TABLE fac_faq MODIFY
    faq_ins_discipline_key
        NOT_NULL(faq_ins_discipline_key, faq_c_ins_discipline_key)
        CONSTRAINT faq_r_ins_discipine_key REFERENCES ins_tab_discipline(ins_discipline_key);

ALTER TABLE faq_kenmerk ADD
    faq_discipline_key
        NUMBER(10)
        CONSTRAINT faq_c_faq_kenmerk_discipline REFERENCES ins_tab_discipline(ins_discipline_key);

ALTER TABLE faq_kenmerk ADD
       CONSTRAINT faq_u_faq_kenmerk UNIQUE(faq_discipline_key, faq_kenmerk_omschrijving, faq_kenmerk_groep, faq_kenmerk_verwijder);

ALTER TABLE fac_imp_faq ADD
    faq_discipline_omschrijving
        VARCHAR2(60);

CREATE_VIEW(faq_discipline, 0)
AS
 SELECT * FROM ins_tab_discipline WHERE ins_discipline_module = 'FAQ';

DEFINIEER_VIEW_AANWEZIG(faq_discipline, ins_discipline_verwijder,
  faq_v_aanwezigdiscipline,0);

DEF_MENUITEM(2, 'lcl_menu_faq_catalogi', '', 'FAQ', 'appl/mgt/faq_discipline.asp', 0, 0, 'WEB_FAQMSU', 'W', '1', '');
MENU_INS_AFTER('lcl_menu_faq_catalogi', 'lcl_menu_faq_kenmerk', -2);

UPDATE fac_functie
   SET fac_functie_discipline = 1
 WHERE fac_functie_code IN ('WEB_FAQUSE', 'WEB_FAQFOF', 'WEB_FAQBOF');

-- De groeprechten tabel heeft voor FAQUSE, FAQFOF, FAQBOF en de verschillende autorisatiegroepen nu maar 1 record
-- om aan te geven welke rechten men heeft voor betreffende autorisatie.
-- Dit moet uitgebreid worden voor elke discipline 1 record.
-- Nu is er bij de records geen ins_discipline_key ingevuld. Een record moet vervangen worden door een record voor elke discipline.
ALTER PACKAGE fac COMPILE; // anders (nog) invalid en lukt het volgende niet niet
ALTER PACKAGE fac COMPILE BODY;
DECLARE
    CURSOR groeprechten IS
        -- Welke groepen hebben de 'WEB_CTRUSE' rechten. Deze rechten moeten in de groep naar de nieuwe rechten 'WEB_CTRSEN' worden gekopieerd.
        SELECT gr.fac_groeprechten_key,
               gr.fac_functie_key,
               gr.fac_gebruiker_prs_level_read,
               gr.fac_gebruiker_prs_level_write,
               gr.fac_gebruiker_alg_level_read,
               gr.fac_gebruiker_alg_level_write,
               gr.fac_groep_key
          FROM fac_groeprechten gr,
               fac_functie f
         WHERE gr.fac_functie_key = f.fac_functie_key
           AND f.fac_functie_code IN ('WEB_FAQUSE', 'WEB_FAQFOF', 'WEB_FAQBOF')
           AND gr.ins_discipline_key IS NULL
        ORDER BY gr.fac_functie_key,
                 gr.fac_groep_key;
    CURSOR faqdisciplines IS
        -- Alle faq disciplines
        SELECT ins_discipline_key
          FROM ins_tab_discipline
         WHERE ins_discipline_module = 'FAQ'
        ORDER BY ins_discipline_key;

BEGIN
    -- Elk record vervangen door een record voor elke discipline
    FOR f_groeprechten IN groeprechten
    LOOP
        FOR f_faqdisciplines IN faqdisciplines
        LOOP
            -- Groepsrechten toevoegen.
            INSERT INTO fac_groeprechten (fac_groep_key,
                                          fac_functie_key,
                                          fac_gebruiker_alg_level_read,
                                          fac_gebruiker_alg_level_write,
                                          fac_gebruiker_prs_level_read,
                                          fac_gebruiker_prs_level_write,
                                          ins_discipline_key)
                                  VALUES (f_groeprechten.fac_groep_key,
                                          f_groeprechten.fac_functie_key,
                                          f_groeprechten.fac_gebruiker_alg_level_read,
                                          f_groeprechten.fac_gebruiker_alg_level_write,
                                          f_groeprechten.fac_gebruiker_prs_level_read,
                                          f_groeprechten.fac_gebruiker_prs_level_write,
                                          f_faqdisciplines.ins_discipline_key);
        END LOOP;
        DELETE FROM fac_groeprechten WHERE fac_groeprechten_key = f_groeprechten.fac_groeprechten_key;
    END LOOP;
END;
/

/////////////////////////////////////////////////////////////////////////////////////////// MNNL#65925
ALTER TABLE mld_disc_params ADD mld_disc_params_mldgroup NUMBER(3) DEFAULT 0 NOT NULL;

/////////////////////////////////////////////////////////////////////////////////////////// BLST#68301
UPDATE fac_menuitems
   SET fac_menuitems_url = 'https://forum.facilitor.nl'
 WHERE fac_menuitems_url = 'http://forum.facilitor.nl';

UPDATE fac_imp_menu
   SET menuitems_url = 'https://forum.facilitor.nl'
 WHERE menuitems_url = 'http://forum.facilitor.nl';

UPDATE fac_message
   SET fac_message_text = ' https://www.facilitor.nl/'
 WHERE UPPER (fac_message_code) = 'FAC_M038';

UPDATE bes_srtdeel
   SET bes_srtdeel_image =
           REGEXP_REPLACE (bes_srtdeel_image, '^http:', 'https:')
 WHERE bes_srtdeel_image LIKE 'http:%' AND bes_srtdeel_verwijder IS NULL;

UPDATE fac_bookmark
   SET fac_bookmark_unauth_url =
           REGEXP_REPLACE (fac_bookmark_unauth_url, '^http:', 'https:')
 WHERE     fac_bookmark_unauth_url LIKE 'http:%'
       AND (fac_bookmark_expire > SYSDATE OR fac_bookmark_expire IS NULL);

UPDATE bes_disc_params
   SET bes_disc_params_logo_url =
           REGEXP_REPLACE (bes_disc_params_logo_url, '^http:', 'https:')
 WHERE bes_disc_params_logo_url LIKE 'http:%';

/////////////////////////////////////////////////////////////////////////////////////////// VLKC#62008
ALTER TABLE bez_afspraak ADD
    mld_opdr_key
        NUMBER(10)
        REFERENCES mld_opdr(mld_opdr_key);

ALTER TABLE mld_typeopdr ADD
    mld_typeopdr_bez
        NUMBER(1)
        DEFAULT(0)
        CONSTRAINT mld_c_mld_typeopdr_bez CHECK(mld_typeopdr_bez IN (0,1));


/////////////////////////////////////////////////////////////////////////////////////////// MARX#69537
ALTER TABLE prs_bedrijf_bedrijf ADD (  prs_bedrijf_bedrijf_startdt2 DATE);
ALTER TABLE prs_bedrijf_bedrijf ADD (  prs_bedrijf_bedrijf_einddt2 DATE);
ALTER TABLE prs_bedrijf_bedrijf ADD (  prs_bedrijf_bedrijf_fee4 NUMBER(6,2));

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#68444
UPDATE prs_bedrijfadres
   SET prs_bedrijfadres_error_handler = 0
 WHERE prs_bedrijfadres_error_handler = 1
   AND (prs_bedrijfadres_ordermode IS NULL OR BITAND (prs_bedrijfadres_ordermode, 1) = 0);

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#69639
ALTER TABLE prs_bedrijfadres ADD  (prs_bedrijfadres_oa_token_url VARCHAR2(512),
                                   prs_bedrijfadres_oa_scope VARCHAR2(512),
                                   prs_bedrijfadres_oa_resource VARCHAR2(512));

/////////////////////////////////////////////////////////////////////////////////////////// YKPN#69517
ALTER TABLE mld_typeopdr DROP CONSTRAINT ins_c_mld_typeopdr_materiaal;
ALTER TABLE mld_typeopdr ADD CONSTRAINT ins_c_mld_typeopdr_materiaal CHECK(mld_typeopdr_materiaal IN (0, 1, 2, 3));

/////////////////////////////////////////////////////////////////////////////////////////// PGHE#69733
INSERT INTO fac_setting (fac_setting_module,
                         fac_setting_name,
                         fac_setting_type,
                         fac_setting_description,
                         fac_setting_pvalue)
     VALUES('MLD',
           'mld_negatief_respijt',
           'number',
           'Sta negatieve respijt toe {0=no | 1=yes}',
            '0');
UPDATE fac_setting
   SET fac_setting_pvalue = (SELECT fac_setting_pvalue FROM fac_setting WHERE fac_setting_name = 'fac_reserved_number2')
 WHERE fac_setting_name = 'mld_negatief_respijt';
UPDATE fac_setting
   SET fac_setting_pvalue = ''
 WHERE fac_setting_name = 'fac_reserved_number2';

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#69573
UPDATE fac_setting
   SET fac_setting_pvalue = (SELECT fac_setting_pvalue FROM fac_setting WHERE fac_setting_name = 'fac_reserved_number1')
 WHERE fac_setting_name = 'fac_html_strictness';
UPDATE fac_setting
   SET fac_setting_pvalue = ''
 WHERE fac_setting_name = 'fac_reserved_number1';

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#64044

UPDATE fac_menu m
   SET fac_menu_altgroep =
           (SELECT fac_menuitems_groep
              FROM fac_menuitems mi
             WHERE mi.fac_menuitems_key = m.fac_menuitems_key)
 WHERE fac_menu_altgroep IS NULL AND fac_menuitems_key IS NOT NULL;

-- Beiden ondersteunen lcl's dus dit kan gewoon
UPDATE fac_menu m
   SET fac_menu_altlabel =
           (SELECT fac_menuitems_label
              FROM fac_menuitems mi
             WHERE mi.fac_menuitems_key = m.fac_menuitems_key)
 WHERE fac_menu_altlabel IS NULL AND fac_menuitems_key IS NOT NULL;

UPDATE fac_menu m
   SET fac_menu_alturl =
           (SELECT fac_menuitems_url
              FROM fac_menuitems mi
             WHERE mi.fac_menuitems_key = m.fac_menuitems_key)
 WHERE fac_menu_alturl IS NULL AND fac_menuitems_key IS NOT NULL;

UPDATE fac_menu m
   SET fac_menu_image =
           (SELECT fac_menuitems_image
              FROM fac_menuitems mi
             WHERE mi.fac_menuitems_key = m.fac_menuitems_key)
 WHERE fac_menu_image IS NULL AND fac_menuitems_key IS NOT NULL;

UPDATE fac_menu m
   SET fac_menu_info =
           (SELECT fac_menuitems_oms
              FROM fac_menuitems mi
             WHERE mi.fac_menuitems_key = m.fac_menuitems_key)
 WHERE fac_menu_info IS NULL AND fac_menuitems_key IS NOT NULL;

-- Correctie van een bugje
 UPDATE fac_menuitems
   SET fac_menuitems_image = 'fa-window-close'
 WHERE     fac_menuitems_label = 'lcl_logoff'
       AND fac_menuitems_image = 'fa-fa-window-close';

-- Desktop, fac_menu_level NULL & 0 => 2
UPDATE fac_menu
   SET fac_menu_level = 2
 WHERE     fac_menu_altgroep IN (0, 1, 2)
       AND (fac_menu_level IS NULL OR fac_menu_level = 0);

-- Mobile, fac_menu_level NULL & 0 => 1
UPDATE fac_menu
   SET fac_menu_level = 1
 WHERE     fac_menu_altgroep IN (3, 7)
       AND (fac_menu_level IS NULL OR fac_menu_level = 0);

-- Overig, fac_menu_level NULL & 0 => 2
UPDATE fac_menu
   SET fac_menu_level = 2
 WHERE     fac_menu_altgroep IN (5, 6)
       AND (fac_menu_level IS NULL OR fac_menu_level = 0);

ALTER TABLE fac_menu
    MODIFY fac_menu_level NUMBER(1) DEFAULT 1 NOT NULL;

DEF_MENUITEM(2, 'lcl_menu_fac_menustructuur_m'  , '', 'FAC', 'appl/fac/fac_menu_search.asp?urole=bo&mobile=1', 0, 0, 'WEB_PRSSYS', 'W', '0', '');

DECLARE
    mobile_enabled   fac_setting.fac_setting_pvalue%TYPE;
BEGIN
    SELECT COALESCE (fac_setting_pvalue, fac_setting_default)
      INTO mobile_enabled
      FROM fac_setting
     WHERE fac_setting_name = 'mobile_enabled';

    IF mobile_enabled = 1
    THEN
        MENU_INS_AFTER('lcl_menu_fac_menustructuur_m', 'lcl_menu_fac_menustructuur', 2);
    END IF;
END;
/

-- fac_menu_portal doet mobile (nog) niets, zet het op NULL waar dit nog niet zo is
UPDATE fac_menu
   SET fac_menu_portal = NULL
 WHERE fac_menu_altgroep = 3;

-- fac_menu_portal betekent (vanaf nu) 'App only', converteer groep = 7 (oude 'App only') naar groep = 3 & portal = 1 (nieuwe 'App only')
UPDATE fac_menu
   SET fac_menu_portal = 1, fac_menu_altgroep = 3
 WHERE fac_menu_altgroep = 7;

-- En ook het fac_menuitems record nog even
UPDATE fac_menuitems
   SET fac_menuitems_groep = 3
 WHERE fac_menuitems_groep = 7;

/////////////////////////////////////////////////////////////////////////////////////////// MARX#69975
ALTER TABLE prs_perslid_inzetbaar ADD (
    alg_locatie_key
        NUMBER(10)
        CONSTRAINT prs_r_alg_locatie_key5 REFERENCES alg_locatie(alg_locatie_key),
    prs_perslid_inzetbaar_plaats
        VARCHAR2 (200),
    prs_perslid_inzetbaar_radius
        NUMBER (6)
    );

ALTER TABLE prs_perslid_inzetbaar ADD (
    prs_perslid_inzetbaar_plaats_x
        NUMBER(16,6),
    prs_perslid_inzetbaar_plaats_y
        NUMBER(16,6)
    );

/////////////////////////////////////////////////////////////////////////////////////////// SGGR#67109
DEF_FAC_FUNCTIE3('WEB_INSMIN', 'INS',  0, 1,    0);

/////////////////////////////////////////////////////////////////////////////////////////// HSLE#66651

CREATE_TABLE(mld_discipline_discipline, 0)                        -- Kruistabel voor ins_tab_discipline
(
    mld_discipline_discipline_key
        NUMBER (10) PRIMARY KEY NOT NULL,
    mld_discipline_key1
        NUMBER(10)
        CONSTRAINT mld_k_mld_disc_disc_key1
        REFERENCES ins_tab_discipline(ins_discipline_key) NOT NULL,
    mld_discipline_key2
        NUMBER(10)
        CONSTRAINT mld_r_mld_disc_disc_key2
        REFERENCES ins_tab_discipline(ins_discipline_key) NOT NULL
);
CREATE SEQUENCE mld_s_mld_disc_disc_key MINVALUE 1;
CREATE UNIQUE INDEX mld_i_mld_disc_disc1 ON mld_discipline_discipline(mld_discipline_key1, mld_discipline_key2);

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#70097
UPDATE fac_srtnotificatie
   SET fac_srtnotificatie_mode = BITAND(fac_srtnotificatie_mode ,63)
 WHERE fac_srtnotificatie_code = 'PRSREG';

UPDATE fac_notificatie
   SET fac_notificatie_status = BITAND(fac_notificatie_status, 63)
 WHERE fac_srtnotificatie_key = (SELECT fac_srtnotificatie_key FROM fac_srtnotificatie WHERE fac_srtnotificatie_code = 'PRSREG');

/////////////////////////////////////////////////////////////////////////////////////////// BLST#69062
ALTER TABLE mld_stdmeldinggroep ADD (
    mld_stdmeldinggroep_image
        VARCHAR2(255),
    mld_stdmeldinggroep_color
        VARCHAR2 (60)
);

DEF_MENUITEM(3, 'lcl_mobile_mld_menu', '', 'MLD', 'appl/pda/melding_menu.asp', 0, 0, 'WEB_MLDUSE', 'W', '0', 'fa-play');

/////////////////////////////////////////////////////////////////////////////////////////// FCLT#69738
CREATE INDEX mld_i_mld_melding24 ON mld_melding(mld_ins_discipline_key,mld_melding_behandelaar2_key);

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

REGISTERONCE('$Id$')

#include "epilogue.inc"
