CREATE OR REPLACE TRIGGER LOG_FAI_PARFILIALFAT
AFTER DELETE OR INSERT OR UPDATE
ON PARFILIALFAT FOR EACH ROW
DECLARE
  V_USUARIO   VARCHAR2(40);
  V_TIPO      VARCHAR2(1);
  V_ITPESQ    VARCHAR2(40);
  V_DESC      VARCHAR2(300);
  V_DTOPER    DATE;
  V_DSREMOTEADDR VARCHAR2(15);
  V_DSREMOTEHOST VARCHAR2(15);
  V_DSSERVERRDM  VARCHAR2(20);
  V_DSSERVMETHOD VARCHAR2(30);
  V_DSREMOTEOPER VARCHAR2(12);
BEGIN
   V_TIPO := '0';
   V_DESC := '';

   WEBTUNNING(V_DSREMOTEADDR,
              V_DSREMOTEHOST,
              V_DSSERVERRDM,
              V_DSSERVMETHOD,
              V_DSREMOTEOPER);

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

      IF :OLD.VRPERECUPIMP IS NOT NULL THEN
         V_DESC := V_DESC || 'Vr. Percentual Imposto=' || :OLD.VRPERECUPIMP;
      END IF;

      IF :OLD.NRFUNCFATUR IS NOT NULL THEN
         V_DESC := V_DESC || 'Nrfuncfatur=' || :OLD.NRFUNCFATUR;
      END IF;

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

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

      IF :OLD.VRPECREDICMFAT IS NOT NULL THEN
         V_DESC := V_DESC || 'Vr. Perc. Cred.ICMS=' || :OLD.VRPECREDICMFAT;
      END IF;
      V_TIPO := '3';
      V_ITPESQ := :OLD.CDFILIAL;
   ELSE
      IF UPDATING THEN
         IF (NVL(:OLD.VRPERECUPIMP,0)  <> NVL(:NEW.VRPERECUPIMP,0))  OR
            (NVL(:OLD.VRPECREDICMFAT,0)  <> NVL(:NEW.VRPECREDICMFAT,0))  OR
            (NVL(:OLD.NRFUNCFATUR,'!') <> NVL(:NEW.NRFUNCFATUR,'!')) OR
            (NVL(:OLD.IDMRCONSCGO,'!') <> NVL(:NEW.IDMRCONSCGO,'!')) OR
            (NVL(:OLD.IDEFECONSCGO,'!') <> NVL(:NEW.IDEFECONSCGO,'!')) THEN
            V_TIPO   := '2';
            V_ITPESQ := :OLD.CDFILIAL;


            IF :OLD.IDEFECONSCGO <> :NEW.IDEFECONSCGO THEN
               V_DESC := V_DESC || ' IDEFECONSCGO ' || :OLD.IDEFECONSCGO;
            END IF;

            IF :OLD.VRPERECUPIMP <> :NEW.VRPERECUPIMP THEN
               V_DESC := V_DESC || 'Vr. Percentual Imposto=' || TO_CHAR(:OLD.VRPERECUPIMP);
            END IF;

            IF :OLD.VRPECREDICMFAT <> :NEW.VRPECREDICMFAT THEN
               V_DESC := V_DESC || 'Vr. Perc. Cred.ICMS=' || TO_CHAR(:OLD.VRPECREDICMFAT);
            END IF;

            IF :OLD.NRFUNCFATUR <> :NEW.NRFUNCFATUR THEN
               V_DESC := V_DESC || ' NRFUNCFATUR ' || :OLD.NRFUNCFATUR;
            END IF;

            IF :OLD.IDMRCONSCGO <> :NEW.IDMRCONSCGO THEN
               V_DESC := V_DESC || ' IDMRCONSCGO ' || :OLD.IDMRCONSCGO;
            END IF;
         END IF;
      ELSE
         V_TIPO   := '1';
         V_ITPESQ := :NEW.CDFILIAL;
         V_DESC := V_DESC ||'Filial=' ||:NEW.CDFILIAL;
         V_DESC := V_DESC ||'Vr. Percentual Imposto=' ||TO_CHAR(:NEW.VRPERECUPIMP);
         V_DESC := V_DESC ||'NRFUNCFATUR=' ||:NEW.NRFUNCFATUR;
         V_DESC := V_DESC ||'IDMRCONSCGO=' ||:NEW.IDMRCONSCGO;
         V_DESC := V_DESC ||'IDEFECONSCGO=' ||:NEW.IDEFECONSCGO;
         V_DESC := V_DESC ||'Vr. Perc. Cred.ICMS=' ||:NEW.IDMRCONSCGO;
      END IF;
   END IF;

   IF V_TIPO <> '0' THEN
      IF NOT V_DESC IS NULL THEN
         NMUSUARIO(V_USUARIO);
         IF NOT V_USUARIO IS NULL THEN
            V_DTOPER := SYSDATE;

            INSERT INTO LOGOPERA
               (NMTABELA,   DTOPERACAO, NMRESPOPER, IDTIPOOPER, CDPESQITEM, DSOPERACAO,
                DSRDMUSUOPER, DSENDUSUOPER, DSNMUSUOPER, DSMETUSUOPER, CDORESPOPER)
            VALUES
               ('PARFILIALFAT',  V_DTOPER, V_USUARIO,
                V_TIPO,         V_ITPESQ, SUBSTR(V_DESC, 1, 300),V_DSREMOTEADDR, V_DSREMOTEHOST, V_DSSERVERRDM,
                V_DSSERVMETHOD, V_DSREMOTEOPER);
         END IF;
      END IF;
   END IF;
END;
/
-- VERSAO --
BEGIN ATUVEROBJ('LOG_FAI_PARFILIALFAT','5.20.xxx'); END;
/
