#ifdef  CNT
/*  $Revision$
 *  $Id$
 */

AUDIT_BEGIN(cnt_disc_params)
AUDIT_VALUE(cnt_disc_params, cnt_srtcontract_type)
AUDIT_VALUE(cnt_disc_params, cnt_disc_params_factuurschema)
AUDIT_VALUE(cnt_disc_params, cnt_disc_params_factuurpct)
AUDIT_VALUE(cnt_disc_params, cnt_disc_params_factuurmarge)
AUDIT_VALUE(cnt_disc_params, cnt_disc_params_factuurappr)
AUDIT_VALUE(cnt_disc_params, cnt_disc_params_factuurgrens)
AUDIT_VALUE(cnt_disc_params, cnt_disc_params_opmerking)
AUDIT_END()

CREATE_TRIGGER(cnt_t_cnt_disc_params_b_iu)
BEFORE INSERT OR UPDATE ON cnt_disc_params
FOR EACH ROW
BEGIN
   UPDATE_PRIMARY_KEY(cnt_disc_params_key, cnt_s_cnt_disc_params_key);
END;
/

CREATE_TRIGGER(cnt_t_cnt_contract_object_B_IU)
BEFORE INSERT OR UPDATE ON cnt_contract_object
FOR EACH ROW
BEGIN
  UPDATE_PRIMARY_KEY(cnt_contract_object_key,cnt_s_cnt_contract_object_key);
END;
/

CREATE_TRIGGER(cnt_t_cnt_contract_plaats_B_IU)
BEFORE INSERT OR UPDATE ON cnt_contract_plaats
FOR EACH ROW
BEGIN
  UPDATE_PRIMARY_KEY(cnt_contract_plaats_key,cnt_s_cnt_contract_plaats_key);

  IF ((:old.cnt_alg_plaats_key <> :new.cnt_alg_plaats_key)
  OR  (:old.cnt_alg_plaats_key IS NULL))
  THEN
    IF :new.cnt_alg_plaats_code = 'R'
    THEN
      CHECK_KEY_REFERENCE(alg_v_aanwezigruimte       , alg_ruimte_key       , :new.cnt_alg_plaats_key, 'cnt_m007');
    ELSIF :new.cnt_alg_plaats_code = 'V'
    THEN
      CHECK_KEY_REFERENCE(alg_v_aanwezigverdieping   , alg_verdieping_key   , :new.cnt_alg_plaats_key, 'cnt_m006');
    ELSIF :new.cnt_alg_plaats_code = 'G'
    THEN
      CHECK_KEY_REFERENCE(alg_v_aanweziggebouw       , alg_gebouw_key       , :new.cnt_alg_plaats_key, 'cnt_m005');
    ELSIF :new.cnt_alg_plaats_code = 'T'
    THEN
      CHECK_KEY_REFERENCE(alg_v_aanwezigterreinsector, alg_terreinsector_key, :new.cnt_alg_plaats_key, 'cnt_m004');
    ELSIF :new.cnt_alg_plaats_code = 'L'
    THEN
      CHECK_KEY_REFERENCE(alg_v_aanweziglocatie      , alg_locatie_key      , :new.cnt_alg_plaats_key, 'cnt_m003');
    ELSE
      raise_application_error(-20000, 'cnt_m008');
    END IF;
  END IF;
END;
/

CREATE_TRIGGER(cnt_t_cnt_contract_B_IU)
BEFORE INSERT OR UPDATE ON cnt_contract
FOR EACH ROW
BEGIN
  IF (   :new.cnt_prs_bedrijf_key  IS NULL
     AND :new.cnt_prs_afdeling_key IS NULL
     AND :new.cnt_prs_perslid_key  IS NULL)
  OR (   :new.cnt_prs_bedrijf_key  IS NOT NULL
     AND :new.cnt_prs_afdeling_key IS NOT NULL)
  OR (   :new.cnt_prs_bedrijf_key  IS NOT NULL
     AND :new.cnt_prs_perslid_key IS NOT NULL)
  OR (   :new.cnt_prs_perslid_key  IS NOT NULL
     AND :new.cnt_prs_afdeling_key IS NOT NULL)
  THEN
    raise_application_error(-20000,'cnt_m002');
  ELSE
    UPDATE_PRIMARY_KEY(cnt_contract_key,cnt_s_cnt_contract_key);
    IF :new.cnt_contract_nummer_intern IS NULL
    THEN
       :new.cnt_contract_nummer_intern := TO_CHAR(:new.cnt_contract_key);
    END IF;
  END IF;

  SET_VERWIJDER_CHILDREN(cnt_contract_object,cnt_contract_key,cnt_contract_verwijder,cnt_contract_object_verwijder,cnt_contract);
  SET_VERWIJDER_CHILDREN(cnt_contract_plaats,cnt_contract_key,cnt_contract_verwijder,cnt_contract_plaats_verwijder,cnt_contract);
END;
/

CREATE_TRIGGER(cnt_t_cnt_contract_dienst_B_IU)
BEFORE INSERT OR UPDATE ON cnt_contract_dienst
FOR EACH ROW
BEGIN
    IF :new.cnt_contract_dienst_key IS NULL THEN
      SELECT cnt_s_cnt_contr_dienst_key.nextval
      INTO :new.cnt_contract_dienst_key FROM DUAL;
    END IF;
    IF :new.PRS_BEDRIJF_KEY IS NULL THEN
      SELECT C.cnt_prs_bedrijf_key, P.prs_perslid_naam_full
      INTO :new.prs_bedrijf_key, :new.CNT_CONTRACT_DIENST_CONTACTEXT
      FROM cnt_contract C, PRS_V_PERSLID_FULLNAMES_ALL P
      WHERE C.cnt_contract_key=:new.cnt_contract_key
      AND P.prs_perslid_key = C.prs_contactpersoon_key;
      --:new.CNT_CONTRACT_DIENST_CONTACTEXT := NULL;
    END IF;
    IF :new.CNT_CONTRACT_DIENST_CONTACTINT IS NULL THEN
      SELECT P.prs_perslid_naam_full
      INTO :new.CNT_CONTRACT_DIENST_CONTACTINT
      FROM cnt_contract C, PRS_V_PERSLID_FULLNAMES_ALL P
      WHERE C.cnt_contract_key=:new.cnt_contract_key
      AND P.prs_perslid_key = C.prs_contactpersoon_key;
    END IF;
    DECLARE
        dummy VARCHAR2(1);
     BEGIN
      SELECT 'x'
      INTO dummy
      from mld_stdmelding S, cnt_contract C
      where mld_stdmelding_key=:new.mld_stdmelding_key
      and c.ins_discipline_key=s.mld_ins_discipline_key
      and c.CNT_CONTRACT_KEY=:new.cnt_contract_key;
     EXCEPTION
        WHEN NO_DATA_FOUND
          THEN raise_application_error(-20000, 'cnt_m031');
     END;

END;
/

CREATE_TRIGGER(cnt_t_cnt_srtkenmerk_b_iu)
BEFORE INSERT OR UPDATE ON cnt_srtkenmerk
FOR EACH ROW
BEGIN
  UPDATE_PRIMARY_KEY(cnt_srtkenmerk_key, cnt_s_cnt_srtkenmerk_key);
  UPDATE_UPPER(cnt_srtkenmerk_omschrijving, cnt_srtkenmerk_upper, cnt_srtkenmerk);
  UPDATE_UPPER(cnt_srtkenmerk_code, cnt_srtkenmerk_code, cnt_srtkenmerk);

  UPDATE_AANMAAKDATUM(cnt_srtkenmerk, cnt_srtkenmerk_aanmaak);
  IF (:new.cnt_srtkenmerk_verwijder IS NOT NULL AND :old.cnt_srtkenmerk_verwijder IS NULL AND
      :new.cnt_srtkenmerk_systeem = 1)
  THEN
    raise_application_error(-20000, 'CNT_C_CNT_SRTKENMERK_SYSTEEM');
  END IF;
  --
  IF :new.cnt_srtkenmerk_kenmerktype IN ('C', 'N', 'B') AND :new.cnt_srtkenmerk_lengte IS NULL
  THEN
    raise_application_error(-20000, 'CNT_C_CNT_SRTKENMERK_LENGTE');
  END IF;
  -- Numeriek mag niet te lang zijn (numeric overflow)
  IF :new.cnt_srtkenmerk_kenmerktype IN ('N', 'B')
  AND :new.cnt_srtkenmerk_lengte > 20
  THEN
     :new.cnt_srtkenmerk_lengte := 20;
  END IF;
  --
  IF  :new.cnt_srtkenmerk_kenmerktype IN ('N', 'B')
  AND :old.cnt_srtkenmerk_key IS NOT NULL
  THEN
    IF :old.cnt_srtkenmerk_nmin <> :new.cnt_srtkenmerk_nmin
    OR (:old.cnt_srtkenmerk_nmin IS NULL AND :new.cnt_srtkenmerk_nmin IS NOT NULL)
    THEN
      DECLARE
        Dummy CHAR;
      BEGIN
        SELECT 'X'
        INTO   Dummy
        FROM   cnt_kenmerkcontract
        WHERE  FAC.SAFE_TO_NUMBER(cnt_kenmerkcontract_waarde) < :new.cnt_srtkenmerk_nmin
        AND    cnt_kenmerk_key IN
              (SELECT cnt_kenmerk_key
               FROM   cnt_kenmerk
               WHERE  cnt_srtkenmerk_key = :old.cnt_srtkenmerk_key
              );
        raise_application_error(-20000, 'CNT_C_CNT_SRTKENMERK_MIN');
      EXCEPTION
        WHEN NO_DATA_FOUND
          THEN NULL;
        WHEN TOO_MANY_ROWS
          THEN raise_application_error(-20000, 'CNT_C_CNT_SRTKENMERK_MIN');
      END;
    END IF;
    --
    IF :old.cnt_srtkenmerk_nmax <> :new.cnt_srtkenmerk_nmax
    OR (:old.cnt_srtkenmerk_nmax IS NULL AND :new.cnt_srtkenmerk_nmax IS NOT NULL)
    THEN
      DECLARE
        Dummy CHAR;
      BEGIN
        SELECT 'X'
        INTO   Dummy
        FROM   cnt_kenmerkcontract
        WHERE  FAC.SAFE_TO_NUMBER(cnt_kenmerkcontract_waarde) > :new.cnt_srtkenmerk_nmax
        AND    cnt_kenmerk_key IN
              (SELECT cnt_kenmerk_key
               FROM   cnt_kenmerk
               WHERE  cnt_srtkenmerk_key = :old.cnt_srtkenmerk_key
              );
        raise_application_error(-20000, 'CNT_C_CNT_SRTKENMERK_MAX');
      EXCEPTION
        WHEN NO_DATA_FOUND
          THEN NULL;
        WHEN TOO_MANY_ROWS
          THEN raise_application_error(-20000, 'CNT_C_CNT_SRTKENMERK_MAX');
      END;
    END IF;
  END IF;
END;
/

CREATE_TRIGGER(cnt_t_cnt_kenmerk_b_iu)
BEFORE INSERT OR UPDATE ON cnt_kenmerk
FOR EACH ROW
BEGIN
   -- FSN#13902: we tolereren het achteraf verplicht maken
  UPDATE_PRIMARY_KEY(cnt_kenmerk_key, cnt_s_cnt_kenmerk_key);
  UPDATE_AANMAAKDATUM(cnt_kenmerk, cnt_kenmerk_aanmaak);
  UPDATE_UPPER(cnt_kenmerk_code, cnt_kenmerk_code, cnt_kenmerk);
END;
/

CREATE_TRIGGER(cnt_t_cnt_kenmerkcontract_b_iu)
BEFORE INSERT OR UPDATE ON cnt_kenmerkcontract
FOR EACH ROW
BEGIN
  -- Controleer eerst of de ingevulde waarde goed is.
  --
  IF :new.cnt_kenmerkcontract_waarde <> :old.cnt_kenmerkcontract_waarde
  OR :old.cnt_kenmerkcontract_waarde IS NULL
  OR :new.cnt_kenmerkcontract_waarde IS NULL
  THEN
    DECLARE
      SrtKenmerkOmschr      cnt_srtkenmerk.cnt_srtkenmerk_omschrijving%TYPE;
      SrtKenmerkType        cnt_srtkenmerk.cnt_srtkenmerk_kenmerktype%TYPE;
      SrtKenmerkLengte      cnt_srtkenmerk.cnt_srtkenmerk_lengte%TYPE;
      SrtKenmerkDec         cnt_srtkenmerk.cnt_srtkenmerk_dec%TYPE;
      SrtKenmerkNMin        cnt_srtkenmerk.cnt_srtkenmerk_nmin%TYPE;
      SrtKenmerkNMax        cnt_srtkenmerk.cnt_srtkenmerk_nmax%TYPE;
      KenmerkVerplicht      cnt_kenmerk.cnt_kenmerk_verplicht%TYPE;
      NumberWaarde          NUMBER(25,5);
      FormatMask            VARCHAR2(60);
      KenmerkContractWaarde  cnt_kenmerkcontract.cnt_kenmerkcontract_waarde%TYPE;
    BEGIN
      :new.cnt_kenmerkcontract_wijzig := SYSDATE;
      KenmerkContractWaarde := :new.cnt_kenmerkcontract_waarde;
      --
      SELECT CNT_SK.cnt_srtkenmerk_omschrijving
      ,      CNT_SK.cnt_srtkenmerk_kenmerktype
      ,      CNT_SK.cnt_srtkenmerk_lengte
      ,      CNT_SK.cnt_srtkenmerk_dec
      ,      CNT_SK.cnt_srtkenmerk_nmin
      ,      CNT_SK.cnt_srtkenmerk_nmax
      ,      CNT_K.cnt_kenmerk_verplicht
      INTO   SrtKenmerkOmschr
      ,      SrtKenmerkType
      ,      SrtKenmerkLengte
      ,      SrtKenmerkDec
      ,      SrtKenmerkNMin
      ,      SrtKenmerkNMax
      ,      KenmerkVerplicht
      FROM   cnt_srtkenmerk CNT_SK
      ,      cnt_kenmerk    CNT_K
      WHERE  CNT_SK.cnt_srtkenmerk_key = CNT_K.cnt_srtkenmerk_key
      AND    CNT_K.cnt_kenmerk_key     = :new.cnt_kenmerk_key;
      --
      -- Controleer eerst of het verplichte veld ingevuld is.
      --
      IF KenmerkVerplicht IS NOT NULL AND KenmerkContractWaarde IS NULL
      THEN
        APPLICATION_ERROR_GOTO(-20004, 'cnt_m240,'||SrtKenmerkOmschr);
        --
        -- Kijk welk KenmerkType gebruikt wordt.
        -- Aan de hand daarvan moet een controle uitgevoerd worden.
        --
      ELSIF SrtKenmerkType = 'C'
      THEN
        -- CHAR
        -- Controleer of de lengte te groot of goed is.
        IF LENGTH(KenmerkContractWaarde) > SrtKenmerkLengte
        THEN
          APPLICATION_ERROR_GOTO(-20004, 'cnt_m241,'||SrtKenmerkOmschr||','||TO_CHAR(SrtKenmerkLengte));
        END IF;
      ELSIF SrtKenmerkType IN ('N', 'B')
      THEN
        -- NUMBER
        -- Controleer of er een numerieke waarde ingevuld is.
        IF SrtKenmerkDec IS NULL OR SrtKenmerkDec = 0
        THEN
          FormatMask := RPAD('9', SrtKenmerkLengte, '9');
        ELSE
          FormatMask := RPAD('9', SrtKenmerkLengte - SrtKenmerkDec, '9')||
                             'D'||RPAD('9', SrtKenmerkDec, '9');
        END IF;
        --
        BEGIN
          NumberWaarde := TO_NUMBER(KenmerkContractWaarde);
        EXCEPTION
          WHEN OTHERS
          THEN FormatMask := REPLACE(FormatMask, '9', '#');
               FormatMask := REPLACE(FormatMask, '0', '#');
               FormatMask := REPLACE(FormatMask, 'D', '.');
               APPLICATION_ERROR_GOTO(-20004,'cnt_m242,'||SrtKenmerkOmschr||','||FormatMask);
        END;
        -- Controleer of de waarde binnen NMin en NMax ligt.
        IF (SrtKenmerkNMin IS NOT NULL AND NumberWaarde < SrtKenmerkNMin)
        OR (SrtKenmerkNMax IS NOT NULL AND NumberWaarde > SrtKenmerkNMax)
        THEN
          APPLICATION_ERROR_GOTO(-20004, 'cnt_m243,'||SrtKenmerkOmschr||','||
                                                TO_CHAR(SrtKenmerkNMin)||','||
                                                TO_CHAR(SrtKenmerkNMax));
        ELSE
          --  Controleer of de waarde aan het formaat voldoet.
          KenmerkContractWaarde := LTRIM(NUMBER_TO_CHAR(NumberWaarde, FormatMask));
          IF INSTR2(KenmerkContractWaarde, '#') <> 0
          THEN
            FormatMask := REPLACE(FormatMask, '9', '#');
            FormatMask := REPLACE(FormatMask, '0', '#');
            FormatMask := REPLACE(FormatMask, 'D', '.');
            APPLICATION_ERROR_GOTO(-20004,'cnt_m242,'||SrtKenmerkOmschr||','||FormatMask);
          END IF;
        END IF;
        -- Zet de geconverteerde waarde terug.
        :new.cnt_kenmerkcontract_waarde := KenmerkContractWaarde;
      ELSIF SrtKenmerkType = 'D'
      THEN
        -- DATUM
        KenmerkContractWaarde := DATE_TO_CHAR(_TO_DATE2(KenmerkContractWaarde,'DD-MM-YY'),'DD-MM-YYYY');
        -- Zet de geconverteerde waarde terug.
        :new.cnt_kenmerkcontract_waarde := KenmerkContractWaarde;
      END IF;
    END;
    UPDATE_PRIMARY_KEY(cnt_kenmerkcontract_key, cnt_s_cnt_kenmerkcontract_key);
  END IF;
END;
/

CREATE_TRIGGER(cnt_t_cnt_termijn_B_IU)
BEFORE INSERT OR UPDATE ON cnt_termijn
FOR EACH ROW
BEGIN
  UPDATE_PRIMARY_KEY(cnt_termijn_key, cnt_s_cnt_termijn_key);
END;
/

CREATE_TRIGGER(mld_t_cnt_contract_note_b_iu)
BEFORE INSERT OR UPDATE ON cnt_contract_note
FOR EACH ROW
BEGIN
  UPDATE_PRIMARY_KEY(cnt_contract_note_key, cnt_s_cnt_contract_note_key);
END;
/
CREATE OR REPLACE TRIGGER cnt_t_cnt_factuurschema_B_IU
BEFORE INSERT OR UPDATE ON cnt_factuurschema
FOR EACH ROW
BEGIN
  UPDATE_PRIMARY_KEY(cnt_factuurschema_key, cnt_s_cnt_factuurschema_key);
END;
/

REGISTERRUN('$Id$')

#endif  // CNT
