CREATE OR REPLACE TRIGGER LOG_FIS_SERIENF
AFTER DELETE OR INSERT OR UPDATE
ON SERIENF 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(600);
  V_DTOPER       DATE;
BEGIN
   V_TIPO := '0';
   V_DESC := '';

   IF DELETING THEN
      IF :OLD.CDSERIE IS NOT NULL THEN
         V_DESC := V_DESC || 'Serie=' || :OLD.CDSERIE;
      END IF;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      V_TIPO         := '3';
      V_ITPESQ       := :OLD.CDEMPRESA || :OLD.CDINSCESTA || :OLD.CDINSCMUNI || :OLD.CDSERIE;
   ELSE
      IF UPDATING THEN
         IF (NVL(:OLD.CDSERIE,'!')        <> NVL(:NEW.CDSERIE,'!'))      OR
            (NVL(:OLD.DSSERIE,'!')        <> NVL(:NEW.DSSERIE,'!'))      OR
            (NVL(:OLD.NRMAXITNF,0)        <> NVL(:NEW.NRMAXITNF,0))      OR
            (NVL(:OLD.NRMAXSERVNF,0)      <> NVL(:NEW.NRMAXSERVNF,0))    OR
            (NVL(:OLD.CDCTRLNUMENF,'!')   <> NVL(:NEW.CDCTRLNUMENF,'!')) OR
            (NVL(:OLD.CDRELMODENF,'!')    <> NVL(:NEW.CDRELMODENF,'!'))  OR
            (NVL(:OLD.CDRELESPENF,'!')    <> NVL(:NEW.CDRELESPENF,'!'))  OR
            (NVL(:OLD.CDRELDANFE,'!')     <> NVL(:NEW.CDRELDANFE ,'!'))  OR 
            (NVL(:OLD.CDRELCCE,'!')       <> NVL(:NEW.CDRELCCE ,'!'))    OR
            (NVL(:OLD.CDEXPSERIENF,'!')   <> NVL(:NEW.CDEXPSERIENF,'!')) OR
            (NVL(:OLD.IDCONMAXITEM,'!')   <> NVL(:NEW.IDCONMAXITEM,'!')) OR
            (NVL(:OLD.IDUTDTPREMIS,'!')   <> NVL(:NEW.IDUTDTPREMIS,'!')) OR
            (NVL(:OLD.CDSERIENFE,'!')     <> NVL(:NEW.CDSERIENFE,'!'))   OR
            (NVL(:OLD.CDSERIERPS,'!')     <> NVL(:NEW.CDSERIERPS,'!'))   OR
            (NVL(:OLD.CDSUBSERIE,'!')     <> NVL(:NEW.CDSUBSERIE,'!'))   OR
            (NVL(:OLD.CDSERIENFESCAN,'!') <> NVL(:NEW.CDSERIENFESCAN,'!')) THEN

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

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

            IF NVL(:OLD.NRMAXITNF,0) <> NVL(:NEW.NRMAXITNF,0) THEN
               V_DESC := V_DESC || ' MaxProdNF=' || :NEW.NRMAXITNF;
            END IF;

            IF NVL(:OLD.NRMAXSERVNF,0) <> NVL(:NEW.NRMAXSERVNF,0) THEN
               V_DESC := V_DESC || ' MaxServNF=' || :NEW.NRMAXSERVNF;
            END IF;

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

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

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

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

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

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

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

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

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

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

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

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

            V_TIPO   := '2';
            V_ITPESQ := :OLD.CDEMPRESA || :OLD.CDINSCESTA || :OLD.CDINSCMUNI || :OLD.CDSERIE;
         END IF;
      ELSE
         IF :NEW.CDSERIE IS NOT NULL THEN
            V_DESC := V_DESC || 'Serie=' || :NEW.CDSERIE;
         END IF;

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

         IF :NEW.NRMAXITNF IS NOT NULL THEN
            V_DESC := V_DESC || ' MaxProdNF=' || :NEW.NRMAXITNF;
         END IF;

         IF :NEW.NRMAXSERVNF IS NOT NULL THEN
            V_DESC := V_DESC || ' MaxServNF=' || :NEW.NRMAXSERVNF;
         END IF;

         IF :NEW.CDCTRLNUMENF IS NOT NULL THEN
            V_DESC := V_DESC || ' ContrNum=' || :NEW.CDCTRLNUMENF;
         END IF;

         IF :NEW.CDRELMODENF IS NOT NULL THEN
            V_DESC := V_DESC || ' ModeloNF=' || :NEW.CDRELMODENF ;
         END IF;

         IF :NEW.CDRELESPENF IS NOT NULL THEN
            V_DESC := V_DESC || ' ModeloEsp=' || :NEW.CDRELESPENF;
         END IF;

         IF :NEW.CDRELDANFE IS NOT NULL THEN
            V_DESC := V_DESC || ' ModeloDanfe=' || :NEW.CDRELDANFE;
         END IF;  

         IF :NEW.CDRELCCE IS NOT NULL THEN
            V_DESC := V_DESC || ' ModeloCCe=' || :NEW.CDRELCCE;
         END IF;

         IF :NEW.CDEXPSERIENF IS NOT NULL THEN
            V_DESC := V_DESC || ' CodExport=' || :NEW.CDEXPSERIENF;
         END IF;

         IF :NEW.IDCONMAXITEM IS NOT NULL THEN
            V_DESC := V_DESC || ' ConsMaxProd=' || :NEW.IDCONMAXITEM;
         END IF;

         IF :NEW.IDUTDTPREMIS IS NOT NULL THEN
            V_DESC := V_DESC || ' UtilDtProces=' || :NEW.IDUTDTPREMIS;
         END IF;

         IF :NEW.CDSERIENFE IS NOT NULL THEN
            V_DESC := V_DESC || ' SerieNFe=' || :NEW.CDSERIENFE;
         END IF;

         IF :NEW.CDSERIENFESCAN IS NOT NULL THEN
            V_DESC := V_DESC || ' SerieScan=' || :NEW.CDSERIENFESCAN;
         END IF;

         IF :NEW.CDSERIERPS IS NOT NULL THEN
            V_DESC := V_DESC || ' SerieRps=' || :NEW.CDSERIERPS;
         END IF;

         V_TIPO   := '1';
         V_ITPESQ := :NEW.CDEMPRESA || :NEW.CDINSCESTA || :NEW.CDINSCMUNI || :NEW.CDSERIE;
      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;

            V_DESC := SUBSTR(V_DESC, 1, 600);

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