CREATE OR REPLACE TRIGGER LOG_EST_INVENTESTCEG
AFTER DELETE OR INSERT OR UPDATE
ON INVENTESTCEGO FOR EACH ROW
DECLARE
  V_USUARIO   VARCHAR2(40);
  V_TIPO      VARCHAR2(1);
  V_ITPESQ    VARCHAR2(40);
  V_DESC      VARCHAR2(300);
  V_DTOPER    DATE;

  V_DSREMOTEADDR VARCHAR2(15);
  V_DSREMOTEHOST VARCHAR2(15);
  V_DSSERVERRDM  VARCHAR2(20);
  V_DSSERVMETHOD VARCHAR2(30);
  V_DSREMOTEOPER VARCHAR2(12);
  V_CDFILLOGOPER VARCHAR2(4);
BEGIN
   V_TIPO := '0';
   V_DESC := '';

   IF DELETING THEN
      IF :OLD.CDLOCALESTOQ IS NOT NULL THEN
         V_DESC := V_DESC || 'LocalEstoq=' || :OLD.CDLOCALESTOQ;
      END IF;

      IF :OLD.DTCONTAGEM IS NOT NULL THEN
         V_DESC := V_DESC || ' DataContagem=' || :OLD.DTCONTAGEM;
      END IF;

      IF :OLD.DTGERACAOINVEST IS NOT NULL THEN
         V_DESC := V_DESC || ' DataGeracaoInvest=' || :OLD.DTGERACAOINVEST;
      END IF;

      IF :OLD.IDSITESTCEGO IS NOT NULL THEN
         V_DESC := V_DESC || ' SituacaoEstCego=' || :OLD.IDSITESTCEGO;
      END IF;

      IF :OLD.DTAPROVINVESTCE IS NOT NULL THEN
         V_DESC := V_DESC || ' DataAprovInvCego=' || :OLD.DTAPROVINVESTCE;
      END IF;

      IF :OLD.IDSITUACEITEINV IS NOT NULL THEN
         V_DESC := V_DESC || ' SitAceiteInv=' || :OLD.IDSITUACEITEINV;
      END IF;

      IF :OLD.IDINVGERAAJUSTE IS NOT NULL THEN
         V_DESC := V_DESC || ' InvGeraAjuste=' || :OLD.IDINVGERAAJUSTE;
      END IF;

      IF :OLD.CDOPERADOR IS NOT NULL THEN
         V_DESC := V_DESC || ' CodOperador=' || :OLD.CDOPERADOR;
      END IF;

      V_TIPO := '3';
      V_ITPESQ := :OLD.CDFILIAL || :OLD.CDALMOXARIFE || :OLD.CDLOCALESTOQ || :OLD.DTCONTAGEM;
      V_CDFILLOGOPER := :OLD.CDFILIAL;
   ELSE
      IF UPDATING THEN
         IF
            (:OLD.CDFILIAL  <> :NEW.CDFILIAL) OR
            (:OLD.CDALMOXARIFE  <> :NEW.CDALMOXARIFE) OR
            (:OLD.CDLOCALESTOQ  <> :NEW.CDLOCALESTOQ) OR
            (:OLD.DTCONTAGEM  <> :NEW.DTCONTAGEM) OR
   		   (NVL(:OLD.DTGERACAOINVEST, TO_DATE(NULL)) <> NVL(:NEW.DTGERACAOINVEST, TO_DATE(NULL)))  OR
            (:OLD.IDSITESTCEGO  <> :NEW.IDSITESTCEGO) OR
            (NVL(:OLD.DTAPROVINVESTCE, TO_DATE(NULL)) <> NVL(:NEW.DTAPROVINVESTCE, TO_DATE(NULL)))  OR
            (:OLD.IDSITUACEITEINV  <> :NEW.IDSITUACEITEINV) OR
            (:OLD.IDINVGERAAJUSTE  <> :NEW.IDINVGERAAJUSTE) OR
            (NVL(:OLD.CDOPERADOR, 'NULO') <> NVL(:NEW.CDOPERADOR, 'NULO')) THEN
            V_TIPO   := '2';
            V_ITPESQ := :OLD.CDFILIAL || :OLD.CDALMOXARIFE || :OLD.CDLOCALESTOQ || :OLD.DTCONTAGEM;
            V_CDFILLOGOPER := :OLD.CDFILIAL;
         END IF;
      ELSE
         V_TIPO   := '1';
         V_ITPESQ := :NEW.CDFILIAL || :NEW.CDALMOXARIFE || :NEW.CDLOCALESTOQ || :NEW.DTCONTAGEM;
         V_CDFILLOGOPER := :NEW.CDFILIAL;
      END IF;

      IF NVL(:OLD.CDFILIAL, '!') <> NVL(:NEW.CDFILIAL, '!') THEN
         V_DESC := V_DESC || 'CodFilial=' || :NEW.CDFILIAL;
      END IF;

      IF NVL(:OLD.CDALMOXARIFE, '!') <> NVL(:NEW.CDALMOXARIFE, '!') THEN
         V_DESC := V_DESC || ' CodAlmoxarifado=' || :NEW.CDALMOXARIFE;
      END IF;

      IF NVL(:OLD.CDLOCALESTOQ, '!') <> NVL(:NEW.CDLOCALESTOQ, '!') THEN
         V_DESC := V_DESC || ' CodLocalEstoque=' || :NEW.CDLOCALESTOQ;
      END IF;

      IF NVL(:OLD.DTCONTAGEM, TO_DATE(NULL)) <> NVL(:NEW.DTCONTAGEM, TO_DATE(NULL)) THEN
         V_DESC := V_DESC || ' DataContagem=' || :NEW.DTCONTAGEM;
      END IF;

      IF NVL(:OLD.DTGERACAOINVEST, TO_DATE(NULL)) <> NVL(:NEW.DTGERACAOINVEST, TO_DATE(NULL)) THEN
         V_DESC := V_DESC || ' DataGeracaoInvest=' || :NEW.DTGERACAOINVEST;
      END IF;

      IF NVL(:OLD.IDSITESTCEGO, '!') <> NVL(:NEW.IDSITESTCEGO, '!') THEN
         V_DESC := V_DESC || ' SitEstCego=' || :NEW.IDSITESTCEGO;
      END IF;

      IF NVL(:OLD.DTAPROVINVESTCE, TO_DATE(NULL)) <> NVL(:NEW.DTAPROVINVESTCE, TO_DATE(NULL)) THEN
         V_DESC := V_DESC || ' DtAprovInvEstCego=' || :NEW.DTAPROVINVESTCE;
      END IF;

      IF NVL(:OLD.IDSITUACEITEINV, '!') <> NVL(:NEW.IDSITUACEITEINV, '!') THEN
         V_DESC := V_DESC || ' EntPos=' || :NEW.IDSITUACEITEINV;
      END IF;

      IF NVL(:OLD.IDINVGERAAJUSTE, '!') <> NVL(:NEW.IDINVGERAAJUSTE, '!') THEN
         V_DESC := V_DESC || ' IdInvGeraAjuste=' || :NEW.IDINVGERAAJUSTE;
      END IF;

      IF NVL(:OLD.CDOPERADOR, '!') <> NVL(:NEW.CDOPERADOR, '!') THEN
         V_DESC := V_DESC || ' CodOperador=' || :NEW.CDOPERADOR;
      END IF;
   END IF;

   IF (V_TIPO <> '0')  THEN
      IF NOT V_DESC IS NULL THEN
         NMUSUARIO(V_USUARIO);
         WEBTUNNING(V_DSREMOTEADDR,
                    V_DSREMOTEHOST,
                    V_DSSERVERRDM,
                    V_DSSERVMETHOD,
                    V_DSREMOTEOPER);
         IF NOT V_USUARIO IS NULL THEN
            V_DTOPER := SYSDATE;
            INSERT INTO LOGOPERA
               (NMTABELA,   DTOPERACAO, NMRESPOPER,
                IDTIPOOPER, CDPESQITEM, DSOPERACAO, CDFILLOGOPER,
                DSRDMUSUOPER, DSENDUSUOPER, DSNMUSUOPER, DSMETUSUOPER, CDORESPOPER)
            VALUES
               ('INVENTESTCEGO',  V_DTOPER, V_USUARIO,
                V_TIPO,         V_ITPESQ, V_DESC, V_CDFILLOGOPER,
                V_DSSERVERRDM, V_DSREMOTEADDR, V_DSREMOTEHOST, V_DSSERVMETHOD, V_DSREMOTEOPER);
         END IF;
      END IF;
   END IF;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('LOG_EST_INVENTESTCEG','5.06.026'); END;
/
