CREATE OR REPLACE TRIGGER LOG_GER_EMPRESA
AFTER DELETE OR INSERT OR UPDATE
ON EMPRESA 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_DESC         VARCHAR2(3000);
   V_IDTIPOOPER   VARCHAR2(1);
   V_ITPESQ       VARCHAR(2);
   V_USUARIO      VARCHAR2(40);
BEGIN
   V_DESC   := '';
   V_ITPESQ := '';
   NMUSUARIO(V_USUARIO);
   WEBTUNNING(V_DSREMOTEADDR,
              V_DSREMOTEHOST,
              V_DSSERVERRDM,
              V_DSSERVMETHOD,
              V_DSREMOTEOPER);
   IF DELETING THEN
      IF :OLD.CDEMPRESA IS NOT NULL THEN
         V_DESC := V_DESC || ' CdEmpresa=' || :OLD.CDEMPRESA;
      END IF;
      IF :OLD.NMEMPRESA IS NOT NULL THEN
         V_DESC := V_DESC || ' NmEmpresa=' || :OLD.NMEMPRESA;
      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 || ' Estado=' || :OLD.SGESTADO;
      END IF;
      IF :OLD.CDMUNICIPIO IS NOT NULL THEN
         V_DESC := V_DESC || ' Municipio=' || :OLD.CDMUNICIPIO;
      END IF;
      IF :OLD.NMRAZSOCEMPR IS NOT NULL THEN
         V_DESC := V_DESC || ' RazSocial=' || :OLD.NMRAZSOCEMPR;
      END IF;
      IF :OLD.DSENDEEMPR IS NOT NULL THEN
         V_DESC := V_DESC || ' EndeEmpr=' || :OLD.DSENDEEMPR;
      END IF;
      IF :OLD.NRINSJUREMPR IS NOT NULL THEN
         V_DESC := V_DESC || ' InsJur=' || :OLD.NRINSJUREMPR;
      END IF;
      IF :OLD.NRINSCESTA IS NOT NULL THEN
         V_DESC := V_DESC || ' InsEst=' || :OLD.NRINSCESTA;
      END IF;
      IF :OLD.IDTPIJUREMPR IS NOT NULL THEN
         V_DESC := V_DESC || ' TpInscJur=' || :OLD.IDTPIJUREMPR;
      END IF;
      IF :OLD.NRREGJUNCOM IS NOT NULL THEN
         V_DESC := V_DESC || ' NRegJun=' || :OLD.NRREGJUNCOM;
      END IF;
      IF :OLD.DSEMAILEMPRESA IS NOT NULL THEN
         V_DESC := V_DESC || ' Email=' || :OLD.DSEMAILEMPRESA;
      END IF;
      IF :OLD.DSMENSEMAILNFE IS NOT NULL THEN
         V_DESC := V_DESC || ' DSEmailNfe=' || :OLD.DSMENSEMAILNFE;
      END IF;
      IF :OLD.IDIMPCUPMEI IS NOT NULL THEN
         V_DESC := V_DESC || ' IdObrEmisNFCe=' || :OLD.IDIMPCUPMEI;
      END IF;

      V_ITPESQ     := :OLD.CDEMPRESA;
      V_IDTIPOOPER := 3;
   ELSE
      IF UPDATING THEN
         V_ITPESQ     := :NEW.CDEMPRESA;
         V_IDTIPOOPER := 2;
      ELSE
         V_ITPESQ     := :NEW.CDEMPRESA;
         V_IDTIPOOPER := 1;
      END IF;
      IF NVL(:OLD.CDEMPRESA, '#') <> NVL(:NEW.CDEMPRESA, '#') THEN
         V_DESC := V_DESC || ' CdEmpresa=' || :NEW.CDEMPRESA;
      END IF;
      IF NVL(:OLD.NMEMPRESA, '#') <> NVL(:NEW.NMEMPRESA, '#') THEN
         V_DESC := V_DESC || ' NmEmpresa=' || :NEW.NMEMPRESA;
      END IF;
      IF NVL(:OLD.CDPAIS, '#') <> NVL(:NEW.CDPAIS, '#') THEN
         V_DESC := V_DESC || ' Pais=' || :NEW.CDPAIS;
      END IF;
      IF NVL(:OLD.SGESTADO, '#') <> NVL(:NEW.SGESTADO, '#') THEN
         V_DESC := V_DESC || ' Estado=' || :NEW.SGESTADO;
      END IF;
      IF NVL(:OLD.CDMUNICIPIO, '#') <> NVL(:NEW.CDMUNICIPIO, '#') THEN
         V_DESC := V_DESC || ' Municipio=' || :NEW.CDMUNICIPIO;
      END IF;
      IF NVL(:OLD.NMRAZSOCEMPR, '#') <> NVL(:NEW.NMRAZSOCEMPR, '#') THEN
         V_DESC := V_DESC || ' RazSocial=' || :NEW.NMRAZSOCEMPR;
      END IF;
      IF NVL(:OLD.DSENDEEMPR, '#') <> NVL(:NEW.DSENDEEMPR, '#') THEN
         V_DESC := V_DESC || ' EndeEmpr=' || :NEW.DSENDEEMPR;
      END IF;
      IF NVL(:OLD.NRINSJUREMPR, '#') <> NVL(:NEW.NRINSJUREMPR, '#') THEN
         V_DESC := V_DESC || ' InsJur=' || :NEW.NRINSJUREMPR;
      END IF;
      IF NVL(:OLD.NRINSCESTA, '#') <> NVL(:NEW.NRINSCESTA, '#') THEN
         V_DESC := V_DESC || ' InsEst=' || :NEW.NRINSCESTA;
      END IF;
      IF NVL(:OLD.IDTPIJUREMPR, '#') <> NVL(:NEW.IDTPIJUREMPR, '#') THEN
         V_DESC := V_DESC || ' TpInscJur=' || :NEW.IDTPIJUREMPR;
      END IF;
      IF NVL(:OLD.NRREGJUNCOM, '#') <> NVL(:NEW.NRREGJUNCOM, '#') THEN
         V_DESC := V_DESC || ' NRegJun=' || :NEW.NRREGJUNCOM;
      END IF;
      IF NVL(:OLD.DSEMAILEMPRESA, '#') <> NVL(:NEW.DSEMAILEMPRESA, '#') THEN
         V_DESC := V_DESC || ' Email=' || :NEW.DSEMAILEMPRESA;
      END IF;
      IF NVL(:OLD.DSMENSEMAILNFE, '#') <> NVL(:NEW.DSMENSEMAILNFE, '#') THEN
         V_DESC := V_DESC || ' DSEmailNfe=' || :NEW.DSMENSEMAILNFE;
      END IF;
      IF NVL(:OLD.IDIMPCUPMEI, '#') <> NVL(:NEW.IDIMPCUPMEI, '#') THEN
         V_DESC := V_DESC || ' IdObrEmisNFCe=' || :NEW.IDIMPCUPMEI;
      END IF;
   END IF;
   IF NOT V_DESC IS NULL THEN
      V_DESC := SUBSTR(V_DESC, 1, 300);
      INSERT INTO LOGOPERA
         (NMTABELA,   DTOPERACAO, NMRESPOPER,
          IDTIPOOPER, CDPESQITEM, DSOPERACAO,
          DSRDMUSUOPER, DSENDUSUOPER, DSNMUSUOPER, DSMETUSUOPER, CDORESPOPER)
      VALUES
         ('EMPRESA',  SYSDATE, V_USUARIO,
          V_IDTIPOOPER,   V_ITPESQ, V_DESC, 
          V_DSSERVERRDM, V_DSREMOTEADDR, V_DSREMOTEHOST, V_DSSERVMETHOD, V_DSREMOTEOPER);
   END IF;
END;
/
