#ifdef  BGT
/*
 * $Revision$
 * $Id$
 */


AUDIT_BEGIN(bgt_disc_params)
AUDIT_VALUE(bgt_disc_params, bgt_ins_discipline_key)
AUDIT_END()

CREATE_TRIGGER(bgt_t_bgt_disc_params_b_i)
BEFORE INSERT ON bgt_disc_params
FOR EACH ROW
BEGIN
   UPDATE_PRIMARY_KEY(bgt_disc_params_key, bgt_s_bgt_disc_params_key);
END;
/

CREATE_TRIGGER(bgt_t_bgt_project_B_I)
BEFORE INSERT ON bgt_project
FOR EACH ROW
BEGIN
    UPDATE_PRIMARY_KEY(bgt_project_key,bgt_s_bgt_project_key);
END;
/

CREATE_TRIGGER(bgt_t_bgt_kostenrubriek_B_I)
BEFORE INSERT ON bgt_kostenrubriek
FOR EACH ROW
BEGIN
    UPDATE_PRIMARY_KEY(bgt_kostenrubriek_key,bgt_s_bgt_kostenrubriek_key);
END;
/

CREATE_TRIGGER(bgt_t_bgt_budget_B_I)
BEFORE INSERT ON bgt_budget
FOR EACH ROW
BEGIN
    UPDATE_PRIMARY_KEY(bgt_budget_key,bgt_s_bgt_budget_key);
END;
/

CREATE_TRIGGER(bgt_t_bgt_budgetmutatie_B_I)
BEFORE INSERT ON bgt_budgetmutatie
FOR EACH ROW
BEGIN
    UPDATE_PRIMARY_KEY(bgt_budgetmutatie_key,bgt_s_bgt_budgetmutatie_key);
END;
/

CREATE_TRIGGER(bgt_t_bgt_budgetmutatie_B_IU)
BEFORE INSERT OR UPDATE ON bgt_budgetmutatie
FOR EACH ROW
BEGIN
    -- Bij insert moet het nieuwe mutatiebedrag bij het budgetbedrag worden opgeteld.
    -- Bij update moet het VERSCHIL tussen het oude- en nieuwe mutatiebedrag bij het budgetbedrag worden opgeteld.
    IF (:new.bgt_budget_key_van IS NOT NULL) THEN
        UPDATE bgt_budget
           SET bgt_budget_bedrag    = NVL(bgt_budget_bedrag, 0)    + (NVL(:new.bgt_budget_bedrag_van, 0) - NVL(:old.bgt_budget_bedrag_van, 0))
             , bgt_budget_btwbedrag = NVL(bgt_budget_btwbedrag, 0) + (NVL(:new.bgt_budget_btwbedrag_van, 0) - NVL(:old.bgt_budget_btwbedrag_van, 0))
         WHERE bgt_budget_key = :new.bgt_budget_key_van;
    END IF;
    IF (:new.bgt_budget_key_naar IS NOT NULL) THEN
        UPDATE bgt_budget
           SET bgt_budget_bedrag =    NVL(bgt_budget_bedrag, 0)    + (NVL(:new.bgt_budget_bedrag_naar, 0) - NVL(:old.bgt_budget_bedrag_naar, 0))
             , bgt_budget_btwbedrag = NVL(bgt_budget_btwbedrag, 0) + (NVL(:new.bgt_budget_btwbedrag_naar, 0) - NVL(:old.bgt_budget_btwbedrag_naar, 0))
         WHERE bgt_budget_key = :new.bgt_budget_key_naar;
    END IF;
END;
/

CREATE_TRIGGER(bgt_t_bgt_budgetmutatie_B_D)
BEFORE DELETE ON bgt_budgetmutatie
FOR EACH ROW
BEGIN
    IF (:old.bgt_budget_key_van IS NOT NULL) THEN
        UPDATE bgt_budget
           SET bgt_budget_bedrag    = NVL(bgt_budget_bedrag, 0)    - NVL(:old.bgt_budget_bedrag_van, 0)
             , bgt_budget_btwbedrag = NVL(bgt_budget_btwbedrag, 0) - NVL(:old.bgt_budget_btwbedrag_van, 0)
         WHERE bgt_budget_key = :old.bgt_budget_key_van;
    END IF;
    IF (:old.bgt_budget_key_naar IS NOT NULL) THEN
        UPDATE bgt_budget
           SET bgt_budget_bedrag    = NVL(bgt_budget_bedrag, 0)    - NVL(:old.bgt_budget_bedrag_naar, 0)
             , bgt_budget_btwbedrag = NVL(bgt_budget_btwbedrag, 0) - NVL(:old.bgt_budget_btwbedrag_naar, 0)
         WHERE bgt_budget_key = :old.bgt_budget_key_naar;
    END IF;
END;
/

REGISTERRUN('$Id$')

#endif  // BGT
