--
-- $Id$
--
-- Correctie op MJOB KW1C waar de stardatum niet waren gevuld.
-- Voor dit script wordt uitgevoerd dient fac_imp_csv gevuld te zijn met de MJOB export van KW1C

DEFINE thisfile = 'KW1C#89661.SQL'
DEFINE dbuser   = 'KW1C'

SET ECHO ON
SET DEFINE ON
COLUMN fcltlogfile NEW_VALUE fcltlogfile NOPRINT;
COLUMN fcltcusterr NEW_VALUE fcltcusterr NOPRINT;
WHENEVER SQLERROR CONTINUE;
SELECT adm.getscriptspoolfile('&thisfile') AS fcltlogfile FROM DUAL;
SPOOL &fcltlogfile
WHENEVER SQLERROR EXIT;
SELECT adm.checkscriptcust('&dbuser') AS fcltcusterr FROM DUAL;
WHENEVER SQLERROR CONTINUE;
PROMPT &fcltcusterr
SET DEFINE OFF

------ payload begin ------

DECLARE
    CURSOR c1 IS
        SELECT i.ins_deel_key                     deel_key,
               s.ins_srtcontrole_key              ctr_key,
               ins_srtcontroledl_xcp_key          xcp_key,
               c.fac_imp_csv_col17                startjaar,
               fac.safe_to_date ('01-01-' || c.fac_imp_csv_col17,
                                 'DD-MM-YYYY')    startdat,
               s.ins_srtcontrole_omschrijving     taak_omschrijving
          FROM ins_srtcontroledl_xcp  xcp,
               ins_srtcontrole        s,
               ins_deel               i,
               fac_imp_csv            c
         WHERE     UPPER (i.ins_deel_omschrijving) =
                   UPPER (c.fac_imp_csv_col02)           -- objectomschrijving
               AND UPPER (SUBSTR (s.ins_srtcontrole_omschrijving, 13, 3)) =
                   UPPER (SUBSTR (fac_imp_csv_col06, 1, 3))
               AND i.ins_deel_key = xcp.ins_deel_key
               AND s.ins_srtcontrole_key = xcp.ins_srtcontrole_key;
BEGIN
    FOR rec IN c1
    LOOP
        BEGIN
            UPDATE ins_srtcontroledl_xcp
               SET ins_srtcontroledl_xcp_startdat = rec.startdat
             WHERE ins_srtcontroledl_xcp_key = rec.xcp_key;

            fac.trackaction (
                'INSUPD',
                rec.deel_key,
                6,                                                  -- Systeem
                NULL,
                   'Startdatum voor taak '
                || rec.taak_omschrijving
                || ' gezet op '
                || TO_CHAR (rec.startdat));
        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
