

CREATE PROCEDURE [dbo].[API_WMSDokLinesCreate]
	 @Direction as INT,	 
	 @RepositoryId as INT,	 
	 @ObjectId as INT,
	 @ObjectType as INT,
	 @WmsDocId AS INT,
	 @NumberString as nvarchar(30),
	 @Additionally as int
AS 
BEGIN

-- tworzymy tabelkę tymczasową tam dodamy te linie
create table #Lines
(
		Lp int
	   ,SerialNumberId int 
	   ,[LineId] int	    
      ,[ObjectId] int
      ,[ObjectType] int
      ,[ItemId] int
      ,[ItemCode] nvarchar(100)
      ,[ItemName] nvarchar(200)
      ,[Quantity] float
      ,[UM] nvarchar(10)
      ,[Quantity2] float
      ,[UM2] nvarchar(10)
      ,[UMRatio] decimal(18,0)
      ,[Barcode] nvarchar(130)
      ,[ItemFeatures] nvarchar(100)	  
      ,[FeatureType]   nvarchar(50)
	  ,[FeaturesRequired] int
      ,StockBarcode nvarchar(100)
	  ,[Description] nvarchar(200)
)
IF (@Additionally=0) BEGIN
INSERT INTO #Lines
SELECT * FROM  [dbo].[ApiWmsBaseLinesView]  where ObjectId = @ObjectId and ObjectType = @ObjectType;
END
ELSE BEGIN
INSERT INTO #Lines
SELECT * FROM  [dbo].[ApiWmsBaseLinesAditionallyView]  where ObjectId = @ObjectId and ObjectType = @ObjectType;
END


-- Należy ustalić takie  zmienne
IF (@Direction = 1) 
 BEGIN
 INSERT INTO [dbo].[ApiWmsDocLines]
           ([ErpId],[DocumentId],[Direction],[ItemId],[ItemCode],[ItemName]
           ,[OrgQuantity],[Quantity],[UM],[Quantity2],[UM2],[UMRatio],[Barcode],[FeaturesRequired], [ItemFeatures], StockBarcode, FeatureType
           ,[OrgLocationId],[OrgLocationAdress],[LocationId],[LocationAdress],[TS],[Description],RepositoryId,DocumentNumber,OrgLineId, Lp ,SerialNumberId)		   
select p.LineId ErpId, @WmsDocId DocumentId, @Direction Direction, ItemId, 
ItemCode,  ItemName, p.[Quantity] [OrgQuantity], cast ( 0 as decimal) [Quantity] ,  UM , p.[Quantity] [Quantity2], UM2, cast( 1 as decimal) [UMRatio],
p.Barcode, 
p.[FeaturesRequired], ItemFeatures, StockBarcode, FeatureType, 0 OrgLocationId, '' OrgLocationAdress, 0 LocationId, '' LocationAdress, CURRENT_TIMESTAMP TS, '' Description, 
@RepositoryId RepositoryId , @NumberString DocumentNumber, 0 OrgLineId, p.Lp, p.SerialNumberId
 from #Lines p
 --where ObjectId = @ObjectId and ObjectType = @ObjectType
 END
 ELSE 
 BEGIN
 
 INSERT INTO [dbo].[ApiWmsDocLines]
           ([ErpId],[DocumentId],[Direction],[ItemId],[ItemCode],[ItemName]
           ,[OrgQuantity],[Quantity],[UM],[Quantity2],[UM2],[UMRatio],[Barcode],[FeaturesRequired], [ItemFeatures], StockBarcode, FeatureType
           ,[OrgLocationId],[OrgLocationAdress],[LocationId],[LocationAdress],[TS],[Description], Stock, RepositoryId, DocumentNumber,OrgLineId,
		   Lp ,SerialNumberId)		   
		   
select p.LineId ErpId, @WmsDocId DocumentId, @Direction Direction, p.ItemId, 
 ItemCode, ItemName, 
p.Quantity [OrgQuantity], case when( p.Quantity> s.SQuantity) then s.SQuantity else p.Quantity end  [Quantity] ,  UM , p.Quantity [Quantity2],  UM2, cast( 1 as decimal) [UMRatio],
p.Barcode Barcode, 
p.[FeaturesRequired],ISNULL(s.ItemFeatures,'') ItemFeatures , ISNULL(s.ItemFeatures,'') StockBarcode ,FeatureType, s.LocationId OrgLocationId, s.LocationAdress OrgLocationAdress, s.LocationId LocationId, s.LocationAdress LocationAdress, CURRENT_TIMESTAMP TS, '' Description
, s.SQuantity Stock, @RepositoryId RepositoryId , @NumberString DocumentNumber, 0 OrgLineId, p.Lp, p.SerialNumberId
 from #Lines p
 JOIN dbo.ApiWmsStockRealView s ON p.ItemId = s.ItemId and s.RepositoryId = @RepositoryId and s.SQuantity>0 
 --where ObjectId = @ObjectId and ObjectType = @ObjectType
 ORDER BY  ISNULL(s.ItemFeatures,''), s.TS





 

 --select @ObjectId
 -- ustawiamy właściwe ilości
  update dbo.[ApiWmsDocLines] 
 set Quantity =  IIF(OrgQuantity - [dbo].apiWmsLineSumQuantAbove(Id)<0, 0, IIF( (OrgQuantity - [dbo].apiWmsLineSumQuantAbove(Id))<Quantity,(OrgQuantity - [dbo].apiWmsLineSumQuantAbove(Id)), Quantity ))
 where DocumentId=@WmsDocId

  
 -- usuwamy zera
 delete dbo.[ApiWmsDocLines] where Quantity=0 and DocumentId=@WmsDocId;

 -- przepisujemy dane we właściwy sposób
 update dbo.[ApiWmsDocLines]  set OrgQuantity = Quantity , Quantity=0 where DocumentId=@WmsDocId;
 
 -- a może jak nie ma pozycji to najlepiej usunąć???
 select count(*) from dbo.[ApiWmsDocLines] where DocumentId=@WmsDocId;
 END
 -- jeśli nie ma żadnych elementów to usuwamy
 -- lub jeśli na wszystkie elementy nie starczyło wydania... do wykomentowania ewentualnie... pomijamy usuługi

 DROP TABLE #Lines;

 END
