
/****** Object:  StoredProcedure [dbo].[ApiGetStockReservation]    Script Date: 13.09.2021 16:54:15 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE PROCEDURE [dbo].[ApiGetStockReservation]		 
	 @RepositoryId AS INT
	, @ItemId int
	, @LineId int

AS 
BEGIN

/*
declare @RepositoryId int;
declare @ItemId int;
declare @LineId int;

set @RepositoryId=1;
set @ItemId =  18877;
set @LineId = 0;
*/
select 0 [BatchId], 0 [LineId], 1 [FlowDirection]
      ,@ItemId [ItemId] ,@RepositoryId [MagId],'' [LocationName]  ,CURRENT_TIMESTAMP [Date]
      ,'' [Batch] ,0 [SourceBatchId] ,'' [Description] ,cast(0 as decimal) [Price]
      ,ISNULL((select SUM(Stock) from dbo.ApiStockView2 where ItemId =@ItemId and MagId = @RepositoryId),cast(0 as decimal)) [OrgStock]
      ,ISNULL((select SUM(Stock) from dbo.ApiStockView2 where ItemId =@ItemId and MagId = @RepositoryId),cast(0 as decimal)) - 
	  ISNULL( (select SUM(quantity - veryfiedQuantity) from dbo.ApiLines ll  join dbo.ApiDocuments  dd ON dd.Id = ll.documentId and ll.ReservationType>0 and ll.Direction<0  and  dd.State<2
			where ll.RepositoryId = @RepositoryId and itemId = @ItemId and BatchId=0  and ll.Id <>@LineId
	  ), cast( 0 as decimal)) 
  
	  [Stock]
      ,0 [Type]

  END

  
--    exec [dbo].ApiGetDocResoucreLines	 0,1,0,0;
GO


