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

   IF DELETING THEN
      IF :OLD.CDSERIEENTR IS NOT NULL THEN
         V_DESC := V_DESC || 'Cod=' || :OLD.CDSERIEENTR;
      END IF;

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

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

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

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

      V_TIPO         := '3';
      V_ITPESQ       := :OLD.CDSERIEENTR;
   ELSE
      IF UPDATING THEN
         IF (NVL(:OLD.CDSERIEENTR,'!')    <> NVL(:NEW.CDSERIEENTR,'!'))    OR
            (NVL(:OLD.DSSERIEENTR,'!')    <> NVL(:NEW.DSSERIEENTR,'!'))    OR
            (NVL(:OLD.CDSERIEENTRNFE,'!') <> NVL(:NEW.CDSERIEENTRNFE,'!')) OR
            (NVL(:OLD.CDSERIEENTSCAN,'!') <> NVL(:NEW.CDSERIEENTSCAN,'!')) OR
            (NVL(:OLD.CDSERIETRANS,'!')   <> NVL(:NEW.CDSERIETRANS,'!')) THEN

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

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

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

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

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

            V_TIPO         := '2';
            V_ITPESQ       := :OLD.CDSERIEENTR;
         END IF;
      ELSE
         IF :NEW.CDSERIEENTR IS NOT NULL THEN
            V_DESC := V_DESC || 'Cod=' || :NEW.CDSERIEENTR;
         END IF;

         IF :NEW.DSSERIEENTR IS NOT NULL THEN
            V_DESC := V_DESC || ' Descr=' || :NEW.DSSERIEENTR;
         END IF;

         IF :NEW.CDSERIEENTRNFE IS NOT NULL THEN
            V_DESC := V_DESC || ' SNFe=' || :NEW.CDSERIEENTRNFE;
         END IF;

         IF :NEW.CDSERIEENTSCAN IS NOT NULL THEN
            V_DESC := V_DESC || ' SScan=' || :NEW.CDSERIEENTSCAN;
         END IF;

         IF :NEW.CDSERIETRANS IS NOT NULL THEN
            V_DESC := V_DESC || ' STransNFe=' || :NEW.CDSERIETRANS;
         END IF;

         V_TIPO         := '1';
         V_ITPESQ       := :NEW.CDSERIEENTR;
      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,
                DSRDMUSUOPER, DSENDUSUOPER, DSNMUSUOPER,
                DSMETUSUOPER, CDORESPOPER)
            VALUES
               ('SERIEENTR',  V_DTOPER, V_USUARIO,
                V_TIPO,      V_ITPESQ, V_DESC,
                V_DSSERVERRDM, V_DSREMOTEADDR, V_DSREMOTEHOST,
                V_DSSERVMETHOD, V_DSREMOTEOPER);
         END IF;
      END IF;
   END IF;
END;
/