CREATE OR REPLACE TRIGGER LOG_PED_ITEMPEDIDO
AFTER DELETE OR INSERT OR UPDATE
ON ITEMPEDIDO 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;
  V_STSITPED     VARCHAR2(2);
  V_CDARVPROD    VARCHAR2(20);
BEGIN
   V_TIPO := '0';
   V_DESC := '';
   V_CDFILLOGOPER := '';

   IF DELETING THEN
      IF :OLD.CDPRODUTO IS NOT NULL THEN
         V_DESC := V_DESC || 'Produto=' || :OLD.CDPRODUTO;
         SELECT MAX(CDARVPROD)
           INTO V_CDARVPROD
           FROM PRODUTO
          WHERE CDPRODUTO = :OLD.CDPRODUTO;
         V_DESC := V_DESC || ' CdArvProd=' || V_CDARVPROD;
      END IF;

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

      IF :OLD.IDSITITEMPED IS NOT NULL THEN
         IF :OLD.IDSITITEMPED = '1' THEN V_STSITPED := 'PE'; END IF;
         IF :OLD.IDSITITEMPED = '2' THEN V_STSITPED := 'CF'; END IF;
         IF :OLD.IDSITITEMPED = '3' THEN V_STSITPED := 'AP'; END IF;
         IF :OLD.IDSITITEMPED = '4' THEN V_STSITPED := 'LQ'; END IF;
         IF :OLD.IDSITITEMPED = '5' THEN V_STSITPED := 'CN'; END IF;
         IF :OLD.IDSITITEMPED = '6' THEN V_STSITPED := 'LQ'; END IF;
         IF :OLD.IDSITITEMPED = '7' THEN V_STSITPED := 'PA'; END IF;
         V_DESC := V_DESC || ' SitItem=' || V_STSITPED;
      END IF;

      IF :OLD.QTITEMPED IS NOT NULL THEN
         V_DESC := V_DESC || ' Qtde. Ped=' || :OLD.QTITEMPED;
      END IF;

      IF :OLD.VRPREUNITPED IS NOT NULL THEN
         V_DESC := V_DESC || ' Preco Unit.=' || :OLD.VRPREUNITPED;
      END IF;

      IF :OLD.VRPRELIQITPED IS NOT NULL THEN
         V_DESC := V_DESC || ' Preco Liq.=' || :OLD.VRPRELIQITPED;
      END IF;

      IF :OLD.VRDESITPED IS NOT NULL THEN
         V_DESC := V_DESC || ' Vr. Desc.=' || :OLD.VRDESITPED;
      END IF;

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

      IF :OLD.QTEXPITEMPED IS NOT NULL THEN
         V_DESC := V_DESC || ' Qt. Exp=' || :OLD.QTEXPITEMPED;
      END IF;

      IF :OLD.QTITEMPED2 IS NOT NULL THEN
         V_DESC := V_DESC || ' Qtde. Conv=' || :OLD.QTITEMPED2;
      END IF;

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

      IF :OLD.VRFATPRECOIT IS NOT NULL THEN
         V_DESC := V_DESC || ' VrFator' || TO_CHAR(ROUND(:OLD.VRFATPRECOIT, 2));
      END IF;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


      V_TIPO         := '3';
      V_ITPESQ       := :OLD.CDFILIAL||:OLD.NRPEDIDO||:OLD.NRITEMPED;
      V_CDFILLOGOPER := :OLD.CDFILIAL;
   ELSE
      IF UPDATING THEN
         IF (NVL(:OLD.CDPRODUTO, '!')       <> NVL(:NEW.CDPRODUTO, '!'))       OR
            (:OLD.DTENTITPED                <> :NEW.DTENTITPED)                OR
            (NVL(:OLD.IDSITITEMPED, '!')    <> NVL(:NEW.IDSITITEMPED, '!'))    OR
            (:OLD.QTITEMPED                 <> :NEW.QTITEMPED)                 OR
            (:OLD.VRPREUNITPED              <> :NEW.VRPREUNITPED)              OR
            (:OLD.VRPRELIQITPED             <> :NEW.VRPRELIQITPED)             OR
            (:OLD.VRDESITPED                <> :NEW.VRDESITPED)                OR
            (NVL(:OLD.IDITPEDDIFER, '!')    <> NVL(:NEW.IDITPEDDIFER, '!'))    OR
            (:OLD.QTEXPITEMPED              <> :NEW.QTEXPITEMPED)              OR
            (:OLD.QTITEMPED2                <> :NEW.QTITEMPED2)                OR
            (NVL(:OLD.CDFATPRECO, '!')      <> NVL(:NEW.CDFATPRECO, '!'))      OR
            (:OLD.VRFATPRECOIT              <> :NEW.VRFATPRECOIT)              OR
            (:OLD.VRPESOITPED               <> :NEW.VRPESOITPED)               OR
            (NVL(:OLD.IDITPEDRESER, '!')    <> NVL(:NEW.IDITPEDRESER, '!'))    OR
            (:OLD.VRPRTABITPED              <> :NEW.VRPRTABITPED)              OR
            (:OLD.VRPEDEMAXIPE              <> :NEW.VRPEDEMAXIPE)              OR
            (NVL(:OLD.CDOPERADOR, '!')      <> NVL(:NEW.CDOPERADOR, '!'))      OR
            (:OLD.VRPERCMARKUPIT            <> :NEW.VRPERCMARKUPIT)            OR
            (NVL(:OLD.CDTIPODESC, '!')      <> NVL(:NEW.CDTIPODESC, '!'))      OR
            (NVL(:OLD.DSITEMPED, '!')       <> NVL(:NEW.DSITEMPED, '!'))       OR
            (NVL(:OLD.CDEMISSAOITPED, '!')  <> NVL(:NEW.CDEMISSAOITPED, '!'))  OR
            (NVL(:OLD.DSEMISSAOITPED, '!')  <> NVL(:NEW.DSEMISSAOITPED, '!'))  OR
            (NVL(:OLD.DSPRAZOENTITPED, '!') <> NVL(:NEW.DSPRAZOENTITPED, '!')) OR
            (NVL(:OLD.NRITORDEMCMPPED, '!') <> NVL(:NEW.NRITORDEMCMPPED, '!')) OR
            (NVL(:OLD.IDPRECOPROM, '!')     <> NVL(:NEW.IDPRECOPROM, '!'))     OR
            (:OLD.VRVARIAVEL                <> :NEW.VRVARIAVEL)                OR
            (NVL(:OLD.IDITEMPEDTERC, '!')   <> NVL(:NEW.IDITEMPEDTERC, '!'))   OR
            (:OLD.VRALTURAPROD              <> :NEW.VRALTURAPROD)              OR
            (:OLD.VRCOMPRIPROD              <> :NEW.VRCOMPRIPROD)              OR
            (NVL(:OLD.IDPRODBONIF, '!')     <> NVL(:NEW.IDPRODBONIF, '!'))     THEN

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

      IF NVL(:NEW.CDPRODUTO, '!') <> NVL(:OLD.CDPRODUTO, '!') THEN
         V_DESC := V_DESC || 'Produto=' || :NEW.CDPRODUTO;
         SELECT MAX(CDARVPROD)
           INTO V_CDARVPROD
           FROM PRODUTO
          WHERE CDPRODUTO = :NEW.CDPRODUTO;
         V_DESC := V_DESC || ' CdArvProd=' || V_CDARVPROD;
      END IF;

      IF (:NEW.DTENTITPED IS NOT NULL) AND (:NEW.DTENTITPED <> :OLD.DTENTITPED) THEN
         V_DESC := V_DESC || ' DtEntrega=' || :NEW.DTENTITPED;
      END IF;

      IF NVL(:NEW.IDSITITEMPED, '!') <> NVL(:OLD.IDSITITEMPED, '!') THEN
         IF :NEW.IDSITITEMPED = '1' THEN V_STSITPED := 'PE'; END IF;
         IF :NEW.IDSITITEMPED = '2' THEN V_STSITPED := 'CF'; END IF;
         IF :NEW.IDSITITEMPED = '3' THEN V_STSITPED := 'AP'; END IF;
         IF :NEW.IDSITITEMPED = '4' THEN V_STSITPED := 'LQ'; END IF;
         IF :NEW.IDSITITEMPED = '5' THEN V_STSITPED := 'CN'; END IF;
         IF :NEW.IDSITITEMPED = '6' THEN V_STSITPED := 'LQ'; END IF;
         IF :NEW.IDSITITEMPED = '7' THEN V_STSITPED := 'PA'; END IF;
         V_DESC := V_DESC || ' SitPed=' || V_STSITPED;
      END IF;

      IF (:NEW.QTITEMPED IS NOT NULL) AND (:NEW.QTITEMPED <> NVL(:OLD.QTITEMPED,0)) THEN
         V_DESC := V_DESC || ' Qtde. Ped=' || :NEW.QTITEMPED;
      END IF;

      IF (:NEW.VRPREUNITPED IS NOT NULL) AND (:NEW.VRPREUNITPED <> NVL(:OLD.VRPREUNITPED,0)) THEN
         V_DESC := V_DESC || ' Preco Unit.=' || :NEW.VRPREUNITPED;
      END IF;

      IF (:NEW.VRPRELIQITPED IS NOT NULL) AND (:NEW.VRPRELIQITPED <> NVL(:OLD.VRPRELIQITPED,0)) THEN
         V_DESC := V_DESC || ' Preco Liq.=' || :NEW.VRPRELIQITPED;
      END IF;

      IF (:NEW.VRDESITPED IS NOT NULL) AND (:NEW.VRDESITPED <> NVL(:OLD.VRDESITPED,0)) THEN
         V_DESC := V_DESC || ' Vr. Desc.=' || :NEW.VRDESITPED;
      END IF;

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

      IF (:NEW.QTEXPITEMPED IS NOT NULL) AND (:NEW.QTEXPITEMPED <> NVL(:OLD.QTEXPITEMPED,0)) THEN
         V_DESC := V_DESC || ' Qt. Exp=' || :NEW.QTEXPITEMPED;
      END IF;

      IF (:NEW.QTITEMPED2 IS NOT NULL) AND (:NEW.QTITEMPED2 <> NVL(:OLD.QTITEMPED2,0)) THEN
         V_DESC := V_DESC || ' Qtde. Conv=' || :NEW.QTITEMPED2;
      END IF;

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

      IF (:NEW.VRFATPRECOIT IS NOT NULL) AND (:NEW.VRFATPRECOIT <> NVL(:OLD.VRFATPRECOIT,0)) THEN
         V_DESC := V_DESC || ' VrFator=' || :NEW.VRFATPRECOIT;
      END IF;

      IF (:NEW.VRPESOITPED IS NOT NULL) AND (:NEW.VRPESOITPED <> NVL(:OLD.VRPESOITPED,0)) THEN
         V_DESC := V_DESC || ' Peso=' || :NEW.VRPESOITPED;
      END IF;

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

      IF (:NEW.VRPRTABITPED IS NOT NULL) AND (:NEW.VRPRTABITPED <> NVL(:OLD.VRPRTABITPED,0)) THEN
         V_DESC := V_DESC || ' VrTab.=' || :NEW.VRPRTABITPED;
      END IF;

      IF (:NEW.VRPEDEMAXIPE IS NOT NULL) AND (:NEW.VRPEDEMAXIPE <> NVL(:OLD.VRPEDEMAXIPE,0)) THEN
         V_DESC := V_DESC || ' VrPeMax=' || :NEW.VRPEDEMAXIPE;
      END IF;

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

      IF (:NEW.VRPERCMARKUPIT IS NOT NULL) AND (:NEW.VRPERCMARKUPIT <> NVL(:OLD.VRPERCMARKUPIT,0)) THEN
         V_DESC := V_DESC || ' Markup=' || :NEW.VRPERCMARKUPIT;
      END IF;

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

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

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

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

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

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

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

      IF (:NEW.VRVARIAVEL IS NOT NULL) AND (:NEW.VRVARIAVEL <> NVL(:OLD.VRVARIAVEL,0)) THEN
         V_DESC := V_DESC || ' VrVar=' || :NEW.VRVARIAVEL;
      END IF;

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

      IF (:NEW.VRALTURAPROD IS NOT NULL) AND (:NEW.VRALTURAPROD <> NVL(:OLD.VRALTURAPROD,0)) THEN
         V_DESC := V_DESC || ' VrAltura=' || :NEW.VRALTURAPROD;
      END IF;

      IF (:NEW.VRCOMPRIPROD IS NOT NULL) AND (:NEW.VRCOMPRIPROD <> NVL(:OLD.VRCOMPRIPROD,0)) THEN
         V_DESC := V_DESC || ' VrComprim=' || :NEW.VRCOMPRIPROD;
      END IF;

      IF NVL(:NEW.IDPRODBONIF, '!') <> NVL(:OLD.IDPRODBONIF, '!') THEN
         V_DESC := V_DESC || ' Bonif=' || :NEW.IDPRODBONIF;
      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
                  ('ITEMPEDIDO',
                   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;
/