create or replace TRIGGER LOG_PED_RESERPRODPED
AFTER DELETE OR INSERT OR UPDATE
ON RESERPRODPED FOR EACH ROW
DECLARE
  V_USUARIO        VARCHAR2(40);
  V_TIPO           VARCHAR2(1);
  V_ITPESQ         VARCHAR2(40);
  V_DESC           VARCHAR2(300);
  V_CDFILLOGOPER   VARCHAR(4);
  V_DTOPER         DATE;
  V_CDARVPROD      VARCHAR2(20);
  V_NMPRODUTO      VARCHAR2(50);

  V_DSREMOTEADDR VARCHAR2(15);
  V_DSREMOTEHOST VARCHAR2(15);
  V_DSSERVERRDM  VARCHAR2(20);
  V_DSSERVMETHOD VARCHAR2(30);
  V_DSREMOTEOPER VARCHAR2(12);
BEGIN
   WEBTUNNING(V_DSREMOTEADDR,
              V_DSREMOTEHOST,
              V_DSSERVERRDM,
              V_DSSERVMETHOD,
              V_DSREMOTEOPER);

   IF INSERTING THEN
      SELECT CDARVPROD, NMPRODUTO
      INTO V_CDARVPROD, V_NMPRODUTO
      FROM PRODUTO
      WHERE CDPRODUTO = :NEW.CDPRODUTO;

      V_DESC   := 'Filial '    || :NEW.CDFILIAL     || ' ' ||
                  'Pedido '    || :NEW.NRPEDIDO     || ' ' ||
                  'Produto '   || V_CDARVPROD       || ' ' ||
                  'Qtde. '     || TO_CHAR(ROUND(:NEW.QTRESPRODPED, 3));

      IF (:NEW.CDALMOXARIFE IS NOT NULL) OR
         (:NEW.CDLOCALESTOQ IS NOT NULL) OR
         (:NEW.NRLOTEESTQ   IS NOT NULL) OR
         (:NEW.NRSUBLOTE    IS NOT NULL) THEN
         V_DESC :=  V_DESC || ' ' || 'Estoque ';
         IF (:NEW.CDALMOXARIFE IS NOT NULL) THEN
            V_DESC :=  V_DESC || ' ' || :NEW.CDALMOXARIFE;
         END IF;
         IF (:NEW.CDLOCALESTOQ IS NOT NULL) THEN
            V_DESC :=  V_DESC || ' ' || :NEW.CDLOCALESTOQ;
         END IF;
         IF (:OLD.NRLOTEESTQ IS NOT NULL) THEN
            V_DESC :=  V_DESC || ' ' || :NEW.NRLOTEESTQ;
         END IF;
         IF (:NEW.NRSUBLOTE IS NOT NULL) THEN
            V_DESC :=  V_DESC || ' ' || :NEW.NRSUBLOTE;
         END IF;
      END IF;

      V_ITPESQ  := :NEW.CDFILIAL||:NEW.NRPEDIDO||V_CDARVPROD;
      V_TIPO := '1';
      V_CDFILLOGOPER := :NEW.CDFILIAL;
   ELSE
      SELECT CDARVPROD, NMPRODUTO
      INTO V_CDARVPROD, V_NMPRODUTO
      FROM PRODUTO
      WHERE CDPRODUTO = :OLD.CDPRODUTO;

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

      IF (:OLD.CDALMOXARIFE IS NOT NULL) OR
         (:OLD.CDLOCALESTOQ IS NOT NULL) OR
         (:OLD.NRLOTEESTQ   IS NOT NULL) OR
         (:OLD.NRSUBLOTE    IS NOT NULL) THEN
         V_DESC :=  V_DESC || ' ' || 'Estoque ';
         IF (:OLD.CDALMOXARIFE IS NOT NULL) THEN
            V_DESC :=  V_DESC || ' ' || :OLD.CDALMOXARIFE;
         END IF;
         IF (:OLD.CDLOCALESTOQ IS NOT NULL) THEN
            V_DESC :=  V_DESC || ' ' || :OLD.CDLOCALESTOQ;
         END IF;
         IF (:OLD.NRLOTEESTQ IS NOT NULL) THEN
            V_DESC :=  V_DESC || ' ' || :OLD.NRLOTEESTQ;
         END IF;
         IF (:OLD.NRSUBLOTE IS NOT NULL) THEN
            V_DESC :=  V_DESC || ' ' || :OLD.NRSUBLOTE;
         END IF;
      END IF;

      V_ITPESQ  := :OLD.CDFILIAL||:OLD.NRPEDIDO||V_CDARVPROD;
      V_CDFILLOGOPER := :OLD.CDFILIAL;

      IF UPDATING THEN
         V_TIPO := '2';
      ELSE
         IF DELETING  THEN
            V_TIPO := '3';
         END IF;
      END IF;
   END IF;

   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, CDFILLOGOPER,
             DSRDMUSUOPER, DSENDUSUOPER,
             DSNMUSUOPER,  DSMETUSUOPER, CDORESPOPER)
         VALUES
            ('RESERPRODPED', 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;