/*
   @C:\web_619\sql\end_conv\oracle\02CR_GERTRG_LOG_EST_CUSTOCMPPROD.SQL;
*/
CREATE OR REPLACE TRIGGER LOG_EST_CUSTOCMPPROD
AFTER DELETE OR INSERT OR UPDATE
ON CUSTOCMPPROD FOR EACH ROW
DECLARE
  V_USUARIO      VARCHAR2(40);
  V_TIPO         VARCHAR2(1);
  V_ITPESQ       VARCHAR2(80);
  V_DESC         VARCHAR2(1300);
  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.CDFILIAL IS NOT NULL THEN
         V_DESC := V_DESC || 'Filial=' || :OLD.CDFILIAL;
      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 || ' Local=' || :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.CDPRODUTO IS NOT NULL THEN
         V_DESC := V_DESC || ' Prod=' || :OLD.CDPRODUTO;
      END IF;

      IF :OLD.DTCUSTCMPPRO IS NOT NULL THEN
         V_DESC := V_DESC || ' Data=' || :OLD.DTCUSTCMPPRO;
      END IF;

      IF :OLD.CDFILLANCEST IS NOT NULL THEN
         V_DESC := V_DESC || ' FilLanc=' || :OLD.CDFILLANCEST;
      END IF;

      IF :OLD.NRLANCESTQ IS NOT NULL THEN
         V_DESC := V_DESC || ' Lanc=' || :OLD.NRLANCESTQ;
      END IF;

      IF :OLD.VRLIQCMPPROD IS NOT NULL THEN
         V_DESC := V_DESC || ' VrLiq=' || :OLD.VRLIQCMPPROD;
      END IF;

      IF :OLD.VRBRUCMPPROD IS NOT NULL THEN
         V_DESC := V_DESC || ' VrBrut=' || :OLD.VRBRUCMPPROD;
      END IF;

      V_TIPO := '3';

      V_ITPESQ       := :OLD.CDFILIAL||:OLD.CDALMOXARIFE||:OLD.CDLOCALESTOQ||:OLD.NRLOTEESTQ||:OLD.NRSUBLOTE||:OLD.DTCUSTCMPPRO;
      V_CDFILLOGOPER := :OLD.CDFILIAL;
   ELSE
      IF UPDATING THEN
         IF (:OLD.CDFILLANCEST <> :NEW.CDFILLANCEST)     OR
            (:OLD.VRLIQCMPPROD <> :NEW.VRLIQCMPPROD)     OR
            (:OLD.VRLIQCMPPRM1 <> :NEW.VRLIQCMPPRM1)     OR
            (:OLD.VRLIQCMPPRM2 <> :NEW.VRLIQCMPPRM2)     OR
            (:OLD.VRBRUCMPPROD <> :NEW.VRBRUCMPPROD)     OR
            (:OLD.VRBRUCMPPRM1 <> :NEW.VRBRUCMPPRM1)     OR
            (:OLD.VRBRUCMPPRM2 <> :NEW.VRBRUCMPPRM2) THEN

            V_TIPO         := '2';
            V_ITPESQ       := :OLD.CDFILIAL||:OLD.CDALMOXARIFE||:OLD.CDLOCALESTOQ||:OLD.NRLOTEESTQ||:OLD.NRSUBLOTE||:OLD.DTCUSTCMPPRO;
            V_CDFILLOGOPER := :OLD.CDFILIAL;


         END IF;
      ELSE
         V_TIPO         := '1';
         V_ITPESQ       := :NEW.CDFILIAL||:NEW.CDALMOXARIFE||:NEW.CDLOCALESTOQ||:NEW.NRLOTEESTQ||:NEW.NRSUBLOTE||:NEW.DTCUSTCMPPRO;
         V_CDFILLOGOPER := :NEW.CDFILIAL;
      END IF;

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

      IF NVL(:OLD.VRLIQCMPPROD,0) <> NVL(:NEW.VRLIQCMPPROD,0) THEN
         V_DESC := V_DESC || ' VrLiq=' || :NEW.VRLIQCMPPROD;
      END IF;

      IF NVL(:OLD.VRLIQCMPPRM1,0) <> NVL(:NEW.VRLIQCMPPRM1,0) THEN
         V_DESC := V_DESC || ' VrLiqInd1=' || :NEW.VRLIQCMPPRM1;
      END IF;

      IF NVL(:OLD.VRLIQCMPPRM2,0) <> NVL(:NEW.VRLIQCMPPRM2,0) THEN
         V_DESC := V_DESC || ' VrLiqInd2=' || :NEW.VRLIQCMPPRM2;
      END IF;

      IF NVL(:OLD.VRBRUCMPPROD,0) <> NVL(:NEW.VRBRUCMPPROD,0) THEN
         V_DESC := V_DESC || ' VrBrut=' || :OLD.VRBRUCMPPROD;
      END IF;

      IF NVL(:OLD.VRBRUCMPPRM1,0) <> NVL(:NEW.VRBRUCMPPRM1,0) THEN
         V_DESC := V_DESC || ' VrBrutInd1=' || :OLD.VRBRUCMPPRM1;
      END IF;

      IF NVL(:OLD.VRBRUCMPPRM2,0) <> NVL(:NEW.VRBRUCMPPRM2,0) THEN
         V_DESC := V_DESC || ' VrBrutInd2=' || :OLD.VRBRUCMPPRM2;
      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
               ('CUSTOCMPPROD',  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;
/