create or replace TRIGGER LOG_PED_PRECOUTI
AFTER DELETE OR INSERT OR UPDATE 
ON PRECOUTI 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;
BEGIN
   V_TIPO := '0';
   V_DESC := '';
   V_CDFILLOGOPER := '';

   IF DELETING THEN
      IF :OLD.CDEMPRESA IS NOT NULL THEN
         V_DESC := V_DESC || 'Empr=' || :OLD.CDEMPRESA;
      END IF;

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

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

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

      IF :OLD.VRPEADPBASE IS NOT NULL THEN
         V_DESC := V_DESC || ' VrPeAd=' || TO_CHAR(ROUND(:OLD.VRPEADPBASE, 5));
      END IF;

      V_TIPO         := '3';
      V_ITPESQ       := :OLD.NRSEQPREUTI;
      V_CDFILLOGOPER := :OLD.CDFILIAL;
   ELSE
      IF UPDATING THEN
         IF (:OLD.CDEMPRESA   <> :NEW.CDEMPRESA  ) OR
            (:OLD.CDFILIAL    <> :NEW.CDFILIAL   ) OR
            (:OLD.CDMODPGTO   <> :NEW.CDMODPGTO  ) OR
            (:OLD.CDPRECOBASE <> :NEW.CDPRECOBASE) OR
            (:OLD.VRPEADPBASE <> :NEW.VRPEADPBASE) THEN

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

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

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

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

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

      IF :NEW.VRPEADPBASE <> :OLD.VRPEADPBASE THEN
         V_DESC := V_DESC || ' VrPeAd=' || TO_CHAR(ROUND(:NEW.VRPEADPBASE, 5));
      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;

            INSERT INTO LOGOPERA
               (NMTABELA,
                DTOPERACAO,
                NMRESPOPER,
                IDTIPOOPER,
                CDPESQITEM,
                DSOPERACAO,
                CDFILLOGOPER,
                DSRDMUSUOPER,
                DSENDUSUOPER,
                DSNMUSUOPER,
                DSMETUSUOPER,
                CDORESPOPER)
            VALUES
               ('PRECOUTI',
                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);
         END IF;
      END IF;
   END IF;
END;