ALTER trigger [dbo].[TR_tAct_Delete_UpdatePersonTicketDisplay] on [dbo].[tAct] after delete as
begin
	set nocount on

	declare @t table (id int primary key, done bit default 0)
	declare @ticketId int = 0


	insert @t (id)
		select distinct d.liActHdTicketId
		from deleted d 
			join tHdTicket t on t.iHdTicketId=d.liActHdTicketId
			left join tPerson p on p.iPersonId=d.liActFromPersonId and p.bPersonSystem=1
			left join (select liDocumentActId from tDocument group by liDocumentActId) do on do.liDocumentActId=d.iActId
		where d.liActKindId in (2, 3, 4, 5, 6, 7, 8, 10)
			or (d.liActKindId=1 and d.liActFromPersonId is not null and p.iPersonId is null)
			or (d.liActKindId=9 and d.ActOperationId in (2, 3, 4, 5, 6, 7, 9, 10, 13) and (d.mActNotice is not null or do.liDocumentActId  is not null))
			or t.liHdTicketStartingActId=d.iActId
		group by d.liActHdTicketId
	
	if not exists(select 1 from @t)
		return

	while exists(select 1 from @t where done=0)
	begin
		select top 1 @ticketId=id from @t where done=0

		update ptd set ptd.EventsNotDisplayed=0
		from PersonTicketDisplay ptd
			join (
									select tpr.liPersonId
				from deleted d
					join tAct a on a.liActHdTicketId=d.liActHdTicketId
					join tHdTicket t on t.iHdTicketId=d.liActHdTicketId
					join vHdTicketPersonRead tpr on tpr.liHdTicketId=d.liActHdTicketId
					left join PersonEventDisplay ped on ped.PersonId=tpr.liPersonId and ped.ActId=a.iActId
					left join tPerson p on p.iPersonId=a.liActFromPersonId and p.bPersonSystem=1
					left join tActHd ahd on ahd.liActHdActId=a.iActId
					left join (select distinct liDocumentActId from tDocument) doc on doc.liDocumentActId=a.iActId
				where d.iActId!=a.iActId
						and a.liActFromPersonId!=tpr.liPersonId							and (a.liActKindId in (2, 3, 4, 5, 6, 7, 8, 10)								or (a.liActKindId=1 and a.liActFromPersonId is not null and p.iPersonId is null)								or (a.liActKindId=9 and a.ActOperationId in (2, 3, 4, 5, 6, 7, 9, 10, 13) and (a.mActNotice is not null or doc.liDocumentActId is not null)) 							or t.liHdTicketStartingActId=a.iActId							)
					and (tpr.OnlyRequester=0 or (tpr.OnlyRequester=1 and (a.liActFromPersonId=t.liHdTicketUserPersonId or a.liActToPersonId=t.liHdTicketUserPersonId or isnull(ahd.bActHdUserRead,0)=1)))
					and tpr.liHdTicketId=@ticketId
				group by tpr.liPersonId
				having min(isnull(ped.Id,0))>0
			) act on act.liPersonId=ptd.PersonId
			join (
									select
					tpr.liPersonId
				from deleted d
					join tHdTicketApproval ta on ta.liHdTicketApprovalHdTicketId=d.liActHdTicketId
					join vHdTicketPersonRead tpr on tpr.liHdTicketId=d.liActHdTicketId and tpr.OnlyRequester=0
					left join PersonEventDisplay ped on ped.PersonId=tpr.liPersonId and ped.TicketApprovalId=ta.iHdTicketApprovalId
				where ta.liHdTicketApprovalStartedPersonId!=tpr.liPersonId
					and tpr.liHdTicketId=@ticketId
				group by tpr.liPersonId
				having min(isnull(ped.Id,0))>0
			) appr on appr.liPersonId=ptd.PersonId
			join (
									select
					tpr.liPersonId
				from deleted d
					join tHdTicketApproval ta on ta.liHdTicketApprovalHdTicketId=d.liActHdTicketId
					join tHdTicketApprovalItem tai on tai.liHdTicketApprovalItemHdTicketApprovalId=ta.iHdTicketApprovalId
					join vHdTicketPersonRead tpr on tpr.liHdTicketId=d.liActHdTicketId and tpr.OnlyRequester=0
					left join PersonEventDisplay ped on ped.PersonId=tpr.liPersonId and ped.TicketApprovalItemId=tai.iHdTicketApprovalItemId
				where tai.liHdTicketApprovalItemPersonId!=tpr.liPersonId
					and tpr.liHdTicketId=@ticketId
				group by tpr.liPersonId
				having min(isnull(ped.Id,0))>0
			) apprit on apprit.liPersonId=ptd.PersonId
		where ptd.EventsNotDisplayed=1					and ptd.TicketId=@ticketId

		update @t set done=1 where id=@ticketId
	end
	return
end;