SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_UpdateTicketInfo]') and xtype = N'P')
drop procedure [dbo].[sp_UpdateTicketInfo]
GO

-- =============================================
-- Author:		Marat Zigangarayev
-- Create date: 2023/05/31
-- Description:	установка бита "Явка по посещению" в атрибуте TICKET.TicketInfo
-- =============================================
CREATE PROCEDURE [dbo].[sp_UpdateTicketInfo] 
	@AmbulanceEvent uniqueidentifier,
	@Post int,
	@VisitDate datetime,
	@Action int
As
Begin
	SET NOCOUNT ON;

	if @Action = 2 -- DELETE VISIT
		update t
		set t.TicketInfo = (t.TicketInfo & (~4))
		from akuz.t_ticket t
		join akuz.t_entry_base eb on t.EntryBase=eb.EntryBaseID
		where eb.AmbulanceEvent = @AmbulanceEvent
			and eb.[Resource] = @Post
			and eb.EntryDate = @VisitDate
	else
		update t
		set t.TicketInfo = (t.TicketInfo | 4)
		from akuz.t_ticket t
		join akuz.t_entry_base eb on t.EntryBase=eb.EntryBaseID
		where eb.AmbulanceEvent = @AmbulanceEvent
			and eb.[Resource] = @Post
			and eb.EntryDate = @VisitDate
End
go