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

   IF DELETING THEN
      IF :OLD.DSWEBSERVICE IS NOT NULL THEN
         V_DESC := V_DESC || 'DsWebServ=' || :OLD.DSWEBSERVICE;
      END IF;

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

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

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

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

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

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

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

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

      IF :OLD.IDTIPOOPERWEB IS NOT NULL THEN
         V_DESC := V_DESC || ' E/S=' || :OLD.IDTIPOOPERWEB;
      END IF;

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

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

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

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


      V_TIPO         := '3';
      V_ITPESQ       := :OLD.NRSEQWEBSERVICE;
      V_CDFILLOGOPER := :OLD.CDFILIAL;
   ELSE
      IF UPDATING THEN
         IF (NVL(:OLD.DSWEBSERVICE, '!')    <> NVL(:NEW.DSWEBSERVICE, '!'))    OR
            (NVL(:OLD.DSWEBSERVICEURL, '!') <> NVL(:NEW.DSWEBSERVICEURL, '!')) OR
            (NVL(:OLD.DSCHAVECLIENTE, '!')  <> NVL(:NEW.DSCHAVECLIENTE, '!'))  OR
            (NVL(:OLD.DSSENHACLIENTE, '!')  <> NVL(:NEW.DSSENHACLIENTE, '!'))  OR
            (NVL(:OLD.CDFILIAL, '!')        <> NVL(:NEW.CDFILIAL, '!'))        OR
            (NVL(:OLD.CDPRECOBASE, '!')     <> NVL(:NEW.CDPRECOBASE, '!'))     OR
            (NVL(:OLD.CDOPERADOR, '!')      <> NVL(:NEW.CDOPERADOR, '!'))      OR
            (NVL(:OLD.CDVENDEDOR, '!')      <> NVL(:NEW.CDVENDEDOR, '!'))      OR
            (NVL(:OLD.CDMODPGTO, '!')       <> NVL(:NEW.CDMODPGTO, '!'))       OR
            (NVL(:OLD.IDTIPOOPERWEB, '!')   <> NVL(:NEW.IDTIPOOPERWEB, '!'))   OR
            (NVL(:OLD.CDTIPOOPERWEB, '!')   <> NVL(:NEW.CDTIPOOPERWEB, '!'))   OR
            (NVL(:OLD.IDUTTABELAAUX, '!')   <> NVL(:NEW.IDUTTABELAAUX, '!'))   OR
            (NVL(:OLD.IDTIPOPEDIDOWEB, '!') <> NVL(:NEW.IDTIPOPEDIDOWEB, '!')) OR
            (NVL(:OLD.IDUTENDENTR, '!')     <> NVL(:NEW.IDUTENDENTR, '!'))     THEN

            V_TIPO         := '2';
            V_ITPESQ       := :OLD.NRSEQWEBSERVICE;
            V_CDFILLOGOPER := :OLD.CDFILIAL;
         END IF;
      ELSE
         V_TIPO         := '1';
         V_ITPESQ       := :NEW.NRSEQWEBSERVICE;
         V_CDFILLOGOPER := :NEW.CDFILIAL;
      END IF;

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

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

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

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

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

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

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

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

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

      IF NVL(:NEW.IDTIPOOPERWEB, '!') <> NVL(:OLD.IDTIPOOPERWEB, '!') THEN
         V_DESC := V_DESC || ' E/S=' || :NEW.IDTIPOOPERWEB;
      END IF;

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

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

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

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

   IF V_TIPO <> '0' THEN
      NMUSUARIO(V_USUARIO);
      WEBTUNNING(V_DSREMOTEADDR,
                 V_DSREMOTEHOST,
                 V_DSSERVERRDM,
                 V_DSSERVMETHOD,
                 V_DSREMOTEOPER);
      IF NOT V_DESC IS NULL THEN
         IF NOT V_USUARIO IS NULL THEN
            V_DTOPER := SYSDATE;
            V_TAM := LENGTH(V_DESC);
            WHILE (V_TAM >= 0) LOOP
               INSERT INTO LOGOPERA
                  (NMTABELA,
                   DTOPERACAO,
                   NMRESPOPER,
                   IDTIPOOPER,
                   CDPESQITEM,
                   DSOPERACAO,
                   CDFILLOGOPER,
                   DSRDMUSUOPER,
                   DSENDUSUOPER,
                   DSNMUSUOPER,
                   DSMETUSUOPER,
                   CDORESPOPER)
               VALUES
                  ('PEDIDOWEB',
                   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);
               V_DESC := SUBSTR(V_DESC, 700, V_TAM);
               V_TAM := LENGTH(V_DESC);
            END LOOP;
         END IF;
      END IF;
   END IF;
END;
/