CREATE OR REPLACE TRIGGER LOG_DFE_PRODXFORNDF
AFTER DELETE OR INSERT OR UPDATE
ON PRODXFORNDF FOR EACH ROW
DECLARE
  V_USUARIO   VARCHAR2(40);
  V_TIPO      VARCHAR2(1);
  V_ITPESQ    VARCHAR2(255);
  V_DESC      VARCHAR2(4000);
  V_DTOPER    DATE;
  V_DSREMOTEADDR VARCHAR2(15);
  V_DSREMOTEHOST VARCHAR2(15);
  V_DSSERVERRDM  VARCHAR2(20);
  V_DSSERVMETHOD VARCHAR2(30);
  V_DSREMOTEOPER VARCHAR2(12);
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 || ' IdEntrSaidOp=' || :OLD.IDENTRSAIDOP;
      END IF;

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

      IF :OLD.CDFORNECED IS NOT NULL THEN
         V_DESC := V_DESC || ' CdForneced=' || :OLD.CDFORNECED;
      END IF;

      IF :OLD.CDPRODUTO IS NOT NULL THEN
         V_DESC := V_DESC || ' CdProduto=' || :OLD.CDPRODUTO;
      END IF;

      IF :OLD.IDUTILIZA IS NOT NULL THEN
         V_DESC := V_DESC || ' IdUtiliza=' || :OLD.IDUTILIZA;
      END IF;

      IF :OLD.CDPAIS IS NOT NULL THEN
         V_DESC := V_DESC || ' CdPais=' || :OLD.CDPAIS;
      END IF;

      IF :OLD.SGESTADO IS NOT NULL THEN
         V_DESC := V_DESC || ' SgEstado=' || :OLD.SGESTADO;
      END IF;

      IF :OLD.CDMUNICIPIO IS NOT NULL THEN
         V_DESC := V_DESC || ' CdMunicipio=' || :OLD.CDMUNICIPIO;
      END IF;

      V_TIPO := '3';
      V_ITPESQ := :OLD.IDENTRSAIDOP||:OLD.CDTIPOOPER||:OLD.CDFORNECED||:OLD.CDPAIS||:OLD.SGESTADO||:OLD.CDMUNICIPIO||:OLD.CDPRODUTO;
   ELSE
      IF UPDATING THEN
         IF (NVL(:OLD.IDENTRSAIDOP,'!') <> NVL(:NEW.IDENTRSAIDOP,'!')) OR
            (NVL(:OLD.CDTIPOOPER,'!') <> NVL(:NEW.CDTIPOOPER,'!')) OR
            (NVL(:OLD.CDFORNECED,'!') <> NVL(:NEW.CDFORNECED,'!')) OR
            (NVL(:OLD.CDPRODUTO,'!') <> NVL(:NEW.CDPRODUTO,'!')) OR
            (NVL(:OLD.IDUTILIZA,'!') <> NVL(:NEW.IDUTILIZA,'!')) OR
            (NVL(:OLD.CDPAIS,'!') <> NVL(:NEW.CDPAIS,'!')) OR
            (NVL(:OLD.SGESTADO,'!') <> NVL(:NEW.SGESTADO,'!')) OR
            (NVL(:OLD.CDMUNICIPIO,'!') <> NVL(:NEW.CDMUNICIPIO,'!'))
         THEN

            V_TIPO   := '2';
            V_ITPESQ := :OLD.IDENTRSAIDOP||:OLD.CDTIPOOPER||:OLD.CDFORNECED||:OLD.CDPAIS||:OLD.SGESTADO||:OLD.CDMUNICIPIO||:OLD.CDPRODUTO;
         END IF;
      ELSE
         V_TIPO   := '1';
         V_ITPESQ := :NEW.IDENTRSAIDOP||:NEW.CDTIPOOPER||:NEW.CDFORNECED||:NEW.CDPAIS||:NEW.SGESTADO||:NEW.CDMUNICIPIO||:NEW.CDPRODUTO;
      END IF;

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

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

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

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

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

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

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

      IF NVL(:OLD.CDMUNICIPIO, '!') <>  NVL(:NEW.CDMUNICIPIO, '!') THEN
         V_DESC := V_DESC || ' CdMunicipio=' || :NEW.CDMUNICIPIO;
      END IF;
   END IF;
   IF V_TIPO <> '0' THEN
      IF NOT V_DESC IS NULL THEN
         NMUSUARIO(V_USUARIO);
         IF NOT V_USUARIO IS NULL THEN
            V_DTOPER := SYSDATE;

            INSERT INTO LOGOPERA
               (NMTABELA,
                DTOPERACAO, 
                NMRESPOPER, 
                IDTIPOOPER, 
                CDPESQITEM, 
                DSOPERACAO,
                DSRDMUSUOPER, 
                DSENDUSUOPER, 
                DSNMUSUOPER, 
                DSMETUSUOPER, 
                CDORESPOPER)
            VALUES
               ('PRODXFORNDF',  
                V_DTOPER, 
                V_USUARIO,
                V_TIPO,
                V_ITPESQ,
                SUBSTR(V_DESC, 0, 400),
                V_DSREMOTEADDR, 
                V_DSREMOTEHOST, 
                V_DSSERVERRDM,
                V_DSSERVMETHOD, 
                V_DSREMOTEOPER);
         END IF;
      END IF;
   END IF;
END;
/
