﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [C6MartPeriodico].[getNextTrimestre] 
(
	@data datetime,
	@primo bit
)
RETURNS VARCHAR(8)
AS
BEGIN
	-- Declare the return variable here
	DECLARE @retData VARCHAR(8)
--select c6martperiodico.getTrimestre2(cast ('20120101' as datetime),1)
--select cast ('20110630' as datetime)-30
	-- Add the T-SQL statements to compute the return value here
	IF @primo = 0 -- Restituisce l'ultimo giorno del trimestre precedente
	SET @retData =
	CASE
		WHEN @data >= CAST(CAST(YEAR(@data) AS VARCHAR) + dbo.getParametro('TrimestreMarzo') AS DATETIME) -30 AND @data < CAST(CAST(YEAR(@data) AS VARCHAR) + dbo.getParametro('TrimestreGiugno') AS DATETIME) - 29 THEN CAST(YEAR(@data) AS VARCHAR) + dbo.getParametro('TrimestreGiugno')
		WHEN @data >= CAST(CAST(YEAR(@data) AS VARCHAR) + dbo.getParametro('TrimestreGiugno') AS DATETIME) - 29 AND @data < CAST(CAST(YEAR(@data) AS VARCHAR) + dbo.getParametro('TrimestreSettembre') AS DATETIME) - 29 THEN CAST(YEAR(@data) AS VARCHAR) + dbo.getParametro('TrimestreSettembre')
		WHEN @data >= CAST(CAST(YEAR(@data) AS VARCHAR) + dbo.getParametro('TrimestreSettembre') AS DATETIME) -29 AND @data < CAST(CAST(YEAR(@data) AS VARCHAR) + dbo.getParametro('TrimestreDicembre') AS DATETIME) - 30 THEN CAST(YEAR(@data) AS VARCHAR) + dbo.getParametro('TrimestreDicembre')
		WHEN @data >= CAST(YEAR(@data) AS VARCHAR)+'0101' AND @data < CAST(CAST(YEAR(@data) AS VARCHAR) + dbo.getParametro('TrimestreMarzo') AS DATETIME)-30 THEN CAST(YEAR(@data) - 1 AS VARCHAR) + dbo.getParametro('TrimestreMarzo')
		WHEN @data >= CAST(CAST(YEAR(@data) AS VARCHAR) + dbo.getParametro('TrimestreDicembre') AS DATETIME) -30 AND @data <= CAST(CAST(YEAR(@data) AS VARCHAR) + dbo.getParametro('TrimestreDicembre') AS DATETIME) THEN CAST(YEAR(@data) AS VARCHAR) + dbo.getParametro('TrimestreMarzo')
	END
	ELSE -- Restituisce il primo giorno del trimestre precedente
	SET @retData = 
	CASE
		WHEN @data >= CAST(CAST(YEAR(@data) AS VARCHAR) + dbo.getParametro('TrimestreMarzo') AS DATETIME) -30 AND @data < CAST(CAST(YEAR(@data) AS VARCHAR) + dbo.getParametro('TrimestreGiugno') AS DATETIME) - 29 THEN CAST(YEAR(@data) AS VARCHAR) + SUBSTRING(dbo.getParametro('TrimestreGiugno'),1,2) + '01'
		WHEN @data >= CAST(CAST(YEAR(@data) AS VARCHAR) + dbo.getParametro('TrimestreGiugno') AS DATETIME) - 29 AND @data < CAST(CAST(YEAR(@data) AS VARCHAR) + dbo.getParametro('TrimestreSettembre') AS DATETIME) - 29 THEN CAST(YEAR(@data) AS VARCHAR) + SUBSTRING(dbo.getParametro('TrimestreSettembre'),1,2) + '01'
		WHEN @data >= CAST(CAST(YEAR(@data) AS VARCHAR) + dbo.getParametro('TrimestreSettembre') AS DATETIME) -29 AND @data < CAST(CAST(YEAR(@data) AS VARCHAR) + dbo.getParametro('TrimestreDicembre') AS DATETIME) - 30 THEN CAST(YEAR(@data) AS VARCHAR) + SUBSTRING(dbo.getParametro('TrimestreDicembre'),1,2) + '01'
		WHEN @data >= CAST(YEAR(@data) AS VARCHAR)+'0101' AND @data < CAST(CAST(YEAR(@data) AS VARCHAR) + dbo.getParametro('TrimestreMarzo') AS DATETIME)-16 THEN CAST(YEAR(@data) - 1 AS VARCHAR) + SUBSTRING(dbo.getParametro('TrimestreMarzo'),1,2) + '01'
		WHEN @data >= CAST(CAST(YEAR(@data) AS VARCHAR) + dbo.getParametro('TrimestreDicembre') AS DATETIME) -30 AND @data <= CAST(CAST(YEAR(@data) AS VARCHAR) + dbo.getParametro('TrimestreDicembre') AS DATETIME) THEN CAST(YEAR(@data) AS VARCHAR) + SUBSTRING(dbo.getParametro('TrimestreMarzo'),1,2) + '01'
	END
	-- Return the result of the function
	RETURN @retData
END
--select c6martperiodico.gettrimestre2(CAST('20100101' AS DATETIME),1)
--SELECT CAST(CAST(YEAR(CAST('20101216' AS DATETIME)) AS VARCHAR) + dbo.getParametro('TrimestreDICEMBRE') AS DATETIME)-16
--CAST(CAST(YEAR(@data) AS VARCHAR) + dbo.getParametro('TrimestreDicembre') AS DATETIME)