--
-- $Id$
--
-- Customer specific once-script BLCC#53374.SQL.

DEFINE thisfile = 'BLCC#53374.SQL'
DEFINE dbuser   = '^BLCC'

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

-- BLCC#53374: Verwerk alle Workorders en Splitorders die voor 01-01-2018 zijn
--             afgemeld.
SET SERVEROUTPUT ON size 1000000;

DECLARE
   CURSOR c_opdr
   IS
        SELECT '[' || TO_CHAR (mld_melding_key) || '|' || TO_CHAR (mld_opdr_key) || '] ' aanduiding,
               mld_opdr_key, mld_melding_key,
               'Verwerkt nav. opschoonactie, zie melding 53374 in FACILITOR logcentre.' notitie
          FROM mld_opdr
         WHERE mld_typeopdr_key IN (101, 102)            -- Workorder/Splitorder
               AND mld_statusopdr_key = 6                            -- Afgemeld
               AND fac.gettrackingdate ('ORDAFM', mld_opdr_key) < TO_DATE ('01-01-2018', 'dd-mm-yyyy')
      ORDER BY mld_opdr_key;

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

   FOR rec IN c_opdr
   LOOP
      BEGIN
         v_errormsg := 'Fout toevoegen notitie';

         INSERT INTO mld_opdr_note (mld_opdr_key,
                                    prs_perslid_key,
                                    mld_opdr_note_omschrijving)
             VALUES (rec.mld_opdr_key,
                     4,                                          -- _FACILITOR
                     rec.notitie);

         v_errormsg := 'Fout verwerken opdracht';

         MLD.setopdrachtstatus (rec.mld_opdr_key, 7, NULL);        -- Verwerkt

         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: ' || rec.aanduiding || v_errormsg);
            COMMIT;
      END;
   END LOOP;

   DBMS_OUTPUT.PUT_LINE ('S: ' || TO_CHAR (v_count) || ' opdrachten verwerkt');
   COMMIT;
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
