CREATE OR REPLACE TRIGGER LOG_MOVREAL
AFTER DELETE OR INSERT OR UPDATE
ON MOVREAL FOR EACH ROW
DECLARE
  V_USUARIO      VARCHAR2(40);
  V_TIPO         VARCHAR2(1);
  V_ITPESQ       VARCHAR2(40);
  V_DESC         VARCHAR2(4000);
  V_CDFILLOGOPER VARCHAR2(4);
  V_DTOPER       DATE;
  V_DSREMOTEADDR VARCHAR2(15);
  V_DSREMOTEHOST VARCHAR2(15);
  V_DSSERVERRDM  VARCHAR2(20);
  V_DSSERVMETHOD VARCHAR2(30);
  V_DSREMOTEOPER VARCHAR2(12);
BEGIN
   V_CDFILLOGOPER := '';
   IF DELETING THEN
      V_TIPO   := '3';
      V_DESC   := 'NRDOCMOVREAL  '   || :OLD.NRDOCMOVREAL                    ||
                  ' DTMOVREAL '      || :OLD.DTMOVREAL                       ||
                  ' VRMOVREAL '      || :OLD.VRMOVREAL                       ||
                  ' IDMOVREAL '   	 || :OLD.IDMOVREAL                       ||
                  ' DSHISTMOVREAL '  || :OLD.DSHISTMOVREAL                   ||
                  ' IDSTCONMVBAN '   || :OLD.IDSTCONMVBAN                    ||
                  ' IDERCONMVBAN '   || :OLD.IDERCONMVBAN                    ||       
                  ' IDTIPOREGI '     || :OLD.IDTIPOREGI                      ||
                  ' CDCATELANC '     || :OLD.CDCATELANC                      ||
                  ' IDCONCREAL '   	 || :OLD.IDCONCREAL                      ||
                  ' NRMOVBARE  '     || :OLD.NRMOVBARE                       ||
                  ' IDTPMOVREAL '    || :OLD.IDTPMOVREAL                     ||
                  ' NRCONCIMOVB '    || :OLD.NRCONCIMOVB;     						

						
      V_ITPESQ := :OLD.CDREDUCONT||:OLD.CDSEQUEXTR || :OLD.NRSEQMOVREAL;
   ELSE
		IF UPDATING THEN			
			V_ITPESQ := :OLD.CDREDUCONT||:OLD.CDSEQUEXTR || :OLD.NRSEQMOVREAL;
			V_TIPO := '2';
		ELSE	
			V_TIPO   := '1';
			V_ITPESQ := :NEW.CDREDUCONT||:NEW.CDSEQUEXTR || :NEW.NRSEQMOVREAL;			
		END IF;
		V_DESC   := 'NRDOCMOVREAL  '   || :NEW.NRDOCMOVREAL                    ||
						' DTMOVREAL '      || :NEW.DTMOVREAL                       ||
						' VRMOVREAL '      || :NEW.VRMOVREAL                       ||
						' IDMOVREAL '   	 || :NEW.IDMOVREAL                       ||
						' DSHISTMOVREAL '  || :NEW.DSHISTMOVREAL                   ||
						' IDSTCONMVBAN '   || :NEW.IDSTCONMVBAN                    ||
						' IDERCONMVBAN '   || :NEW.IDERCONMVBAN                    ||       
						' IDTIPOREGI '     || :NEW.IDTIPOREGI                      ||
						' CDCATELANC '     || :NEW.CDCATELANC                      ||
						' IDCONCREAL '   	 || :NEW.IDCONCREAL                      ||
						' NRMOVBARE  '     || :NEW.NRMOVBARE                       ||
						' IDTPMOVREAL '    || :NEW.IDTPMOVREAL                     ||
						' NRCONCIMOVB '    || :NEW.NRCONCIMOVB; 			
	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
               ('MOVREAL', 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;
/