--
-- $Id$
--
-- Customer specific once-script BLCC#87600.SQL.
DEFINE thisfile = 'BLCC#87600.sql'
DEFINE dbuser   = '^BLCC'

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

-- BLCC#87600: TWK-vullen SCH_TARIEFNIVEAU2 voor de maanden 2025-07 en 2025-08
--             volgens contracten 6272 t/m 6276, want vergeten mee te nemen op
--             maandelijkse foto.
/*
SELECT baf.freeze_datum, baf.alg_ruimte_key, baf.sch_tariefniveau2,
           (SELECT MIN (DECODE (cnt_contract_key, 6272, 'Excellent', 6273, 'Premium', 6274, 'Excellent', 6275, 'Excellent', 'Excellent Plus'))
              FROM cnt_contract_plaats
             WHERE cnt_contract_key IN (6272, 6273, 6274, 6275, 6276)
                   AND cnt_alg_plaats_code = 'R'
                   AND cnt_alg_plaats_key = baf.alg_ruimte_key)
  FROM blcc_alg_freeze baf
 WHERE TRUNC (baf.freeze_datum) IN (fac.safe_to_date ('01-07-2025', 'dd-mm-yyyy'), fac.safe_to_date ('01-08-2025', 'dd-mm-yyyy'))
       AND baf.sch_tariefniveau2 IS NULL
       AND EXISTS
               (SELECT 1
                  FROM cnt_contract_plaats
                 WHERE cnt_contract_key IN (6272, 6273, 6274, 6275, 6276)
                       AND cnt_alg_plaats_code = 'R'
                       AND cnt_alg_plaats_key = baf.alg_ruimte_key);
*/
UPDATE blcc_alg_freeze baf
   SET baf.sch_tariefniveau2 =
           (SELECT MIN (DECODE (cnt_contract_key, 6272, 'Excellent', 6273, 'Premium', 6274, 'Excellent', 6275, 'Excellent', 'Excellent Plus'))
              FROM cnt_contract_plaats
             WHERE cnt_contract_key IN (6272, 6273, 6274, 6275, 6276)
                   AND cnt_alg_plaats_code = 'R'
                   AND cnt_alg_plaats_key = baf.alg_ruimte_key)
 WHERE TRUNC (baf.freeze_datum) IN (fac.safe_to_date ('01-07-2025', 'dd-mm-yyyy'), fac.safe_to_date ('01-08-2025', 'dd-mm-yyyy'))
       AND baf.sch_tariefniveau2 IS NULL
       AND EXISTS
               (SELECT 1
                  FROM cnt_contract_plaats
                 WHERE cnt_contract_key IN (6272, 6273, 6274, 6275, 6276)
                       AND cnt_alg_plaats_code = 'R'
                       AND cnt_alg_plaats_key = baf.alg_ruimte_key);

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