CREATE OR REPLACE TRIGGER GERA_LOG_CNFCAMES
BEFORE INSERT OR UPDATE OR DELETE ON CNFCAMES 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_NRSEQCNFMES NUMBER(3);
    V_CDTIPOPRAT VARCHAR2(2);

    V_IDOPCCNFMESANT VARCHAR2(1);  
    V_IDOPCCNFMESATU VARCHAR2(1);  
    V_IDIMPCNFMESANT VARCHAR2(1);
    V_IDIMPCNFMESATU VARCHAR2(1);
    V_IDCONCNFMESANT CHAR(1);
    V_IDCONCNFMESATU CHAR(1);
    V_IDRESERVAMYMENUANT VARCHAR2(1);
    V_IDRESERVAMYMENUATU VARCHAR2(1);
    V_NRORDCNFMESANT NUMBER(15);
    V_NRORDCNFMESATU NUMBER(15);
    V_DSCOMPLTPMESANT VARCHAR2(15);
    V_DSCOMPLTPMESATU VARCHAR2(15);
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.DTREFECNFMES;
        V_NRSEQCNFMES := :NEW.NRSEQCNFMES;
        V_CDTIPOPRAT := :NEW.CDTIPOPRAT;
    ELSIF UPDATING THEN
        V_IDOPERACAO := 'U';
        V_CDFILIAL := :OLD.CDFILIAL;
        V_CDSERVFILI := :OLD.CDSERVFILI;
        V_DTMESCARD := :OLD.DTREFECNFMES;
        V_NRSEQCNFMES := :OLD.NRSEQCNFMES;
        V_CDTIPOPRAT := :OLD.CDTIPOPRAT;
    ELSE
        V_IDOPERACAO := 'D';
        V_CDFILIAL := :OLD.CDFILIAL;
        V_CDSERVFILI := :OLD.CDSERVFILI;
        V_DTMESCARD := :OLD.DTREFECNFMES;
        V_NRSEQCNFMES := :OLD.NRSEQCNFMES;
        V_CDTIPOPRAT := :OLD.CDTIPOPRAT;
    END IF;

    V_IDOPCCNFMESANT := :OLD.IDOPCCNFMES;
    V_IDOPCCNFMESATU := :NEW.IDOPCCNFMES;
    V_IDIMPCNFMESANT := :OLD.IDIMPCNFMES;
    V_IDIMPCNFMESATU := :NEW.IDIMPCNFMES;
    V_IDCONCNFMESANT := :OLD.IDCONCNFMES;
    V_IDCONCNFMESATU := :NEW.IDCONCNFMES;
    V_IDRESERVAMYMENUANT := :OLD.IDRESERVAMYMENU;
    V_IDRESERVAMYMENUATU := :NEW.IDRESERVAMYMENU;
    V_NRORDCNFMESANT := :OLD.NRORDCNFMES;
    V_NRORDCNFMESATU := :NEW.NRORDCNFMES;
    V_DSCOMPLTPMESANT := :OLD.DSCOMPLTPMES;
    V_DSCOMPLTPMESATU := :NEW.DSCOMPLTPMES;

    IF UPDATING AND
    (((V_IDOPCCNFMESANT = V_IDOPCCNFMESATU) OR
     (V_IDOPCCNFMESANT IS NULL AND V_IDOPCCNFMESATU IS NULL)) AND
     ((V_IDIMPCNFMESANT = V_IDIMPCNFMESATU) OR
     (V_IDIMPCNFMESANT IS NULL AND V_IDIMPCNFMESATU IS NULL)) AND
     ((V_IDCONCNFMESANT = V_IDCONCNFMESATU) OR
     (V_IDCONCNFMESANT IS NULL AND V_IDCONCNFMESATU IS NULL)) AND
     ((V_IDRESERVAMYMENUANT = V_IDRESERVAMYMENUATU) OR
     (V_IDRESERVAMYMENUANT IS NULL AND V_IDRESERVAMYMENUATU IS NULL)) AND
     ((V_NRORDCNFMESANT = V_NRORDCNFMESATU) OR
     (V_NRORDCNFMESANT IS NULL AND V_NRORDCNFMESATU IS NULL)) AND
     ((V_DSCOMPLTPMESANT = V_DSCOMPLTPMESATU) OR
     (V_DSCOMPLTPMESANT IS NULL AND V_DSCOMPLTPMESATU IS NULL))
    ) THEN
        RETURN;
    END IF;

    INSERT INTO HISTLOGCNFCAMES
    (CDFILIAL, CDSERVFILI, DTMESCARD, NRSEQCNFMES, CDTIPOPRAT, CDOPERADOR, DTALTERACAO, IDOPERACAO,
    IDOPCCNFMESANT, IDOPCCNFMESATU, IDIMPCNFMESANT, IDIMPCNFMESATU,
    IDCONCNFMESANT, IDCONCNFMESATU, IDRESERVAMYMENUANT, IDRESERVAMYMENUATU,
    NRORDCNFMESANT, NRORDCNFMESATU, DSCOMPLTPMESANT, DSCOMPLTPMESATU,
    DSRDMUSU, DSENDUSU, DSNMUSU, DSMETODO,
    DTINCLUSAO, DTULTATU,
    CDOPERINCLUSAO, CDOPERULTATU)
    VALUES
    (V_CDFILIAL, V_CDSERVFILI, V_DTMESCARD, V_NRSEQCNFMES, V_CDTIPOPRAT, V_DSREMOTEOPER, SYSDATE, V_IDOPERACAO,
    V_IDOPCCNFMESANT, V_IDOPCCNFMESATU, V_IDIMPCNFMESANT, V_IDIMPCNFMESATU,
    V_IDCONCNFMESANT, V_IDCONCNFMESATU, V_IDRESERVAMYMENUANT, V_IDRESERVAMYMENUATU,
    V_NRORDCNFMESANT, V_NRORDCNFMESATU, V_DSCOMPLTPMESANT, V_DSCOMPLTPMESATU,
    V_DSSERVERRDM, V_DSREMOTEADDR, V_DSREMOTEHOST, V_DSSERVMETHOD,
    SYSDATE, SYSDATE,
    V_DSREMOTEOPER, V_DSREMOTEOPER);
END;
/


