--
-- $Id$
--
-- Status van alle in mei ten onrechte verwerkte dispuut-opdrachten terug naar
-- status Afgerond!
DEFINE thisfile = 'KFSG#51990_onverwerken.SQL'
DEFINE dbuser   = '^KFSG'

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

SET SERVEROUTPUT ON size 1000000;

DECLARE
   v_errormsg       VARCHAR2 (1000);
   oracle_err_num   NUMBER;
   oracle_err_mes   VARCHAR2 (200);
   v_count          NUMBER;
BEGIN
   v_count := 0;

   -- Cursor loopt over alle in mei ten onrechte verwerkte dispuut-opdrachten;
   -- deze gaan terug naar status Afgerond!
   FOR rc
      IN (  SELECT v.mld_opdr_key
              FROM kfsg_v_frozen_fact v
             WHERE v.mld_opdr_key IS NOT NULL
                   AND EXISTS                  -- Niet gemarkeerd als dispuut!
                         (SELECT 1
                            FROM mld_v_aanwezigkenmerkopdr ko, mld_kenmerk k
                           WHERE ko.mld_kenmerk_key = k.mld_kenmerk_key
                                 AND k.mld_srtkenmerk_key = 141     -- Dispuut
                                 AND ko.mld_kenmerkopdr_waarde = '1'
                                 AND ko.mld_opdr_key = v.mld_opdr_key)
          ORDER BY v.mld_opdr_key)
   LOOP
      BEGIN
         EXECUTE IMMEDIATE 'ALTER TRIGGER MLD_T_MLD_OPDR_B_IU DISABLE';

         UPDATE mld_opdr
            SET mld_opdr_ordernr = NULL, mld_statusopdr_key = 9    -- Afgerond
          WHERE mld_statusopdr_key = 7          -- Zou zo moeten zijn/Verwerkt
                AND mld_opdr_key = rc.mld_opdr_key;

         EXECUTE IMMEDIATE 'ALTER TRIGGER MLD_T_MLD_OPDR_B_IU ENABLE';

         --DELETE FROM fac_tracking
         --      WHERE fac_srtnotificatie_key = 72                     -- ORDVER
         --            AND fac_tracking_refkey = rc.mld_opdr_key;
         UPDATE fac_tracking
            SET fac_tracking_datum = SYSDATE,
                fac_srtnotificatie_key = 63,                         -- ORDUPD
                fac_tracking_oms = 'Verwerkt-status teruggedraaid'
          WHERE fac_srtnotificatie_key = 72                          -- ORDVER
                AND fac_tracking_refkey = rc.mld_opdr_key;

         v_count := v_count + 1;
      EXCEPTION
         WHEN OTHERS
         THEN
            oracle_err_num := SQLCODE;
            oracle_err_mes := SUBSTR (SQLERRM, 1, 200);
            v_errormsg := v_errormsg || ' ORACLE (error ' || oracle_err_num || '/' || oracle_err_mes || ')';
            DBMS_OUTPUT.PUT_LINE ('W: ' || v_errormsg);
            COMMIT;
      END;
   END LOOP;

   DBMS_OUTPUT.PUT_LINE ('S: ' || TO_CHAR (v_count) || ' dispuut-opdrachten terug naar status Afgerond');
   COMMIT;
END;
/

COMMIT;

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