
CREATE PROCEDURE [dbo].[API_WMSWmDokSpisCreate]
	 @WmsId as INT
AS 
BEGIN
/*
declare @WmsId int;
set @WmsId = 10;
*/
declare @DefId int;
declare @DefSymbol nvarchar(30);

select  @DefId =Id, @DefSymbol = Symbol from dbo.ApiDefinitions where [Default] = 1 and Type=40 and FlowDirection = -1 ;

-- ustalamy numer
declare @number int;
set @number = 1;
set @number = ISNULL(( select MAX(Number) from dbo.ApiWmsDocuments where DefId = @DefId and [Year] = Year(CURRENT_TIMESTAMP) and Month = MONTH(CURRENT_TIMESTAMP)),0)+1;



declare @NumberString nvarchar(30);
set @NumberString= CONCAT(@DefSymbol,'/' ,RIGHT('0000'+CAST(@number AS VARCHAR(4)),4), '/',RIGHT('00'+CAST(MONTH(CURRENT_TIMESTAMP) AS VARCHAR(2)),2),'/',  Year(CURRENT_TIMESTAMP)%100 );

select @number, @DefId;



insert into dbo.ApiWmsDocuments(
	   [ErpId],[Direction],[DefId],[StateId],[StateDoc]
      ,[DepartamentId]
      ,[Number],[Year],[Month],[Series],[NumberString]
      ,[Priority],[PlaceId],[CustId],[CustInfo],[Date],[OperatorId],[CreateTS],[OwnerId]
      ,[TS],[Description],[ObjectId],[ObjectType],[OrgDocumentNumber],[RepositoryId],[Info],[Info2],[Link],[ExtId]
      ,[ReciverId],[ForeginNumber],[Additionally],[CategoryId], ProjectId, ProjectLineId)

select  0 [ErpId], -1 [Direction],@DefId [DefId],0 [StateId],0 [StateDoc]
      ,[DepartamentId]
      , @number [Number],YEAR(CURRENT_TIMESTAMP) [Year],MONTH(CURRENT_TIMESTAMP) [Month],[Series],
	  @NumberString [NumberString]
      ,[Priority],[PlaceId],[CustId],[CustInfo],CURRENT_TIMESTAMP [Date],[OperatorId], CURRENT_TIMESTAMP [CreateTS],[OwnerId]
      , CURRENT_TIMESTAMP [TS],[Description],@WmsId [ObjectId], 40 [ObjectType], [NumberString] [OrgDocumentNumber], [RepositoryId],'' [Info],'' [Info2],'' [Link],'' [ExtId]
      ,[ReciverId],[ForeginNumber],[Additionally],[CategoryId] ,ProjectId, ProjectLineId from dbo.ApiWmsDocuments where Id  =@WmsId;
-- namierzamy id dokumentu
declare @WmsDocId int;
 set @WmsDocId = (SELECT SCOPE_IDENTITY());

-- teraz linie


insert into dbo.ApiWmsDocLines
([ErpId]
      ,[Lp],[DocumentId],[Direction],[ItemId],[ItemCode],[ItemName],[OrgQuantity],[Quantity],[UMRatio],[UM],[Quantity2],[UM2]
      ,[Barcode],[FeaturesRequired],[ItemFeatures],StockBarcode, [SerialNumberId],[LocationId],[LocationAdress],[OrgLocationId],[OrgLocationAdress],[TS],[Description]
      ,[Stock],[RepositoryId],[DocumentNumber],[FeatureType],[OrgLineId])

	  select [ErpId]
      ,[Lp], @WmsDocId [DocumentId],-1 [Direction],p.[ItemId],p.[ItemCode],p.[ItemName], (p.[OrgQuantity]- p.[Quantity]) [OrgQuantity], cast ( 0 as decimal) [Quantity],
	  p.[UMRatio],p.[UM], cast ( 0 as decimal) [Quantity2],p.[UM2]
      ,p.[Barcode],p.[FeaturesRequired],p.[ItemFeatures], p.StockBarcode ,p.[SerialNumberId],p.[LocationId],p.[LocationAdress],
	  p.[OrgLocationId],p.[OrgLocationAdress],CURRENT_TIMESTAMP [TS],p.[Description]
      ,s.SQuantity [Stock],p.[RepositoryId], @NumberString [DocumentNumber],p.[FeatureType],p.[OrgLineId]
	  from dbo.ApiWmsDocLines p	  
	  JOIN dbo.ApiWmsStockRealView s ON p.ItemId = s.ItemId and s.RepositoryId = p.RepositoryId and s.SQuantity>0 
	  where  p.Quantity < p.OrgQuantity and  p.DocumentId = @WmsId;

update dbo.ApiWmsDocLines set OrgQuantity = Stock where Stock< OrgQuantity and DocumentId = @WmsDocId;


IF ( NOT EXIStS ( select * from dbo.ApiWmsDocLines where DocumentId = @WmsDocId))
BEGIN
	delete dbo.ApiWmsDocuments where Id = @WmsDocId;

END
ELSE
BEGIN

Insert INTO [dbo].[ApiObjectRelations](	  
      [ObjectType],[ObjectId],[Description],[TS] ,[RelObjectId],[RelObjectType],[OperatorId],[Value],[HistoryParentId])
Values( 40,@WmsId,'Spis', CURRENT_TIMESTAMP, @WmsDocId, 40, 0 , 0 , 0 );
END
update dbo.ApiWmsDocuments set StateId = 3 where  Id  =@WmsId;
 END

 