/****** Object:  StoredProcedure [dbo].[CreateBreifByDateNumbers]    Script Date: 5/20/2021 09:38:45 ق.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[CreateBreifByDateNumbers] 
(
	@DAYS int=1,
	@StartDate as datetime
)
AS
BEGIN
	SET NOCOUNT ON;

	DECLARE @ID as bigint
	DECLARE @DocDate as datetime
	DECLARE @DocDateFrom as datetime
	DECLARE @DocDateTo as datetime
	DECLARE @LastDate as datetime

	SET @ID=0

	SELECT IDENTITY(bigint,1,1) as ID, DocDate
	INTO #TEMP FROM AccDocumentGoods WITH(NOLOCK) Where  not TotalID is null  AND ID>0  and Flags = 2 GROUP BY DocDate order by DocDate

	--select * from #TEMP

    Select @LastDate = Max(DocDate) from AccDocHeader 

	DECLARE MyCursor CURSOR LOCAL FAST_FORWARD READ_ONLY FOR 
				Select DocDate from #TEMP WITH(NOLOCK)
					Order by ID
	Open MyCursor

	FETCH NEXT FROM MyCursor INTO @DocDate
	SET @DocDateTo=DATEADD(day, @DAYS-1, @StartDate) --@DocDate
	if @DocDateTo > @LastDate 
		SET @DocDateTo = @LastDate 
	--print '>>'+ Str(@ID) +'   '+ convert(nvarchar(20),@DocDateTo)
	WHILE @@FETCH_STATUS = 0 
	BEGIN
		--print DATEDIFF(Day, @DocDateTo, @DocDate)
		--print DATEDIFF(Day, @DocDate, @DocDateTo)
		WHILE  DATEDIFF(Day, @DocDateTo, @DocDate) >= 1
		BEGIN
			SET @DocDateTo = DATEADD(day, @DAYS, @DocDateTo)
			if @DocDateTo > @LastDate 
				SET @DocDateTo = @LastDate 
			SET @ID=@ID+1
			--print '<<'+Str(@ID) +'   '+ convert(nvarchar(20), @DocDateTo)
		END
		--SELECT 2,@DocDate,@DocDateTo, * FROM AccDocHeader where DocDate=@DocDate and Flags = 2
		UPDATE AccDocHeader SET TempNumber=@ID,  TempDate= @DocDateTo where DocDate=@DocDate and Flags = 2

		FETCH NEXT FROM MyCursor INTO @DocDate
	END

	CLOSE MyCursor
	DEALLOCATE MyCursor

	DROP TABLE #TEMP

	SELECT IDENTITY(bigint,1,1) as ID, DocDate
		INTO #TEMP1 FROM AccDocumentGoods WITH(NOLOCK) Where  not TotalID is null  AND ID>0  and Flags = 1 GROUP BY DocDate order by DocDate

	--select * from #TEMP1

	DECLARE MyCursor CURSOR LOCAL FAST_FORWARD READ_ONLY FOR 
				Select DocDate from #TEMP1 WITH(NOLOCK)
					Order by DocDate
	Open MyCursor

	--SET @ID=1
			SET @ID=@ID+1

	FETCH NEXT FROM MyCursor INTO @DocDate
	SET @DocDateFrom = @StartDate
	SET @DocDateTo = DATEADD(day, @DAYS-1, @StartDate) 
	if @DocDateTo > @LastDate 
		SET @DocDateTo = @LastDate 
--print Str(@ID) +' *1*  '+ convert(nvarchar(20),@DocDateTo)

	IF NOT @DocDate between @DocDateFrom AND @DocDateTo and @DocDate < @DocDateFrom --Go back
	BEGIN
		WHILE NOT @DocDate between @DocDateFrom AND @DocDateTo
		BEGIN
--print Str(@ID) +' *1*  ' + convert(nvarchar(20),@DocDateFrom) +' *1*  ' + convert(nvarchar(20),@DocDate) +' *1*  ' + convert(nvarchar(20),@DocDateTo)
			SET @DocDateFrom = DATEADD(day, -@DAYS, @DocDateFrom)
			if @DocDateFrom > @LastDate 
				SET @DocDateFrom = @LastDate 
			SET @DocDateTo = DATEADD(day, -@DAYS, @DocDateTo)
			if @DocDateTo > @LastDate 
				SET @DocDateTo = @LastDate 
		END
		SET @ID=@ID+1
--		print Str(@ID) +' *1*  '+ convert(nvarchar(20), @DocDateTo)
	END


	IF NOT @DocDate between @DocDateFrom AND @DocDateTo and @DocDate < @DocDateFrom
	BEGIN
		WHILE NOT @DocDate between @DocDateFrom AND @DocDateTo
		BEGIN
--print Str(@ID) +' *1*  ' + convert(nvarchar(20),@DocDateFrom) +' *1*  ' + convert(nvarchar(20),@DocDate) +' *1*  ' + convert(nvarchar(20),@DocDateTo)
			SET @DocDateFrom = DATEADD(day, @DAYS, @DocDateFrom)
			if @DocDateFrom > @LastDate 
				SET @DocDateFrom = @LastDate 
			SET @DocDateTo = DATEADD(day, @DAYS, @DocDateTo)
			if @DocDateTo > @LastDate 
				SET @DocDateTo = @LastDate 
		END
		SET @ID=@ID+1
--		print Str(@ID) +' *1*  '+ convert(nvarchar(20), @DocDateTo)
	END
	UPDATE AccDocHeader SET TempNumber=@ID,  TempDate= @DocDateTo where DocDate=@DocDate and Flags=1

	WHILE @@FETCH_STATUS = 0 
	BEGIN
		FETCH NEXT FROM MyCursor INTO @DocDate
		IF NOT @DocDate between @DocDateFrom AND @DocDateTo and @DocDate > @DocDateFrom
		BEGIN
			WHILE NOT @DocDate between @DocDateFrom AND @DocDateTo
			BEGIN
				SET @DocDateFrom = DATEADD(day, @DAYS, @DocDateFrom)
				if @DocDateFrom > @LastDate 
					SET @DocDateFrom = @LastDate 
				SET @DocDateTo = DATEADD(day, @DAYS, @DocDateTo)
				if @DocDateTo > @LastDate 
					SET @DocDateTo = @LastDate 
			END
			SET @ID=@ID+1
--			print Str(@ID) +' *1*  '+ convert(nvarchar(20), @DocDateTo)
		END
		UPDATE AccDocHeader SET TempNumber=@ID,  TempDate= @DocDateTo where DocDate=@DocDate and Flags=1
	END

	CLOSE MyCursor
	DEALLOCATE MyCursor

	DROP TABLE #TEMP1

	SELECT IDENTITY(bigint,1,1) as ID, DocDate
	INTO #TEMP2 FROM AccDocumentGoods WITH(NOLOCK) Where  not TotalID is null  AND ID>0  and Flags = 3 GROUP BY DocDate order by DocDate

	DECLARE MyCursor CURSOR LOCAL FAST_FORWARD READ_ONLY FOR 
				Select DocDate from #TEMP2 WITH(NOLOCK)
					Order by ID
	Open MyCursor

	FETCH NEXT FROM MyCursor INTO @DocDate
	SET @DocDateTo=DATEADD(day, @DAYS-1, @StartDate) --@DocDate
	if @DocDateTo > @LastDate 
		SET @DocDateTo = @LastDate 
	print 'x>>'+ Str(@ID) +'   '+ convert(nvarchar(20),@DocDateTo)
	WHILE @@FETCH_STATUS = 0 
	BEGIN
		print DATEDIFF(Day, @DocDateTo, @DocDate)
		print DATEDIFF(Day, @DocDate, @DocDateTo)
		while DATEDIFF(Day, @DocDateTo, @DocDate) >= 1
		BEGIN
		print 'x<<'+STr( DATEDIFF(Day, @DocDateTo, @DocDate))
			SET @DocDateTo = DATEADD(day, @DAYS, @DocDateTo)
			if @DocDateTo > @LastDate 
				SET @DocDateTo = @LastDate 
			SET @ID=@ID+1
			print 'x<<'+Str(@ID) +'   '+ convert(nvarchar(20), @DocDateTo)
		END
		SELECT 3,@DocDate,@DocDateTo, * FROM AccDocHeader where DocDate=@DocDate and Flags = 3
		UPDATE AccDocHeader SET TempNumber=@ID,  TempDate= @DocDateTo where DocDate=@DocDate and Flags = 3

		FETCH NEXT FROM MyCursor INTO @DocDate
	END

	CLOSE MyCursor
	DEALLOCATE MyCursor

	DROP TABLE #TEMP2

END