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(600);
  V_CDFILLOGOPER    VARCHAR2(4);
  V_DTOPER          DATE;
  V_STSITPED        VARCHAR2(2);
  V_CDARVPROD       VARCHAR2(20);
  V_DSREMOTEADDR    VARCHAR2(15);
  V_DSREMOTEHOST    VARCHAR2(15);
  V_DSSERVERRDM     VARCHAR2(20);
  V_DSSERVMETHOD    VARCHAR2(30);
  V_DSREMOTEOPER    VARCHAR2(12);
BEGIN
   V_DESC         := '';
   V_STSITPED     := '';
   V_TIPO         := '0';
   V_CDFILLOGOPER := '';

   IF DELETING  THEN
      V_TIPO := '3';

      SELECT MAX(CDARVPROD)
        INTO V_CDARVPROD
        FROM PRODUTO
       WHERE CDPRODUTO = :OLD.CDPRODUTO;

      V_DESC   := 'Filial '     || :OLD.CDFILIAL  || ' ' ||
                  'Pedido '     || :OLD.NRPEDIDO  || ' ' ||
                  'Item '       || :OLD.NRITEMPED || ' ' ||
                  'Produto '    || V_CDARVPROD    || ' ' ||
                  'Qtde. Ped '  || TO_CHAR(ROUND(:OLD.QTITEMPED, 3));

      IF :OLD.VRPREUNITPED IS NOT NULL THEN
         V_DESC := V_DESC || ' Preco Unit. ' || TO_CHAR(ROUND(:OLD.VRPREUNITPED, 2));
      END IF;
      
      IF :OLD.VRPRELIQITPED IS NOT NULL THEN
         V_DESC := V_DESC || ' Preco Liq. ' || TO_CHAR(ROUND(NVL(:OLD.VRPRELIQITPED, 0), 2));
      END IF;
      
      IF :OLD.CDFATPRECO IS NOT NULL THEN
         V_DESC := V_DESC || ' SG. Fator ' || :OLD.CDFATPRECO;
      END IF;
      
      IF :OLD.VRFATPRECOIT IS NOT NULL THEN
         V_DESC := V_DESC || ' Fator ' || TO_CHAR(ROUND(:OLD.VRFATPRECOIT, 2));
      END IF;
      
      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;

      IF (V_STSITPED <> '') THEN
         V_DESC := V_DESC || ' Situacao ' || V_STSITPED;
      END IF;
     
      IF :OLD.VRDESITPED IS NOT NULL THEN
         V_DESC := V_DESC || ' Vr. Desc. ' || TO_CHAR(ROUND(:OLD.VRDESITPED, 2));
      END IF;

      IF (:OLD.CDEMISSAOITPED IS NOT NULL) THEN
         V_DESC := V_DESC || ' Cd. Emis ' || :OLD.CDEMISSAOITPED;
      END IF;

      IF (:OLD.DSEMISSAOITPED IS NOT NULL) THEN
         V_DESC := V_DESC || ' Ds. Emis ' || :OLD.DSEMISSAOITPED;
      END IF;
      
      IF (:OLD.QTEXPITEMPED IS NOT NULL) THEN
         V_DESC := V_DESC || ' Qt. Exp ' || TO_CHAR(ROUND(:OLD.QTEXPITEMPED, 2));
      END IF;
      
      IF (:OLD.QTITEMPED2 IS NOT NULL) THEN
         V_DESC := V_DESC || ' Qtde. Conv ' || TO_CHAR(ROUND(:OLD.QTITEMPED2, 2));
      END IF;
      
      IF (:OLD.VRPESOITPED IS NOT NULL) THEN
         V_DESC := V_DESC || ' Peso ' || :OLD.VRPESOITPED;
      END IF;
      
      IF (:OLD.DSITEMPED IS NOT NULL) THEN
         V_DESC := V_DESC || ' Obs ' || :OLD.DSITEMPED;
      END IF;
      
      IF (:OLD.VRPERCMARKUPIT IS NOT NULL) THEN
         V_DESC := V_DESC || ' Markup ' || TO_CHAR(ROUND(:OLD.VRPERCMARKUPIT, 2));
      END IF;
      
      IF :OLD.VRPRTABITPED IS NOT NULL THEN
         V_DESC := V_DESC || ' Vr Tab. ' || TO_CHAR(ROUND(:OLD.VRPRTABITPED, 2));
      END IF;

      V_ITPESQ  := :OLD.CDFILIAL||:OLD.NRPEDIDO||:OLD.NRITEMPED;
      V_CDFILLOGOPER := :OLD.CDFILIAL;
   ELSE
      IF UPDATING THEN
         V_DESC   := 'Filial '     || :OLD.CDFILIAL  || ' ' ||
                     'Pedido '     || :OLD.NRPEDIDO  || ' ' ||
                     'Item '       || :OLD.NRITEMPED;

         IF (NVL(:OLD.CDPRODUTO,'!')        <> NVL(:NEW.CDPRODUTO,'!'))        OR
            (:OLD.QTITEMPED                 <> :NEW.QTITEMPED)                 OR
            (:OLD.VRPREUNITPED              <> :NEW.VRPREUNITPED)              OR
            (NVL(:OLD.IDSITITEMPED,'!')     <> NVL(:NEW.IDSITITEMPED,'!'))     OR
            (:OLD.VRPRELIQITPED             <> :NEW.VRPRELIQITPED)             OR
            (:OLD.QTEXPITEMPED              <> :NEW.QTEXPITEMPED)              OR
            (NVL(:OLD.CDFATPRECO,'!')       <> NVL(:NEW.CDFATPRECO,'!'))       OR
            (:OLD.QTITEMPED2                <> :NEW.QTITEMPED2)                OR
            (:OLD.DTENTITPED                <> :NEW.DTENTITPED)                OR
            (NVL(:OLD.IDITPEDDIFER,'!')     <> NVL(:NEW.IDITPEDDIFER,'!'))     OR
            (:OLD.VRPESOITPED               <> :NEW.VRPESOITPED)               OR
            (:OLD.VRDESITPED                <> :NEW.VRDESITPED)                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.CDEMISSAOITPED,'!')   <> NVL(:NEW.CDEMISSAOITPED,'!'))   OR
            (NVL(:OLD.DSEMISSAOITPED,'!')   <> NVL(:NEW.DSEMISSAOITPED,'!'))   OR
            (NVL(:OLD.DSPRAZOENTITPED,'!')  <> NVL(:NEW.DSPRAZOENTITPED,'!'))  OR
            (NVL(:OLD.DSITEMPED, '!')       <> NVL(:NEW.DSITEMPED, '!'))       OR
            (NVL(:OLD.NRITORDEMCMPPED, '!') <> NVL(:NEW.NRITORDEMCMPPED, '!')) OR
            (NVL(:OLD.IDPRECOPROM, '!')     <> NVL(:NEW.IDPRECOPROM, '!'))     OR
            (:OLD.VRVARIAVEL                <> :NEW.VRVARIAVEL)                OR
            (NVL(:OLD.CDFATOR, '!')         <> NVL(:NEW.CDFATOR, '!'))         OR
            (NVL(:OLD.IDITEMPEDTERC, '!')   <> NVL(:NEW.IDITEMPEDTERC, '!'))   OR
            (:OLD.VRFATPRECOIT            <> :NEW.VRFATPRECOIT)                THEN
            V_DESC := V_DESC || ' Alterado:';
            V_TIPO := '2';
         END IF;
      ELSE
         V_TIPO := '1';

         SELECT MAX(CDARVPROD)
           INTO V_CDARVPROD
           FROM PRODUTO
          WHERE CDPRODUTO = :NEW.CDPRODUTO;

         V_DESC   := 'Filial '     || :NEW.CDFILIAL  || ' ' ||
                     'Pedido '     || :NEW.NRPEDIDO  || ' ' ||
                     'Item '       || :NEW.NRITEMPED || ' ' ||
                     'Produto '    || V_CDARVPROD    || ' ' ||
                     'Qtde. Ped '  || TO_CHAR(ROUND(:NEW.QTITEMPED, 3));
      END IF;

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

         V_DESC := V_DESC || ' Produto ' || V_CDARVPROD;
      END IF;

      IF (:OLD.QTITEMPED <> :NEW.QTITEMPED) THEN
         V_DESC := V_DESC || ' Qtde. Ped ' || TO_CHAR(ROUND(:NEW.QTITEMPED,2));
      END IF;

      IF (:OLD.VRPREUNITPED <> :NEW.VRPREUNITPED) THEN
         V_DESC := V_DESC || ' Preco Unit. ' || TO_CHAR(ROUND(:NEW.VRPREUNITPED,2));
      END IF;

      IF (:OLD.IDSITITEMPED <> :NEW.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;

         IF (V_STSITPED <> '') THEN
            V_DESC := V_DESC || ' Situacao ' || V_STSITPED;
         END IF;
      END IF;

      IF (:OLD.QTEXPITEMPED <> :NEW.QTEXPITEMPED) THEN
         V_DESC := V_DESC || ' Qt. Exp ' || TO_CHAR(ROUND(:NEW.QTEXPITEMPED,2));
      END IF;

      IF (:OLD.CDFATPRECO <> :NEW.CDFATPRECO) THEN
         V_DESC := V_DESC || ' SG. Fator ' || :NEW.CDFATPRECO;
      END IF;

      IF (:OLD.QTITEMPED2 <> :NEW.QTITEMPED2) THEN
         V_DESC := V_DESC || ' Qtde. Conv ' || TO_CHAR(ROUND(:NEW.QTITEMPED2,2));
      END IF;

      IF (:OLD.DTENTITPED <> :NEW.DTENTITPED) THEN
         V_DESC := V_DESC || ' Dt. Entrega ' || :NEW.DTENTITPED;
      END IF;

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

      IF (:OLD.VRPESOITPED <> :NEW.VRPESOITPED) THEN
         V_DESC := V_DESC || ' Peso ' || TO_CHAR(ROUND(:NEW.VRPESOITPED,2));
      END IF;

      IF (:OLD.VRDESITPED <> :NEW.VRDESITPED)  THEN
         V_DESC := V_DESC || ' Vr. Desc. ' || TO_CHAR(ROUND(:NEW.VRDESITPED,2));
      END IF;

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

      IF (:OLD.VRPRTABITPED <> :NEW.VRPRTABITPED) THEN
         V_DESC := V_DESC || ' Vr Tab. ' || TO_CHAR(ROUND(:NEW.VRPRTABITPED,2));
      END IF;

      IF (:OLD.VRPEDEMAXIPE <> :NEW.VRPEDEMAXIPE) THEN
         V_DESC := V_DESC || ' Vr PeMax. ' || TO_CHAR(ROUND(:NEW.VRPEDEMAXIPE,2));
      END IF;

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

      IF (:OLD.VRPERCMARKUPIT <> :NEW.VRPERCMARKUPIT)  THEN
         V_DESC := V_DESC || ' Markup ' || TO_CHAR(ROUND(:NEW.VRPERCMARKUPIT,2));
      END IF;

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

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

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

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

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

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

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

      IF (:OLD.VRPRELIQITPED <> :NEW.VRPRELIQITPED) THEN
         V_DESC := V_DESC|| 'Preco Liq. '|| TO_CHAR(ROUND(:NEW.VRPRELIQITPED,2));
      END IF;

      IF (:OLD.VRFATPRECOIT <> :NEW.VRFATPRECOIT) THEN
         V_DESC := V_DESC || ' Fator ' || TO_CHAR(ROUND(:NEW.VRFATPRECOIT,2));
      END IF;

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

      IF (NVL(:OLD.CDFATOR,'!') <> NVL(:NEW.CDFATOR,'!')) THEN
         V_DESC := V_DESC || ' Cd Fator ' || NVL(:NEW.CDFATOR, 'Null');
      END IF;

      V_ITPESQ  := :NEW.CDFILIAL||:NEW.NRPEDIDO||:NEW.NRITEMPED;
      V_CDFILLOGOPER := :NEW.CDFILIAL;
   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
         V_DTOPER := SYSDATE;

         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, V_DESC, V_CDFILLOGOPER,
             V_DSSERVERRDM, V_DSREMOTEADDR, V_DSREMOTEHOST, V_DSSERVMETHOD, V_DSREMOTEOPER);
      END IF;
   END IF;
END;
/