IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_SetCancelReferralXml]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sp_SetCancelReferralXml]
--GO
--SET ANSI_NULLS ON
--GO
--SET QUOTED_IDENTIFIER ON
--GO
---- =============================================
---- Author:		Vitacore (с)
---- Create date: 2014-03-01
---- Description:	Выгрузка/загрузка информации об анулировании направлении на госпитализацию из RDS 
---- =============================================
--CREATE PROCEDURE [dbo].[sp_SetCancelReferralXml]
--	@LpuCode varchar(max) = null,
--	@Xml xml = null,
--	@IsNew bit = 1,
--	@ID uniqueidentifier = null,
--	@LpuTo varchar(max) = null
--AS
--BEGIN

--	declare @Result varchar(max)
--			,@ContentEncoding varchar(255)
--			,@Type varchar(255)
--			,@OperationType varchar(255) = 'Insert'
--			,@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 [RDS].T_CANCEL_HOSPITALIZATION_OR_REFERRAL chr  where chr.CancelHospitalizationOrReferralID = @ID))
--		BEGIN
--			select @OperationType = 'Update'
--		END
--		ELSE IF (@IsNew = 0 and not exists (select * from [RDS].T_CANCEL_HOSPITALIZATION_OR_REFERRAL chr where chr.CancelHospitalizationOrReferralID = @ID))
--		BEGIN
--			select @OperationType = 'Delete'
--				select @Result = (select 
--				@LpuCode				as 'header/LPU'
--				,@OperationType			as 'header/TYPE'
--				,@ID					as 'header/ID'
--				,@LpuTo					as 'header/LPU_TO'
--				FOR XML PATH(''),
--				ROOT('CANCEL_HOSPITALISATION_REFERRAL')
--			) 
--		END

--select @ContentEncoding = 'utf-8'
--   ,@Type = 'CANCEL_HOSPITALISATION_REFERRAL'
--   ,@OwnerID = @ID
--		IF(@OperationType <> 'Delete')
--		BEGIN

--	SELECT @Result = ( 
--		select 
--			@LpuCode										as 'header/LPU'
--			,@OperationType									as 'header/TYPE'
--			,@ID											as 'header/ID'
--			,@LpuTo											as 'header/LPU_TO'

--           ,chr.[ReasonCancel]								as 'body/REASON'
--           ,chr.[CancellationSource]						as 'body/CANCEL_SOURSE'
--           ,chr.PatientID									as 'body/PATIENT'
--		   ,ISNULL(hr.ReferralNumber, chr.ReferralNumber)	as 'body/REFERRAL_NUMBER'
--		   ,ISNULL(convert(varchar(10), hr.ReferralDate, 20), convert(varchar(10), chr.[Date], 20)) as 'body/DATE'
--           ,branch.BranchCode								as 'body/BRANCH'
--		   ,ISNULL(br.LpuMain, chr.ReferralLpu)				as 'body/REFERRAL_LPU'
--		   ,convert(varchar(10), chr.DateCancel, 20)		as 'body/DATE_CANCEL'
--		   ,0												as 'body/CANCEL_TYPE'
--		   ,chr.Descr										as 'body/CANCEL_DESCRIPTION'
--		from [RDS].T_CANCEL_HOSPITALIZATION_OR_REFERRAL chr
--		left join [RDS].T_HOSPITALIZATION_REFERRAL hr on hr.HospitalizationReferralID = chr.HospitalizationReferral
--		left join [RDS].T_BRANCH br on br.BranchID = hr.BranchFrom
--		join [RDS].T_BRANCH branch on branch.BranchID = chr.Branch
--		where chr.CancelHospitalizationOrReferralID = @ID
--		FOR XML PATH(''),
--		ROOT('CANCEL_HOSPITALISATION_REFERRAL')
--	) 
--	END

--		INSERT INTO [RDS].[T_RABBIT_SEND_OBJECT]
--		   ([RabbitSendObjectID]
--		   ,[QueueName]
--		   ,[Content]
--		   ,[MessageID]
--		   ,[Headers]
--		   ,[ContentEncoding]
--		   ,[Type]
--		   ,[UserID]
--		   ,[State]
--		   ,[CreateDateTime]
--		   ,[OwnerID])
--		select
--		   NewID()--(<RabbitSendObjectID, uniqueidentifier,>
--		   ,s.ServerName--,<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
--		   from [RDS].T_SYNC_SERVERS s
--			where 
--			exists (select * from [RDS].T_SYNC_SERVERS s1
--			join [RDS].T_SERVERS_LINK sl on sl.SyncServers = s1.SyncServersID
--			join [RDS].T_SERVERS_LINK_LPU ll on ll.ServersLinkID = sl.ServersLinkID
--			join [RDS].T_LPU_MAIN lp on lp.MCOD = ll.LpuMain
--			where lp.MCOD = @LpuTo and s1.SyncServersID = s.SyncServersID)
--		END
--	END
--GO