--
-- $Id$
--
-- <<Korte omschrijving wat het script doet>>

DEFINE thisfile = 'MDUX#38437.SQL'
DEFINE dbuser   = '^MDUX'
DEFINE custid   = 'MDUX'

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

DECLARE
   CURSOR c
   IS
      SELECT fin_factuur_key
        FROM fin_kenmerkfactuur
       WHERE fin_kenmerkfactuur_waarde IN
                   ('IF16018605',
                    'IF16018606',
                    'IF16018476',
                    'IF16018581',
                    'IF16018802',
                    'IF16019728',
                    'IF16019946',
                    'IF16019844',
                    'IF16019845',
                    'IF16020089',
                    'IF16020105',
                    'IF16020103',
                    'IF16020108',
                    'IF16020116',
                    'IF16020119',
                    'IF16020241',
                    'IF16020370',
                    'IF16020264',
                    'IF16020343',
                    'IF16020344',
                    'IF16020352',
                    'IF16020399',
                    'IF16020470',
                    'IF16020472',
                    'IF16020473',
                    'IF16021177',
                    'IF16021294',
                    'IF16021411',
                    'IF16021424',
                    'IF16021441',
                    'IF16022030',
                    'IF16022031',
                    'IF16022186',
                    'IF16022185',
                    'IF16022174',
                    'IF16022172',
                    'IF16022168',
                    'IF16022167',
                    'IF16022169',
                    'IF16022166',
                    'IF16022165',
                    'IF16022163',
                    'IF16022157',
                    'IF16022154',
                    'IF16022155',
                    'IF16022187',
                    'IF16022284',
                    'IF16022197',
                    'IF16022201',
                    'IF16022509',
                    'IF16022506',
                    'IF16022507',
                    'IF16022505',
                    'IF16022504',
                    'IF16022503',
                    'IF16022502',
                    'IF16022501',
                    'IF16022500',
                    'IF16022497',
                    'IF16022375',
                    'IF16022374',
                    'IF16022384',
                    'IF16022380',
                    'IF16022381',
                    'IF16022383',
                    'IF16022398',
                    'IF16022397',
                    'IF16022404',
                    'IF16022402',
                    'IF16022403',
                    'IF16022452',
                    'IF16022454',
                    'IF16022453',
                    'IF16022455',
                    'IF16022456',
                    'IF16022225',
                    'IF16022555',
                    'IF16022560',
                    'IF16022562',
                    'IF16022564',
                    'IF16022275',
                    'IF16022277',
                    'IF16022420',
                    'IF16022421',
                    'IF16022260',
                    'IF16022394',
                    'IF16022427',
                    'IF16022429',
                    'IF16022428',
                    'IF16022430',
                    'IF16022431',
                    'IF16022433',
                    'IF16022432',
                    'IF16022436',
                    'IF16022434',
                    'IF16022435',
                    'IF16022438',
                    'IF16022437',
                    'IF16022439',
                    'IF16022441',
                    'IF16022443',
                    'IF16022444',
                    'IF16022510',
                    'IF16022512',
                    'IF16022511',
                    'IF16022248',
                    'IF16022247',
                    'IF16022244',
                    'IF16022243',
                    'IF16022239',
                    'IF16022237',
                    'IF16022515',
                    'IF16022548',
                    'IF16022550',
                    'IF16022552',
                    'IF16022240',
                    'IF16022459',
                    'IF16022235',
                    'IF16022252');

   v_errormsg         VARCHAR2 (1000);
   oracle_err_num     NUMBER;
   oracle_err_mes     VARCHAR2 (200);
   v_tracking_key     FAC_TRACKING.FAC_TRACKING_KEY%TYPE;

BEGIN
   FOR rec IN c
   LOOP
      BEGIN
         -- set de factuur een status terug. We gaan van verwerk in eerste instantie naar goedgekeurd.
         -- fac_srtnotificatie_key  118  is finupd
         INSERT INTO fac_tracking (fac_tracking_refkey, fac_srtnotificatie_key, fac_tracking_oms)
             VALUES (rec.fin_factuur_key, 118, 'Factuurstatus gewijzigd: MDUX#38437');

         -- Verwijder de finver tracking (key voor FINVER = 122)
         DELETE fac_tracking
          WHERE fac_tracking_refkey = rec.fin_factuur_key AND fac_srtnotificatie_key = 122;

         -- Status verwerkt (7) naar goedgekeurd (6)
         UPDATE fin_factuur
            SET fin_factuur_statuses_key = 6
          WHERE fin_factuur_key = rec.fin_factuur_key AND fin_factuur_statuses_key = 7;

         BEGIN
            -- fac_srtnotificatie_key FINFOK is 119
            -- Admin user is key 3
            SELECT fac_tracking_key
              INTO v_tracking_key
              FROM fac_tracking
             WHERE fac_srtnotificatie_key = 119
               AND fac_tracking_refkey = rec.fin_factuur_key
               AND prs_perslid_key = 3;

            -- Status goedgekeurd (6) naar ingevoerd(2)
            UPDATE fin_factuur
               SET fin_factuur_statuses_key = 2
             WHERE fin_factuur_key = rec.fin_factuur_key AND fin_factuur_statuses_key = 6;

            -- en dit gaan we tracken met FINFUN key = 121
            INSERT INTO fac_tracking (fac_tracking_refkey,
                                      fac_srtnotificatie_key,
                                      fac_tracking_oms)
                VALUES (rec.fin_factuur_key, 121, NULL);
         EXCEPTION
            WHEN NO_DATA_FOUND
            THEN
               -- De admin user heeft de factuur niet goedgekeurd. We hoeven dus niets meer te doen
               NULL;
         END;
      EXCEPTION
         WHEN OTHERS
         THEN
            oracle_err_num := SQLCODE;
            oracle_err_mes := SUBSTR (SQLERRM, 1, 100);
            v_errormsg :=
               v_errormsg || ' ORACLE (error ' || oracle_err_num || '/' || oracle_err_mes || ')';
            fac.writelog ('MDUX#38437',
                          'E',
                          rec.fin_factuur_key || ' - ' || v_errormsg,
                          'Fout bij aanpassen factuur');
      END;
   END LOOP;
END;
/

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