CREATE OR REPLACE TRIGGER GERA_LOG_COMPCARD
BEFORE INSERT OR UPDATE OR DELETE ON COMPCARD 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_OPERACAO     VARCHAR2(1);
  V_CDFILIAL     VARCHAR2(4);
  V_CDSERVFILI   VARCHAR2(5);
  V_CDTIPOPRAT   VARCHAR2(2);
  V_DTMESCARD    DATE;
  V_DTCARDAPIO   DATE;
  V_NRCARDAPIO VARCHAR2(10);
  V_NRSEQPRATCAR NUMBER(3);

  V_CDFILAUXPRATANT VARCHAR2(4);
  V_CDFILAUXPRATATU VARCHAR2(4);
  V_CDPRATOANT   VARCHAR2(10);
  V_CDPRATOATUAL VARCHAR2(10);
  V_VRPECONSCARDANT FLOAT;
  V_VRPECONSCARDATU FLOAT;

BEGIN
   WEBTUNNING(V_DSREMOTEADDR,
            V_DSREMOTEHOST,
            V_DSSERVERRDM,
            V_DSSERVMETHOD,
            V_DSREMOTEOPER);

   IF INSERTING THEN
      V_OPERACAO := 'I';
      V_CDTIPOPRAT := :NEW.CDTIPOPRAT;
      V_CDFILIAL := :NEW.CDFILIAL;
      V_NRCARDAPIO := :NEW.NRCARDAPIO;
      V_NRSEQPRATCAR := :NEW.NRSEQPRATCAR;
   ELSIF UPDATING THEN
      V_OPERACAO     := 'U';
      V_CDTIPOPRAT := :OLD.CDTIPOPRAT;
      V_CDFILIAL := :OLD.CDFILIAL;
      V_NRCARDAPIO := :OLD.NRCARDAPIO;
      V_NRSEQPRATCAR := :OLD.NRSEQPRATCAR;
   ELSE
      V_OPERACAO     := 'D';
      V_CDTIPOPRAT := :OLD.CDTIPOPRAT;
      V_CDFILIAL := :OLD.CDFILIAL;
      V_NRCARDAPIO := :OLD.NRCARDAPIO;
      V_NRSEQPRATCAR := :OLD.NRSEQPRATCAR;
   END IF;

   V_CDPRATOANT := :OLD.CDPRATO;
   V_CDPRATOATUAL := :NEW.CDPRATO;
   V_CDFILAUXPRATANT := :OLD.CDFILAUXPRAT;   
   V_CDFILAUXPRATATU := :NEW.CDFILAUXPRAT;
   V_VRPECONSCARDANT := :OLD.VRPECONSCARD;
   V_VRPECONSCARDATU := :NEW.VRPECONSCARD;

   IF UPDATING AND
   (
      ((V_CDPRATOANT = V_CDPRATOATUAL) OR
      (V_CDPRATOANT IS NULL AND V_CDPRATOATUAL IS NULL)) AND
      ((V_CDFILAUXPRATANT = V_CDFILAUXPRATATU) OR
      (V_CDFILAUXPRATANT IS NULL AND V_CDFILAUXPRATATU IS NULL)) AND
      ((V_VRPECONSCARDANT = V_VRPECONSCARDATU) OR
      (V_VRPECONSCARDANT IS NULL AND V_VRPECONSCARDATU IS NULL))
   ) THEN
      RETURN;
   END IF;
   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
      BEGIN
         SELECT CDSERVFILI, DTMESCARD, DTCARDAPIO
            INTO V_CDSERVFILI, V_DTMESCARD, V_DTCARDAPIO
            FROM CARDADIA
            WHERE CDFILIAL = V_CDFILIAL
               AND NRCARDAPIO = V_NRCARDAPIO;
      EXCEPTION
         WHEN OTHERS THEN
            V_CDSERVFILI := NULL;
            V_DTMESCARD  := NULL;
            V_DTCARDAPIO := NULL;
      END;
   END IF;
   
   INSERT INTO HISTLOGCOMPCARD 
   (CDFILIAL, CDSERVFILI, DTMESCARD, DTCARDAPIO, 
   CDTIPOPRAT, NRSEQPRATCAR, NRCARDAPIO,
   CDOPERADOR, IDOPERACAO, DTALTERACAO,
   CDPRATOANT, CDPRATOATUAL, CDFILAUXPRATANT, CDFILAUXPRATATU,
   VRPECONSCARDANT, VRPECONSCARDATU,
   DSRDMUSU, DSENDUSU, DSNMUSU, DSMETODO,
   DTINCLUSAO, DTULTATU,
   CDOPERINCLUSAO, CDOPERULTATU)
   VALUES 
   (V_CDFILIAL, V_CDSERVFILI, V_DTMESCARD, V_DTCARDAPIO,
   V_CDTIPOPRAT, V_NRSEQPRATCAR, V_NRCARDAPIO,
   V_DSREMOTEOPER, V_OPERACAO, SYSDATE,
   V_CDPRATOANT, V_CDPRATOATUAL, V_CDFILAUXPRATANT, V_CDFILAUXPRATATU,
   V_VRPECONSCARDANT, V_VRPECONSCARDATU,
   V_DSSERVERRDM, V_DSREMOTEADDR, V_DSREMOTEHOST, V_DSSERVMETHOD,
   SYSDATE, SYSDATE,
   V_DSREMOTEOPER, V_DSREMOTEOPER);
END;
/
