
/****** Object:  UserDefinedFunction [dbo].[GetExistenciaTeoricaSeccion]    Script Date: 05/02/2016 11:43:55 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION GetExistenciaTeoricaSeccion 
			 (@id_arti integer,@id_secc smallint,
			  @id_operacion integer)
					 

RETURNS numeric(12,4)  
AS 

/*
    Date: Mar 10,2016
    Devuelve la existencia teorica de un articulo en una seccion 

*/

BEGIN 

	Declare @existencia as  numeric(12,4)
	
/* =====================================
   Region 01
   Inicio de Existe 
   =====================================
*/

	If Exists(Select * 
				From stokseex 
				Where id_arti = @id_arti and id_secc = @id_secc)
		Begin
		
			Select @existencia = existencia 
			   From stokseex 
			   Where id_arti = @id_arti and id_secc = @id_secc
		End
		
	Else
	
		Begin
		
			Set @existencia = 0
			
		End
		
/* =====================================
   Fin de Existe 
   =====================================
*/

If @id_operacion=-2147483647
	Begin
	
		Set @id_operacion = NULL
		
	End
	
	
	Select @existencia = @existencia + 
			dbo.GetSaldoPendienteSeccionBeforeOper(@id_arti,@id_secc,@id_operacion)

	Return @existencia
	
END












