CREATE OR REPLACE TRIGGER GERA_LOG_CARDAMES
BEFORE INSERT OR UPDATE OR DELETE ON CARDAMES FOR EACH ROW
DECLARE
    V_DSREMOTEADDR VARCHAR2(15);
    V_DSREMOTEHOST VARCHAR2(15);
    V_DSSERVERRDM  VARCHAR2(20);
    V_DSSERVMETHOD VARCHAR2(30);
    V_DSREMOTEOPER VARCHAR2(12);
    V_IDOPERACAO     VARCHAR2(1);

    V_CDFILIAL VARCHAR2(4);
    V_CDSERVFILI VARCHAR2(5);
    V_DTMESCARD DATE;
    V_DTREFECUSTMESANT DATE;
    V_DTREFECUSTMESATU DATE;
    V_DTREFECUSTRECANT DATE;
    V_DTREFECUSTRECATU DATE;
    V_IDCARDLIBCENTANT VARCHAR2(1);
    V_IDCARDLIBCENTATU VARCHAR2(1);
    V_IDCARDLIBAGREANT VARCHAR2(1);
    V_IDCARDLIBAGREATU VARCHAR2(1);
    V_CDCARDCICLOANT VARCHAR2(5);
    V_CDCARDCICLOATU VARCHAR2(5);
    V_IDVALIDADOANT VARCHAR2(1);
    V_IDVALIDADOATU VARCHAR2(1);
    V_DTVALIDACAOANT DATE;
    V_DTVALIDACAOATU DATE;
    V_CDOPERVALIDACAOANT VARCHAR2(12);
    V_CDOPERVALIDACAOATU VARCHAR2(12);
BEGIN
    WEBTUNNING(V_DSREMOTEADDR,
            V_DSREMOTEHOST,
            V_DSSERVERRDM,
            V_DSSERVMETHOD,
            V_DSREMOTEOPER);

    IF INSERTING THEN
        V_IDOPERACAO := 'I';
        V_CDFILIAL := :NEW.CDFILIAL;
        V_CDSERVFILI := :NEW.CDSERVFILI;
        V_DTMESCARD := :NEW.DTMESCARDAPIO;
    ELSIF UPDATING THEN
        V_IDOPERACAO := 'U';
        V_CDFILIAL := :OLD.CDFILIAL;
        V_CDSERVFILI := :OLD.CDSERVFILI;
        V_DTMESCARD := :OLD.DTMESCARDAPIO;
    ELSE
        V_IDOPERACAO := 'D';
        V_CDFILIAL := :OLD.CDFILIAL;
        V_CDSERVFILI := :OLD.CDSERVFILI;
        V_DTMESCARD := :OLD.DTMESCARDAPIO;
    END IF;

    V_DTREFECUSTMESANT := :OLD.DTREFECUSTMES;
    V_DTREFECUSTMESATU := :NEW.DTREFECUSTMES;
    V_DTREFECUSTRECANT := :OLD.DTREFECUSTREC;
    V_DTREFECUSTRECATU := :NEW.DTREFECUSTREC;
    V_IDCARDLIBCENTANT := :OLD.IDCARDLIBCENT;
    V_IDCARDLIBCENTATU := :NEW.IDCARDLIBCENT;
    V_IDCARDLIBAGREANT := :OLD.IDCARDLIBAGRE;
    V_IDCARDLIBAGREATU := :NEW.IDCARDLIBAGRE;
    V_CDCARDCICLOANT := :OLD.CDCARDCICLO;
    V_CDCARDCICLOATU := :NEW.CDCARDCICLO;
    V_IDVALIDADOANT := :OLD.IDVALIDADO;
    V_IDVALIDADOATU := :NEW.IDVALIDADO;
    V_DTVALIDACAOANT := :OLD.DTVALIDACAO;
    V_DTVALIDACAOATU := :NEW.DTVALIDACAO;
    V_CDOPERVALIDACAOANT := :OLD.CDOPERVALIDACAO;
    V_CDOPERVALIDACAOATU := :NEW.CDOPERVALIDACAO;

    IF UPDATING AND
    (((V_DTREFECUSTMESANT = V_DTREFECUSTMESATU) OR
     (V_DTREFECUSTMESANT IS NULL AND V_DTREFECUSTMESATU IS NULL)) AND
     ((V_DTREFECUSTRECANT = V_DTREFECUSTRECATU) OR
     (V_DTREFECUSTRECANT IS NULL AND V_DTREFECUSTRECATU IS NULL)) AND
     ((V_IDCARDLIBCENTANT = V_IDCARDLIBCENTATU) OR
     (V_IDCARDLIBCENTANT IS NULL AND V_IDCARDLIBCENTATU IS NULL)) AND
     ((V_IDCARDLIBAGREANT = V_IDCARDLIBAGREATU) OR
     (V_IDCARDLIBAGREANT IS NULL AND V_IDCARDLIBAGREATU IS NULL)) AND
     ((V_CDCARDCICLOANT = V_CDCARDCICLOATU) OR
     (V_CDCARDCICLOANT IS NULL AND V_CDCARDCICLOATU IS NULL)) AND
     ((V_IDVALIDADOANT = V_IDVALIDADOATU) OR
     (V_IDVALIDADOANT IS NULL AND V_IDVALIDADOATU IS NULL)) AND
     ((V_DTVALIDACAOANT = V_DTVALIDACAOATU) OR
     (V_DTVALIDACAOANT IS NULL AND V_DTVALIDACAOATU IS NULL)) AND
     ((V_CDOPERVALIDACAOANT = V_CDOPERVALIDACAOATU) OR
     (V_CDOPERVALIDACAOANT IS NULL AND V_CDOPERVALIDACAOATU IS NULL))
    ) THEN
        RETURN;
    END IF;

    INSERT INTO HISTLOGCARDAMES
    (CDFILIAL, CDSERVFILI, DTMESCARD, CDOPERADOR, DTALTERACAO, IDOPERACAO,
    DTREFECUSTMESANT, DTREFECUSTMESATU, DTREFECUSTRECANT, DTREFECUSTRECATU,
    IDCARDLIBCENTANT, IDCARDLIBCENTATU, IDCARDLIBAGREANT, IDCARDLIBAGREATU,
    CDCARDCICLOANT, CDCARDCICLOATU, IDVALIDADOANT, IDVALIDADOATU,
    DTVALIDACAOANT, DTVALIDACAOATU, CDOPERVALIDACAOANT, CDOPERVALIDACAOATU,
    DSRDMUSU, DSENDUSU, DSNMUSU, DSMETODO,
    DTINCLUSAO, DTULTATU,
    CDOPERINCLUSAO, CDOPERULTATU)
    VALUES
    (V_CDFILIAL, V_CDSERVFILI, V_DTMESCARD, V_DSREMOTEOPER, SYSDATE, V_IDOPERACAO,
    V_DTREFECUSTMESANT, V_DTREFECUSTMESATU, V_DTREFECUSTRECANT, V_DTREFECUSTRECATU,
    V_IDCARDLIBCENTANT, V_IDCARDLIBCENTATU, V_IDCARDLIBAGREANT, V_IDCARDLIBAGREATU,
    V_CDCARDCICLOANT, V_CDCARDCICLOATU, V_IDVALIDADOANT, V_IDVALIDADOATU,
    V_DTVALIDACAOANT, V_DTVALIDACAOATU, V_CDOPERVALIDACAOANT, V_CDOPERVALIDACAOATU,
    V_DSSERVERRDM, V_DSREMOTEADDR, V_DSREMOTEHOST, V_DSSERVMETHOD,
    SYSDATE, SYSDATE,
    V_DSREMOTEOPER, V_DSREMOTEOPER);
END;
/

