CREATE OR REPLACE TRIGGER LOG_SUP_PEDCOLGERAL
AFTER DELETE OR INSERT OR UPDATE ON PEDCOLGERAL 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.NRPEDIDOCOL IS NOT NULL THEN
         V_DESC := V_DESC || ' NRPEDIDOCOL=' || :OLD.NRPEDIDOCOL;
      END IF;

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

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

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

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

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

      IF :OLD.IDSITUPEDCOLETA IS NOT NULL THEN
         V_DESC := V_DESC || ' IDSITUPEDCOLETA=' || :OLD.IDSITUPEDCOLETA;
      END IF;
      
      IF :OLD.IDRENEGOCIAPCOL IS NOT NULL THEN
         V_DESC := V_DESC || ' IDRENEGOCIAPCOL=' || :OLD.IDRENEGOCIAPCOL;
      END IF;
      
      V_TIPO := '3';
      V_ITPESQ := :OLD.CDFILIAL || :OLD.NRPEDIDOCOL;
   ELSE
      IF UPDATING THEN      
         IF (:OLD.CDFILIAL    <> :NEW.CDFILIAL) OR
            (:OLD.NRPEDIDOCOL <> :NEW.NRPEDIDOCOL ) OR
            (NVL(:OLD.DTPEDCOLGERAL, SYSDATE)  <> NVL(:NEW.DTPEDCOLGERAL, SYSDATE)) OR
            (NVL(:OLD.DTINIPERGERAL, SYSDATE)  <> NVL(:NEW.DTINIPERGERAL, SYSDATE)) OR
            (NVL(:OLD.DTFINPERGERAL, SYSDATE)  <> NVL(:NEW.DTFINPERGERAL, SYSDATE)) OR
            (NVL(:OLD.DTEXPIRAPEDCOL, SYSDATE) <> NVL(:NEW.DTEXPIRAPEDCOL, SYSDATE)) OR
            (NVL(:OLD.IDPEDCOLCONS, ' ')       <> NVL(:NEW.IDPEDCOLCONS, ' '))  OR
            (NVL(:OLD.CDOPERADOR, ' ')         <> NVL(:NEW.CDOPERADOR, ' '))  OR
            (NVL(:OLD.CDOPERLANC, ' ')         <> NVL(:NEW.CDOPERLANC, ' '))  OR
            (NVL(:OLD.IDSITUPEDCOLETA, ' ')    <> NVL(:NEW.IDSITUPEDCOLETA, ' '))  OR
            (NVL(:OLD.IDRENEGOCIAPCOL, ' ')    <> NVL(:NEW.IDRENEGOCIAPCOL, ' ')) THEN
             
            IF NVL(:OLD.CDFILIAL, ' ') <> NVL(:NEW.CDFILIAL, ' ') THEN
               V_DESC := V_DESC || ' CDFILIAL=' || :OLD.CDFILIAL || '|' || :NEW.CDFILIAL;
            END IF;
            
            IF NVL(:OLD.NRPEDIDOCOL, ' ') <> NVL(:NEW.NRPEDIDOCOL, ' ') THEN
               V_DESC := V_DESC || ' NRPEDIDOCOL=' || :OLD.NRPEDIDOCOL || '|' || :NEW.NRPEDIDOCOL;
            END IF;

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

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

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

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

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

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

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

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

            V_TIPO   := '2';
            V_ITPESQ := :OLD.CDFILIAL || :OLD.NRPEDIDOCOL;
         END IF;
      ELSE
         V_DESC := V_DESC || ' CDFILIAL=' || :NEW.CDFILIAL;
         V_DESC := V_DESC || ' NRPEDIDOCOL=' || :NEW.NRPEDIDOCOL;
         V_DESC := V_DESC || ' DTPEDCOLGERAL=' || :NEW.DTPEDCOLGERAL;
         V_DESC := V_DESC || ' DTINIPERGERAL=' || :NEW.DTINIPERGERAL;         
         V_DESC := V_DESC || ' DTFINPERGERAL=' || :NEW.DTFINPERGERAL;
         V_DESC := V_DESC || ' DTEXPIRAPEDCOL=' || :NEW.DTEXPIRAPEDCOL;        
         V_DESC := V_DESC || ' IDPEDCOLCONS=' || :NEW.IDPEDCOLCONS;
         V_DESC := V_DESC || ' CDOPERADOR=' || :NEW.CDOPERADOR;
         V_DESC := V_DESC || ' CDOPERLANC=' || :NEW.CDOPERLANC;
         V_DESC := V_DESC || ' IDSITUPEDCOLETA=' || :NEW.IDSITUPEDCOLETA;
         V_DESC := V_DESC || ' IDRENEGOCIAPCOL=' || :NEW.IDRENEGOCIAPCOL;
              
         V_TIPO   := '1';
         V_ITPESQ := :NEW.CDFILIAL || :NEW.NRPEDIDOCOL;
      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
               ('PEDCOLGERAL', 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;
/
