if OBJECT_ID('sp_RDS_CancelHospitalisationXml', 'P') is not null
	drop procedure sp_RDS_CancelHospitalisationXml
GO
--SET ANSI_NULLS ON
--GO
--SET QUOTED_IDENTIFIER ON
--GO
---- =============================================
---- Author:		Vitacore (с)
---- Create date: 2014-03-01
---- Description:	Выгрузка информации об анулировании госпитализации в RDS 
---- =============================================
--CREATE PROCEDURE [dbo].[sp_RDS_CancelHospitalisationXml]
--	@LpuCode varchar(max) = null,
--	@Xml xml = null,
--	@IsNew bit = 1,
--	@ID uniqueidentifier = null,
--	@QueueName varchar(255) = null
--AS
--BEGIN
--	declare @Result varchar(max)
--			,@ContentEncoding varchar(255)
--			,@Type varchar(255)
--			,@OperationType varchar(255)
--			,@OwnerID uniqueidentifier

--	SET NOCOUNT ON;
--	IF (@Xml is null)
--	BEGIN
--		--выясняем тип операции
--		IF(@IsNew = 1)
--		BEGIN
--			select @OperationType = 'Insert'
--		END
--		ELSE IF (@IsNew = 0 and exists (select * from [AKUZ].T_TAKE_OVER_HOSPITALISATION toh  where toh.MedicalRecordID = @ID))
--		BEGIN
--			select @OperationType = 'Update'
--		END
--		ELSE IF (@IsNew = 0 and not exists (select * from [AKUZ].T_TAKE_OVER_HOSPITALISATION toh where toh.MedicalRecordID = @ID))
--		BEGIN
--			select @OperationType = 'Delete'
--				select @Result = (select 
--				@LpuCode				as 'header/LPU'
--				,@OperationType			as 'header/TYPE'
--				,@ID					as 'header/ID'
--				FOR XML PATH(''),
--				ROOT('CANCEL_HOSPITALISATION')
--			)
--		END

--select @ContentEncoding = 'utf-8'
--   ,@Type = 'CANCEL_HOSPITALISATION'
--   ,@OwnerID = @ID
--		IF(@OperationType <> 'Delete')
--		BEGIN
--	select @Result = ( 
--	select
--			@LpuCode											as 'header/LPU'
--			,@OperationType										as 'header/TYPE'
--			,@ID												as 'header/ID'
			
--			,mcr.MovementID										as 'body/HOSPITALISATION_ID'
--			,CASE toh.DenialHospitalisation
--			WHEN 22901 THEN 2
--			WHEN 22902 THEN 5
--			WHEN 22903 THEN 1
--			ELSE 4 END											as 'body/REASON'
--           ,1													as 'body/CANCEL_SOURSE'
--           ,br.BranchCode										as 'body/BRANCH'
--		   ,convert(varchar(10), toh.DateOver , 20)				as 'body/DATE_CANCEL'
--		   ,1													as 'body/CANCEL_TYPE'
--		   ,d.Code												as 'body/HOSPITALISATION_DIVISION'
--		   ,convert(varchar(10), m.MovementDate, 20)			as 'body/DATE'
--		   ,m.MedCardNumber										as 'body/MED_CARD_NUMBER'
--           ,p.PatientID											as 'body/PATIENT'
--		from [AKUZ].T_TAKE_OVER_HOSPITALISATION toh
--		--join [AKUZ].T_AMBULANCE_RECORD ar on ar.MedicalRecordID = toh.MedicalRecordID
--		join [AKUZ].T_AMBULANCE_CARD_RECORD acr on acr.MedicalRecordID = toh.MedicalRecordID
--		--join [AKUZ].T_EVENT_BASE eb on eb.EventID = ar.AmbulanceEvent
--		join [AKUZ].T_AMBULANCE_CARD ac on ac.AmbulanceCardID = acr.AmbulanceCard
--		join [AKUZ].T_PATIENT p on p.PatientID = ac.Patient
--		--join [AKUZ].T_MOVEMENT m on m.EmergencyEvent = eb.EventID
--		join [AKUZ].T_MOVEMENT m on m.EmergencyEvent = acr.AmbulanceEvent
--		Join [AKUZ].T_MEDICALCARD_REGISTRATION mcr on mcr.MovementID = m.MovementID
--		join [AKUZ].T_DIVISION d on d.DivisionID = m.Division
--		join [AKUZ].T_BRANCH br on br.LpuID = d.Branch
--		where toh.MedicalRecordID = @ID
--		FOR XML PATH(''),
--		ROOT('CANCEL_HOSPITALISATION')
--	) 
--		END

--		INSERT INTO [AKUZ].[T_RABBIT_SEND_OBJECT]
--		   ([RabbitSendObjectID]
--		   ,[QueueName]
--		   ,[Content]
--		   ,[MessageID]
--		   ,[Headers]
--		   ,[ContentEncoding]
--		   ,[Type]
--		   ,[UserID]
--		   ,[State]
--		   ,[CreateDateTime]
--		   ,[OwnerID])
--		select
--		   NewID()--(<RabbitSendObjectID, uniqueidentifier,>
--		   ,@QueueName--,<QueueName, varchar(255),>
--		   ,dbo.ConvertTextToBinaryZip(@Result, '')--,<Content, text,>
--		   ,null--,<MessageID, varchar(255),>
--		   ,null--,<Headers, varchar(255),>
--		   ,@ContentEncoding--,<ContentEncoding, varchar(255),>
--		   ,@Type--,<Type, varchar(255),>
--		   ,null--,<UserID, varchar(255),>
--		   ,0--,<State, int,>)
--		   ,GETDATE()
--		   ,@OwnerID
--		END
--END