CREATE OR REPLACE TRIGGER LOG_TPOPERCFOP
AFTER DELETE OR INSERT OR UPDATE
ON TPOPERCFOP FOR EACH ROW
DECLARE
  V_USUARIO      VARCHAR2(40);
  V_TIPO         VARCHAR2(1);
  V_ITPESQ       VARCHAR2(40);
  V_DESC         VARCHAR2(2000);    
  V_DTOPER       DATE;
  V_DSREMOTEADDR VARCHAR2(15);
  V_DSREMOTEHOST VARCHAR2(15);
  V_DSSERVERRDM  VARCHAR2(20);
  V_DSSERVMETHOD VARCHAR2(30);
  V_DSREMOTEOPER VARCHAR2(12);
  V_TAM          INTEGER;
BEGIN
   V_TIPO := '0';
   V_DESC := '';

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

   IF DELETING THEN
      IF :OLD.IDENTRSAIDOP IS NOT NULL THEN
         V_DESC := V_DESC || ' E/S=' || :OLD.IDENTRSAIDOP;
      END IF;

      IF :OLD.CDTIPOOPER IS NOT NULL THEN
         V_DESC := V_DESC || ' CdTipoOPer=' || :OLD.CDTIPOOPER;
      END IF;

      IF :OLD.CDFISCOPER IS NOT NULL THEN
         V_DESC := V_DESC || ' CdFiscOper=' || :OLD.CDFISCOPER;
      END IF;

      IF :OLD.IDATUESTTPCFOP IS NOT NULL THEN
         V_DESC := V_DESC || ' IdAtuEstTpCfop=' || :OLD.IDATUESTTPCFOP;
      END IF;

      IF :OLD.IDCOMPOEVENC IS NOT NULL THEN
         V_DESC := V_DESC || ' IdCompoeVenc=' || :OLD.IDCOMPOEVENC;
      END IF;

      V_TIPO         := '3';
      V_ITPESQ       := :OLD.IDENTRSAIDOP||:OLD.CDTIPOOPER;
   ELSE
      IF UPDATING THEN
         IF (NVL(:OLD.IDENTRSAIDOP,'!')   <> NVL(:NEW.IDENTRSAIDOP,'!'))   OR
            (NVL(:OLD.CDTIPOOPER,'!')     <> NVL(:NEW.CDTIPOOPER,'!'))     OR
            (NVL(:OLD.CDFISCOPER,'!')     <> NVL(:NEW.CDFISCOPER,'!'))     OR
            (NVL(:OLD.IDATUESTTPCFOP,'!') <> NVL(:NEW.IDATUESTTPCFOP,'!')) OR
            (NVL(:OLD.IDCOMPOEVENC, '!')  <> NVL(:NEW.IDCOMPOEVENC, '!'))  THEN

            V_TIPO         := '2';
            V_ITPESQ       := :OLD.IDENTRSAIDOP||:OLD.CDTIPOOPER;
         END IF;
      ELSE
         V_TIPO         := '1';
         V_ITPESQ       := :NEW.IDENTRSAIDOP||:NEW.CDTIPOOPER;
      END IF;

      IF NVL(:NEW.IDENTRSAIDOP, '!') <> NVL(:OLD.IDENTRSAIDOP, '!')     THEN
         V_DESC := V_DESC || ' E/S=' || :NEW.IDENTRSAIDOP;
      END IF;

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

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

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

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

   IF V_TIPO <> '0' THEN
      NMUSUARIO(V_USUARIO);

      V_DTOPER := SYSDATE;
      V_TAM := LENGTH(V_DESC);
      WHILE (V_TAM >= 0) LOOP
         INSERT INTO LOGOPERA
            (NMTABELA,     DTOPERACAO,   NMRESPOPER,  IDTIPOOPER,   CDPESQITEM,  DSOPERACAO,
             DSRDMUSUOPER, DSENDUSUOPER, DSNMUSUOPER, DSMETUSUOPER, CDORESPOPER)
         VALUES
            ('TPOPERCFOP',   V_DTOPER,       V_USUARIO,     V_TIPO,         V_ITPESQ,       SUBSTR(V_DESC,1,400),
             V_DSREMOTEADDR, V_DSREMOTEHOST, V_DSSERVERRDM, V_DSSERVMETHOD, V_DSREMOTEOPER);
         V_DESC := SUBSTR(V_DESC, 400, V_TAM);
         V_TAM := LENGTH(V_DESC);
      END LOOP;
   END IF;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('LOG_TPOPERCFOP','5.75.xxx'); END;
/