--
-- $Id$
--
-- Script containing customer specific configuration sql statements for aqqa: St Jacob

DEFINE thisfile = 'AQQA.SQL'
DEFINE dbuser   = '^AQQA'

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

-- exact verkoop
CREATE OR REPLACE VIEW aqqa_v_export_exact_verkoop    (RESULT, result_order) AS SELECT RESULT, result_order FROM aaxx_v_export_exact_verkoop;
CREATE OR REPLACE PROCEDURE aqqa_select_exact_verkoop (
   p_applname   IN   VARCHAR2,
   p_applrun    IN   VARCHAR2
)
AS
BEGIN
  aaxx_select_exact_verkoop(p_applname, p_applrun, to_char(sysdate, 'yyyy'));
END;
/
CREATE OR REPLACE PROCEDURE aqqa_export_exact_verkoop (
   p_applname   IN   VARCHAR2,
   p_applrun    IN   VARCHAR2,
   p_filedir    IN   VARCHAR2,
   p_filename   IN   VARCHAR2
)
AS
BEGIN
  aaxx_export_exact_verkoop(p_applname, p_applrun, p_filedir, p_filename, to_char(sysdate, 'yyyy'));
END;
/

-- exact verkoop over het vorige jaar
CREATE OR REPLACE VIEW aqqa_v_export_exact_verkoop_vj    (RESULT, result_order) AS SELECT RESULT, result_order FROM aaxx_v_export_exact_verkoop;
CREATE OR REPLACE PROCEDURE aqqa_select_exact_verkoop_vj (
   p_applname   IN   VARCHAR2,
   p_applrun    IN   VARCHAR2
)
AS
BEGIN
  aaxx_select_exact_verkoop(p_applname, p_applrun, to_char(to_number(to_char(sysdate, 'yyyy'))-1));
END;
/
CREATE OR REPLACE PROCEDURE aqqa_export_exact_verkoop_vj (
   p_applname   IN   VARCHAR2,
   p_applrun    IN   VARCHAR2,
   p_filedir    IN   VARCHAR2,
   p_filename   IN   VARCHAR2
)
AS
BEGIN
  aaxx_export_exact_verkoop(p_applname, p_applrun, p_filedir, p_filename, to_char(to_number(to_char(sysdate, 'yyyy'))-1));
END;
/

-- exact projecturen (XML)
CREATE OR REPLACE VIEW aqqa_v_export_exact_uren (result, result_order)
AS SELECT result, result_order FROM aaxx_v_export_exact_uren_xml;
CREATE OR REPLACE PROCEDURE aqqa_select_exact_uren (
   p_applname   IN   VARCHAR2,
   p_applrun    IN   VARCHAR2
)
AS
BEGIN
  aaxx_select_exact_uren_xml(p_applname, p_applrun, '');
END;
/

-- verwerk exact projecturen (XML)
CREATE OR REPLACE PROCEDURE aqqa_export_verwerk_exact_uren (
   p_applname   IN   VARCHAR2,
   p_applrun    IN   VARCHAR2,
   p_filedir    IN   VARCHAR2,
   p_filename   IN   VARCHAR2
)
AS
BEGIN
  aaxx_export_verwerk_exact_uren(p_applname, p_applrun, p_filedir, p_filename, '');
END;
/


-- bundel catering
CREATE OR REPLACE VIEW aqqa_v_export_bundel_catering    (RESULT, result_order) AS SELECT RESULT, result_order FROM aaxx_v_export_bundel_catering;
CREATE OR REPLACE PROCEDURE aqqa_export_bundel_catering (
   p_applname   IN   VARCHAR2,
   p_applrun    IN   VARCHAR2,
   p_filedir    IN   VARCHAR2,
   p_filename   IN   VARCHAR2
)
AS
BEGIN
  aaxx_export_bundel_catering(p_applname, p_applrun, p_filedir, p_filename);
END;
/


CREATE OR REPLACE PROCEDURE aqqa_import_perslid (p_import_key IN NUMBER)
AS
   v_seq_of_columns   VARCHAR2(255);
   v_old_login        VARCHAR2(30);

   -- cursor om de tussenvoegsels goed in te kunnen vullen. Deze staan nu achter de achternaam.
   CURSOR c
   IS
        SELECT   fac_usrdata_omschr, prs_perslid_oslogin
          FROM   fac_imp_perslid p, fac_usrdata u
         WHERE   UPPER(p.prs_perslid_naam) LIKE UPPER(u.fac_usrdata_omschr) || ' ' || '%'
           AND   fac_usrtab_key = 104
      ORDER BY   p.prs_perslid_oslogin, LENGTH (fac_usrdata_omschr) DESC;
BEGIN
   v_old_login := 'QueQeLeQue';
   -- AQQA gebruikt de pipe '|' als scheidingsteken. Deze gaan we even omzetten naar onze ';'

   UPDATE fac_imp_file
      SET fac_imp_file_line = REPLACE (fac_imp_file_line, '","', '";"')
    WHERE fac_import_key = p_import_key;

--   UPDATE fac_imp_file
--      SET fac_imp_file_line = REPLACE (fac_imp_file_line, '"')
--    WHERE fac_import_key = p_import_key
--      AND fac_imp_file_line LIKE '%givenName%';



   v_seq_of_columns :=
      '0;0;0;0;0;0;7;2;0;1;0;0;0;0;0;0;0;6;0;3;4;5;0;0;0;0;8;28;29;30;31;32;33;34;35;36;37;38;39;40;41;42;43;44;45;46';
   prs.import_perslid (
      p_import_key,
      v_seq_of_columns,
      '"givenName";"sn";"EmployeeID";"Title";"sAMAccountName";"mail";"CostCenter";"Company"%');

   -- Telefoonnummers worden niet meegeleverd in het importbestand. Zorg er dan voor dat ze niet overschreven worden.
   UPDATE fac_imp_perslid i
      SET (prs_perslid_telefoonnr, prs_perslid_mobiel) =
          (SELECT p.prs_perslid_telefoonnr, p.prs_perslid_mobiel
             FROM prs_perslid p
            WHERE p.prs_perslid_verwijder IS NULL
              AND p.prs_perslid_nr = i.prs_perslid_nr);

   FOR rec IN c
   LOOP
      BEGIN
         IF rec.prs_perslid_oslogin <> v_old_login
         THEN
            DBMS_OUTPUT.put_line (
               rec.prs_perslid_oslogin || '-' || rec.fac_usrdata_omschr);
            v_old_login := rec.prs_perslid_oslogin;
            UPDATE fac_imp_perslid p
               SET prs_perslid_tussenvoegsel = rec.fac_usrdata_omschr,
                   prs_perslid_naam = TRIM(SUBSTR (prs_perslid_naam, LENGTH (rec.fac_usrdata_omschr) + 1))
             WHERE p.prs_perslid_oslogin = rec.prs_perslid_oslogin;
         END IF;
      END;
   END LOOP;

   -- Maak standaard verdieping aan voor de werkplek
   INSERT INTO alg_verdieping (alg_gebouw_key, alg_verdieping_code, alg_verdieping_volgnr, alg_verdieping_omschrijving)
   SELECT alg_gebouw_key, '_', 99, 'Dummy'
     FROM alg_gebouw g
    WHERE alg_gebouw_verwijder IS NULL
      AND NOT EXISTS (SELECT v.alg_gebouw_key
                        FROM alg_verdieping v
                       WHERE v.alg_gebouw_key = g.alg_gebouw_key
                         AND v.alg_verdieping_code = '_');

   -- Maak standaard ruimte aan voor de werkplek
   INSERT INTO alg_ruimte (alg_verdieping_key, alg_ruimte_nr, alg_srtruimte_key)
   SELECT v.alg_verdieping_key, '_', 161
     FROM alg_verdieping v
    WHERE alg_verdieping_verwijder IS NULL
      AND NOT EXISTS (SELECT r.alg_verdieping_key
                        FROM alg_ruimte r
                       WHERE r.alg_verdieping_key = v.alg_verdieping_key
                         AND r.alg_ruimte_nr = '_');

   UPDATE fac_imp_perslid i
      SET (i.alg_locatie_code,
           i.alg_gebouw_code,
           i.alg_verdieping_volgnr,
           alg_ruimte_nr) =
              (SELECT l.alg_locatie_code,
                      g.alg_gebouw_code,
                      99,
                      '_'
                 FROM alg_locatie l, alg_gebouw g
                WHERE     l.alg_locatie_key = g.alg_locatie_key
                      AND g.ALG_GEBOUW_NAAM = i.prs_kenmerk1
                      AND i.prs_kenmerk1 IS NOT NULL);

END aqqa_import_perslid;
/


CREATE OR REPLACE PROCEDURE aqqa_update_perslid (
   p_import_key IN NUMBER
) IS

CURSOR c_del
IS
    SELECT p.prs_perslid_key, p.prs_perslid_nr, pf.prs_perslid_naam_full
      FROM fac_imp_perslid i, prs_perslid p, prs_v_perslid_fullnames_all pf
     WHERE p.prs_perslid_nr = i.prs_perslid_nr(+)
       AND pf.prs_perslid_key = p.prs_perslid_key
       AND p.prs_perslid_nr IS NOT NULL
       AND i.prs_perslid_nr IS NULL
       AND p.prs_perslid_verwijder IS NULL
    ORDER BY 2;

CURSOR c_afd_del
IS
    SELECT prs_afdeling_key, prs_afdeling_naam
      FROM prs_afdeling a
     WHERE prs_bedrijf_key = 501     -- Stichting Sint Jacob
       AND prs_afdeling_verwijder IS NULL
       AND NOT EXISTS (SELECT prs_perslid_key
                         FROM prs_perslid p
                        WHERE p.prs_afdeling_key = a.prs_afdeling_key
                          AND p.prs_perslid_verwijder IS NULL);


v_errormsg          VARCHAR2 (1000);
v_error_hint        VARCHAR2 (1000);
oracle_err_mes      VARCHAR2 (150);
oracle_err_num      VARCHAR2 (150);
v_perslid_key       NUMBER;
v_perslid_nr        VARCHAR2(100);
v_kostenplaats_key  NUMBER;
v_count             NUMBER;

BEGIN

   SELECT count(*)
     INTO v_count
     FROM fac_imp_perslid;

   IF v_count < 500
   THEN
      aaxx_imp_writelog (p_import_key,
                         'E',
                         'Aantal records in importbestand is te klein.',
                         'Aantal aangeboden records: ' || v_count
                         );
      RETURN;
   END IF;

   DELETE fac_imp_organisatie;

   INSERT INTO fac_imp_organisatie (prs_bedrijf_naam, prs_afdeling_naam,prs_afdeling_omschrijving, prs_kostenplaats_nr)
   SELECT DISTINCT 'Stichting Sint Jacob', prs_afdeling_naam, 'Costcenter ' || prs_afdeling_naam, prs_afdeling_naam
     FROM fac_imp_perslid;

   fac_update_organisatie(p_import_key);

	-- generic update
   prs.update_perslid (p_import_key, 'NR', 'A');

   FOR rec IN c_del
   LOOP
      BEGIN
        prs.delete_perslid (p_import_key, rec.prs_perslid_key);
      END;
   END LOOP;

   FOR rec in c_afd_del
   LOOP
      BEGIN
         UPDATE prs_afdeling
            SET prs_afdeling_verwijder = SYSDATE
          WHERE prs_afdeling_key = rec.prs_afdeling_key;

         SELECT prs_kostenplaats_key
           INTO v_kostenplaats_key
           FROM prs_kostenplaats k
          WHERE prs_kostenplaats_verwijder IS NULL
            AND prs_kostenplaats_nr = rec.prs_afdeling_naam;

         UPDATE prs_kostenplaats
            SET prs_kostenplaats_verwijder = SYSDATE
          WHERE prs_kostenplaats_key = v_kostenplaats_key;

      EXCEPTION WHEN OTHERS
      THEN
         v_error_hint := v_errormsg;
         oracle_err_num := SQLCODE;
         oracle_err_mes := SUBSTR (SQLERRM, 1, 100);
         v_errormsg := 'ORACLE (error '
                       || oracle_err_num
                       || '/'
                       || oracle_err_mes
                       || ')';
         aaxx_imp_writelog (p_import_key,
                            'E',
                            v_errormsg,
                            'Afdeling/Kostenplaats ' || rec.prs_afdeling_naam || ' kan niet verwijderd worden.');

      END;
   END LOOP;
END aqqa_update_perslid;
/

------ payload end   ------

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

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