
/****** Object:  UserDefinedFunction [dbo].[GetExistenciaTeoricaAlmacen]    Script Date: 05/02/2016 11:43:54 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO



ALTER FUNCTION GetExistenciaTeoricaAlmacen 
               (@id_arti integer,
			    @id_alma smallint,
				@id_operacion integer)

--Devuelve la existencia teorica de un articulo en un almacen
RETURNS numeric(12,4)  AS 
BEGIN 

	declare @existencia as  numeric(12,4)

	if @id_operacion=-2147483647
		begin
			set @id_operacion = NULL
		end


	if exists(select * from stokalex where id_arti=@id_arti and id_alma=@id_alma)
		begin
			select @existencia=existencia from stokalex where id_arti=@id_arti and id_alma=@id_alma
		end
	else
		begin
			set @existencia = 0
		end

		--Set @existencia = 0

	 select @existencia=@existencia + dbo.GetSaldoPendienteAlmacenBeforeOper(@id_arti,@id_alma,@id_operacion)

	return @existencia
END














