CREATE OR REPLACE TRIGGER GERA_LOG_COMPCARD
BEFORE INSERT OR UPDATE OR DELETE ON COMPCARD FOR EACH ROW
DECLARE
  V_USUARIO      VARCHAR2(40);
  V_DSREMOTEADDR VARCHAR2(15);
  V_DSREMOTEHOST VARCHAR2(15);
  V_DSSERVERRDM  VARCHAR2(20);
  V_DSSERVMETHOD VARCHAR2(30);
  V_DSREMOTEOPER VARCHAR2(12);
  V_OPERACAO     VARCHAR2(1);
  V_CDPRATOANT   VARCHAR2(10);
  V_CDPRATOATUAL VARCHAR2(10);
  V_CDFILIAL     VARCHAR2(4);
  V_CDSERVFILI   VARCHAR2(5);
  V_CDTIPOPRAT   VARCHAR2(2);
  V_DTMESCARD    DATE;
  V_DTCARDAPIO   DATE;

  V_CDFILAUXPRATANT VARCHAR2(4);
  V_CDFILAUXPRATATU VARCHAR2(4);
BEGIN
    IF :OLD.CDPRATO <> :NEW.CDPRATO
    OR :OLD.CDFILAUXPRAT <> :NEW.CDFILAUXPRAT 
    OR :OLD.CDPRATO IS NULL AND :NEW.CDPRATO IS NOT NULL 
    OR :OLD.CDPRATO IS NOT NULL AND :NEW.CDPRATO IS NULL THEN
       NMUSUARIO(V_USUARIO);
       WEBTUNNING(V_DSREMOTEADDR,
                  V_DSREMOTEHOST,
                  V_DSSERVERRDM,
                  V_DSSERVMETHOD,
                  V_DSREMOTEOPER);
    
        BEGIN
            SELECT CDSERVFILI, DTMESCARD, DTCARDAPIO
              INTO V_CDSERVFILI, V_DTMESCARD, V_DTCARDAPIO
              FROM TMP_LOGCOMPCARD
             WHERE CDFILIAL   = NVL(:NEW.CDFILIAL, :OLD.CDFILIAL)
                AND NRCARDAPIO = NVL(:NEW.NRCARDAPIO, :OLD.NRCARDAPIO);
        EXCEPTION
            WHEN NO_DATA_FOUND THEN
                V_CDSERVFILI := NULL;
                V_DTMESCARD  := NULL;
                V_DTCARDAPIO := NULL;
        END;
    
        IF V_CDSERVFILI IS NULL THEN
            SELECT CDSERVFILI, DTMESCARD, DTCARDAPIO
              INTO V_CDSERVFILI, V_DTMESCARD, V_DTCARDAPIO
              FROM CARDADIA
             WHERE CDFILIAL   = NVL(:NEW.CDFILIAL, :OLD.CDFILIAL)
                AND NRCARDAPIO = NVL(:NEW.NRCARDAPIO, :OLD.NRCARDAPIO);
        END IF;
    
       IF INSERTING THEN
          V_OPERACAO     := 'I';
          V_CDPRATOANT   := NULL;
          V_CDFILAUXPRATANT := NULL;
          V_CDPRATOATUAL := :NEW.CDPRATO;
          V_CDFILAUXPRATATU := :NEW.CDFILAUXPRAT;
          V_CDTIPOPRAT   := :NEW.CDTIPOPRAT;
          V_CDFILIAL     := :NEW.CDFILIAL;
       ELSE
          IF UPDATING THEN
             V_OPERACAO     := 'U';
             V_CDPRATOANT   := :OLD.CDPRATO;
             V_CDFILAUXPRATANT := :OLD.CDFILAUXPRAT;
             V_CDPRATOATUAL := :NEW.CDPRATO;
             V_CDFILAUXPRATATU := :NEW.CDFILAUXPRAT;
             V_CDTIPOPRAT   := :NEW.CDTIPOPRAT;
             V_CDFILIAL     := :NEW.CDFILIAL;
          ELSE
             V_OPERACAO     := 'D';
             V_CDPRATOANT   := :OLD.CDPRATO;
             V_CDFILAUXPRATANT := :OLD.CDFILAUXPRAT;
             V_CDPRATOATUAL := NULL;
             V_CDFILAUXPRATATU := NULL;
             V_CDTIPOPRAT   := :OLD.CDTIPOPRAT;
             V_CDFILIAL     := :OLD.CDFILIAL;
          END IF;
       END IF;
       
       INSERT INTO HISTLOGCOMPCARD (CDFILIAL, CDSERVFILI, DTMESCARD, DTCARDAPIO, CDTIPOPRAT, 
                                    CDPRATOANT, CDPRATOATUAL, CDOPERADOR, IDOPERACAO, DTALTERACAO,
                                    CDFILAUXPRATANT, CDFILAUXPRATATU)
       VALUES (V_CDFILIAL, V_CDSERVFILI, V_DTMESCARD, V_DTCARDAPIO, V_CDTIPOPRAT, 
               V_CDPRATOANT, V_CDPRATOATUAL, V_DSREMOTEOPER, V_OPERACAO, SYSDATE,
               V_CDFILAUXPRATANT, V_CDFILAUXPRATATU);
    END IF;
END;
/