--
-- $Id$
--
-- Once script om nieuwe package alvast op te leveren. Omdat de regulere BLCC.sql nog even op zich laat wachten.

DEFINE thisfile = 'BLCC#55131.sql'
DEFINE dbuser   = '^BLCC'

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 ------
CREATE OR REPLACE PACKAGE BLCC
IS
    PROCEDURE mld_after_insert (p_melding_key IN NUMBER);
END;
/

CREATE OR REPLACE PACKAGE BODY BLCC
IS
    -- Procedure om bij het aanmaken van de melding te kijken of er al een oudere meldding is met
    -- dezelfde externe referentie (melding gedeelte zonder volgnummer)
    PROCEDURE mld_after_insert (p_melding_key IN NUMBER)
    AS
        v_melding_key   NUMBER;
    BEGIN
        SELECT (SELECT MIN (mld_melding_key)
                  FROM mld_melding m2
                 WHERE     SUBSTR (m2.mld_melding_externnr,
                                   1,
                                   INSTR (m2.mld_melding_externnr, '/')) =
                           SUBSTR (m1.mld_melding_externnr,
                                   1,
                                   INSTR (m1.mld_melding_externnr, '/'))
                       AND m1.mld_melding_key > m2.mld_melding_key)
                   refe
          INTO v_melding_key
          FROM mld_melding m1
         WHERE     mld_melding_externnr IS NOT NULL
               AND mld_melding_status = 2
               AND m1.mld_melding_key = p_melding_key;

        IF v_melding_key IS NOT NULL
        THEN
            DBMS_OUTPUT.put_line (
                'Melding update ' || p_melding_key || '-' || v_melding_key);

            UPDATE mld_melding
               SET mld_melding_opmerking =
                          'Startmelding met zelfde extern nummer: melding '
                       || v_melding_key
                       || CHR (13)
                       || CHR (10)
                       || SUBSTR (mld_melding_opmerking, 1, 3920)
             WHERE     mld_melding_key = p_melding_key
                   AND COALESCE (mld_melding_opmerking, 'QueQeLeQue') NOT LIKE
                           'Startmelding met zelfde extern nummer: melding %';
        END IF;
    EXCEPTION
        WHEN NO_DATA_FOUND
        THEN
            RETURN;
    END;
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
