CREATE OR REPLACE TRIGGER LOG_GER_INSCRESTAD
AFTER DELETE OR INSERT OR UPDATE
ON INSCRESTAD 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(500);
  V_CDFILLOGOPER VARCHAR2(4);
  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 || 'CDEmpresa=' || :OLD.CDEMPRESA;
      END IF;

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

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

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

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

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

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

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

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

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

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

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

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

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

      IF :OLD.NRSERIECERTIFIC IS NOT NULL THEN
         V_DESC := V_DESC || 'Num.Certificado=' || :OLD.NRSERIECERTIFIC;
      END IF;
      
      IF :OLD.IDTPIMPRESDANFE IS NOT NULL THEN
         V_DESC := V_DESC || 'Impr.Danfe=' || :OLD.IDTPIMPRESDANFE;
      END IF;

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

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

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

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

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

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

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

      V_TIPO         := '3';
      V_ITPESQ       := :OLD.CDEMPRESA||:OLD.CDINSCESTA;
      V_CDFILLOGOPER := :OLD.CDFILIAL;
   ELSE
      IF UPDATING THEN
         IF (:OLD.CDEMPRESA    <> :NEW.CDEMPRESA)    OR
            (:OLD.CDINSCESTA   <> :NEW.CDINSCESTA)   OR
            (:OLD.NMREPRINSEST <> :NEW.NMREPRINSEST) OR
            (:OLD.NRINSJURIEST <> :NEW.NRINSJURIEST) OR
            (:OLD.CDCSTDIG1SAI <> :NEW.CDCSTDIG1SAI) OR
            (:OLD.DSENDEIEST   <> :NEW.DSENDEIEST)   OR
            (:OLD.DSCOMPENDIES <> :NEW.DSCOMPENDIES) OR
            (:OLD.NRCEPIEST    <> :NEW.NRCEPIEST)    OR
            (:OLD.CDPAIS       <> :NEW.CDPAIS)       OR
            (:OLD.SGESTADO     <> :NEW.SGESTADO)     OR
            (:OLD.CDBAIRRO     <> :NEW.CDBAIRRO)     OR
            (:OLD.NMBAIRIEST   <> :NEW.NMBAIRIEST)   OR
            (:OLD.NRTELEIEST   <> :NEW.NRTELEIEST)   OR
            (:OLD.NRFAXIEST    <> :NEW.NRFAXIEST)    OR
            (:OLD.NRSERIECERTIFIC <> :NEW.NRSERIECERTIFIC) OR
            (:OLD.IDTPIMPRESDANFE <> :NEW.IDTPIMPRESDANFE) OR
            (:OLD.IDTPAMBIENTENFE <> :NEW.IDTPAMBIENTENFE) OR
            (:OLD.IDSUGREGSTENTR  <> :NEW.IDSUGREGSTENTR)  OR
            (:OLD.CDENCARGOST  <> :NEW.CDENCARGOST)  OR
            (:OLD.DTINIVIGIEST <> :NEW.DTINIVIGIEST) OR
            (:OLD.DTFIMVIGIEST <> :NEW.DTFIMVIGIEST) OR
			   (:OLD.VRPERCREDST <> :NEW.VRPERCREDST)   OR
            (:OLD.IDUTALIQINTOPPRES <> :NEW.IDUTALIQINTOPPRES) THEN

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      IF NVL(:NEW.IDTPIMPRESDANFE, '!') <> NVL(:OLD.IDTPIMPRESDANFE, '!') THEN
         V_DESC := V_DESC || 'Impr.Danfe=' || :NEW.IDTPIMPRESDANFE;
      END IF;
      
      IF NVL(:NEW.IDTPAMBIENTENFE, '!') <> NVL(:OLD.IDTPAMBIENTENFE, '!')  THEN
         V_DESC := V_DESC || 'AmbienteNFE='   || :NEW.IDTPAMBIENTENFE;
      END IF;

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

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

      IF (:NEW.DTINIVIGIEST IS NOT NULL) AND (:NEW.DTINIVIGIEST <> :OLD.DTINIVIGIEST) THEN
         V_DESC := V_DESC || 'VigenciaIni='   || :OLD.DTINIVIGIEST;
      END IF;

      IF (:NEW.DTFIMVIGIEST IS NOT NULL) AND (:NEW.DTFIMVIGIEST <> :OLD.DTFIMVIGIEST) THEN
         V_DESC := V_DESC || 'VigenciaFim='   || :OLD.DTFIMVIGIEST;
      END IF;
	  
      IF (:NEW.VRPERCREDST IS NOT NULL) AND (:NEW.VRPERCREDST <> :OLD.VRPERCREDST) THEN
         V_DESC := V_DESC || ' VRPerCredST='   || :NEW.VRPERCREDST;
      END IF;

      IF (:NEW.IDUTALIQINTOPPRES IS NOT NULL) AND (:NEW.IDUTALIQINTOPPRES <> :OLD.IDUTALIQINTOPPRES) THEN
         V_DESC := V_DESC || 'IdUtAliqIntOpPres='   || :NEW.IDUTALIQINTOPPRES;
      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, 300);

            INSERT INTO LOGOPERA
               (NMTABELA,   DTOPERACAO, NMRESPOPER,
                IDTIPOOPER, CDPESQITEM, DSOPERACAO, CDFILLOGOPER,
                DSRDMUSUOPER, DSENDUSUOPER, DSNMUSUOPER,
                DSMETUSUOPER, CDORESPOPER)
            VALUES
               ('INSCRESTAD',  V_DTOPER, V_USUARIO,
                V_TIPO,      V_ITPESQ, V_DESC, V_CDFILLOGOPER,
                V_DSSERVERRDM, V_DSREMOTEADDR, V_DSREMOTEHOST,
                V_DSSERVMETHOD, V_DSREMOTEOPER);
         END IF;
      END IF;
   END IF;
END;
/
