--
-- $Id$
--
-- Aanpassingen om DB40 (accept) naar de (definitieve?) DB40a te brengen
-- Merk op dat de recreate er nog bij komt die de DB-version bijwerkt

DEFINE thisfile = 'DB40to40a.sql'
DEFINE dbuser   = '*'

SET ECHO ON
SET DEFINE ON
COLUMN fcltlogfile NEW_VALUE fcltlogfile NOPRINT;
WHENEVER SQLERROR EXIT;
SELECT adm.scriptspoolfile('&dbuser', '&thisfile') AS fcltlogfile FROM DUAL;
WHENEVER SQLERROR CONTINUE;
SPOOL &fcltlogfile
SET DEFINE OFF

------ payload begin ------

-- /////////////////////////////////////////////////////////////////////////////////////////// PCHX#66970
UPDATE fac_menuitems
   SET fac_menuitems_disc = 0
 WHERE fac_menuitems_label = 'lcl_menu_cht_chatkanalen';

-- /////////////////////////////////////////////////////////////////////////////////////////// FCLT#66677
-- Gefaald bij sommige accepts omdat FAC package invalid was. Hier herhalen.
-- Toevoegen ontbrekende gebruiker om HMAIL-API uit te voeren
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,
          'API-user tbv. hMail-koppeling',
          '_HMAIL',
          DBMS_RANDOM.string ('a', 32),
          36,
          'System account for incoming email.'||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 = '_HMAIL');

-- Toevoegen ontbrekende gebruiker om PUTORDERS-API uit te voeren
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,
          'API-user tbv. Putorders-koppeling',
          '_PUTORDERS',
          fac.getsetting('puo_fclt_web_apikey'),
          36,
          'System account for outgoing email.'||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 = '_PUTORDERS');

-- /////////////////////////////////////////////////////////////////////////////////////////// KFHT#67002
ALTER TABLE prs_contactpersoon MODIFY (prs_contactpersoon_email VARCHAR2(200));

-- /////////////////////////////////////////////////////////////////////////////////////////// DLLG#60172
INSERT INTO fac_menuitems (fac_menuitems_groep, fac_menuitems_label, fac_menuitems_oms, fac_menuitems_module, fac_menuitems_url, fac_menuitems_srtdisc, fac_menuitems_disc, fac_functie_key, fac_menuitems_rw, fac_menuitems_default, fac_menuitems_image) SELECT 3, 'lcl_mobile_res_catering', '', 'RES', 'appl/pda/reservering.asp?restype=C', 0, 0, fac_functie_key, 'W', '0', 'fa-coffee' FROM fac_functie WHERE fac_functie_code='WEB_RESUSE';
INSERT INTO fac_menu (fac_menuitems_key, fac_menu_volgnr) SELECT i.fac_menuitems_key, m.fac_menu_volgnr + 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_mobile_res_catering') AND m.fac_menuitems_key = (SELECT MAX(mm.fac_menuitems_key) FROM fac_menu mm, fac_menuitems mmi WHERE mm.fac_menuitems_key = mmi.fac_menuitems_key AND mmi.fac_menuitems_label = 'lcl_mobile_res_voorziening');
INSERT INTO fac_menuitems (fac_menuitems_groep, fac_menuitems_label, fac_menuitems_oms, fac_menuitems_module, fac_menuitems_url, fac_menuitems_srtdisc, fac_menuitems_disc, fac_functie_key, fac_menuitems_rw, fac_menuitems_default, fac_menuitems_image) SELECT 3, 'lcl_mobile_res_looplijst', '', 'RES', 'appl/pda/catering_looplijst.asp', 0, 0, fac_functie_key, 'W', '0', 'fa-person-carry' FROM fac_functie WHERE fac_functie_code='WEB_RESUSE';
INSERT INTO fac_menu (fac_menuitems_key, fac_menu_volgnr) SELECT i.fac_menuitems_key, m.fac_menu_volgnr + 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_mobile_res_looplijst') AND m.fac_menuitems_key = (SELECT MAX(mm.fac_menuitems_key) FROM fac_menu mm, fac_menuitems mmi WHERE mm.fac_menuitems_key = mmi.fac_menuitems_key AND mmi.fac_menuitems_label = 'lcl_mobile_res_catering');

-- /////////////////////////////////////////////////////////////////////////////////////////// FCLT#67053

-- Verwijder alle kenmerken met kenmerktype 'Q' / Blanco, incl. references (ALG, BEZ, FAQ, FIN, PRS)
DELETE FROM alg_onrgoedkenmerk
      WHERE alg_kenmerk_key IN
                (SELECT alg_kenmerk_key
                   FROM alg_kenmerk
                  WHERE alg_kenmerk_kenmerktype = 'Q');

DELETE FROM alg_kenmerk
      WHERE alg_kenmerk_kenmerktype = 'Q';

DELETE FROM bez_kenmerkwaarde
      WHERE bez_kenmerk_key IN
                (SELECT bez_kenmerk_key
                   FROM bez_kenmerk
                  WHERE bez_kenmerk_kenmerktype = 'Q');

DELETE FROM bez_kenmerk
      WHERE bez_kenmerk_kenmerktype = 'Q';

DELETE FROM faq_kenmerk
      WHERE faq_kenmerk_kenmerktype = 'Q';

DELETE FROM fin_kenmerkfactregel
      WHERE fin_kenmerk_key IN
                (SELECT fin_kenmerk_key
                   FROM fin_kenmerk
                  WHERE fin_kenmerk_kenmerktype = 'Q');

DELETE FROM fin_kenmerk
      WHERE fin_kenmerk_kenmerktype = 'Q';

DELETE FROM prs_kenmerk
      WHERE prs_kenmerk_kenmerktype = 'Q';

-- Verwijder alle kenmerken van een kenmerksoort met kenmerktype 'Q' / Blanco, incl. references (BES, CNT, INS, MLD, RES)
DELETE FROM bes_kenmerkbestell
      WHERE bes_kenmerk_key IN
                (SELECT bes_kenmerk_key
                   FROM bes_kenmerk
                  WHERE bes_srtkenmerk_key IN
                            (SELECT bes_srtkenmerk_key
                               FROM bes_srtkenmerk
                              WHERE bes_srtkenmerk_kenmerktype = 'Q'));

DELETE FROM bes_kenmerkbesteli
      WHERE bes_kenmerk_key IN
                (SELECT bes_kenmerk_key
                   FROM bes_kenmerk
                  WHERE bes_srtkenmerk_key IN
                            (SELECT bes_srtkenmerk_key
                               FROM bes_srtkenmerk
                              WHERE bes_srtkenmerk_kenmerktype = 'Q'));

DELETE FROM bes_kenmerk
      WHERE bes_srtkenmerk_key IN
                (SELECT bes_srtkenmerk_key
                   FROM bes_srtkenmerk
                  WHERE bes_srtkenmerk_kenmerktype = 'Q');

DELETE FROM cnt_kenmerkcontract
      WHERE cnt_kenmerk_key IN
                (SELECT cnt_kenmerk_key
                   FROM cnt_kenmerk
                  WHERE cnt_srtkenmerk_key IN
                            (SELECT cnt_srtkenmerk_key
                               FROM cnt_srtkenmerk
                              WHERE cnt_srtkenmerk_kenmerktype = 'Q'));

DELETE FROM cnt_kenmerk
      WHERE cnt_srtkenmerk_key IN
                (SELECT cnt_srtkenmerk_key
                   FROM cnt_srtkenmerk
                  WHERE cnt_srtkenmerk_kenmerktype = 'Q');

DELETE FROM ins_kenmerkdeel
      WHERE ins_kenmerk_key IN
                (SELECT ins_kenmerk_key
                   FROM ins_kenmerk
                  WHERE ins_srtkenmerk_key IN
                            (SELECT ins_srtkenmerk_key
                               FROM ins_srtkenmerk
                              WHERE ins_srtkenmerk_kenmerktype = 'Q'));

DELETE FROM ins_kenmerk
      WHERE ins_srtkenmerk_key IN (SELECT ins_srtkenmerk_key
                                     FROM ins_srtkenmerk
                                    WHERE ins_srtkenmerk_kenmerktype = 'Q');

DELETE FROM mld_kenmerkmelding
      WHERE mld_kenmerk_key IN
                (SELECT mld_kenmerk_key
                   FROM mld_kenmerk
                  WHERE mld_srtkenmerk_key IN
                            (SELECT mld_srtkenmerk_key
                               FROM mld_srtkenmerk
                              WHERE mld_srtkenmerk_kenmerktype = 'Q'));

DELETE FROM mld_kenmerkopdr
      WHERE mld_kenmerk_key IN
                (SELECT mld_kenmerk_key
                   FROM mld_kenmerk
                  WHERE mld_srtkenmerk_key IN
                            (SELECT mld_srtkenmerk_key
                               FROM mld_srtkenmerk
                              WHERE mld_srtkenmerk_kenmerktype = 'Q'));

DELETE FROM mld_kenmerk
      WHERE mld_srtkenmerk_key IN (SELECT mld_srtkenmerk_key
                                     FROM mld_srtkenmerk
                                    WHERE mld_srtkenmerk_kenmerktype = 'Q');

DELETE FROM res_kenmerk
      WHERE res_srtkenmerk_key IN (SELECT res_srtkenmerk_key
                                     FROM res_srtkenmerk
                                    WHERE res_srtkenmerk_kenmerktype = 'Q');

-- Verwijder alle kenmerksoorten met kenmerktype 'Q' / Blanco, incl. references (BES, CNT, INS, MLD, RES)
DELETE FROM bes_srtkenmerk
      WHERE bes_srtkenmerk_kenmerktype = 'Q';

DELETE FROM cnt_srtkenmerk
      WHERE cnt_srtkenmerk_kenmerktype = 'Q';

DELETE FROM ins_srtkenmerk
      WHERE ins_srtkenmerk_kenmerktype = 'Q';

DELETE FROM mld_srtkenmerk
      WHERE mld_srtkenmerk_kenmerktype = 'Q';

DELETE FROM res_srtkenmerk
      WHERE res_srtkenmerk_kenmerktype = 'Q';

-- /////////////////////////////////////////////////////////////////////////////////////////// XXXX#12345
------ payload end   ------

SET DEFINE OFF
BEGIN adm.systrackscriptId ('$Id$', 1); END;
/

COMMIT;
SET ECHO OFF
SPOOL OFF
SET DEFINE ON
PROMPT Logfile of this upgrade is: &fcltlogfile
