CREATE OR REPLACE TRIGGER GERA_LOG_CARDADIA
BEFORE INSERT OR UPDATE OR DELETE ON CARDADIA 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_DTCARDAPIO DATE;
    V_NRCARDAPIO VARCHAR2(10);

    V_QTCOMESTICARANT FLOAT;
    V_QTCOMESTICARATU FLOAT;
    V_QTCLIESTICARANT FLOAT;
    V_QTCLIESTICARATU FLOAT;
    V_QTFUNESTICARANT FLOAT;
    V_QTFUNESTICARATU FLOAT;
    V_QTDOAESTICARANT FLOAT;
    V_QTDOAESTICARATU FLOAT;
    V_QTCOMEFETCARANT FLOAT;
    V_QTCOMEFETCARATU FLOAT;
    V_QTCLIEFETCARANT FLOAT;
    V_QTCLIEFETCARATU FLOAT;
    V_QTFUNEFETCARANT FLOAT;
    V_QTFUNEFETCARATU FLOAT;
    V_QTDOAEFETCARANT FLOAT;
    V_QTDOAEFETCARATU FLOAT;
    V_QTESTITURNO1ANT FLOAT;
    V_QTESTITURNO1ATU FLOAT;
    V_QTESTITURNO2ANT FLOAT;
    V_QTESTITURNO2ATU FLOAT;
    V_QTESTITURNO3ANT FLOAT;
    V_QTESTITURNO3ATU FLOAT;
    V_QTESTITURNO4ANT FLOAT;
    V_QTESTITURNO4ATU FLOAT;
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.DTMESCARD;
        V_DTCARDAPIO := :NEW.DTCARDAPIO;
        V_NRCARDAPIO := :NEW.NRCARDAPIO;
    ELSIF UPDATING THEN
        V_IDOPERACAO := 'U';
        V_CDFILIAL := :OLD.CDFILIAL;
        V_CDSERVFILI := :OLD.CDSERVFILI;
        V_DTMESCARD := :OLD.DTMESCARD;
        V_DTCARDAPIO := :OLD.DTCARDAPIO;
        V_NRCARDAPIO := :OLD.NRCARDAPIO;
    ELSE
        V_IDOPERACAO := 'D';
        V_CDFILIAL := :OLD.CDFILIAL;
        V_CDSERVFILI := :OLD.CDSERVFILI;
        V_DTMESCARD := :OLD.DTMESCARD;
        V_DTCARDAPIO := :OLD.DTCARDAPIO;
        V_NRCARDAPIO := :OLD.NRCARDAPIO;
    END IF;

    V_QTCOMESTICARANT := :OLD.QTCOMESTICAR;
    V_QTCOMESTICARATU := :NEW.QTCOMESTICAR;
    V_QTCLIESTICARANT := :OLD.QTCLIESTICAR;
    V_QTCLIESTICARATU := :NEW.QTCLIESTICAR;
    V_QTFUNESTICARANT := :OLD.QTFUNESTICAR;
    V_QTFUNESTICARATU := :NEW.QTFUNESTICAR;
    V_QTDOAESTICARANT := :OLD.QTDOAESTICAR;
    V_QTDOAESTICARATU := :NEW.QTDOAESTICAR;
    V_QTCOMEFETCARANT := :OLD.QTCOMEFETCAR;
    V_QTCOMEFETCARATU := :NEW.QTCOMEFETCAR;
    V_QTCLIEFETCARANT := :OLD.QTCLIEFETCAR;
    V_QTCLIEFETCARATU := :NEW.QTCLIEFETCAR;
    V_QTFUNEFETCARANT := :OLD.QTFUNEFETCAR;
    V_QTFUNEFETCARATU := :NEW.QTFUNEFETCAR;
    V_QTDOAEFETCARANT := :OLD.QTDOAEFETCAR;
    V_QTDOAEFETCARATU := :NEW.QTDOAEFETCAR;
    V_QTESTITURNO1ANT := :OLD.QTESTITURNO1;
    V_QTESTITURNO1ATU := :NEW.QTESTITURNO1;
    V_QTESTITURNO2ANT := :OLD.QTESTITURNO2;
    V_QTESTITURNO2ATU := :NEW.QTESTITURNO2;
    V_QTESTITURNO3ANT := :OLD.QTESTITURNO3;
    V_QTESTITURNO3ATU := :NEW.QTESTITURNO3;
    V_QTESTITURNO4ANT := :OLD.QTESTITURNO4;
    V_QTESTITURNO4ATU := :NEW.QTESTITURNO4;
    

    IF UPDATING AND
    (((V_QTCOMESTICARANT = V_QTCOMESTICARATU) OR
     (V_QTCOMESTICARANT IS NULL AND V_QTCOMESTICARATU IS NULL)) AND
     ((V_QTCLIESTICARANT = V_QTCLIESTICARATU) OR
     (V_QTCLIESTICARANT IS NULL AND V_QTCLIESTICARATU IS NULL)) AND
     ((V_QTFUNESTICARANT = V_QTFUNESTICARATU) OR
     (V_QTFUNESTICARANT IS NULL AND V_QTFUNESTICARATU IS NULL)) AND
     ((V_QTDOAESTICARANT = V_QTDOAESTICARATU) OR
     (V_QTDOAESTICARANT IS NULL AND V_QTDOAESTICARATU IS NULL)) AND
     ((V_QTCOMEFETCARANT = V_QTCOMEFETCARATU) OR
     (V_QTCOMEFETCARANT IS NULL AND V_QTCOMEFETCARATU IS NULL)) AND
     ((V_QTCLIEFETCARANT = V_QTCLIEFETCARATU) OR
     (V_QTCLIEFETCARANT IS NULL AND V_QTCLIEFETCARATU IS NULL)) AND
     ((V_QTFUNEFETCARANT = V_QTFUNEFETCARATU) OR
     (V_QTFUNEFETCARANT IS NULL AND V_QTFUNEFETCARATU IS NULL)) AND
     ((V_QTDOAEFETCARANT = V_QTDOAEFETCARATU) OR
     (V_QTDOAEFETCARANT IS NULL AND V_QTDOAEFETCARATU IS NULL)) AND
     ((V_QTESTITURNO1ANT = V_QTESTITURNO1ATU) OR
     (V_QTESTITURNO1ANT IS NULL AND V_QTESTITURNO1ATU IS NULL)) AND
     ((V_QTESTITURNO2ANT = V_QTESTITURNO2ATU) OR
     (V_QTESTITURNO2ANT IS NULL AND V_QTESTITURNO2ATU IS NULL)) AND
     ((V_QTESTITURNO3ANT = V_QTESTITURNO3ATU) OR
     (V_QTESTITURNO3ANT IS NULL AND V_QTESTITURNO3ATU IS NULL)) AND
     ((V_QTESTITURNO4ANT = V_QTESTITURNO4ATU) OR
     (V_QTESTITURNO4ANT IS NULL AND V_QTESTITURNO4ATU IS NULL))
    ) THEN
        RETURN;
    END IF;
    
    INSERT INTO HISTLOGCARDADIA
    (CDFILIAL, CDSERVFILI, DTMESCARD, NRCARDAPIO, DTCARDAPIO, CDOPERADOR, DTALTERACAO, IDOPERACAO,
    QTCOMESTICARANT, QTCOMESTICARATU, QTCLIESTICARANT, QTCLIESTICARATU,
    QTFUNESTICARANT, QTFUNESTICARATU, QTDOAESTICARANT, QTDOAESTICARATU,
    QTCOMEFETCARANT, QTCOMEFETCARATU, QTCLIEFETCARANT, QTCLIEFETCARATU, 
    QTFUNEFETCARANT, QTFUNEFETCARATU, QTDOAEFETCARANT, QTDOAEFETCARATU,
    QTESTITURNO1ANT, QTESTITURNO1ATU, QTESTITURNO2ANT, QTESTITURNO2ATU,
    QTESTITURNO3ANT, QTESTITURNO3ATU, QTESTITURNO4ANT, QTESTITURNO4ATU,
    DSRDMUSU, DSENDUSU, DSNMUSU, DSMETODO,
    DTINCLUSAO, DTULTATU,
    CDOPERINCLUSAO, CDOPERULTATU)
    VALUES
    (V_CDFILIAL, V_CDSERVFILI, V_DTMESCARD, V_NRCARDAPIO, V_DTCARDAPIO, V_DSREMOTEOPER, SYSDATE, V_IDOPERACAO,
    V_QTCOMESTICARANT, V_QTCOMESTICARATU, V_QTCLIESTICARANT, V_QTCLIESTICARATU,
    V_QTFUNESTICARANT, V_QTFUNESTICARATU, V_QTDOAESTICARANT, V_QTDOAESTICARATU,
    V_QTCOMEFETCARANT, V_QTCOMEFETCARATU, V_QTCLIEFETCARANT, V_QTCLIEFETCARATU, 
    V_QTFUNEFETCARANT, V_QTFUNEFETCARATU, V_QTDOAEFETCARANT, V_QTDOAEFETCARATU,
    V_QTESTITURNO1ANT, V_QTESTITURNO1ATU, V_QTESTITURNO2ANT, V_QTESTITURNO2ATU,
    V_QTESTITURNO3ANT, V_QTESTITURNO3ATU, V_QTESTITURNO4ANT, V_QTESTITURNO4ATU,
    V_DSSERVERRDM, V_DSREMOTEADDR, V_DSREMOTEHOST, V_DSSERVMETHOD,
    SYSDATE, SYSDATE,
    V_DSREMOTEOPER, V_DSREMOTEOPER);
END;
/

