﻿CREATE FUNCTION [dbo].[Replace_Subject_Email]
(
	@SUBJECT as varchar(255),
	@NOME_CLIENTE as varchar(50) = '', 
	@COGNOME_CLIENTE as varchar(50) = '', 
	@NOME_PB as varchar(50) = '', 
	@COGNOME_PB as varchar(50) = '', 
	@CODICE_PB as varchar(50) = '',
	@TIP_CONTRATTO as int = -1
)
RETURNS varchar(5000)
AS
BEGIN 
	DECLARE @SUBJECT_RITORNO as varchar(5000);
	DECLARE @TIP_CONTRATTO_TEXT as nvarchar(20)
	SET @TIP_CONTRATTO_TEXT = case @TIP_CONTRATTO when 1 then 'Sei' when 2 then 'Sei Versione Private' else 'Sei' end
	SET @SUBJECT_RITORNO = @SUBJECT;
	SET @SUBJECT_RITORNO = REPLACE(@SUBJECT_RITORNO,'/$NOME_CLIENTE$/',isnull(@NOME_CLIENTE,'--NOME CLIENTE NP--'));
	SET @SUBJECT_RITORNO = REPLACE(@SUBJECT_RITORNO,'/$COGNOME_CLIENTE$/',isnull(@COGNOME_CLIENTE,'--COGNOME CLIENTE NP--'));
	SET @SUBJECT_RITORNO = REPLACE(@SUBJECT_RITORNO,'/$NOME_PB$/',isnull(@NOME_PB,'--NOME PB NP--'));
	SET @SUBJECT_RITORNO = REPLACE(@SUBJECT_RITORNO,'/$COGNOME_PB$/',isnull(@COGNOME_PB,'--COGNOME PB NP--'));
	SET @SUBJECT_RITORNO = REPLACE(@SUBJECT_RITORNO,'/$CODICE_PB$/',isnull(@CODICE_PB,'--CODICE PB NP--'));
	SET @SUBJECT_RITORNO = REPLACE(@SUBJECT_RITORNO,'/$TIP_CONTRATTO$/',isnull(@TIP_CONTRATTO_TEXT,'--TIP CONTRATTO--'));
	--RETURN 'rettifica mailing informativo'
	RETURN @SUBJECT_RITORNO
END