USE [FastMoneyPro_Remit]
GO
/****** Object:  StoredProcedure [dbo].[PROC_HIGH_CHART]    Script Date: 7/4/2019 11:35:48 AM ******/
DROP PROCEDURE [dbo].[PROC_HIGH_CHART]
GO
/****** Object:  StoredProcedure [dbo].[PROC_HIGH_CHART]    Script Date: 7/4/2019 11:35:48 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROC [dbo].[PROC_HIGH_CHART]
(
	@flag VARCHAR(20) ='high-chart'
	,@country VARCHAR(30) = NULL
	,@User		VARCHAR(50) 
)
AS
SET NOCOUNT ON;
SET XACT_ABORT ON;
BEGIN
	DECLARE @DATE VARCHAR(10),@FirstMonthStart DATE
		, @FirstMontEnd DATETIME
		, @SecondMonthStart DATE
		, @SecondMonthEnd DATETIME
        , @ThirdMonthStart DATE
		, @ThirdMonthEnd DATETIME
        , @FourthdMonthStart DATE
		, @FourthMonthEnd DATETIME
        , @FifthdMonthStart DATE
		, @FifthMonthEnd DATETIME
        , @LastMonthStart DATE
		, @LastMonthEnd DATETIME
        , @todayDate DATETIME 
		, @previousSixMonthDate Date

	IF @flag = 'country'
	BEGIN

		SELECT @FirstMonthStart = DATEADD(mm,-5,DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0)) 

		SELECT TOP 5 pCountry, count(10) [count] 
		FROM remittran (NOLOCK) 
		WHERE approvedDate between @FirstMonthStart and GETDATE()
		AND tranStatus <> 'CANCEL'
		GROUP BY pcountry 
		ORDER BY [count] DESC
	END
	IF @flag = 'high-chart'
	BEGIN
		IF (SELECT dbo.FNAHasRight(@User,'90100000') )='N'
			RETURN
		
		
		SELECT @todayDate  = DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))+' 23:59:59'
		SELECT @previousSixMonthDate = DATEADD(mm,-5,DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE())))  

		SELECT @FirstMonthStart = DATEADD(mm,-5,DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0)) 
		SELECT @SecondMonthStart = DATEADD(mm,1,@FirstMonthStart)
		SELECT @ThirdMonthStart = DATEADD(mm,2,@FirstMonthStart)
		SELECT @FourthdMonthStart = DATEADD(mm,3,@FirstMonthStart)
		SELECT @FifthdMonthStart = DATEADD(mm,4,@FirstMonthStart)
		SELECT @LastMonthStart = DATEADD(mm,5,@FirstMonthStart)

		SELECT @LastMonthEnd = DATEADD (dd, -1, DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) + 1, 0)) +' 23:59:59'
		SELECT @FifthMonthEnd = DATEADD(mm,-1,@LastMonthEnd)
		SELECT @FourthMonthEnd = DATEADD(mm,-2,@LastMonthEnd)
		SELECT @ThirdMonthEnd = DATEADD(mm,-3,@LastMonthEnd)
		SELECT @SecondMonthEnd = DATEADD(mm,-4,@LastMonthEnd)
		SELECT @FirstMontEnd = DATEADD(mm,-5,@LastMonthEnd)

		IF OBJECT_ID('tempdb..#tempRemit') IS NOT NULL DROP TABLE #tempRemit
		SELECT approvedDate,pCountry into #tempRemit 
		FROM remittran(NOLOCK)
		where approvedDate between @FirstMonthStart and GETDATE()
		AND tranStatus <> 'CANCEL'

		IF OBJECT_ID('tempdb..#TEMPCOUNTRY') IS NOT NULL DROP TABLE #TEMPCOUNTRY
		SELECT TOP 5 pCountry, count(10) [count] INTO #TEMPCOUNTRY FROM #tempRemit(nolock)
		--WHERE approvedDate BETWEEN  @FirstMonthStart and GETDATE()
		GROUP BY pcountry 
		ORDER BY [count] DESC
		
		DECLARE @cntCountry INT; SET @cntCountry = 1
		WHILE @cntCountry <= 5
		BEGIN
			SELECT TOP 1 @country = pCountry FROM #TEMPCOUNTRY(nolock)
			ORDER BY [count] DESC

			DELETE FROM #TEMPCOUNTRY WHERE pCountry = @country
			SET @cntCountry = @cntCountry + 1

			IF OBJECT_ID('tempdb..#TEMPJAN') IS NOT NULL DROP TABLE #TEMPJAN
			IF OBJECT_ID('tempdb..#TEMPFEB') IS NOT NULL DROP TABLE #TEMPFEB
			IF OBJECT_ID('tempdb..#TEMPMAR') IS NOT NULL DROP TABLE #TEMPMAR
			IF OBJECT_ID('tempdb..#TEMPAPR') IS NOT NULL DROP TABLE #TEMPAPR
			IF OBJECT_ID('tempdb..#TEMPMAY') IS NOT NULL DROP TABLE #TEMPMAY
			IF OBJECT_ID('tempdb..#TEMPJUNE') IS NOT NULL DROP TABLE #TEMPJUNE
		
			SELECT DAY(approveddate) [day], COUNT(1) [count] INTO #TEMPJAN FROM #tempRemit (nolock)
			WHERE approvedDate BETWEEN @FirstMonthStart AND @FirstMontEnd
			AND pCountry = @country
			GROUP BY DAY(approveddate)
			ORDER BY [day]

			SELECT DAY(approveddate) [day], COUNT(1) [count] INTO #TEMPFEB FROM #tempRemit (nolock)
			WHERE approvedDate BETWEEN @SecondMonthStart AND @SecondMonthEnd
			AND pCountry = @country
			GROUP BY DAY(approveddate)
			ORDER BY [day]
			
			SELECT DAY(approveddate) [day], COUNT(1) [count] INTO #TEMPMAR FROM #tempRemit (nolock)
			WHERE approvedDate BETWEEN @ThirdMonthStart AND @ThirdMonthEnd
			AND pCountry = @country
			GROUP BY DAY(approveddate)
			ORDER BY [day]

			SELECT DAY(approveddate) [day], COUNT(1) [count] INTO #TEMPAPR FROM #tempRemit (nolock)
			WHERE approvedDate BETWEEN @FourthdMonthStart AND @FourthMonthEnd
			AND pCountry = @country
			GROUP BY DAY(approveddate)
			ORDER BY [day]

			SELECT DAY(approveddate) [day], COUNT(1) [count] INTO #TEMPMAY FROM #tempRemit (nolock)
			WHERE approvedDate BETWEEN @FifthdMonthStart AND @FifthMonthEnd
			AND pCountry = @country
			GROUP BY DAY(approveddate)
			ORDER BY [day]

			SELECT DAY(approveddate) [day], COUNT(1) [count] INTO #TEMPJUNE FROM #tempRemit (nolock)
			WHERE approvedDate BETWEEN @LastMonthStart AND @todayDate
			AND pCountry = @country
			GROUP BY DAY(approveddate)
			ORDER BY [day]

			DECLARE @HIGHCHART TABLE([DAY] INT, FirstMonth  INT, SecondMonth INT, ThirdMonth INT, FourthMonth INT, FifthMonth INT, SixthMonth INT)
	
			DECLARE @cnt INT; SET @cnt = 1
			WHILE @cnt <=32
			BEGIN
				INSERT INTO @HIGHCHART 
				SELECT @CNT, 0,0,0,0,0,0
				SET @cnt = @cnt + 1
			END
		
			UPDATE T2 SET T2.FirstMonth = T1.cumulative
			FROM (SELECT t1.[day], t1.[count], SUM(ISNULL(t2.[count], 0)) as cumulative
			FROM #TEMPJAN t1 (nolock)
			INNER JOIN #TEMPJAN t2 (nolock) on t1.[day] >= t2.[day]
			GROUP BY t1.[day], t1.[count]
			) T1 
			INNER JOIN @HIGHCHART T2 ON T2.[DAY] = T1.[day]

			UPDATE T2 SET T2.SecondMonth = T1.cumulative
			FROM 
				(SELECT t1.[day], t1.[count], SUM(ISNULL(t2.[count], 0)) as cumulative
				FROM #TEMPFEB t1 (nolock)
				INNER JOIN #TEMPFEB t2 (nolock) on t1.[day] >= t2.[day]
				GROUP BY t1.[day], t1.[count]) T1 
			INNER JOIN @HIGHCHART T2 ON T2.[DAY] = T1.[day]

			UPDATE T2 SET T2.ThirdMonth = T1.cumulative
			FROM 
				(SELECT t1.[day], t1.[count], SUM(ISNULL(t2.[count], 0)) as cumulative
				FROM #TEMPMAR t1 (nolock)
				INNER JOIN #TEMPMAR t2 (nolock) on t1.[day] >= t2.[day]
				GROUP BY t1.[day], t1.[count]) T1 
			INNER JOIN @HIGHCHART T2 ON T2.[DAY] = T1.[day]

			UPDATE T2 SET T2.FourthMonth = T1.cumulative
			FROM 
				(SELECT t1.[day], t1.[count], SUM(ISNULL(t2.[count], 0)) as cumulative
				FROM #TEMPAPR t1 (nolock)
				INNER JOIN #TEMPAPR t2 (nolock) on t1.[day] >= t2.[day]
				GROUP BY t1.[day], t1.[count]) T1 
			INNER JOIN @HIGHCHART T2 ON T2.[DAY] = T1.[day]

			UPDATE T2 SET T2.FifthMonth = T1.cumulative
			FROM 
				(SELECT t1.[day], t1.[count], SUM(ISNULL(t2.[count], 0)) as cumulative
				FROM #TEMPMAY t1 (nolock)
				INNER JOIN #TEMPMAY t2 (nolock) on t1.[day] >= t2.[day]
				GROUP BY t1.[day], t1.[count]) T1 
			INNER JOIN @HIGHCHART T2 ON T2.[DAY] = T1.[day]

			UPDATE T2 SET T2.SixthMonth = T1.cumulative
			FROM 
				(SELECT t1.[day], t1.[count], SUM(ISNULL(t2.[count], 0)) as cumulative
				FROM #TEMPJUNE t1 (nolock)
				INNER JOIN #TEMPJUNE t2 (nolock) on t1.[day] >= t2.[day]
				GROUP BY t1.[day], t1.[count]) T1 
			INNER JOIN @HIGHCHART T2 ON T2.[DAY] = T1.[day]

				SELECT @country countryName
			IF OBJECT_ID('tempdb..#tempdata') IS NOT NULL DROP TABLE #tempdata
			SELECT * INTO #tempdata FROM @HIGHCHART
			DECLARE @sql VARCHAR(max);
			SET @sql = 'SELECT [DAY]'
			+',FirstMonth  AS ' +  DATENAME(MONTH,@FirstMonthStart) 
			+',SecondMonth  AS ' + DATENAME(MONTH,@SecondMonthStart)
			+ ',ThirdMonth  AS ' +  DATENAME(MONTH,@ThirdMonthEnd)
			+',FourthMonth  AS ' +  DATENAME(MONTH,@FourthdMonthStart)
			+ ',FifthMonth  AS ' +  DATENAME(MONTH,@FifthdMonthStart)
			+',SixthMonth  AS ' + DATENAME(MONTH,@LastMonthStart)
			+ ' from   #tempdata'
			--PRINT (@sql)
			EXEC (@sql)
			SET @cnt = @cnt + 1
			DELETE FROM @HIGHCHART
		END
		
	END
END
GO
