-- Customer specific once-script UWVA#31378.
--
-- (c) 2015 Facilitor BV
SET ECHO ON
SPOOL xuwva#31378.lst
SET DEFINE OFF

-- STAP1: Huidige inhoud van de kenmerken onder regel 7 verwijderen (want
-- foutief geconverteerd)!
UPDATE cnt_kenmerkcontract
   SET cnt_kenmerkcontract_verwijder = SYSDATE
 WHERE TRUNC (cnt_kenmerkcontract_aanmaak) = TO_DATE ('17122014', 'ddmmyyyy')
       AND cnt_kenmerk_key IN (1009, 1010, 1011, 1012, 1013);

-- STAP2: De kenmerken onder regel 7 opnieuw vullen met de correcte inhoud.
SET SERVEROUTPUT ON size 1000000;

DECLARE
   oracle_err_num   NUMBER;
   oracle_err_mes   VARCHAR2 (255);
   v_aanduiding     VARCHAR2 (255);
   v_errormsg       VARCHAR2 (1024);
   v_errorhint      VARCHAR2 (1024);
   v_lines_cnt      NUMBER;
   v_line2_cnt      NUMBER;
   v_line4_cnt      NUMBER;
   v_line5_cnt      NUMBER;
   v_line6_cnt      NUMBER;

   CURSOR c
   IS
        SELECT DISTINCT x.cnt_contract_key, c.cnt_contract_nummer_intern
          FROM uwva_tmp_hv_contractregels x, cnt_contract c
         WHERE x.volgorde = 6 AND x.cnt_contract_key = c.cnt_contract_key
      ORDER BY x.cnt_contract_key;
BEGIN
   FOR r1 IN c
   LOOP
      BEGIN
         v_aanduiding :=
               '['
            || TO_CHAR (r1.cnt_contract_key)
            || '| '
            || r1.cnt_contract_nummer_intern
            || '] ';


         v_errorhint := 'Fout bepalen contractregels';

         v_lines_cnt := 1;
         v_line2_cnt := 1;
         v_line4_cnt := 1;
         v_line5_cnt := 1;
         v_line6_cnt := 1;

         FOR r2
            IN (  SELECT x.volgorde,
                         x.rubriek,
                         x.aantal,
                         x.eenheid,
                         x.kosten_excl_btw,
                         x.btw_tarief
                    FROM uwva_tmp_hv_contractregels x
                   WHERE x.volgorde = 6 AND x.cnt_contract_key = r1.cnt_contract_key
                ORDER BY x.volgorde, x.aantal DESC)
         LOOP
            BEGIN
               v_errorhint :=
                  'Fout toevoegen contractregel: ' || TO_CHAR (v_lines_cnt);

               IF (r2.volgorde = 6)               -- INSERT op 6, anders fout!
               THEN
                  IF (v_line6_cnt = 1)
                  THEN
                     --INSERT op 6; 1009, 1010, 1011, 1012, 1013
                     INSERT INTO cnt_kenmerkcontract (cnt_contract_key,
                                                      cnt_kenmerk_key,
                                                      cnt_kenmerkcontract_waarde)
                         VALUES (r1.cnt_contract_key, 1009, r2.rubriek);

                     IF r2.aantal IS NOT NULL
                     THEN
                        INSERT INTO cnt_kenmerkcontract (cnt_contract_key,
                                                         cnt_kenmerk_key,
                                                         cnt_kenmerkcontract_waarde)
                            VALUES (r1.cnt_contract_key, 1011, r2.aantal);
                     END IF;

                     IF r2.eenheid IS NOT NULL
                     THEN
                        INSERT INTO cnt_kenmerkcontract (cnt_contract_key,
                                                         cnt_kenmerk_key,
                                                         cnt_kenmerkcontract_waarde)
                            VALUES (r1.cnt_contract_key, 1012, r2.eenheid);
                     END IF;

                     IF r2.kosten_excl_btw IS NOT NULL
                     THEN
                        INSERT INTO cnt_kenmerkcontract (cnt_contract_key,
                                                         cnt_kenmerk_key,
                                                         cnt_kenmerkcontract_waarde)
                            VALUES (r1.cnt_contract_key,
                                    1013,
                                    r2.kosten_excl_btw);
                     END IF;

                     --IF r2.btw_tarief IS NOT NULL
                     --THEN
                     --   INSERT INTO cnt_kenmerkcontract (cnt_contract_key,
                     --                                    cnt_kenmerk_key,
                     --                                    cnt_kenmerkcontract_waarde)
                     --       VALUES (r1.cnt_contract_key, -1, r2.btw_tarief);
                     --END IF;

                     v_line6_cnt := v_line6_cnt + 1;
                  ELSE
                     -- Iets loopt niet zoals verwacht!
                     DBMS_OUTPUT.PUT_LINE (
                        'Error: Teveel 6-regels; Hint: ' || v_errorhint);
                  END IF;
               END IF;

               v_lines_cnt := v_lines_cnt + 1;
            EXCEPTION
               WHEN OTHERS
               THEN
                  oracle_err_num := SQLCODE;
                  oracle_err_mes := SUBSTR (SQLERRM, 1, 200);
                  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;

         COMMIT;
      EXCEPTION
         WHEN OTHERS
         THEN
            oracle_err_num := SQLCODE;
            oracle_err_mes := SUBSTR (SQLERRM, 1, 200);
            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;
/

COMMIT;
BEGIN adm.systrackscriptId('$Id$'); END;
/
SPOOL OFF
