/* Tabela ITLANCTOEST: Item do Lancamento de Estoque */
/* Descricao: A trigger insere registros na tabela LOGOPERA a partir de alteracoes na tabela ITLANCTOEST */
/* Objetivo: Gerar um log que vai registrar as alteracoes na tabela Item do Lancamento de Estoque,
             utilizando a tabela Geral de Logs de Operacoes */
CREATE OR REPLACE TRIGGER LOG_EST_ITLANCTOEST
AFTER DELETE OR INSERT OR UPDATE
ON ITLANCTOEST FOR EACH ROW
DECLARE
  V_USUARIO      VARCHAR2(40);
  V_TIPO         VARCHAR2(1);
  V_ITPESQ       VARCHAR2(40);
  V_DESC         VARCHAR2(700);
  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.QTLANCTOEST IS NOT NULL THEN
         V_DESC := V_DESC || ' QtLancEst=' || ROUND(:OLD.QTLANCTOEST, 5);
      END IF;

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

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

      IF :OLD.IDTIPOMOVI IS NOT NULL THEN
         V_DESC := V_DESC || ' TipoMovi=' || :OLD.IDTIPOMOVI;
      END IF;

      IF :OLD.DTLANCMOVI IS NOT NULL THEN
         V_DESC := V_DESC || ' DtLanc=' || :OLD.DTLANCMOVI;
      END IF;

      IF :OLD.IDORDEMCLC IS NOT NULL THEN
         V_DESC := V_DESC || ' OrdCLC=' || :OLD.IDORDEMCLC;
      END IF;

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

      IF :OLD.IDALTCMEDMV IS NOT NULL THEN
         V_DESC := V_DESC || ' AltCMed=' || :OLD.IDALTCMEDMV;
      END IF;

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

      IF :OLD.CDPRODMOVI IS NOT NULL THEN
         V_DESC := V_DESC || ' ProdMovi=' || :OLD.CDPRODMOVI;
      END IF;

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

      IF :OLD.IDEVOLESTOQ IS NOT NULL THEN
         V_DESC := V_DESC || ' EvolEstoq=' || :OLD.IDEVOLESTOQ;
      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.NRLANCORIGRP IS NOT NULL THEN
         V_DESC := V_DESC || ' LancOrig=' || :OLD.NRLANCORIGRP;
      END IF;

      IF :OLD.CDCENTCUST IS NOT NULL THEN
         V_DESC := V_DESC || ' CCusto=' || :OLD.CDCENTCUST;
      END IF;

      IF :OLD.CDTIPORETI IS NOT NULL THEN
         V_DESC := V_DESC || ' TipoReti=' || :OLD.CDTIPORETI;
      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 || ' NRPedido=' || :OLD.NRPEDIDO;
      END IF;

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

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

      V_TIPO         := '3';
      V_ITPESQ       := :OLD.CDFILIAL||:OLD.NRLANCESTQ||:OLD.NRSEQUITEM;
      V_CDFILLOGOPER := :OLD.CDFILIAL;
   ELSE
      IF UPDATING THEN
         IF (:OLD.CDPRODUTO    <> :NEW.CDPRODUTO)    OR
            (:OLD.QTLANCTOEST  <> :NEW.QTLANCTOEST)  OR
            (:OLD.VRUNILANCTO  <> :NEW.VRUNILANCTO)  OR
            (:OLD.VRTOTLANCTO  <> :NEW.VRTOTLANCTO)  OR
            (:OLD.IDTIPOMOVI   <> :NEW.IDTIPOMOVI)   OR
            (:OLD.DTLANCMOVI   <> :NEW.DTLANCMOVI)   OR
            (:OLD.IDORDEMCLC   <> :NEW.IDORDEMCLC)   OR
            (:OLD.VRLANCTOEST  <> :NEW.VRLANCTOEST)  OR
            (:OLD.IDALTCMEDMV  <> :NEW.IDALTCMEDMV)  OR
            (:OLD.QTTOTLANCTO  <> :NEW.QTTOTLANCTO)  OR
            (:OLD.CDPRODMOVI   <> :NEW.CDPRODMOVI)   OR
            (:OLD.VRLANCTOBRUT <> :NEW.VRLANCTOBRUT) OR
            (:OLD.IDEVOLESTOQ  <> :NEW.IDEVOLESTOQ)  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.NRLANCORIGRP <> :NEW.NRLANCORIGRP) OR
            (:OLD.CDCENTCUST   <> :NEW.CDCENTCUST)   OR
            (:OLD.CDTIPORETI   <> :NEW.CDTIPORETI)   OR
            (:OLD.CDFILIALPED  <> :NEW.CDFILIALPED)  OR
            (:OLD.NRPEDIDO     <> :NEW.NRPEDIDO)     OR
            (:OLD.NRITEMPED    <> :NEW.NRITEMPED)    OR
            (:OLD.QTDEVOLESTOQ <> :NEW.QTDEVOLESTOQ) THEN

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

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

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

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

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

      IF :NEW.IDTIPOMOVI IS NOT NULL THEN
         V_DESC := V_DESC || ' TipoMovi=' || :NEW.IDTIPOMOVI;
      END IF;

      IF :NEW.DTLANCMOVI IS NOT NULL THEN
         V_DESC := V_DESC || ' DtLanc=' || :NEW.DTLANCMOVI;
      END IF;

      IF :NEW.IDORDEMCLC IS NOT NULL THEN
         V_DESC := V_DESC || ' OrdCLC=' || :NEW.IDORDEMCLC;
      END IF;

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

      IF :NEW.IDALTCMEDMV IS NOT NULL THEN
         V_DESC := V_DESC || ' AltCMed=' || :NEW.IDALTCMEDMV;
      END IF;

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

      IF :NEW.CDPRODMOVI IS NOT NULL THEN
         V_DESC := V_DESC || ' ProdMovi=' || :NEW.CDPRODMOVI;
      END IF;

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

      IF :NEW.IDEVOLESTOQ IS NOT NULL THEN
         V_DESC := V_DESC || ' EvolEstoq=' || :NEW.IDEVOLESTOQ;
      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.NRLANCORIGRP IS NOT NULL THEN
         V_DESC := V_DESC || ' LancOrig=' || :NEW.NRLANCORIGRP;
      END IF;

      IF :NEW.CDCENTCUST IS NOT NULL THEN
         V_DESC := V_DESC || ' CCusto=' || :NEW.CDCENTCUST;
      END IF;

      IF :NEW.CDTIPORETI IS NOT NULL THEN
         V_DESC := V_DESC || ' TipoReti=' || :NEW.CDTIPORETI;
      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 || ' NRPedido=' || :NEW.NRPEDIDO;
      END IF;

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

      IF :NEW.QTDEVOLESTOQ IS NOT NULL THEN
         V_DESC := V_DESC || ' QtDevol=' || ROUND(:NEW.QTDEVOLESTOQ, 5);
      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
               ('ITLANCTOEST',  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;
/