ALTER VIEW [dbo].[VWPMO_FOLLOW_UP_EMAIL] AS
SELECT 
	T1.GUID FOLLOW_UP_ID,
	T1.NAME,
	T1.FOLLOW_UP_SEQUENCE,
	T1.FOLLOW_UP_DAYS,
	T1.FOLLOW_UP_TIME_SPAN,
	CONVERT(CHAR(5),GETDATE(),108) NOW_TIME,
	T.RECORD_ID,
	CONVERT(DATE,T.VALUE) VALUE,
	T1.EMAIL_SUBJECT,
	T1.EMAIL_BODY,
	T1.PROCESS_SELECT1,
	T1.PROCESS_SELECT2,
	T1.PROCESS_SELECT3,
	T1.PROCESS_SELECT4,
	T1.PROCESS_SELECT5,
	T1.PROCESS_SELECT6,
	T1.PROCESS_SELECT7,
	T1.PROCESS_SELECT8,
	T1.PROCESS_SELECT9,
	T1.PROCESS_SELECT10,
	T1.TIME_UNITY,
	T1.TIME_DIST,
	CASE T1.TIME_UNITY 
		WHEN 'Month' THEN 
			DATEDIFF(month, getdate(),convert(Date,T.value))
		WHEN 'Week' THEN 
			DATEDIFF(week, getdate(),convert(Date,T.value))
		END TIME_SPAN,
	T1.DEPENDENT_DATE_CTRL_ID,
	T1.DEPENDENT_DONE_CTRL_ID,
	(SELECT VALUE FROM TBPMO_CONTROL_VALUE WHERE CONTROL_ID = T1.DEPENDENT_DONE_CTRL_ID AND RECORD_ID = T.RECORD_ID) DONE_VALUE,
	T2.GUID AS CONSTRUCTOR_ID,
	T2.FORM_TITLE AS CONSTRUCTOR_TITLE
FROM 
	VWPMO_VALUES T, 
	TBPMO_FOLLOW_UP_EMAIL T1,
	TBPMO_FORM_CONSTRUCTOR T2
WHERE 
	T1.ACTIVE = 1 AND
	(SELECT VALUE FROM TBPMO_CONTROL_VALUE WHERE CONTROL_ID = T1.DEPENDENT_DONE_CTRL_ID AND RECORD_ID = T.RECORD_ID) = 'False' And
	T.CONTROL_ID = T1.DEPENDENT_DATE_CTRL_ID AND
	T1.CONSTRUCTOR_ID = T2.GUID AND
	T1.TIME_SPAN =
	CASE T1.TIME_UNITY 
		WHEN 'Month' THEN 
			DATEDIFF(month, getdate(),convert(Date,T.value))
		WHEN 'Week' THEN 
			DATEDIFF(week, getdate(),convert(Date,T.value))
    END
GO


