CREATE OR REPLACE TRIGGER LOG_GER_PRODREPLICDOC
AFTER DELETE OR INSERT OR UPDATE
ON PRODREPLICDOC FOR EACH ROW
DECLARE
  V_USUARIO   VARCHAR2(40);
  V_TIPO      VARCHAR2(1);
  V_ITPESQ    VARCHAR2(40);
  V_DESC      VARCHAR2(4000);
  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 || ' CDFILIAL: ' || :OLD.CDFILIAL;
      END IF;

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

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

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

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

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

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

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

      V_TIPO := '3';
      V_ITPESQ := :OLD.CDFILIAL||:OLD.NRREPLICDOC||:OLD.CDFILIORIGRD||:OLD.CDFILIDESTRD||:OLD.IDENTRSAIDOP||:OLD.CDTIPOOPER||:OLD.CDPRODORIGEM;
   ELSE
      IF UPDATING THEN
         IF (NVL(:OLD.CDFILIAL, '!') <> NVL(:NEW.CDFILIAL, '!')) OR
            (NVL(:OLD.NRREPLICDOC, '!') <> NVL(:NEW.NRREPLICDOC, '!')) OR
            (NVL(:OLD.CDFILIORIGRD, '!') <> NVL(:NEW.CDFILIORIGRD, '!')) OR
            (NVL(:OLD.CDFILIDESTRD, '!') <> NVL(:NEW.CDFILIDESTRD, '!')) OR
            (NVL(:OLD.IDENTRSAIDOP, '!') <> NVL(:NEW.IDENTRSAIDOP, '!')) OR
            (NVL(:OLD.CDTIPOOPER, '!') <> NVL(:NEW.CDTIPOOPER, '!')) OR
            (NVL(:OLD.CDPRODORIGEM, '!') <> NVL(:NEW.CDPRODORIGEM, '!')) OR
            (NVL(:OLD.CDPRODDEST, '!') <> NVL(:NEW.CDPRODDEST, '!')) THEN

            V_TIPO   := '2';
            V_ITPESQ := :OLD.CDFILIAL||:OLD.NRREPLICDOC||:OLD.CDFILIORIGRD||:OLD.CDFILIDESTRD||:OLD.IDENTRSAIDOP||:OLD.CDTIPOOPER||:OLD.CDPRODORIGEM;
         END IF;
      ELSE
         V_TIPO   := '1';
         V_ITPESQ := :NEW.CDFILIAL||:NEW.NRREPLICDOC||:NEW.CDFILIORIGRD||:NEW.CDFILIDESTRD||:NEW.IDENTRSAIDOP||:NEW.CDTIPOOPER||:NEW.CDPRODORIGEM;
      END IF;

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

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

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

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

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

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

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

      IF NVL(:OLD.CDPRODDEST, '!') <> NVL(:NEW.CDPRODDEST, '!') THEN
         V_DESC := V_DESC || ' CDPRODDEST: ' || :NEW.CDPRODDEST;
      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
               ('PRODREPLICDOC',  V_DTOPER, V_USUARIO,
                V_TIPO,         V_ITPESQ, SUBSTR(V_DESC, 0, 400),V_DSREMOTEADDR, V_DSREMOTEHOST, V_DSSERVERRDM,
                V_DSSERVMETHOD, V_DSREMOTEOPER);
         END IF;
      END IF;
   END IF;
END;
/