/****** Object:  StoredProcedure [dbo].[sp_AddDoctorVisitNotifyShort]    Script Date: 11.02.2015 21:35:00 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF EXISTS ( SELECT  *
            FROM    sys.objects
            WHERE   object_id = OBJECT_ID(N'sp_AddDoctorVisitNotifyShort')
                    AND type IN ( N'P', N'PC' ) ) 
DROP PROCEDURE [dbo].[sp_AddDoctorVisitNotifyShort]
GO
-- =============================================
-- Author:  Timur R. Khakimov  2015
-- Create date: 2015-02-15
-- Description: Заполнение таблицы информацией о посещении врача
-- =============================================

CREATE PROCEDURE [dbo].[sp_AddDoctorVisitNotifyShort] @VisitID uniqueidentifier, @IsNew bit
AS
BEGIN
	 IF @IsNew = 1
	 BEGIN
		DECLARE @DoctorVisitNotifyServiceStatus as int 
		set @DoctorVisitNotifyServiceStatus = (SELECT [Status] from [AKUZ].[T_VCSN_SERVICES] where [VCSNServiceName]='DoctorVisitNotify')
		IF @DoctorVisitNotifyServiceStatus = 1
		BEGIN
			DECLARE @PatientID as uniqueidentifier,
					@AmbulanceEvent as uniqueidentifier, 
					@SpecialityName as varchar(255),
					@DoctorFIO as varchar(255),
					@LpuCode as varchar(4),
					@BranchCode as varchar(16),
					@PostID as uniqueidentifier,
					@VisitDate as datetime,
					@LpuPreferences as int

			SELECT 
			@VisitDate = Visit.VisitDate,
			@PatientID = AmbulanceCard.Patient,
			@AmbulanceEvent = Visit.AmbulanceEvent,
			@SpecialityName = Speciality.Name,
			@DoctorFIO = Worker.FIO,
			@LpuCode = LpuMain.Code,
			@PostID = PResource.ResourceIdGuid,
			@BranchCode = Branch.BranchCode,
			@LpuPreferences = LpuSettings.Preferences
			
			FROM [AKUZ].[T_VISIT] Visit

			JOIN [AKUZ].[T_EVENT_BASE] EventBase on (EventBase.EventID = Visit.AmbulanceEvent)
			JOIN [AKUZ].[T_AMBULANCE_CARD] AmbulanceCard on (AmbulanceCard.AmbulanceCardID = EventBase.AmbulanceCard)
			JOIN [AKUZ].[T_RESOURCE] PResource on (PResource.ResourceID = Visit.Post)
			JOIN [AKUZ].[T_SPECIALITY] Speciality on (Speciality.SpecialityID = PResource.Speciality)
			JOIN [AKUZ].[T_POST] Post on (Post.ResourceID = Visit.Post)
			JOIN [AKUZ].[T_WORKER] Worker on (Worker.WorkerID = Post.Worker)
			JOIN [AKUZ].[T_DIVISION] Division on (Division.DivisionID = PResource.Division)
			JOIN [AKUZ].[T_BRANCH] Branch on (Branch.LpuID = Division.Branch)
			JOIN [AKUZ].[T_LPU_MAIN] LpuMain on (LpuMain.LpuID = Branch.LpuMain)
			JOIN [AKUZ].[T_LPU_SETTINGS] LpuSettings on (LpuSettings.LpuMain = Branch.LpuMain)

			WHERE Visit.VisitID = @VisitID

			if ((@LpuPreferences & 128) > 0 or exists (
				select 1 
				from [AKUZ].T_ENTRY_BASE eb 
				join AKUZ.T_RESOURCE rr on eb.Resource = rr.ResourceID
				where eb.AmbulanceEvent = @AmbulanceEvent and eb.EntryDate = @VisitDate and rr.ResourceIdGuid = @PostID))
			BEGIN			
				INSERT INTO [AKUZ].[T_DOCTOR_VISIT_NOTIFY] (DoctorVisitNotifyID, PatientID, SpecName, DoctorFIO, LpuCode, BranchCode, CreationDT, PostID, VisitDate, VisitID)
				VALUES(NEWID(), @PatientID, @SpecialityName, @DoctorFIO, @LpuCode, @BranchCode, GETDATE(), @PostID, @VisitDate, @VisitID)
			END
		END
	 END
	 ELSE DELETE FROM [AKUZ].[T_DOCTOR_VISIT_NOTIFY] where [VisitID] = @VisitID AND [CreationDT] > DATEADD (DAY , -1 , GETDATE())
END
GO


