﻿-- =============================================
-- Author:		S.E.Locatelli
-- Create date: 2014.02.21
-- Description:	Retituisce un codice offerta valido da yyMMdd + incr
-- =============================================
CREATE FUNCTION [dbo].[f_getNewCodQuote]
(
)
RETURNS INT
AS
BEGIN

	DECLARE @output INT
	DECLARE @oggi DATETIME = GETDATE()
	
	
	SELECT @output = ISNULL(MAX(CodQuote) + 1,CAST(RIGHT(YEAR(@oggi),2) AS NVARCHAR) + dbo.f_padLeft(MONTH(@oggi), 2,'0') + dbo.f_padLeft(DAY(@oggi), 2,'0')+'0001')
	FROM QuoteList
	WHERE codquote LIKE CAST(RIGHT(YEAR(@oggi),2) AS NVARCHAR) + dbo.f_padLeft(MONTH(@oggi), 2,'0') + dbo.f_padLeft(DAY(@oggi), 2,'0') + '%'
    AND (QuoteType = 'Q' OR QuoteType = 'S') 

	RETURN @output

	/* altra modalità (valutare...)
	-- calcolo prox valori "free" x cod Quote (QuoteRev parte a zero...)
	SELECT	@CodQuote = ISNULL(CodQuote, 0)
	FROM QuoteList
	WHERE QuoteType = 'Q'
	AND CodQuote LIKE dbo.fnFormatDate (getdate(), 'YYMMDD') +'%'

	-- se è vuoto prendo primo valore...
	IF(@CodQuote = 0)
	BEGIN
		SELECT @CodQuote = dbo.fnFormatDate (getdate(), 'YYMMDD0000') 
	END


	*/


END