--
-- $Id$
--
-- Dubbele actieve ins_deelsrtcontrole records verwijderen.

DEFINE thisfile = 'PLAT#51637.sql'
DEFINE dbuser   = '^WOMA'

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
   oracle_err_num   NUMBER;
   oracle_err_mes   VARCHAR2 (255);
   v_aanduiding     VARCHAR2 (5120);
   v_errormsg       VARCHAR2 (1024);
   v_errorhint      VARCHAR2 (1024);

   -- Zoek alle taken op die twee actieve inspecties hebben.
   CURSOR c
   IS
      SELECT rec.ins_deel_key,
             rec.ins_srtcontrole_key,
             rec.min_dlsrtcontr_key,
             rec.max_dlsrtcontr_key,
             dsc_max.ins_deelsrtcontrole_key,
             dsc_max.ins_deelsrtcontrole_freezecost,
             dsc_max.ins_deelsrtcontrole_freezedate,
             dsc_max.ins_deelsrtcontrole_opmerking,
             dsc_max.ins_deelsrtcontrole_datum_org,
             dsc_max.prs_perslid_key
        FROM ins_deelsrtcontrole dsc_max,
             (  SELECT xcp.ins_deel_key,
                       xcp.ins_srtcontrole_key,
                       (SELECT COUNT(1)
                          FROM ins_deelsrtcontrole dsc
                         WHERE dsc.ins_deel_key = xcp.ins_deel_key
                           AND dsc.ins_srtcontrole_key = xcp.ins_srtcontrole_key
                           AND dsc.ins_scenario_key = 1
                           AND dsc.ins_deelsrtcontrole_status < 5)
                           plandatum,
                       (SELECT MIN(ins_deelsrtcontrole_key)
                          FROM ins_deelsrtcontrole dsc
                         WHERE dsc.ins_deel_key = xcp.ins_deel_key
                           AND dsc.ins_srtcontrole_key = xcp.ins_srtcontrole_key
                           AND dsc.ins_scenario_key = 1
                           AND dsc.ins_deelsrtcontrole_status < 5) min_dlsrtcontr_key,
                       (SELECT MAX(ins_deelsrtcontrole_key)
                          FROM ins_deelsrtcontrole dsc
                         WHERE dsc.ins_deel_key = xcp.ins_deel_key
                           AND dsc.ins_srtcontrole_key = xcp.ins_srtcontrole_key
                           AND dsc.ins_scenario_key = 1
                           AND dsc.ins_deelsrtcontrole_status < 5) max_dlsrtcontr_key
                  FROM ins_v_defined_inspect_xcp xcp
                 WHERE xcp.ins_srtcontrole_periode > 0
                   AND xcp.ins_scenario_key = 1
                   AND xcp.ctr_ismjob = 1
                   AND (SELECT COUNT(1)
                          FROM ins_deelsrtcontrole dsc
                         WHERE dsc.ins_deel_key = xcp.ins_deel_key
                           AND dsc.ins_srtcontrole_key = xcp.ins_srtcontrole_key
                           AND dsc.ins_scenario_key = 1
                           AND dsc.ins_deelsrtcontrole_status < 5) = 2) rec
       WHERE dsc_max.ins_deelsrtcontrole_key = rec.max_dlsrtcontr_key
      ORDER BY rec.ins_deel_key;
BEGIN
   FOR r IN c
   LOOP
      BEGIN
         v_aanduiding :=   'Freeze waarden worden gekopieerd van ins_deelsrtcontrole  ' || r.max_dlsrtcontr_key
                        || '(max) naar ' || r.min_dlsrtcontr_key
                        || '(min), ins_deel_key = ' || r.ins_deel_key
                        || ', ins_srtcontrole_key = ' || r.ins_srtcontrole_key
                        || ', ins_deelsrtcontrole_freezecost = ' || r.ins_deelsrtcontrole_freezecost
                        || ', ins_deelsrtcontrole_freezedate = ' || r.ins_deelsrtcontrole_freezedate
                        || ', ins_deelsrtcontrole_opmerking = ' || r.ins_deelsrtcontrole_opmerking
                        || ', ins_deelsrtcontrole_datum_org = ' || r.ins_deelsrtcontrole_datum_org;

         v_errorhint := 'Update freeze bij ins_deelsrtcontrole';

         DBMS_OUTPUT.PUT_LINE (v_aanduiding);

         DELETE FROM ins_deelsrtcontrole
          WHERE ins_deelsrtcontrole_key = r.max_dlsrtcontr_key;

         DBMS_OUTPUT.PUT_LINE ('Reocrd verwijderd, ins_deelsrtcontrole_key = ' || r.max_dlsrtcontr_key);

         UPDATE ins_deelsrtcontrole
            SET ins_deelsrtcontrole_freezecost = r.ins_deelsrtcontrole_freezecost,
                ins_deelsrtcontrole_freezedate = r.ins_deelsrtcontrole_freezedate,
                ins_deelsrtcontrole_opmerking = r.ins_deelsrtcontrole_opmerking,
                ins_deelsrtcontrole_datum_org = r.ins_deelsrtcontrole_datum_org
          WHERE ins_deelsrtcontrole_key = r.min_dlsrtcontr_key;

         DBMS_OUTPUT.PUT_LINE ('UPDATED');

         ins.setinspectstatus(r.min_dlsrtcontr_key, 2, r.prs_perslid_key);
         DBMS_OUTPUT.PUT_LINE ('Status naar In behandeling/Gestart');
      EXCEPTION
         WHEN OTHERS
         THEN
            oracle_err_num := SQLCODE;
            oracle_err_mes := SUBSTR (SQLERRM, 1, 200);
            v_errorhint := v_errormsg;
            v_errormsg :=
                  v_aanduiding
               || '(ORACLE error '
               || oracle_err_num
               || '/'
               || oracle_err_mes
               || ')';
            DBMS_OUTPUT.PUT_LINE ('Error: ' || v_errormsg || '; Hint: ' || v_errorhint);
      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
