CREATE OR REPLACE TRIGGER LOG_GER_LOTE_SISTEMA
AFTER DELETE OR INSERT OR UPDATE
ON LOTE FOR EACH ROW
DECLARE
  V_USUARIO   VARCHAR2(40);
  V_TIPO      VARCHAR2(1);
  V_ITPESQ    VARCHAR2(40);
  V_DESC      VARCHAR2(300);
  V_DTOPER    DATE;
  V_DSREMOTEADDR VARCHAR2(15);
  V_DSREMOTEHOST VARCHAR2(15);
  V_DSSERVERRDM  VARCHAR2(20);
  V_DSSERVMETHOD VARCHAR2(30);
  V_DSREMOTEOPER VARCHAR2(12);
  V_CDFILLOGOPER VARCHAR2(4);
BEGIN
   V_TIPO := '0';
   V_DESC := '';

   IF DELETING THEN
      IF :OLD.CDFILIAL <> ' ' THEN
	      V_DESC := V_DESC || 'CodFilial=' || :OLD.CDFILIAL;
      END IF;

      IF :OLD.NRLOTEESTQ <> ' ' THEN
         V_DESC := V_DESC || ' NumLotEs=' || :OLD.NRLOTEESTQ;
      END IF;

      IF :OLD.NRSUBLOTE <> ' ' THEN
         V_DESC := V_DESC || ' NumSubLo=' || :OLD.NRSUBLOTE;
      END IF;

      IF :OLD.CDPRODUTO <> ' ' THEN
	      V_DESC := V_DESC || ' CodProd=' || :OLD.CDPRODUTO;
      END IF;

      IF :OLD.IDSTATLOTE <> ' ' THEN
         V_DESC := V_DESC || ' IdStatus=' || :OLD.IDSTATLOTE;
      END IF;

      IF :OLD.DTREALIBLOTE IS NOT NULL THEN
         V_DESC := V_DESC || ' DtRealLi=' || :OLD.DTREALIBLOTE;
      END IF;

      IF :OLD.DTVALILOTE IS NOT NULL THEN
         V_DESC := V_DESC || ' DtValiLo=' || :OLD.DTVALILOTE;
      END IF;

      IF :OLD.IDSTANALISE <> ' ' THEN
         V_DESC := V_DESC || ' IdSitAna=' || :OLD.IDSTANALISE;
      END IF;

      IF :OLD.DTVALANALOTE IS NOT NULL THEN
         V_DESC := V_DESC || ' DtValAna=' || :OLD.DTVALANALOTE;
      END IF;

      IF :OLD.DTFABRPROD IS NOT NULL THEN
         V_DESC := V_DESC || ' DtFabPro=' || :OLD.DTFABRPROD;
      END IF;

      IF :OLD.CDORDENALOTE <> ' ' THEN
         V_DESC := V_DESC || ' CdOrdena=' || :OLD.CDORDENALOTE;
      END IF;

      V_TIPO   := '3';
      V_ITPESQ := :OLD.CDFILIAL || :OLD.NRLOTEESTQ || :OLD.NRSUBLOTE || :OLD.CDPRODUTO;
      V_CDFILLOGOPER := :OLD.CDFILIAL;
   ELSE
      IF UPDATING THEN
         IF (:OLD.CDFILIAL     <> :NEW.CDFILIAL)     OR
            (:OLD.NRLOTEESTQ   <> :NEW.NRLOTEESTQ)   OR
            (:OLD.NRSUBLOTE    <> :NEW.NRSUBLOTE)    OR
	         (:OLD.CDPRODUTO    <> :NEW.CDPRODUTO)    OR
            (:OLD.IDSTATLOTE   <> :NEW.IDSTATLOTE)   OR
            (:OLD.DTREALIBLOTE <> :NEW.DTREALIBLOTE) OR
            (:OLD.DTVALILOTE   <> :NEW.DTVALILOTE)   OR
            (:OLD.IDSTANALISE  <> :NEW.IDSTANALISE)  OR
            (:OLD.DTVALANALOTE <> :NEW.DTVALANALOTE) OR
            (:OLD.DTFABRPROD   <> :NEW.DTFABRPROD)   OR
            (:OLD.CDORDENALOTE <> :NEW.CDORDENALOTE) THEN

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

      IF :NEW.CDFILIAL <> ' ' THEN
         V_DESC := V_DESC || 'CodFilial=' || :NEW.CDFILIAL;
      END IF;

      IF :NEW.NRLOTEESTQ <> ' ' THEN
         V_DESC := V_DESC || ' NumLotEs=' || :NEW.NRLOTEESTQ;
      END IF;

      IF :NEW.NRSUBLOTE <> ' ' THEN
         V_DESC := V_DESC || ' NumSubLo=' || :NEW.NRSUBLOTE;
      END IF;

      IF :NEW.CDPRODUTO <> ' ' THEN
         V_DESC := V_DESC || ' CodProd=' || :NEW.CDPRODUTO;
      END IF;

      IF :NEW.IDSTATLOTE <> ' ' THEN
         V_DESC := V_DESC || ' IdStatus=' || :NEW.IDSTATLOTE;
      END IF;

      IF :NEW.DTREALIBLOTE IS NOT NULL THEN
         V_DESC := V_DESC || ' DtRealLi=' || :NEW.DTREALIBLOTE;
      END IF;

      IF :NEW.DTVALILOTE IS NOT NULL THEN
         V_DESC := V_DESC || ' DtValiLo=' || :NEW.DTVALILOTE;
      END IF;

      IF :NEW.IDSTANALISE <> ' ' THEN
         V_DESC := V_DESC || ' IdSitAna=' || :NEW.IDSTANALISE;
      END IF;

      IF :NEW.DTVALANALOTE IS NOT NULL THEN
         V_DESC := V_DESC || ' DtValAna=' || :NEW.DTVALANALOTE;
      END IF;

      IF :NEW.DTFABRPROD IS NOT NULL THEN
         V_DESC := V_DESC || ' DtFabPro=' || :NEW.DTFABRPROD;
       END IF;

      IF :NEW.CDORDENALOTE <> ' ' THEN
         V_DESC := V_DESC || ' CdOrdena=' || :NEW.CDORDENALOTE;
      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,
                DSRDMUSUOPER, DSENDUSUOPER, DSNMUSUOPER,
                DSMETUSUOPER, CDORESPOPER, CDFILLOGOPER)
            VALUES
               ('LOTE', V_DTOPER, V_USUARIO,
                V_TIPO, V_ITPESQ, V_DESC,
                V_DSSERVERRDM, V_DSREMOTEADDR, V_DSREMOTEHOST,
                V_DSSERVMETHOD, V_DSREMOTEOPER, V_CDFILLOGOPER);
         END IF;
      END IF;
   END IF;
END;
/
