--
-- $Id$
--
-- Script om melding bij rondleidingen af te melden als alle opdracht ook afgewezen of afgemeld zijn.
--
DEFINE thisfile = 'KMMO#77565.sql'
DEFINE dbuser   = '^KMMO'

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 c IS
        SELECT mld_melding_key
          FROM mld_melding m, mld_stdmelding std
         WHERE     m.mld_melding_status IN (2, 3, 4)                             -- Nieuw, Te accepteren, In behandeling
               AND m.mld_stdmelding_key = std.mld_stdmelding_key
               AND std.mld_ins_discipline_key = 361                                                     -- Rondleidingen
               AND NOT EXISTS
                       (SELECT 1
                          FROM mld_opdr o
                         WHERE o.mld_melding_key = m.mld_melding_key AND o.mld_statusopdr_key NOT IN (6, 1)) -- afgemeld en afgewezen
               AND mld_melding_einddatum < TO_DATE ('20230601', 'yyyymmdd');
BEGIN
    FOR rec IN c
    LOOP
        mld.setmeldingstatus (rec.mld_melding_key, 5, NULL); -- afgemeld
    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
