--Update Old record of Fee.IsPrintMultipleFeeReceipt Config because type is changed from ValidateBoolean to CustomList
UPDATE sConfig.tConfigurationSetup SET ItemValue = CASE WHEN ItemValue = 'True' THEN 'True' ELSE 'False' END WHERE ConfigurationId = 335

-- Remove Configuration from Front
GO
UPDATE sConfig.tConfigurationMaster SET IsHide = 0 WHERE Id IN (489, 490)
GO

-- Add Field in EventMaster 
GO
UPDATE sAlert.tEventMaster SET Fields = Fields + ',[Image]' WHERE Id = 73 AND CHARINDEX('[Image]', Fields) <= 0
UPDATE sAlert.tEventMaster SET Fields = Fields + ',[Image]' WHERE Id = 77 AND CHARINDEX('[Image]', Fields) <= 0
UPDATE sAlert.tEventMaster SET Fields = Fields + ',[Image]' WHERE Id = 78 AND CHARINDEX('[Image]', Fields) <= 0

UPDATE sAlert.tEventMaster SET Template = Template + ',[Image]' WHERE Id = 73 AND CHARINDEX('[Image]', Template) <= 0
UPDATE sAlert.tEventMaster SET Template = Template + ',[Image]' WHERE Id = 77 AND CHARINDEX('[Image]', Template) <= 0
UPDATE sAlert.tEventMaster SET Template = Template + ',[Image]' WHERE Id = 78 AND CHARINDEX('[Image]', Template) <= 0
GO

-- Update Event Template if Configuration is true
IF OBJECT_ID(N'tempdb..#TempEventData') IS NOT NULL
BEGIN
	DROP TABLE #TempEventData
END

SELECT ROW_NUMBER() OVER(ORDER BY OrgId) AS AutoId, Template, EventId, OrgId INTO #TempEventData 
FROM sAlert.tEventTemplate WHERE EventId IN (73,77,78) AND IsAutoSend = 1 AND AlertModeId = 4

DECLARE @lCount int = 0
SELECT @lCount = COUNT(1) FROM #TempEventData

WHILE(@lCount > 0)
BEGIN
	DECLARE @lOrgId int = 0, @lEventId int = 0, @lItemValue nvarchar(500) = ''
	SELECT @lOrgId = OrgId, @lEventId = EventId FROM #TempEventData WHERE AutoId = @lCount
	
	IF(@lEventId = 73)
	BEGIN
		SELECT @lItemValue = ISNULL(CS.ItemValue,CM.ItemValue)
		FROM sConfig.tConfigurationMaster CM LEFT JOIN sConfig.tConfigurationSetup CS ON CS.ConfigurationId = CM.Id AND CS.SourceTypeId=1016 AND SourceId = @lOrgId 
		Where CM.ConfigurationLevelId = 1 AND CM.Id = 490
	END
	IF(@lEventId = 77 OR @lEventId = 78)
	BEGIN
		SELECT @lItemValue = ISNULL(CS.ItemValue,CM.ItemValue)
		FROM sConfig.tConfigurationMaster CM LEFT JOIN sConfig.tConfigurationSetup CS ON CS.ConfigurationId = CM.Id AND CS.SourceTypeId=1016 AND SourceId = @lOrgId 
		Where CM.ConfigurationLevelId = 1 AND CM.Id = 489
	END
	
	IF(@lItemValue = 'True')
	BEGIN
		UPDATE sAlert.tEventTemplate SET Template = Template + ' [Image]' WHERE EventId = @lEventId AND IsAutoSend = 1 AND AlertModeId = 4 AND OrgId = @lOrgId AND CHARINDEX('[Image]', Template) <= 0
	END

	SET @lCount = @lCount - 1
END
