CREATE OR REPLACE TRIGGER LOG_EST_PRODFILI
AFTER DELETE OR UPDATE
ON PRODFILI 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_USUARIO      VARCHAR2(40);
  V_TIPO         VARCHAR2(1);
  V_ITPESQ       VARCHAR2(40);
  V_DESC         VARCHAR2(1500);
  V_CDFILLOGOPER VARCHAR2(4);
  V_DTOPER       DATE;
BEGIN
   V_TIPO := '0';
   V_CDFILLOGOPER := '';
   V_DESC := 'CodProd=' || :NEW.CDPRODUTO;

   IF DELETING THEN
      IF :OLD.IDCNTRESTOQ IS NOT NULL THEN
         V_DESC := V_DESC || ' CtrEst=' || :OLD.IDCNTRESTOQ;
      END IF;

      IF :OLD.QTESTOQMIN IS NOT NULL THEN
         V_DESC := V_DESC || ' EstoqMin=' || :OLD.QTESTOQMIN;
      END IF;

      IF :OLD.CDCCUSTENTRF IS NOT NULL THEN
         V_DESC := V_DESC || ' CCustEntr=' || :OLD.CDCCUSTENTRF;
      END IF;

      IF :OLD.CDCCUSTSAIDF IS NOT NULL THEN
         V_DESC := V_DESC || ' CCustSaid=' || :OLD.CDCCUSTSAIDF;
      END IF;

      IF :OLD.IDPRODATIV IS NOT NULL THEN
         V_DESC := V_DESC || ' ProdAtiv=' || :OLD.IDPRODATIV;
      END IF;

      IF :OLD.VRPESOUNIDFI IS NOT NULL THEN
         V_DESC := V_DESC || ' PesoUnid=' || ROUND(:OLD.VRPESOUNIDFI, 5);
      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 || ' LocEst=' || :OLD.CDLOCALESTOQ;
      END IF;

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

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

      IF :OLD.CDALMOXINSPR IS NOT NULL THEN
         V_DESC := V_DESC || ' AlmInsuPr=' || :OLD.CDALMOXINSPR;
      END IF;

      IF :OLD.CDLOCALINSPR IS NOT NULL THEN
         V_DESC := V_DESC || ' LcInsuPr=' || :OLD.CDLOCALINSPR;
      END IF;

      IF :OLD.CDALMOXPRACA IS NOT NULL THEN
         V_DESC := V_DESC || ' AlmPrAca=' || :OLD.CDALMOXPRACA;
      END IF;

      IF :OLD.CDLOCALPRACA IS NOT NULL THEN
         V_DESC := V_DESC || ' LcPrAca=' || :OLD.CDLOCALPRACA;
      END IF;

      IF :OLD.CDALMOXPRCLI IS NOT NULL THEN
         V_DESC := V_DESC || ' AlmPrCli=' ||  :OLD.CDALMOXPRCLI;
      END IF;

      IF :OLD.CDLOCALPRCLI IS NOT NULL THEN
         V_DESC := V_DESC || ' LcPrCli=' ||  :OLD.CDLOCALPRCLI;
      END IF;

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

      IF :OLD.CDCTBLIMPLPF IS NOT NULL THEN
         V_DESC := V_DESC ||  ' CCtbImpl=' || :OLD.CDCTBLIMPLPF;
      END IF;

      IF :OLD.CDCTBLPRODPF IS NOT NULL THEN
         V_DESC := V_DESC || ' CCtbProducao=' ||  :OLD.CDCTBLPRODPF;
      END IF;

      IF :OLD.CDCTBLCPROPF IS NOT NULL THEN
         V_DESC := V_DESC || ' CCtbCProva=' ||  :OLD.CDCTBLCPROPF;
      END IF;

      IF :OLD.CDCTBLENTRPF IS NOT NULL THEN
         V_DESC := V_DESC || ' CCtbEntr=' ||  :OLD.CDCTBLENTRPF;
      END IF;

      IF :OLD.CDCTBLAJUSPF IS NOT NULL THEN
         V_DESC := V_DESC ||  ' CCtbAju=' || :OLD.CDCTBLAJUSPF;
      END IF;

      IF :OLD.CDCTBLPRACPF IS NOT NULL THEN
         V_DESC := V_DESC ||  ' CCtbCMV=' || :OLD.CDCTBLPRACPF;
      END IF;

      IF :OLD.CDCTBLSAIDPF IS NOT NULL THEN
         V_DESC := V_DESC || ' CCtbSaid=' ||  :OLD.CDCTBLSAIDPF;
      END IF;

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

      IF :OLD.IDUTCTRLLOTE IS NOT NULL THEN
         V_DESC := V_DESC || ' CtrLote=' ||  :OLD.IDUTCTRLLOTE;
      END IF;

      IF :OLD.CDALMOXPRSEMACA IS NOT NULL THEN
         V_DESC := V_DESC || ' AlmPrSemAc=' || :OLD.CDALMOXPRSEMACA;
      END IF;

      IF :OLD.CDLOCALPRSEMACA IS NOT NULL THEN
         V_DESC := V_DESC || ' LcPrSemAc=' || :OLD.CDLOCALPRSEMACA;
      END IF;

      IF :OLD.CDALMOXREPROV IS NOT NULL THEN
         V_DESC := V_DESC || ' AlmReprov=' || :OLD.CDALMOXREPROV;
      END IF;

      IF :OLD.CDLOCALREPROV IS NOT NULL THEN
         V_DESC := V_DESC || ' LcReprov=' || :OLD.CDLOCALREPROV;
      END IF;

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

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

      V_TIPO         := '3';
      V_ITPESQ       := :OLD.CDFILIAL||:OLD.CDPRODUTO;
      V_CDFILLOGOPER := :OLD.CDFILIAL;
   ELSE
      IF UPDATING THEN
         IF (:OLD.IDCNTRESTOQ  <> :NEW.IDCNTRESTOQ)  OR
            (:OLD.QTESTOQMIN   <> :NEW.QTESTOQMIN)   OR
            (:OLD.CDCCUSTENTRF <> :NEW.CDCCUSTENTRF) OR
            (:OLD.CDCCUSTSAIDF <> :NEW.CDCCUSTSAIDF) OR
            (:OLD.IDPRODATIV   <> :NEW.IDPRODATIV)   OR
            (:OLD.VRPESOUNIDFI <> :NEW.VRPESOUNIDFI) OR
            (:OLD.CDALMOXARIFE <> :NEW.CDALMOXARIFE) OR
            (:OLD.CDLOCALESTOQ <> :NEW.CDLOCALESTOQ) OR
            (:OLD.VRPTOLQTITAF <> :NEW.VRPTOLQTITAF) OR
            (:OLD.VRPTOLVRITAF <> :NEW.VRPTOLVRITAF) OR
            (:OLD.CDALMOXINSPR <> :NEW.CDALMOXINSPR) OR
            (:OLD.CDLOCALINSPR <> :NEW.CDLOCALINSPR) OR
            (:OLD.CDALMOXPRACA <> :NEW.CDALMOXPRACA) OR
            (:OLD.CDLOCALPRACA <> :NEW.CDLOCALPRACA) OR
            (:OLD.CDALMOXPRCLI <> :NEW.CDALMOXPRCLI) OR
            (:OLD.CDLOCALPRCLI <> :NEW.CDLOCALPRCLI) OR
            (:OLD.VRTXADMPROD  <> :NEW.VRTXADMPROD)  OR
            (:OLD.CDALMOXPRSEMACA <> :NEW.CDALMOXPRSEMACA) OR
            (:OLD.CDLOCALPRSEMACA <> :NEW.CDLOCALPRSEMACA) OR
            (NVL(:OLD.CDCTBLIMPLPF,'!') <> NVL(:NEW.CDCTBLIMPLPF,'!')) OR
            (NVL(:OLD.CDCTBLPRODPF,'!') <> NVL(:NEW.CDCTBLPRODPF,'!')) OR
            (NVL(:OLD.CDCTBLCPROPF,'!') <> NVL(:NEW.CDCTBLCPROPF,'!')) OR
            (NVL(:OLD.CDCTBLENTRPF,'!') <> NVL(:NEW.CDCTBLENTRPF,'!')) OR
            (NVL(:OLD.CDCTBLAJUSPF,'!') <> NVL(:NEW.CDCTBLAJUSPF,'!')) OR
            (NVL(:OLD.CDCTBLPRACPF,'!') <> NVL(:NEW.CDCTBLPRACPF,'!')) OR
            (NVL(:OLD.CDCTBLSAIDPF,'!') <> NVL(:NEW.CDCTBLSAIDPF,'!')) OR
            (NVL(:OLD.QTESTMINSAID, 0 ) <> NVL(:NEW.QTESTMINSAID, 0 )) OR
            (NVL(:OLD.IDUTCTRLLOTE,'!') <> NVL(:NEW.IDUTCTRLLOTE,'!')) OR
            (:OLD.CDALMOXREPROV <> :NEW.CDALMOXREPROV) OR
            (NVL(:OLD.VRALIQPISSOL, 0)    <> NVL(:NEW.VRALIQPISSOL, 0))     OR
            (NVL(:OLD.VRALIQCOFINSSOL, 0) <> NVL(:NEW.VRALIQCOFINSSOL, 0))  OR
            (:OLD.CDLOCALREPROV <> :NEW.CDLOCALREPROV) THEN

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

      IF NVL(:OLD.IDCNTRESTOQ, '') <> NVL(:NEW.IDCNTRESTOQ, '') THEN
         V_DESC := V_DESC || ' CtrEst=' || :NEW.IDCNTRESTOQ;
      END IF;

      IF NVL(:OLD.QTESTOQMIN, '') <> NVL(:NEW.QTESTOQMIN, '') THEN
         V_DESC := V_DESC || ' EstoqMin=' || ROUND(:NEW.QTESTOQMIN, 5);
      END IF;

      IF NVL(:OLD.CDCCUSTENTRF, '') <> NVL(:NEW.CDCCUSTENTRF, '') THEN
         V_DESC := V_DESC || ' CCustEntr=' || :NEW.CDCCUSTENTRF;
      END IF;

      IF NVL(:OLD.CDCCUSTSAIDF, '') <> NVL(:NEW.CDCCUSTSAIDF, '') THEN
         V_DESC := V_DESC || ' CCustSaid=' || :NEW.CDCCUSTSAIDF;
      END IF;

      IF NVL(:OLD.IDPRODATIV, '') <> NVL(:NEW.IDPRODATIV,'') THEN
         V_DESC := V_DESC || ' ProdAtiv=' || :NEW.IDPRODATIV;
      END IF;

      IF NVL(:OLD.VRPESOUNIDFI, '') <> NVL(:NEW.VRPESOUNIDFI, '') THEN
         V_DESC := V_DESC || ' PesoUnid=' || ROUND(:NEW.VRPESOUNIDFI, 5);
      END IF;

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

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

      IF NVL(:OLD.VRPTOLQTITAF,'') <> NVL(:NEW.VRPTOLQTITAF,'') THEN
         V_DESC := V_DESC || ' TolQtItAF=' || ROUND(:NEW.VRPTOLQTITAF, 5);
      END IF;

      IF NVL(:OLD.VRPTOLVRITAF,'') <>  NVL(:NEW.VRPTOLVRITAF,'') THEN
         V_DESC := V_DESC || ' TolVrItAF=' || ROUND(:NEW.VRPTOLVRITAF, 5);
      END IF;

      IF NVL(:OLD.CDALMOXINSPR,'') <> NVL(:NEW.CDALMOXINSPR,'') THEN
         V_DESC := V_DESC || ' AlmInsuPr=' || :NEW.CDALMOXINSPR;
      END IF;

      IF NVL(:OLD.CDLOCALINSPR,'') <> NVL(:NEW.CDLOCALINSPR,'') THEN
         V_DESC := V_DESC || ' LcInsuPr=' || :NEW.CDLOCALINSPR;
      END IF;

      IF NVL(:OLD.CDALMOXPRACA, '') <> NVL(:NEW.CDALMOXPRACA, '') THEN
         V_DESC := V_DESC || ' AlmPrAca=' || :NEW.CDALMOXPRACA;
      END IF;

      IF NVL(:OLD.CDLOCALPRACA, '') <> NVL(:NEW.CDLOCALPRACA, '') THEN
         V_DESC := V_DESC || ' LcPrAca=' || :NEW.CDLOCALPRACA;
      END IF;

      IF NVL(:OLD.CDALMOXPRCLI, '') <> NVL(:NEW.CDALMOXPRCLI, '') THEN
         V_DESC := V_DESC || ' AlmPrCli=' ||  :NEW.CDALMOXPRCLI;
      END IF;

      IF NVL(:OLD.CDLOCALPRCLI, '') <> NVL(:NEW.CDLOCALPRCLI, '') THEN
         V_DESC := V_DESC || ' LcPrCli=' ||  :NEW.CDLOCALPRCLI;
      END IF;

      IF NVL(:OLD.VRTXADMPROD, '') <> NVL(:NEW.VRTXADMPROD, '') THEN
         V_DESC := V_DESC || ' TxAdmPr=' ||  ROUND(:NEW.VRTXADMPROD, 5);
      END IF;

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

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

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

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

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

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

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

      IF NVL(:OLD.QTESTMINSAID, 0) <> NVL(:NEW.QTESTMINSAID, 0) THEN
         V_DESC := V_DESC || ' EstMinSa=' ||  ROUND(:NEW.QTESTMINSAID, 5);
      END IF;

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

      IF NVL(:OLD.CDALMOXPRSEMACA,'') <> NVL(:NEW.CDALMOXPRSEMACA,'') THEN
         V_DESC := V_DESC || ' AlmPrSemAc=' || :NEW.CDALMOXPRSEMACA;
      END IF;

      IF NVL(:OLD.CDLOCALPRSEMACA,'') <> NVL(:NEW.CDLOCALPRSEMACA,'') THEN
         V_DESC := V_DESC || ' LcPrSemAc=' || :NEW.CDLOCALPRSEMACA;
      END IF;

      IF NVL(:OLD.CDALMOXREPROV,'') <> NVL(:NEW.CDALMOXREPROV,'') THEN
         V_DESC := V_DESC || ' AlmReprov=' || :NEW.CDALMOXREPROV;
      END IF;

      IF NVL(:OLD.CDLOCALREPROV,'') <> NVL(:NEW.CDLOCALREPROV,'') THEN
         V_DESC := V_DESC || ' LcReprov=' || :NEW.CDLOCALREPROV;
      END IF;

      IF NVL(:OLD.VRALIQPISSOL, 0) <> NVL(:NEW.VRALIQPISSOL, 0) THEN
         V_DESC := V_DESC || ' AliqPISOL=' || ROUND(:NEW.VRALIQPISSOL, 2);
      END IF;

      IF NVL(:OLD.VRALIQCOFINSSOL, 0) <> NVL(:NEW.VRALIQCOFINSSOL, 0) THEN
         V_DESC := V_DESC || ' AliqCofinsOL=' || ROUND(:NEW.VRALIQCOFINSSOL,2);
      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
               ('PRODFILI',  V_DTOPER, V_USUARIO,
                V_TIPO,      V_ITPESQ, SUBSTR(V_DESC, 1, 700), V_CDFILLOGOPER,
                V_DSSERVERRDM, V_DSREMOTEADDR, V_DSREMOTEHOST,
                V_DSSERVMETHOD, V_DSREMOTEOPER);
         END IF;
      END IF;
   END IF;
END;
/