CREATE OR REPLACE TRIGGER LOG_SUP_PEDCOLETA
AFTER DELETE OR INSERT OR UPDATE ON PEDCOLETA FOR EACH ROW
DECLARE
   V_USUARIO    VARCHAR2(40);
   V_TIPO       VARCHAR2(1);
   V_ITPESQ     VARCHAR2(60);
   V_DESC       VARCHAR2(500);
   V_DTOPER     DATE;
   V_DSIP       VARCHAR2(50);
   V_DSUSUARIO  VARCHAR2(50);
   V_DSRDM      VARCHAR2(50);
   V_DSMETODO   VARCHAR2(250);
   V_DSOPERADOR VARCHAR2(12);
BEGIN
   V_TIPO := '0';
   V_DESC := '';
   WEBTUNNING(V_DSIP,
              V_DSUSUARIO,
              V_DSRDM,
              V_DSMETODO,
              V_DSOPERADOR);
   IF DELETING THEN   
      IF :OLD.CDREGIAO IS NOT NULL THEN
         V_DESC := V_DESC || ' CDREGIAO=' || :OLD.CDREGIAO;
      END IF;

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

      IF :OLD.CDFILIAL IS NOT NULL THEN
         V_DESC := V_DESC || 'CDFILIAL=' || :OLD.CDFILIAL;
      END IF;
      
      IF :OLD.NRPEDCOLETA IS NOT NULL THEN
         V_DESC := V_DESC || ' NRPEDCOLETA=' || :OLD.NRPEDCOLETA;
      END IF;
      
      IF :OLD.DTPEDCOLETA IS NOT NULL THEN
         V_DESC := V_DESC || ' DTPEDCOLETA=' || :OLD.DTPEDCOLETA;
      END IF;
      
      IF :OLD.CDOPERADOR IS NOT NULL THEN
         V_DESC := V_DESC || ' CDOPERADOR=' || :OLD.CDOPERADOR;
      END IF;
      
      IF :OLD.DTINIPERCOT IS NOT NULL THEN
         V_DESC := V_DESC || ' DTINIPERCOT=' || :OLD.DTINIPERCOT;
      END IF;

      IF :OLD.DTFINPERCOT IS NOT NULL THEN
         V_DESC := V_DESC || ' DTFINPERCOT=' || :OLD.DTFINPERCOT;
      END IF;
      
      IF :OLD.CDLEILAO IS NOT NULL THEN
         V_DESC := V_DESC || ' CDLEILAO=' || :OLD.CDLEILAO;
      END IF;

      IF :OLD.IDSITUPEDCOL IS NOT NULL THEN
         V_DESC := V_DESC || ' IDSITUPEDCOL=' || :OLD.IDSITUPEDCOL;
      END IF;
      
      IF :OLD.NRPEDIDOCOL IS NOT NULL THEN
         V_DESC := V_DESC || ' NRPEDIDOCOL=' || :OLD.NRPEDIDOCOL;
      END IF;
      
      IF :OLD.IDSITUENVPEDMCD IS NOT NULL THEN
         V_DESC := V_DESC || ' IDSITUENVPEDMCD=' || :OLD.IDSITUENVPEDMCD;
      END IF;
      
      IF :OLD.VRFRETEFORNEPEDCOL IS NOT NULL THEN
         V_DESC := V_DESC || ' VRFRETEFORNEPEDCOL=' || :OLD.VRFRETEFORNEPEDCOL;
      END IF;
      
      V_TIPO := '3';
      V_ITPESQ := :OLD.CDREGIAO || :OLD.CDFORNECED || :OLD.CDFILIAL || :OLD.NRPEDCOLETA;
   ELSE
      IF UPDATING THEN      
         IF (:OLD.CDFORNECED  <> :NEW.CDFORNECED) OR
            (:OLD.CDREGIAO    <> :NEW.CDREGIAO ) OR
            (:OLD.CDFILIAL    <> :NEW.CDFILIAL) OR
            (:OLD.NRPEDCOLETA <> :NEW.NRPEDCOLETA) OR
            (NVL(:OLD.DTPEDCOLETA, SYSDATE)  <> NVL(:NEW.DTPEDCOLETA, SYSDATE)) OR
            (NVL(:OLD.DTINIPERCOT, SYSDATE)  <> NVL(:NEW.DTINIPERCOT, SYSDATE)) OR
            (NVL(:OLD.DTFINPERCOT, SYSDATE)  <> NVL(:NEW.DTFINPERCOT, SYSDATE)) OR
            (NVL(:OLD.CDOPERADOR, ' ')       <> NVL(:NEW.CDOPERADOR, ' '))  OR
            (NVL(:OLD.CDLEILAO, ' ')         <> NVL(:NEW.CDLEILAO, ' '))  OR
            (NVL(:OLD.IDSITUPEDCOL, ' ')     <> NVL(:NEW.IDSITUPEDCOL, ' '))  OR
            (NVL(:OLD.NRPEDIDOCOL, ' ')      <> NVL(:NEW.NRPEDIDOCOL, ' '))  OR
            (NVL(:OLD.IDSITUENVPEDMCD, ' ')  <> NVL(:NEW.IDSITUENVPEDMCD, ' '))  OR
            (NVL(:OLD.VRFRETEFORNEPEDCOL, 0) <> NVL(:NEW.VRFRETEFORNEPEDCOL, 0)) THEN
             
            IF NVL(:OLD.CDFORNECED, ' ') <> NVL(:NEW.CDFORNECED, ' ') THEN
               V_DESC := V_DESC || ' CDFORNECED=' || :OLD.CDFORNECED || '|' || :NEW.CDFORNECED;
            END IF;
            
            IF NVL(:OLD.CDREGIAO, ' ') <> NVL(:NEW.CDREGIAO, ' ') THEN
               V_DESC := V_DESC || ' CDREGIAO=' || :OLD.CDREGIAO || '|' || :NEW.CDREGIAO;
            END IF;

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

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

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

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

            IF NVL(:OLD.DTFINPERCOT, SYSDATE) <> NVL(:NEW.DTFINPERCOT, SYSDATE) THEN
               V_DESC := V_DESC || ' DTFINPERCOT=' || :OLD.DTFINPERCOT || '|' || :NEW.DTFINPERCOT;
            END IF;
            
            IF NVL(:OLD.CDOPERADOR, ' ') <> NVL(:NEW.CDOPERADOR, ' ') THEN
               V_DESC := V_DESC || ' CDOPERADOR=' || :OLD.CDOPERADOR || '|' || :NEW.CDOPERADOR;
            END IF;

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

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

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

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

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

            V_TIPO   := '2';
            V_ITPESQ := :OLD.CDREGIAO || :OLD.CDFORNECED || :OLD.CDFILIAL || :OLD.NRPEDCOLETA;
         END IF;
      ELSE
         V_DESC := V_DESC || 'CDFORNECED=' || :NEW.CDFORNECED;
         V_DESC := V_DESC || ' CDREGIAO=' || :NEW.CDREGIAO;
         V_DESC := V_DESC || ' CDFILIAL=' || :NEW.CDFILIAL;
         V_DESC := V_DESC || ' NRPEDCOLETA=' || :NEW.NRPEDCOLETA;         
         V_DESC := V_DESC || ' DTPEDCOLETA=' || :NEW.DTPEDCOLETA;
         V_DESC := V_DESC || ' DTINIPERCOT=' || :NEW.DTINIPERCOT;        
         V_DESC := V_DESC || ' DTFINPERCOT=' || :NEW.DTFINPERCOT;
         V_DESC := V_DESC || ' CDOPERADOR=' || :NEW.CDOPERADOR;
         V_DESC := V_DESC || ' CDLEILAO=' || :NEW.CDLEILAO;
         V_DESC := V_DESC || ' IDSITUPEDCOL=' || :NEW.IDSITUPEDCOL;
         V_DESC := V_DESC || ' NRPEDIDOCOL=' || :NEW.NRPEDIDOCOL;
         V_DESC := V_DESC || ' IDSITUENVPEDMCD=' || :NEW.IDSITUENVPEDMCD;
         V_DESC := V_DESC || ' VRFRETEFORNEPEDCOL=' || :NEW.VRFRETEFORNEPEDCOL;
              
         V_TIPO   := '1';
         V_ITPESQ := :NEW.CDREGIAO || :NEW.CDFORNECED || :NEW.CDFILIAL || :NEW.NRPEDCOLETA;
      END IF;
   END IF;
   IF V_TIPO <> '0' THEN
      NMUSUARIO(V_USUARIO);
      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,
                DSRDMUSUOPER, DSENDUSUOPER, DSNMUSUOPER, DSMETUSUOPER, CDORESPOPER, CDFILLOGOPER)
            VALUES
               ('PEDCOLETA', V_DTOPER, V_USUARIO, V_TIPO, V_ITPESQ, SUBSTR(V_DESC, 1, 300),
                V_DSRDM, V_DSIP, V_DSUSUARIO, V_DSMETODO, V_DSOPERADOR, NVL(:NEW.CDFILIAL, :OLD.CDFILIAL));
         END IF;
      END IF;
   END IF;
END;
/
