CREATE OR REPLACE TRIGGER LOG_FIS_ITEMPRODUTO
AFTER DELETE OR INSERT OR UPDATE
ON ITEMPRODUTO FOR EACH ROW
DECLARE
  V_USUARIO      VARCHAR2(40);
  V_TIPO         VARCHAR2(1);
  V_ITPESQ       VARCHAR2(40);
  V_DESC         VARCHAR2(300);
  V_CDFILLOGOPER VARCHAR2(4);
  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 := '';
   V_CDFILLOGOPER := '';

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

      IF :OLD.CDPRODEMIS IS NOT NULL THEN
         V_DESC := V_DESC || ' ProdEmis=' || :OLD.CDPRODEMIS;
      END IF;

      IF :OLD.QTITEMPROD IS NOT NULL THEN
         V_DESC := V_DESC || ' QtItem=' || ROUND(:OLD.QTITEMPROD, 5);
      END IF;

      IF :OLD.VRUNITITEM IS NOT NULL THEN
         V_DESC := V_DESC || ' UnitItem=' || ROUND(:OLD.VRUNITITEM, 2);
      END IF;

      IF :OLD.VRDESCITEM IS NOT NULL THEN
         V_DESC := V_DESC || ' DescItem=' || ROUND(:OLD.VRDESCITEM, 2);
      END IF;

      IF :OLD.VRTOTAITEM IS NOT NULL THEN
         V_DESC := V_DESC || ' TotaItem=' || ROUND(:OLD.VRTOTAITEM, 2);
      END IF;

      IF :OLD.VRCONTITEM IS NOT NULL THEN
         V_DESC := V_DESC || ' ContItem=' || ROUND(:OLD.VRCONTITEM, 2);
      END IF;

      IF :OLD.CDFISOPEITEM IS NOT NULL THEN
         V_DESC := V_DESC || ' CFOpItem=' || :OLD.CDFISOPEITEM;
      END IF;

      IF :OLD.CDSUFXCFOPPR IS NOT NULL THEN
         V_DESC := V_DESC || ' SufCFOp=' || :OLD.CDSUFXCFOPPR;
      END IF;

      IF :OLD.CDSITUTRIBPR IS NOT NULL THEN
         V_DESC := V_DESC || ' CSTItem=' || :OLD.CDSITUTRIBPR;
      END IF;

      IF :OLD.IDERROSITEM IS NOT NULL THEN
         V_DESC := V_DESC || ' ErroItem=' || :OLD.IDERROSITEM;
      END IF;

      IF :OLD.IDAVISOSITEM IS NOT NULL THEN
         V_DESC := V_DESC || ' AvisoItem=' || :OLD.IDAVISOSITEM;
      END IF;

      IF :OLD.VRBRUTITEM IS NOT NULL THEN
         V_DESC := V_DESC || ' BrutItem=' || ROUND(:OLD.VRBRUTITEM, 2);
      END IF;

      IF :OLD.CDFILIALPED IS NOT NULL THEN
         V_DESC := V_DESC || ' FiliPed=' || :OLD.CDFILIALPED;
      END IF;

      IF :OLD.NRPEDIDO IS NOT NULL THEN
         V_DESC := V_DESC || ' Pedido=' || :OLD.NRPEDIDO;
      END IF;

      IF :OLD.NRITEMPED IS NOT NULL THEN
         V_DESC := V_DESC || ' PedItem=' || :OLD.NRITEMPED;
      END IF;

      IF :OLD.CDFILIMOVI IS NOT NULL THEN
         V_DESC := V_DESC || ' FiliMovi=' || :OLD.CDFILIMOVI;
      END IF;

      IF :OLD.CDALMOXARIFE IS NOT NULL THEN
         V_DESC := V_DESC || ' Almox=' || :OLD.CDALMOXARIFE;
      END IF;

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

      IF :OLD.NRLOTEESTQ IS NOT NULL THEN
         V_DESC := V_DESC || ' Lote=' || :OLD.NRLOTEESTQ;
      END IF;

      IF :OLD.NRSUBLOTE IS NOT NULL THEN
         V_DESC := V_DESC || ' SubLote=' || :OLD.NRSUBLOTE;
      END IF;

      IF :OLD.CDFILIALVEND IS NOT NULL THEN
         V_DESC := V_DESC || ' FiliVend=' || :OLD.CDFILIALVEND;
      END IF;

      IF :OLD.CDCAIXA IS NOT NULL THEN
         V_DESC := V_DESC || ' Caixa=' || :OLD.CDCAIXA;
      END IF;

      IF :OLD.NRSEQVENDA IS NOT NULL THEN
         V_DESC := V_DESC || ' SeqVenda=' || :OLD.NRSEQVENDA;
      END IF;

      IF :OLD.NRSEQUITVEND IS NOT NULL THEN
         V_DESC := V_DESC || ' SeqItVenda=' || :OLD.NRSEQUITVEND;
      END IF;

      IF :OLD.QTDEVOLUCAOOL IS NOT NULL THEN
         V_DESC := V_DESC || ' QtDevoluOL=' || :OLD.QTDEVOLUCAOOL;
      END IF;

      IF :OLD.IDDEVOLUCAOOL IS NOT NULL THEN
         V_DESC := V_DESC || ' IdDevoluOL=' || :OLD.IDDEVOLUCAOOL;
      END IF;

      V_TIPO         := '3';
      V_ITPESQ       := :OLD.CDFILIAL||:OLD.NRLANCTONF||:OLD.NRSEQITPROD;
      V_CDFILLOGOPER := :OLD.CDFILIAL;
   ELSE
      IF UPDATING THEN
         IF (:OLD.CDPRODUTO     <> :NEW.CDPRODUTO)     OR
            (:OLD.CDPRODEMIS    <> :NEW.CDPRODEMIS)    OR
            (:OLD.QTITEMPROD    <> :NEW.QTITEMPROD)    OR
            (:OLD.VRUNITITEM    <> :NEW.VRUNITITEM)    OR
            (:OLD.VRDESCITEM    <> :NEW.VRDESCITEM)    OR
            (:OLD.VRTOTAITEM    <> :NEW.VRTOTAITEM)    OR
            (:OLD.VRCONTITEM    <> :NEW.VRCONTITEM)    OR
            (:OLD.CDFISOPEITEM  <> :NEW.CDFISOPEITEM)  OR
            (:OLD.CDSUFXCFOPPR  <> :NEW.CDSUFXCFOPPR)  OR
            (:OLD.CDSITUTRIBPR  <> :NEW.CDSITUTRIBPR)  OR
            (:OLD.IDERROSITEM   <> :NEW.IDERROSITEM)   OR
            (:OLD.IDAVISOSITEM  <> :NEW.IDAVISOSITEM)  OR
            (:OLD.VRBRUTITEM    <> :NEW.VRBRUTITEM)    OR
            (:OLD.CDFILIALPED   <> :NEW.CDFILIALPED)   OR
            (:OLD.NRPEDIDO      <> :NEW.NRPEDIDO)      OR
            (:OLD.NRITEMPED     <> :NEW.NRITEMPED)     OR
            (:OLD.CDFILIMOVI    <> :NEW.CDFILIMOVI)    OR
            (:OLD.CDALMOXARIFE  <> :NEW.CDALMOXARIFE)  OR
            (:OLD.CDLOCALESTOQ  <> :NEW.CDLOCALESTOQ)  OR
            (:OLD.NRLOTEESTQ    <> :NEW.NRLOTEESTQ)    OR
            (:OLD.NRSUBLOTE     <> :NEW.NRSUBLOTE)     OR
            (:OLD.CDFILIALVEND  <> :NEW.CDFILIALVEND)  OR
            (:OLD.CDCAIXA       <> :NEW.CDCAIXA)       OR
            (:OLD.NRSEQVENDA    <> :NEW.NRSEQVENDA)    OR
            (:OLD.QTDEVOLUCAOOL <> :NEW.QTDEVOLUCAOOL) OR
            (:OLD.IDDEVOLUCAOOL <> :NEW.IDDEVOLUCAOOL) OR
            (:OLD.NRSEQUITVEND  <> :NEW.NRSEQUITVEND) THEN

            V_TIPO         := '2';
            V_ITPESQ       := :OLD.CDFILIAL||:OLD.NRLANCTONF||:OLD.NRSEQITPROD;
            V_CDFILLOGOPER := :OLD.CDFILIAL;
         END IF;
      ELSE
         V_TIPO         := '1';
         V_ITPESQ       := :NEW.CDFILIAL||:NEW.NRLANCTONF||:NEW.NRSEQITPROD;
         V_CDFILLOGOPER := :NEW.CDFILIAL;
      END IF;

      IF :NEW.CDPRODUTO IS NOT NULL THEN
         V_DESC := V_DESC || 'Prod=' || :NEW.CDPRODUTO;
      END IF;

      IF :NEW.CDPRODEMIS IS NOT NULL THEN
         V_DESC := V_DESC || ' ProdEmis=' || :NEW.CDPRODEMIS;
      END IF;

      IF :NEW.QTITEMPROD IS NOT NULL THEN
         V_DESC := V_DESC || ' QtItem=' || ROUND(:NEW.QTITEMPROD, 5);
      END IF;

      IF :NEW.VRUNITITEM IS NOT NULL THEN
         V_DESC := V_DESC || ' UnitItem=' || ROUND(:NEW.VRUNITITEM, 2);
      END IF;

      IF :NEW.VRDESCITEM IS NOT NULL THEN
         V_DESC := V_DESC || ' DescItem=' || ROUND(:NEW.VRDESCITEM, 2);
      END IF;

      IF :NEW.VRTOTAITEM IS NOT NULL THEN
         V_DESC := V_DESC || ' TotaItem=' || ROUND(:NEW.VRTOTAITEM, 2);
      END IF;

      IF :NEW.VRCONTITEM IS NOT NULL THEN
         V_DESC := V_DESC || ' ContItem=' || ROUND(:NEW.VRCONTITEM, 2);
      END IF;

      IF :NEW.CDFISOPEITEM IS NOT NULL THEN
         V_DESC := V_DESC || ' CFOpItem=' || :NEW.CDFISOPEITEM;
      END IF;

      IF :NEW.CDSUFXCFOPPR IS NOT NULL THEN
         V_DESC := V_DESC || ' SufCFOp=' || :NEW.CDSUFXCFOPPR;
      END IF;

      IF :NEW.CDSITUTRIBPR IS NOT NULL THEN
         V_DESC := V_DESC || ' CSTItem=' || :NEW.CDSITUTRIBPR;
      END IF;

      IF :NEW.IDERROSITEM IS NOT NULL THEN
         V_DESC := V_DESC || ' ErroItem=' || :NEW.IDERROSITEM;
      END IF;

      IF :NEW.IDAVISOSITEM IS NOT NULL THEN
         V_DESC := V_DESC || ' AvisoItem=' || :NEW.IDAVISOSITEM;
      END IF;

      IF :NEW.VRBRUTITEM IS NOT NULL THEN
         V_DESC := V_DESC || ' BrutItem=' || ROUND(:NEW.VRBRUTITEM, 2);
      END IF;

      IF :NEW.CDFILIALPED IS NOT NULL THEN
         V_DESC := V_DESC || ' FiliPed=' || :NEW.CDFILIALPED;
      END IF;

      IF :NEW.NRPEDIDO IS NOT NULL THEN
         V_DESC := V_DESC || ' Pedido=' || :NEW.NRPEDIDO;
      END IF;

      IF :NEW.NRITEMPED IS NOT NULL THEN
         V_DESC := V_DESC || ' PedItem=' || :NEW.NRITEMPED;
      END IF;

      IF :NEW.CDFILIMOVI IS NOT NULL THEN
         V_DESC := V_DESC || ' FiliMovi=' || :NEW.CDFILIMOVI;
      END IF;

      IF :NEW.CDALMOXARIFE IS NOT NULL THEN
         V_DESC := V_DESC || ' Almox=' || :NEW.CDALMOXARIFE;
      END IF;

      IF :NEW.CDLOCALESTOQ IS NOT NULL THEN
         V_DESC := V_DESC || ' LcEstoq=' || :NEW.CDLOCALESTOQ;
      END IF;

      IF :NEW.NRLOTEESTQ IS NOT NULL THEN
         V_DESC := V_DESC || ' Lote=' || :NEW.NRLOTEESTQ;
      END IF;

      IF :NEW.NRSUBLOTE IS NOT NULL THEN
         V_DESC := V_DESC || ' SubLote=' || :NEW.NRSUBLOTE;
      END IF;

      IF :NEW.CDFILIALVEND IS NOT NULL THEN
         V_DESC := V_DESC || ' FiliVend=' || :NEW.CDFILIALVEND;
      END IF;

      IF :NEW.CDCAIXA IS NOT NULL THEN
         V_DESC := V_DESC || ' Caixa=' || :NEW.CDCAIXA;
      END IF;

      IF :NEW.NRSEQVENDA IS NOT NULL THEN
         V_DESC := V_DESC || ' SeqVenda=' || :NEW.NRSEQVENDA;
      END IF;

      IF :NEW.QTDEVOLUCAOOL IS NOT NULL THEN
         V_DESC := V_DESC || ' QtDevoluOL=' || :NEW.QTDEVOLUCAOOL;
      END IF;

      IF :NEW.IDDEVOLUCAOOL IS NOT NULL THEN
         V_DESC := V_DESC || ' IdDevoluOL=' || :NEW.IDDEVOLUCAOOL;
      END IF;

      IF :NEW.NRSEQUITVEND IS NOT NULL THEN
         V_DESC := V_DESC || ' SeqItVenda=' || :NEW.NRSEQUITVEND;
      END IF;
   END IF;

   IF V_TIPO <> '0' THEN
      NMUSUARIO(V_USUARIO);
      WEBTUNNING(V_DSREMOTEADDR,
                 V_DSREMOTEHOST,
                 V_DSSERVERRDM,
                 V_DSSERVMETHOD,
                 V_DSREMOTEOPER);

      IF NOT V_DESC IS NULL THEN
         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
               ('ITEMPRODUTO',  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;
/