-- 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-8-2019 13:48:12 (QP5 v5.136.908.31019) */
CREATE OR REPLACE VIEW kfxx_v_user
(
   account,
   tijdstip,
   prs_perslid_key,
   naam,
   email,
   oslogin,
   last_login,
   fe_user,                                                         -- 0 of 1!
   key_user,                                                        -- 0 of 1!
   yask                                                    -- 'X' indien Yask!
)
AS
   SELECT (SELECT fac_version_cust FROM fac_version) account,
          SYSDATE tijdstip,
          v.prs_perslid_key,
          v.naam,
          p.prs_perslid_email,
          v.oslogin,
          v.last_login,
          DECODE (key_user, 1, 0, 1) fe_user,
          DECODE (key_user, 1, 1, 0) key_user,
          DECODE (INSTR (UPPER (p.prs_perslid_email), '@YASK'), NULL, NULL,0, NULL, 'X')
             yask
     FROM fac_v_lcrap_fe_vs_key_data v, prs_perslid p
    WHERE v.prs_perslid_key = p.prs_perslid_key;

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