--
-- $Id$
--
-- Conversie script Connexxion om van licentie naar SaaS te gaan.

DEFINE thisfile = 'CONN#57009.SQL'
DEFINE dbuser   = '^CONN'

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 ------


-- De gebruiker _Facilitor was verwijderd. Deze gaan we reanimeren.
update prs_perslid set prs_perslid_oslogin = '_FACILITOR', prs_perslid_verwijder = NULL
where prs_perslid_key = 104606;

-- En Facilitor koppelen aan de Facilitor groep
insert into fac_gebruikersgroep (fac_groep_key, prs_perslid_key) -- _Facilitor
select fac_groep_key, 104606 from fac_groep WHERE fac_groep_omschrijving = '_FACILITOR';

-- En Facilitor koppelen aan de Beheer groep
insert into fac_gebruikersgroep (fac_groep_key, prs_perslid_key) -- Beheer
values (1, 104606);

-- Een aantal menu-items bestaan nog niet in de connexxion omgeving
-- te weten: Instellingen, Stylesheet editor, Styling en Widgets.
INSERT INTO fac_menu (fac_menu_volgnr, fac_menuitems_key)
    SELECT 18800, fac_menuitems_key
      FROM fac_menuitems
     WHERE fac_menuitems_label = 'lcl_menu_fac_setting';

INSERT INTO fac_menu (fac_menu_volgnr, fac_menuitems_key)
    SELECT 18820, fac_menuitems_key
      FROM fac_menuitems
     WHERE fac_menuitems_label = 'lcl_menu_fac_xsledit';

INSERT INTO fac_menu (fac_menu_volgnr, fac_menuitems_key)
    SELECT 18840, fac_menuitems_key
      FROM fac_menuitems
     WHERE fac_menuitems_label = 'lcl_menu_fac_styling';

INSERT INTO fac_menu (fac_menu_volgnr, fac_menuitems_key)
    SELECT 18860, fac_menuitems_key
      FROM fac_menuitems
     WHERE fac_menuitems_label = 'lcl_menu_fac_widgets';

-- Verwijdere package, functies, procedures en view die niet meer gebruikt worden.
DROP PACKAGE perman;
DROP FUNCTION conn_get_contract_key;
DROP PROCEDURE conn_export_huurmutatie;
DROP PROCEDURE conn_import_budget;
DROP PROCEDURE conn_import_contractmutatie;
DROP PROCEDURE conn_import_kosten;
DROP PROCEDURE conn_import_srtdeel_swap;
DROP PROCEDURE conn_import_xelion_contract;
DROP PROCEDURE conn_update_budget;
DROP PROCEDURE conn_update_contractmutatie;
DROP PROCEDURE conn_update_kosten;
DROP PROCEDURE conn_update_srtdeel_swap;
DROP PROCEDURE conn_update_xelion_contract;
DROP PROCEDURE conn_verwerk_huurmutatie;
DROP TRIGGER conn_etn_ari;
DROP TRIGGER conn_etn_aru;
DROP TRIGGER oac_ced_ariu;
DROP TRIGGER conn_prs_kenmerklink_d2;
DROP TRIGGER conn_prs_kenmerklink_d;
DROP TRIGGER conn_prs_kenmerklink_u_i2;
DROP TRIGGER conn_prs_kenmerklink_u_i;
DROP TRIGGER conn_prs_perslid_d;
DROP TRIGGER conn_prs_perslid_u_i2;
DROP TRIGGER conn_prs_perslid_u_i;
DROP VIEW conn_v_bad_perman_import;
DROP VIEW conn_v_export_contractmutatie;
DROP VIEW conn_v_export_finorders_coda;
DROP VIEW conn_v_export_finorders_nav_fb;
DROP VIEW conn_v_export_finorders_nav_vg;
DROP VIEW conn_v_export_opdracht_;
DROP VIEW conn_v_huurcontracten;
DROP VIEW conn_v_huurcontract_details;
DROP VIEW conn_v_lev_diversen;
DROP VIEW conn_v_lev_geldwaardetransport;
DROP VIEW conn_v_lev_kantoormeubilair;
DROP VIEW conn_v_lev_kluizen;
DROP VIEW conn_v_lev_mobieletelefonie;
DROP VIEW conn_v_lev_sloten;
DROP VIEW conn_v_lev_verkoopapparatuur;
DROP VIEW conn_v_locatiehuurder;
DROP VIEW conn_v_locatie_huurder;
DROP VIEW conn_v_loctype_huur;
DROP VIEW conn_v_loctype_huur_detail;
DROP VIEW conn_v_opplochuur;
DROP VIEW conn_v_rap_locatie_verpl;
DROP VIEW conn_v_ruimtegeg;
DROP VIEW conn_v_rap_locatie_kosten;
DROP VIEW conn_v_rap_srtdeel_swap_expimp;
DROP PACKAGE conn_partners;
DROP PROCEDURE conn_export_contractmutatie;
DROP PROCEDURE conn_export_finorders_coda;
DROP PROCEDURE conn_export_finorders_nav_fb;
DROP PROCEDURE conn_export_finorders_nav_vg;
DROP PROCEDURE conn_select_finorders_coda;
DROP PROCEDURE conn_select_finorders_nav_fb;
DROP PROCEDURE conn_select_finorders_nav_vg;
DROP TABLE conn_contactpersonen_error;
DROP TABLE conn_exp_contractmutatie;
DROP TABLE conn_exp_debiteur;
DROP TABLE conn_exp_debiteur_tmp;
DROP TABLE conn_exp_finorders_coda;
DROP TABLE conn_exp_finorders_nav_fb;
DROP TABLE conn_exp_finorders_nav_vg;
DROP TABLE conn_exp_telefoon;
DROP TABLE conn_imp_budget;
DROP TABLE conn_imp_contractmutatie;
DROP TABLE conn_imp_kosten;
DROP TABLE conn_imp_srtdeel_swap;
DROP TABLE conn_imp_xelion_contract;
DROP TABLE conn_partners_error;
DROP TABLE conn_tmp_import_afd;

-- Voorzie het menu van mooie icons.
UPDATE fac_menu SET fac_menu_image = 'fa-bell-o' WHERE fac_menu_altlabel ='lcl_menu_fe_mld';
UPDATE fac_menu SET fac_menu_image = 'fa-calendar' WHERE fac_menu_altlabel ='lcl_menu_fe_res';
UPDATE fac_menu SET fac_menu_image = 'fa-user-plus' WHERE fac_menu_altlabel ='lcl_menu_fe_bez';
UPDATE fac_menu SET fac_menu_image = 'fa-cart-plus' WHERE fac_menu_altlabel ='lcl_menu_fe_bes';
UPDATE fac_menu SET fac_menu_image = 'fa-random' WHERE fac_menu_altlabel ='lcl_menu_fe_other';
UPDATE fac_menu SET fac_menu_image = 'fa-user-o' WHERE fac_menu_altlabel ='lcl_fo';
UPDATE fac_menu SET fac_menu_image = 'fa-list-ul' WHERE fac_menu_altlabel ='lcl_bo';
UPDATE fac_menu SET fac_menu_image = 'fa-bar-chart' WHERE fac_menu_altlabel ='lcl_maninfo';
UPDATE fac_menu SET fac_menu_image = 'fa-pie-chart' WHERE fac_menu_altlabel ='lcl_menu_fac_qlikview';
UPDATE fac_menu SET fac_menu_image = 'fa-money' WHERE fac_menu_altlabel ='lcl_bgt';
UPDATE fac_menu SET fac_menu_image = 'fa-eur' WHERE fac_menu_altlabel ='lcl_fin';
UPDATE fac_menu SET fac_menu_image = 'fa-wrench' WHERE fac_menu_altlabel ='lcl_beheer';
UPDATE fac_menu SET fac_menu_image = 'fa-map-o' WHERE fac_menu_altlabel ='lcl_fg';
UPDATE fac_menu SET fac_menu_image = 'fa-building-o' WHERE fac_menu_altlabel ='lcl_alg';
UPDATE fac_menu SET fac_menu_image = 'fa-users' WHERE fac_menu_altlabel ='lcl_prs';
UPDATE fac_menu SET fac_menu_image = 'fa-key' WHERE fac_menu_altlabel ='lcl_facmgt_aut';
UPDATE fac_menu SET fac_menu_image = 'fa-life-ring' WHERE fac_menu_altlabel ='lcl_facmgt_fac';
UPDATE fac_menu SET fac_menu_image = 'fa-building-o' WHERE fac_menu_altlabel ='lcl_facmgt_alg';
UPDATE fac_menu SET fac_menu_image = 'fa-users' WHERE fac_menu_altlabel ='lcl_facmgt_prs';
UPDATE fac_menu SET fac_menu_image = 'fa-fire-extinguisher' WHERE fac_menu_altlabel ='lcl_facmgt_ins';
UPDATE fac_menu SET fac_menu_image = 'fa-desktop' WHERE fac_menu_altlabel ='lcl_facmgt_mld';
UPDATE fac_menu SET fac_menu_image = 'fa-calendar' WHERE fac_menu_altlabel ='lcl_facmgt_res';
UPDATE fac_menu SET fac_menu_image = 'fa-cart-plus' WHERE fac_menu_altlabel ='lcl_facmgt_bes';
UPDATE fac_menu SET fac_menu_image = 'fa-file-text-o' WHERE fac_menu_altlabel ='lcl_facmgt_cnt';
UPDATE fac_menu SET fac_menu_image = 'fa-eur' WHERE fac_menu_altlabel ='lcl_facmgt_fin';
UPDATE fac_menu SET fac_menu_image = 'fa-map-o' WHERE fac_menu_altlabel ='lcl_facmgt_fg';
UPDATE fac_menu SET fac_menu_image = 'fa-clipboard' WHERE fac_menu_altlabel ='lcl_facmgt_mrk';
UPDATE fac_menu SET fac_menu_image = 'fa-user-plus' WHERE fac_menu_altlabel ='lcl_facmgt_bez';
UPDATE fac_menu SET fac_menu_image = 'fa-info-circle' WHERE fac_menu_altlabel ='lcl_facmgt_faq';
UPDATE fac_menu SET fac_menu_image = 'fa-cogs' WHERE fac_menu_altlabel ='lcl_facmgt_overig';
UPDATE fac_menu SET fac_menu_image = 'fa-sliders' WHERE fac_menu_altlabel ='lcl_facmgt_setup';

-- Mobile hebben we bij nader inzien toch niet aangezet
--update fac_setting set fac_setting_pvalue = '1' WHERE fac_setting_name = 'mobile_enabled';
-- standaard sortering voor backoffice lijsten is descending
update fac_setting set fac_setting_pvalue = '1' WHERE fac_setting_name = 'mld_bo_sort_descending';
-- Zet het flexpad. LETOP deze moet ook via de userinterface gezet worden.
update fac_setting set fac_setting_pvalue = '*_DATA' WHERE fac_setting_name = 'flexfilespath';
-- Zet os_logon uit
update fac_setting set fac_setting_pvalue = 'false' WHERE fac_setting_name = 'os_logon';
-- Zet de mogelijkheid uit om http te gebruiken.
update fac_setting set fac_setting_pvalue = '3' WHERE fac_setting_name = 'auto_https';

-- Login aanpassen zodat single sign on gaat werken. De UPN is de oude login met domein naam
UPDATE prs_perslid
   SET prs_perslid_oslogin2 = prs_perslid_oslogin,
       prs_perslid_oslogin = prs_perslid_oslogin || '@transdev.nl'
 WHERE prs_perslid_oslogin IS NOT NULL
   AND prs_perslid_oslogin NOT LIKE '%@%'
   AND prs_perslid_oslogin <> '_FACILITOR';

-- Aanpassen styling
UPDATE FAC_SETTING
   SET fac_setting_pvalue =
           '{"headerimage":"../cust/conn/banner.png","buttonbackgroundcolor":"#e20070","buttonhoverbackgroundcolor":"#006772","frameheaderbackgroundcolor":"#d1ea32","frameheadercolor":"#000","iconcolor":"#000","iconhoverbackgroundcolor":"#006772","icontextcolor":"#000","labelbackgroundcolor":"#d1ea32","labelcolor":"#000","mgtmenucolor":"#006772","mywelcomebackgroundcolor":"#d1ea32","mywelcomecolor":"#000","persmenucolor":"#006772","profmenucolor":"#006772","tableheaderbackgroundcolor":"#006772"}'
 WHERE fac_setting_name = 'csstemplate';


-- Reanimeren van de Basware user
UPDATE prs_perslid
   SET prs_perslid_verwijder = NULL,
       prs_perslid_apikey = 'FE0BCD3B3E154F199988D78109CC7D0C',
       prs_perslid_oslogin = '_BASWARE'
 WHERE prs_perslid_key = 200635;

INSERT INTO fac_gebruikersgroep (prs_perslid_key, fac_groep_key) VALUES (200635, 122);

-- verwijder facturen en contracten.
delete fin_factuurregel;

delete fin_factuur;


update mld_opdr set cnt_contract_key = NULL;

UPDATE alg_locatie l
   SET alg_locatie_vervaldatum =
           (SELECT fac.safe_to_date (alg_onrgoedkenmerk_waarde, 'dd-mm-yyyy')
              FROM alg_onrgoedkenmerk
             WHERE     alg_kenmerk_key = 22
                   AND alg_onrgoed_key = l.alg_locatie_key),
       alg_locatie_omschrijving =
           SUBSTR (
               REPLACE (alg_locatie_omschrijving,
                        'UITBEHEER',
                        alg_locatie_adres),
               1,
               60)
 WHERE EXISTS
           (SELECT alg_onrgoed_key
              FROM alg_onrgoedkenmerk
             WHERE     alg_kenmerk_key = 22
                   AND alg_onrgoed_key = l.alg_locatie_key);

DELETE alg_onrgoedkenmerk
 WHERE alg_kenmerk_key = 22;

DELETE alg_kenmerk
 WHERE alg_kenmerk_key = 22;

-- Configureer de SAML interface:
INSERT INTO AUT_IDP (AUT_IDP_CODE,
                     AUT_IDP_TYPE,
                     AUT_IDP_OMSCHRIJVING,
                     AUT_IDP_SECRET,
                     AUT_IDP_ISSUER,
                     AUT_IDP_CLOCKSKEW,
                     AUT_IDP_AUTOCREATE,
                     AUT_IDP_INTERNAL,
                     AUT_IDP_IPAUTO)
     VALUES ('2',
             5,
             'SAML',
             'LrgFXJPZfJXkxc1qEfWreH3w3Y4C082t',
             'http://login.transdev.net/adfs/services/trust',
             30,
             0,
             0,
             0);

INSERT INTO aut_idp_map (aut_idp_key,
                                   aut_idp_map_from,
                                   aut_idp_map_to,
                                   aut_idp_map_identify,
                                   aut_idp_map_autocreate)
    SELECT aut_idp_key,
           'HTTP_FCLTUPN',
           1,
           1,
           0
      FROM aut_idp
     WHERE aut_idp_code = '2';

-- Aanpassen bedrijfadres. Waar eerder de bestanden op de interne FTP server van Connexxion kwamen worden
-- deze nu in een map geplaatst waarbij de via sftp naar Connexxion verzonden worden.
UPDATE prs_bedrijfadres
   SET prs_bedrijfadres_url =
           'file:d:\Apps\Facilitor\Fplace5i\CUST\CONN\export\bestanden\'
 WHERE LOWER (prs_bedrijfadres_url) =
       '\\hvs-lap-fs01\shared\_applicaties\facilitor\basware_import_orders';


-- Deze worden nog niet verwijderd omdat we dan de relaties bij de inspecties missen

-- delete cnt_contract_plaats;
-- delete cnt_contract_object;
-- delete cnt_contract;


-- Opdrachten en meldingen ouder dan 1 jan 2019 worden afgemeld
DECLARE
    CURSOR c IS
        SELECT mld_opdr_key, mld_statusopdr_key, mld_melding_key, mld_opdr_datumbegin
          FROM mld_opdr o
         WHERE     mld_opdr_datumbegin < TO_DATE ('20190101', 'yyyymmdd');

         --      AND o.mld_opdr_key = 59958;

    v_count NUMBER;
    v_date DATE;

BEGIN
    v_date := SYSDATE;
    FOR rec IN c
    LOOP
        CASE rec.mld_statusopdr_key
            WHEN 1
            THEN
                --1 Afgewezen
                NULL;
            --2 Niet akkoord
            WHEN 2
            THEN
                NULL;
            --3 Ter fiattering
            WHEN 3
            THEN
                mld.setopdrachtstatus (rec.mld_opdr_key, 2, 104606); -- Facilitor
            --4 Gefiatteerd
            WHEN 4
            THEN
                mld.setopdrachtstatus (rec.mld_opdr_key, 5, 104606); -- Facilitor
                mld.setopdrachtstatus (rec.mld_opdr_key, 6, 104606); -- Facilitor
                IF rec.mld_opdr_datumbegin < FAC.safe_to_date('01-01-2017', 'dd-mm-yyyy')
                THEN
                   mld.setopdrachtstatus (rec.mld_opdr_key, 7, 104606); -- Facilitor
                END IF;
            --5 Toegekend
            WHEN 5
            THEN
                mld.setopdrachtstatus (rec.mld_opdr_key, 6, 104606); -- Facilitor
                IF rec.mld_opdr_datumbegin < FAC.safe_to_date('01-01-2017', 'dd-mm-yyyy')
                THEN
                   mld.setopdrachtstatus (rec.mld_opdr_key, 7, 104606); -- Facilitor
                END IF;
            --6 Afgemeld
            WHEN 6
            THEN
                IF rec.mld_opdr_datumbegin < FAC.safe_to_date('01-01-2017', 'dd-mm-yyyy')
                THEN
                   mld.setopdrachtstatus (rec.mld_opdr_key, 7, 104606); -- Facilitor
                END IF;
            --7 Verwerkt
            WHEN 7
            THEN
                NULL;
            --8 Geaccepteerd
            WHEN 8
            THEN
                mld.setopdrachtstatus (rec.mld_opdr_key, 6, 104606); -- Facilitor
                IF rec.mld_opdr_datumbegin < FAC.safe_to_date('01-01-2017', 'dd-mm-yyyy')
                THEN
                   mld.setopdrachtstatus (rec.mld_opdr_key, 7, 104606); -- Facilitor
                END IF;
            --9 Afgerond
            WHEN 9
            THEN
                IF rec.mld_opdr_datumbegin < FAC.safe_to_date('01-01-2017', 'dd-mm-yyyy')
                THEN
                   mld.setopdrachtstatus (rec.mld_opdr_key, 7, 104606); -- Facilitor
                END IF;
            WHEN 10
            THEN
                --10 Ter goedkeuring
                mld.setopdrachtstatus (rec.mld_opdr_key, 2, 104606); -- Facilitor
        END CASE;
        SELECT count(*)
          INTO v_count
          FROM fac_v_orderqueue
         WHERE xmlnode = 'opdracht'
           AND key = rec.mld_opdr_key;

        IF v_count = 1
        THEN
           fac.markorderassent ('opdracht', rec.mld_opdr_key, 0, 'Afgemeld door script');
        END IF;
        MLD.updatemeldingstatus (rec.mld_melding_key, 1, 104606);
        DELETE fac_notificatie WHERE fac_notificatie_datum > v_date;
    END LOOP;

    COMMIT;
END;
/

/* Formatted on 7/5/2019 12:57:02 (QP5 v5.336) */
DECLARE
    CURSOR c IS
        SELECT mld_melding_key, mld_melding_status, mld_melding_datum
          FROM mld_melding m
         WHERE mld_melding_datum < TO_DATE ('20190101', 'yyyymmdd');

    --      AND o.mld_opdr_key = 59958;

    v_count   NUMBER;
    v_date    DATE;
BEGIN
    v_date := SYSDATE;

    FOR rec IN c
    LOOP
        CASE rec.mld_melding_status
            --1 Bij FrontOffice
            WHEN 0
            THEN
                mld.setmeldingstatus (rec.mld_melding_key,
                                      5,
                                      104606,
                                      0);
                mld.setmeldingstatus (rec.mld_melding_key,
                                      6,
                                      104606,
                                      0);
            --1 Afgewezen
            WHEN 1
            THEN
                NULL;
            --2 Nieuw
            WHEN 2
            THEN
                mld.setmeldingstatus (rec.mld_melding_key,
                                      5,
                                      104606,
                                      0);

                IF rec.mld_melding_datum < FAC.safe_to_date('01-01-2017', 'dd-mm-yyyy')
                THEN
                    mld.setmeldingstatus (rec.mld_melding_key,
                                          6,
                                          104606,
                                          0);
                END IF;

            --3 Te accepteren
            WHEN 3
            THEN
                mld.setmeldingstatus (rec.mld_melding_key,
                                      5,
                                      104606,
                                      0);
                IF rec.mld_melding_datum < FAC.safe_to_date('01-01-2017', 'dd-mm-yyyy')
                THEN
                    mld.setmeldingstatus (rec.mld_melding_key,
                                          6,
                                          104606,
                                          0);
                END IF;
            --4 In behandeling
            WHEN 4
            THEN
                mld.setmeldingstatus (rec.mld_melding_key,
                                      5,
                                      104606,
                                      0);
                IF rec.mld_melding_datum < FAC.safe_to_date('01-01-2017', 'dd-mm-yyyy')
                THEN
                    mld.setmeldingstatus (rec.mld_melding_key,
                                          6,
                                          104606,
                                          0);
                END IF;
            -- 5 Afgemeld
            WHEN 5
            THEN
                IF rec.mld_melding_datum < FAC.safe_to_date('01-01-2017', 'dd-mm-yyyy')
                THEN
                    mld.setmeldingstatus (rec.mld_melding_key,
                                          6,
                                          104606,
                                          0);
                END IF;
            -- 6 Historie
            WHEN 6
            THEN
                NULL;
            -- 7 Uitgegeven
            WHEN 7
            THEN
                mld.setmeldingstatus (rec.mld_melding_key,
                                      5,
                                      104606,
                                      0);
                IF rec.mld_melding_datum < FAC.safe_to_date('01-01-2017', 'dd-mm-yyyy')
                THEN
                    mld.setmeldingstatus (rec.mld_melding_key,
                                          6,
                                          104606,
                                          0);
                END IF;
            --99 Niet opgelost
            WHEN 99
            THEN
                mld.setmeldingstatus (rec.mld_melding_key,
                                      5,
                                      104606,
                                      0);
                IF rec.mld_melding_datum < FAC.safe_to_date('01-01-2017', 'dd-mm-yyyy')
                THEN
                    mld.setmeldingstatus (rec.mld_melding_key,
                                          6,
                                          104606,
                                          0);
                END IF;
        END CASE;

        DELETE fac_notificatie
         WHERE fac_notificatie_datum > v_date;
    END LOOP;

    COMMIT;
END;
/

PURGE RECYCLEBIN;

------ 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
