-- Script containing customer generic db-configuration for all KF accounts.
-- (c) 2016 Facilitor B.V.
--
-- Support: +31 53 4800700
SET ECHO ON
SET DEFINE ON
column fcltlogfile new_value fcltlogfile NOPRINT;
select UPPER(USER) || '_KFXX' || '.lst' as fcltlogfile from dual;
spool &fcltlogfile
SET DEFINE OFF

/* Formatted on 26-2-2016 15:45:15 (QP5 v5.136.908.31019) */
CREATE OR REPLACE PACKAGE KFXX
AS
   PROCEDURE add_xml_row (p_bestand IN VARCHAR2, p_regel IN VARCHAR2);

   PROCEDURE add_xml_element (p_bestand   IN VARCHAR2,
                              p_tag       IN VARCHAR2,
                              p_value     IN VARCHAR2);

   FUNCTION get_user
      RETURN VARCHAR2;
END;
/

CREATE OR REPLACE PACKAGE BODY KFXX
AS
   PROCEDURE add_xml_row (p_bestand IN VARCHAR2, p_regel IN VARCHAR2)
   AS
      v_index   NUMBER;
   BEGIN
      SELECT COALESCE (MAX (fac_rapport_volgnr), 0) + 1
        INTO v_index
        FROM fac_rapport
       WHERE fac_rapport_node = p_bestand;

      INSERT INTO fac_rapport (fac_rapport_node,
                               fac_rapport_volgnr,
                               fac_rapport_regel)
          VALUES (p_bestand, v_index, p_regel);
   END;

   PROCEDURE add_xml_element (p_bestand   IN VARCHAR2,
                              p_tag       IN VARCHAR2,
                              p_value     IN VARCHAR2)
   AS
   BEGIN
      kfxx.add_xml_row (
         p_bestand,
            '<'
         || p_tag
         || '>'
         || xml.char_to_html (p_value)
         || '</'
         || p_tag
         || '>');
   END;

   FUNCTION get_user
      RETURN VARCHAR2
   AS
      v_user   VARCHAR2 (20);
   BEGIN
      v_user := UPPER (SUBSTR (USER, 1, 4));
      RETURN v_user;
   END;
END;
/


/* Formatted on 26-2-2016 15:50:05 (QP5 v5.136.908.31019) */
CREATE OR REPLACE PROCEDURE kfxx_import_perslid (p_import_key IN NUMBER)
AS
   v_seq_of_columns   VARCHAR (255);
BEGIN
   v_seq_of_columns := '0;0;0;0;0;0;1;2;3;5;4;0;0;0;14;7;8;9;15;12;11;13;0;6;10;0;16;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0';
   prs.import_perslid (p_import_key, v_seq_of_columns, 'AFDELINGSCODE;PERSOONACHTERNAAM;TUSSENVOEGSEL;VOORLETTERS;VOORNAAM;TITEL_AANHEF;TELEFOONNUMMER;MOBIEL;EMAIL;APIKEY;FUNCTIE;PERSONEELSNUMMER;LOGINNAAM;GESLACHT;DIENSTVERBAND%');
END kfxx_import_perslid;
/

CREATE OR REPLACE PROCEDURE kfxx_update_perslid (p_import_key IN NUMBER)
AS
BEGIN
   -- Generic update
   prs.update_perslid (p_import_key, 'APIKEY', NULL);

   UPDATE prs_perslid p
      SET prs_perslid_oslogin2 =
             (SELECT prs_kenmerk1
                FROM fac_imp_perslid i
               WHERE i.prs_perslid_key = p.prs_perslid_key)
    WHERE EXISTS (SELECT i.prs_perslid_key
                    FROM fac_imp_perslid i
                   WHERE i.prs_perslid_key = p.prs_perslid_key);
END kfxx_update_perslid;
/

/* Formatted on 16-6-2016 9:30:30 (QP5 v5.136.908.31019) */
CREATE OR REPLACE VIEW kfxx_v_user
(
   account,
   tijdstip,
   fe_users,
   key_users,
   xdfe_users,                 -- FE met APIKEY onder 'xd_primary_bedrijfkey'!
   xdkey_users,               -- KEY met APIKEY onder 'xd_primary_bedrijfkey'!
   kien_apifes,               -- FE zonder '_' met APIKEY (= XDFE per 2016.2)!
   kien_apikeys,            -- KEY zonder '_' met APIKEY (= XDKEY per 2016.2)!
   key_users_cor
)
AS
   SELECT account,
          tijdstip,
          fe_users,
          key_users,
          xdfe_users,
          xdkey_users,
          kien_apifes,
          kien_apikeys,
          DECODE (account,
                  'VENR', key_users + kien_apifes + kien_apikeys,
                  key_users + xdkey_users - kien_apikeys)
             key_users_cor
     FROM (SELECT (SELECT fac_version_cust FROM fac_version) account,
                  tijdstip, fe_users, key_users,
                  xdfe_users, -- Alleen als 'xd_primary_bedrijfkey' gezet (= VENR)!
                  xdkey_users, -- Alleen als 'xd_primary_bedrijfkey' gezet (= VENR)!
                  (SELECT COUNT ( * )
                     FROM prs_v_aanwezigperslid p
                    WHERE p.prs_perslid_apikey IS NOT NULL
                          --AND INSTR (COALESCE (UPPER (p.prs_perslid_email), 'x'), '@KIEN-FM.NL') > 0
                          AND SUBSTR (prs_perslid_oslogin, 1, 1) != '_'
                          AND NOT EXISTS
                                (SELECT 1
                                   FROM fac_v_lcrap_key_stats
                                  WHERE prs_perslid_key = p.prs_perslid_key))
                     kien_apifes,
                  (SELECT COUNT ( * )
                     FROM prs_v_aanwezigperslid p
                    WHERE p.prs_perslid_apikey IS NOT NULL
                          --AND INSTR (COALESCE (UPPER (p.prs_perslid_email), 'x'), '@KIEN-FM.NL') > 0
                          AND SUBSTR (prs_perslid_oslogin, 1, 1) != '_'
                          AND EXISTS
                                (SELECT 1
                                   FROM fac_v_lcrap_key_stats
                                  WHERE prs_perslid_key = p.prs_perslid_key))
                     kien_apikeys
             FROM fac_v_lcrap_fe_vs_key);

BEGIN adm.systrackscriptId('$Id$', 0); END;
/
COMMIT;
SPOOL OFF
