create or replace TRIGGER OU_REF.CURRENCY_H
BEFORE INSERT OR UPDATE OR DELETE ON OU_REF.CURRENCY
REFERENCING NEW AS new OLD AS old
FOR EACH ROW
DECLARE
  v_current_date DATE := SYSDATE;
  v_future_date DATE  := TO_DATE('31/12/9999', 'DD/MM/YYYY');
BEGIN
  IF INSERTING THEN
    INSERT INTO DW_REF_MAIN.CURRENCY(CURRENCY_CODE, CURRENCY_NAME, COUNTRY_CODE, EUR_LEGACY, LEGACY, A_VALID_FROM, A_VALID_TO, A_KEY)
    VALUES (:new.CURRENCY_CODE, :new.CURRENCY_NAME, :new.COUNTRY_CODE, :new.EUR_LEGACY, :new.LEGACY, v_current_date, v_future_date, NULL);
    INSERT INTO REFERENCE.T_CURRENCY(CURRENCY_CODE, CURRENCY_NAME, COUNTRY_CODE, EUR_LEGACY, LEGACY, A_VALID_FROM, A_VALID_TO, A_KEY, A_MOPDB_LOAD_SET_FK)
    VALUES (:new.CURRENCY_CODE, :new.CURRENCY_NAME, :new.COUNTRY_CODE, :new.EUR_LEGACY, :new.LEGACY, v_current_date, v_future_date, NULL, -1);
  ELSIF DELETING THEN
    UPDATE DW_REF_MAIN.CURRENCY SET
      A_VALID_TO = v_current_date
    WHERE  
        1 = 1 
    AND DECODE(CURRENCY_CODE, :old.CURRENCY_CODE, 1, 0) = 1
    AND DECODE(CURRENCY_NAME, :old.CURRENCY_NAME, 1, 0) = 1
    AND DECODE(COUNTRY_CODE, :old.COUNTRY_CODE, 1, 0) = 1
    AND DECODE(EUR_LEGACY, :old.EUR_LEGACY, 1, 0) = 1
    AND DECODE(LEGACY, :old.LEGACY, 1, 0) = 1
    AND A_VALID_TO = v_future_date
  ;
    UPDATE REFERENCE.T_CURRENCY SET
      A_VALID_TO = v_current_date
    WHERE  
        1 = 1 
    AND DECODE(CURRENCY_CODE, :old.CURRENCY_CODE, 1, 0) = 1
    AND DECODE(CURRENCY_NAME, :old.CURRENCY_NAME, 1, 0) = 1
    AND DECODE(COUNTRY_CODE, :old.COUNTRY_CODE, 1, 0) = 1
    AND DECODE(EUR_LEGACY, :old.EUR_LEGACY, 1, 0) = 1
    AND DECODE(LEGACY, :old.LEGACY, 1, 0) = 1
    AND A_VALID_TO = v_future_date
  ;
  ELSIF UPDATING THEN
    UPDATE DW_REF_MAIN.CURRENCY SET
      A_VALID_TO = v_current_date
    WHERE  
        1 = 1 
    AND DECODE(CURRENCY_CODE, :old.CURRENCY_CODE, 1, 0) = 1
    AND DECODE(CURRENCY_NAME, :old.CURRENCY_NAME, 1, 0) = 1
    AND DECODE(COUNTRY_CODE, :old.COUNTRY_CODE, 1, 0) = 1
    AND DECODE(EUR_LEGACY, :old.EUR_LEGACY, 1, 0) = 1
    AND DECODE(LEGACY, :old.LEGACY, 1, 0) = 1
    AND A_VALID_TO = v_future_date
  ;
    UPDATE REFERENCE.T_CURRENCY SET
      A_VALID_TO = v_current_date
    WHERE  
        1 = 1 
    AND DECODE(CURRENCY_CODE, :old.CURRENCY_CODE, 1, 0) = 1
    AND DECODE(CURRENCY_NAME, :old.CURRENCY_NAME, 1, 0) = 1
    AND DECODE(COUNTRY_CODE, :old.COUNTRY_CODE, 1, 0) = 1
    AND DECODE(EUR_LEGACY, :old.EUR_LEGACY, 1, 0) = 1
    AND DECODE(LEGACY, :old.LEGACY, 1, 0) = 1
    AND A_VALID_TO = v_future_date
  ;
    INSERT INTO DW_REF_MAIN.CURRENCY(CURRENCY_CODE, CURRENCY_NAME, COUNTRY_CODE, EUR_LEGACY, LEGACY, A_VALID_FROM, A_VALID_TO, A_KEY)
    VALUES (:new.CURRENCY_CODE, :new.CURRENCY_NAME, :new.COUNTRY_CODE, :new.EUR_LEGACY, :new.LEGACY, v_current_date, v_future_date, NULL);
    INSERT INTO REFERENCE.T_CURRENCY(CURRENCY_CODE, CURRENCY_NAME, COUNTRY_CODE, EUR_LEGACY, LEGACY, A_VALID_FROM, A_VALID_TO, A_KEY, A_MOPDB_LOAD_SET_FK)
    VALUES (:new.CURRENCY_CODE, :new.CURRENCY_NAME, :new.COUNTRY_CODE, :new.EUR_LEGACY, :new.LEGACY, v_current_date, v_future_date, NULL, -1);
  END IF;
END;
/