CREATE FUNCTION [C6MartPeriodico].[getPreviousTrimestre] 
(
	@data datetime,
	@primo bit
)
RETURNS VARCHAR(8)
AS
BEGIN
	-- Declare the return variable here
	DECLARE @retData VARCHAR(8)
--select c6martperiodico.getPreviousTrimestre(cast ('20120701' as datetime),0)
--select c6martperiodico.getPreviousTrimestre(getDate(),1)
--select dbo.getParametro('TrimestreSettembre') 
--select cast ('20110630' as datetime)-30
IF @primo = 0 -- Restituisce l'ultimo giorno del trimestre precedente
-- Settare per la simulazione	SET @retData = '20110830'
--V Decommentare nel passaggio in produzione
	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)-1 AS VARCHAR) + dbo.getParametro('TrimestreDicembre')
		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('TrimestreMarzo')
		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('TrimestreGiugno')
		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('TrimestreSettembre')
		WHEN 
			(@data >= CAST(CAST(YEAR(@data) AS VARCHAR) + dbo.getParametro('TrimestreDicembre') AS DATETIME) -31 
			AND @data <= CAST(CAST(YEAR(@data) AS VARCHAR) + dbo.getParametro('TrimestreDicembre') AS DATETIME) +1)
		THEN CAST(YEAR(@data) AS VARCHAR) + dbo.getParametro('TrimestreSettembre')
	END
	ELSE -- Restituisce il primo giorno del trimestre precedente
-- Settare per la simulazione	SET @retData = '20110901'
	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)-1 AS VARCHAR) + SUBSTRING(dbo.getParametro('TrimestreDicembre'),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('TrimestreMarzo'),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('TrimestreGiugno'),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('TrimestreSettembre'),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) +1 THEN CAST(YEAR(@data) AS VARCHAR) + SUBSTRING(dbo.getParametro('TrimestreSettembre'),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)