CREATE OR REPLACE TRIGGER LOG_GER_PARAMEMPRESA
AFTER DELETE OR INSERT OR UPDATE
ON PARAMEMPRESA 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_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 || 'CodEmpresa=' || :OLD.CDEMPRESA;
      END IF;

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

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

      IF :OLD.CDCONTDIFEEM IS NOT NULL THEN
         V_DESC := V_DESC || ' Vendas Diferida=' || :OLD.CDCONTDIFEEM;
      END IF;

      IF :OLD.CDCTBLPERDEM IS NOT NULL THEN
         V_DESC := V_DESC || ' Lucros e perdas=' || :OLD.CDCTBLPERDEM;
      END IF;

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

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

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

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

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

      V_TIPO         := '3';
      V_ITPESQ       := :OLD.CDEMPRESA;
   ELSE
      IF UPDATING THEN
         IF (NVL(:OLD.CDEMPRESA, '!')     <> NVL(:NEW.CDEMPRESA,'!'))      OR
            (NVL(:OLD.CDCONFORNEM,'!')    <> NVL(:NEW.CDCONFORNEM,'!'))    OR
            (NVL(:OLD.CDCONCLIEEM,'!')    <> NVL(:NEW.CDCONCLIEEM,'!'))    OR
            (NVL(:OLD.CDCONTDIFEEM,'!')   <> NVL(:NEW.CDCONTDIFEEM,'!'))   OR
            (NVL(:OLD.CDCTBLPERDEM,'!')   <> NVL(:NEW.CDCTBLPERDEM,'!'))   OR
            (NVL(:OLD.IDGERNOSNUMBOL,'!') <> NVL(:NEW.IDGERNOSNUMBOL,'!')) OR
            (NVL(:OLD.CDSITUCRT,'!')      <> NVL(:NEW.CDSITUCRT,'!'))      OR
            (NVL(:OLD.IDAPROVNFTRANS,'!') <> NVL(:NEW.IDAPROVNFTRANS,'!')) OR
            (NVL(:OLD.IDEMAILNFTRANS,'!') <> NVL(:NEW.IDEMAILNFTRANS,'!')) OR
            (NVL(:OLD.IDENVIAEMAILNFREMET,'!') <> NVL(:NEW.IDENVIAEMAILNFREMET,'!')) THEN

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

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

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

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

            IF (NVL(:OLD.CDCTBLPERDEM,'!') <> NVL(:NEW.CDCTBLPERDEM,'!')) THEN
               V_DESC := V_DESC || ' Lucros e perdas=' || :NEW.CDCTBLPERDEM;
            END IF;

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

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

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

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

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

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

         IF :NEW.CDCONFORNEM IS NOT NULL THEN
            V_DESC := V_DESC || ' Forn=' || :NEW.CDCONFORNEM;
         END IF;

         IF :NEW.CDCONCLIEEM IS NOT NULL THEN
            V_DESC := V_DESC || ' Cliente=' || :NEW.CDCONCLIEEM;
         END IF;

         IF :NEW.CDCONTDIFEEM IS NOT NULL THEN
            V_DESC := V_DESC || ' Vendas Diferida=' || :NEW.CDCONTDIFEEM;
         END IF;

         IF :NEW.CDCTBLPERDEM IS NOT NULL THEN
            V_DESC := V_DESC || ' Lucros e perdas=' || :NEW.CDCTBLPERDEM;
         END IF;

         IF :NEW.IDGERNOSNUMBOL IS NOT NULL THEN
            V_DESC := V_DESC || ' NosNum=' || :NEW.IDGERNOSNUMBOL;
         END IF;

         IF :NEW.CDSITUCRT IS NOT NULL THEN
            V_DESC := V_DESC || ' CRT=' || :NEW.CDSITUCRT;
         END IF;

         IF :NEW.IDAPROVNFTRANS IS NOT NULL THEN
            V_DESC := V_DESC || ' AprovNFTrans=' || :NEW.IDAPROVNFTRANS;
         END IF;

         IF :NEW.IDEMAILNFTRANS IS NOT NULL THEN
            V_DESC := V_DESC || ' EmailNFTrans=' || :NEW.IDEMAILNFTRANS;
         END IF;

         IF :NEW.IDENVIAEMAILNFREMET IS NOT NULL THEN
            V_DESC := V_DESC || ' EmailNFRemet=' || :NEW.IDENVIAEMAILNFREMET;
         END IF;

         V_TIPO         := '1';
         V_ITPESQ       := :NEW.CDEMPRESA;
      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, CDFILLOGOPER,
                DSRDMUSUOPER, DSENDUSUOPER, DSNMUSUOPER,
                DSMETUSUOPER, CDORESPOPER)
            VALUES
               ('PARAMEMPRESA',  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;
/