CREATE OR REPLACE TRIGGER LOG_SUP_ITEMPEDCOL
AFTER DELETE OR INSERT OR UPDATE ON ITEMPEDCOL 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.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.CDFORNECED IS NOT NULL THEN
         V_DESC := V_DESC || ' CDFORNECED=' || :OLD.CDFORNECED;
      END IF;
      
      IF :OLD.CDREGIAO IS NOT NULL THEN
         V_DESC := V_DESC || ' CDREGIAO=' || :OLD.CDREGIAO;
      END IF;
      
      IF :OLD.CDPRODUTO IS NOT NULL THEN
         V_DESC := V_DESC || ' CDPRODUTO=' || :OLD.CDPRODUTO;
      END IF;
      
      IF :OLD.CDMARCA IS NOT NULL THEN
         V_DESC := V_DESC || ' CDMARCA=' || :OLD.CDMARCA;
      END IF;
      
      IF :OLD.QTPRODPEDCOL IS NOT NULL THEN
         V_DESC := V_DESC || ' QTPRODPEDCOL=' || :OLD.QTPRODPEDCOL;
      END IF;
      
      IF :OLD.VRITEMPEDCOL IS NOT NULL THEN
         V_DESC := V_DESC || ' VRITEMPEDCOL=' || :OLD.VRITEMPEDCOL;
      END IF;
      
        IF :OLD.IDSELELEILAO IS NOT NULL THEN
         V_DESC := V_DESC || ' IDSELELEILAO=' || :OLD.IDSELELEILAO;
      END IF;
      
      IF :OLD.VRANTITPEDCOL IS NOT NULL THEN
         V_DESC := V_DESC || ' VRANTITPEDCOL=' || :OLD.VRANTITPEDCOL;
      END IF;
      
      IF :OLD.VRPRECECOPEDCOL IS NOT NULL THEN
         V_DESC := V_DESC || ' VRPRECECOPEDCOL=' || :OLD.VRPRECECOPEDCOL;
      END IF;
      
      IF :OLD.VRPERDESPEDCOL IS NOT NULL THEN
         V_DESC := V_DESC || ' VRPERDESPEDCOL=' || :OLD.VRPERDESPEDCOL;
      END IF;
      
      IF :OLD.VRPRECPIPEDCOL IS NOT NULL THEN
         V_DESC := V_DESC || ' VRPRECPIPEDCOL=' || :OLD.VRPRECPIPEDCOL;
      END IF;
      
      IF :OLD.DTULTATUITPCOL IS NOT NULL THEN
         V_DESC := V_DESC || ' DTULTATUITPCOL=' || :OLD.DTULTATUITPCOL;
      END IF;
      
      IF :OLD.IDITEMPEDCOLALT IS NOT NULL THEN
         V_DESC := V_DESC || ' IDITEMPEDCOLALT=' || :OLD.IDITEMPEDCOLALT;
      END IF;
      
      IF :OLD.DSOBSRENEGOCIA IS NOT NULL THEN
         V_DESC := V_DESC || ' DSOBSRENEGOCIA=' || :OLD.DSOBSRENEGOCIA;
      END IF;
           
      IF :OLD.VRPRECREN IS NOT NULL THEN
         V_DESC := V_DESC || ' VRPRECREN=' || :OLD.VRPRECREN;
      END IF;
      
      IF :OLD.IDPRECONPCOLATU IS NOT NULL THEN
         V_DESC := V_DESC || ' IDPRECONPCOLATU=' || :OLD.IDPRECONPCOLATU;
      END IF;
      
      IF :OLD.IDITPEDPRIONEG IS NOT NULL THEN
         V_DESC := V_DESC || ' IDITPEDPRIONEG=' || :OLD.IDITPEDPRIONEG;
      END IF;
      
      IF :OLD.VRPRECRENECO IS NOT NULL THEN
         V_DESC := V_DESC || ' VRPRECRENECO=' || :OLD.VRPRECRENECO;
      END IF;
      
      IF :OLD.VRPRECOOBJETIVO IS NOT NULL THEN
         V_DESC := V_DESC || ' VRPRECOOBJETIVO=' || :OLD.VRPRECOOBJETIVO;
      END IF;
      
      IF :OLD.VRPRECOOBJEECO IS NOT NULL THEN
         V_DESC := V_DESC || ' VRPRECOOBJEECO=' || :OLD.VRPRECOOBJEECO;
      END IF;
      
      IF :OLD.VRPRECOBJEECOPI IS NOT NULL THEN
         V_DESC := V_DESC || ' VRPRECOBJEECOPI=' || :OLD.VRPRECOBJEECOPI;
      END IF;
      
      IF :OLD.VRPRECRENECOPI IS NOT NULL THEN
         V_DESC := V_DESC || ' VRPRECRENECOPI=' || :OLD.VRPRECRENECOPI;
      END IF;
      
      V_TIPO := '3';
      V_ITPESQ := :OLD.CDFILIAL || :OLD.NRPEDCOLETA || :OLD.CDFORNECED || :OLD.CDREGIAO || :OLD.CDPRODUTO || :OLD.CDMARCA;
   ELSE
      IF UPDATING THEN      
         IF (:OLD.CDFILIAL    <> :NEW.CDFILIAL) OR
            (:OLD.NRPEDCOLETA <> :NEW.NRPEDCOLETA) OR
            (:OLD.CDFORNECED  <> :NEW.CDFORNECED) OR
            (:OLD.CDREGIAO    <> :NEW.CDREGIAO ) OR
            (:OLD.CDPRODUTO   <> :NEW.CDPRODUTO) OR
            (:OLD.CDMARCA     <> :NEW.CDMARCA) OR
            (NVL(:OLD.QTPRODPEDCOL, 0)         <> NVL(:NEW.QTPRODPEDCOL, 0))  OR
            (NVL(:OLD.VRITEMPEDCOL, 0)         <> NVL(:NEW.VRITEMPEDCOL, 0))  OR
            (NVL(:OLD.VRANTITPEDCOL, 0)        <> NVL(:NEW.VRANTITPEDCOL, 0))  OR
            (NVL(:OLD.VRPRECECOPEDCOL, 0)      <> NVL(:NEW.VRPRECECOPEDCOL, 0))  OR
            (NVL(:OLD.VRPERDESPEDCOL, 0)       <> NVL(:NEW.VRPERDESPEDCOL, 0))  OR
            (NVL(:OLD.VRPRECPIPEDCOL, 0)       <> NVL(:NEW.VRPRECPIPEDCOL, 0))  OR
            (NVL(:OLD.VRPRECREN, 0)            <> NVL(:NEW.VRPRECREN, 0))  OR
            (NVL(:OLD.VRPRECOOBJETIVO, 0)      <> NVL(:NEW.VRPRECOOBJETIVO, 0))  OR
            (NVL(:OLD.VRPRECRENECO, 0)         <> NVL(:NEW.VRPRECRENECO, 0))  OR
            (NVL(:OLD.VRPRECOOBJEECO, 0)       <> NVL(:NEW.VRPRECOOBJEECO, 0))  OR
            (NVL(:OLD.VRPRECOBJEECOPI, 0)      <> NVL(:NEW.VRPRECOBJEECOPI, 0))  OR
            (NVL(:OLD.VRPRECRENECOPI, 0)       <> NVL(:NEW.VRPRECRENECOPI, 0))  OR
            (NVL(:OLD.DTULTATUITPCOL, SYSDATE) <> NVL(:NEW.DTULTATUITPCOL, SYSDATE)) OR
            (NVL(:OLD.IDITEMPEDCOLALT, ' ')    <> NVL(:NEW.IDITEMPEDCOLALT, ' '))  OR
            (NVL(:OLD.IDPRECONPCOLATU, ' ')    <> NVL(:NEW.IDPRECONPCOLATU, ' '))    OR
            (NVL(:OLD.IDITPEDPRIONEG, ' ')     <> NVL(:NEW.IDITPEDPRIONEG, ' '))   OR
            (NVL(:OLD.DSOBSRENEGOCIA, ' ')     <> NVL(:NEW.DSOBSRENEGOCIA, ' '))    OR
            (NVL(:OLD.IDSELELEILAO, ' ')       <> NVL(:NEW.IDSELELEILAO, ' '))  THEN
             
            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.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.CDPRODUTO, ' ') <> NVL(:NEW.CDPRODUTO, ' ') THEN
               V_DESC := V_DESC || ' CDPRODUTO=' || :OLD.CDPRODUTO || '|' || :NEW.CDPRODUTO;
            END IF;

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

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

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

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

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

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

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

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

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

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

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

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

            IF NVL(:OLD.VRPRECRENECOPI, 0) <> NVL(:NEW.VRPRECRENECOPI, 0) THEN
               V_DESC := V_DESC || ' VRPRECRENECOPI=' || :OLD.VRPRECRENECOPI || '|' || :NEW.VRPRECRENECOPI;
            END IF;
            
            IF NVL(:OLD.DTULTATUITPCOL, SYSDATE) <> NVL(:NEW.DTULTATUITPCOL, SYSDATE) THEN
               V_DESC := V_DESC || ' DTULTATUITPCOL=' || :OLD.DTULTATUITPCOL || '|' || :NEW.DTULTATUITPCOL;
            END IF;
            
            IF NVL(:OLD.IDITEMPEDCOLALT, ' ') <> NVL(:NEW.IDITEMPEDCOLALT, ' ') THEN
               V_DESC := V_DESC || ' IDITEMPEDCOLALT=' || :OLD.IDITEMPEDCOLALT || '|' || :NEW.IDITEMPEDCOLALT;
            END IF;

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

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

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

            IF NVL(:OLD.IDSELELEILAO, ' ') <> NVL(:NEW.IDSELELEILAO, ' ') THEN
               V_DESC := V_DESC || ' IDSELELEILAO=' || :OLD.IDSELELEILAO || '|' || :NEW.IDSELELEILAO;
            END IF;
            
            V_TIPO   := '2';
            V_ITPESQ := :OLD.CDFILIAL || :OLD.NRPEDCOLETA || :OLD.CDFORNECED || :OLD.CDREGIAO || :OLD.CDPRODUTO || :OLD.CDMARCA;
         END IF;
      ELSE
         V_DESC := V_DESC || 'CDFILIAL=' || :NEW.CDFILIAL;
         V_DESC := V_DESC || ' NRPEDCOLETA=' || :NEW.NRPEDCOLETA;
         V_DESC := V_DESC || ' CDFORNECED=' || :NEW.CDFORNECED;
         V_DESC := V_DESC || ' CDREGIAO=' || :NEW.CDREGIAO;
         V_DESC := V_DESC || ' CDPRODUTO=' || :NEW.CDPRODUTO;
         V_DESC := V_DESC || ' CDMARCA=' || :NEW.CDMARCA;        
         V_DESC := V_DESC || ' QTPRODPEDCOL=' || :NEW.QTPRODPEDCOL;
         V_DESC := V_DESC || ' VRITEMPEDCOL=' || :NEW.VRITEMPEDCOL;
         V_DESC := V_DESC || ' VRANTITPEDCOL=' || :NEW.VRANTITPEDCOL;
         V_DESC := V_DESC || ' VRPRECECOPEDCOL=' || :NEW.VRPRECECOPEDCOL;
         V_DESC := V_DESC || ' VRPERDESPEDCOL=' || :NEW.VRPERDESPEDCOL;
         V_DESC := V_DESC || ' VRPRECPIPEDCOL=' || :NEW.VRPRECPIPEDCOL;
         V_DESC := V_DESC || ' VRPRECREN=' || :NEW.VRPRECREN;
         V_DESC := V_DESC || ' VRPRECOOBJETIVO=' || :NEW.VRPRECOOBJETIVO;
         V_DESC := V_DESC || ' VRPRECRENECO=' || :NEW.VRPRECRENECO;
         V_DESC := V_DESC || ' VRPRECOOBJEECO=' || :NEW.VRPRECOOBJEECO;
         V_DESC := V_DESC || ' VRPRECOBJEECOPI=' || :NEW.VRPRECOBJEECOPI;
         V_DESC := V_DESC || ' VRPRECRENECOPI=' || :NEW.VRPRECRENECOPI;
         V_DESC := V_DESC || ' DTULTATUITPCOL=' || :NEW.DTULTATUITPCOL;
         V_DESC := V_DESC || ' IDITEMPEDCOLALT=' || :NEW.IDITEMPEDCOLALT;
         V_DESC := V_DESC || ' IDPRECONPCOLATU=' || :NEW.IDPRECONPCOLATU;
         V_DESC := V_DESC || ' IDITPEDPRIONEG=' || :NEW.IDITPEDPRIONEG;
         V_DESC := V_DESC || ' DSOBSRENEGOCIA=' || :NEW.DSOBSRENEGOCIA;
         V_DESC := V_DESC || ' IDSELELEILAO=' || :NEW.IDSELELEILAO;         
              
         V_TIPO   := '1';
         V_ITPESQ := :NEW.CDFILIAL || :NEW.NRPEDCOLETA || :NEW.CDFORNECED || :NEW.CDREGIAO || :NEW.CDPRODUTO || :NEW.CDMARCA;
      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
               ('ITEMPEDCOL', 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;
/
