-- TICKET
	ALTER TABLE [AKUZ].[T_TICKET] ADD [EPGUAge] int NULL
	GO	
	
	UPDATE tt
	SET tt.EPGUAge = (
		case br.LpuType
			when 1 then 3
			when 2 then 1
			when 3 then 1
			else 0
		end)
	FROM AKUZ.T_TICKET as tt
	JOIN AKUZ.T_WORK_SCHEDULE_ITEM as wsi on tt.WorkScheduleItem = wsi.WorkScheduleItemID
	JOIN AKUZ.T_WORK_SCHEDULE ws on wsi.WorkSchedule = ws.WorkScheduleID
	JOIN AKUZ.T_RESOURCE res on ws.Resource = res.ResourceID
	JOIN AKUZ.T_DIVISION div on res.Division = div.DivisionID
	JOIN AKUZ.T_BRANCH br on br.LpuID = div.Branch
	GO

	ALTER TABLE [AKUZ].[T_TICKET] ALTER COLUMN [EPGUAge] int NOT NULL
	GO


-- T_SCHEDULE_ITEM_TICKET
	ALTER TABLE [AKUZ].[T_SCHEDULE_ITEM_TICKET] ADD [EPGUAge] int NULL
	GO
	
	UPDATE sit
	SET sit.EPGUAge = (
		case br.LpuType
			when 1 then 3
			when 2 then 1
			when 3 then 1
			else 0
		end)
		FROM AKUZ.T_SCHEDULE_ITEM_TICKET sit
	JOIN AKUZ.T_SCHEDULE_ITEM si on si.ScheduleItemID = sit.ScheduleItem
	JOIN AKUZ.T_RESOURCE_SCHEDULE rs on rs.ResourceScheduleID = si.ResourceSchedule
	JOIN AKUZ.T_RESOURCE res on res.ResourceID = rs.Resource
	JOIN AKUZ.T_DIVISION div on div.DivisionID = res.Division
	JOIN AKUZ.T_BRANCH br on br.LpuID = div.Branch
	GO
	
	ALTER TABLE [AKUZ].[T_SCHEDULE_ITEM_TICKET] ALTER COLUMN [EPGUAge] int NOT NULL
	GO

