---- =============================================
---- Author:		Vitacore (с)
---- Create date: 2014-03-01
---- Description:	Выгрузка/загрузка информации об анулировании направлении на госпитализацию из RDS 
---- =============================================
---- DROP FUNCTION sp_SetCancelReferralXml(text, xml, boolean, uuid, text);
--CREATE OR REPLACE FUNCTION sp_SetCancelReferralXml
--(
--	"LpuCode"	text DEFAULT NULL::text
--,	"Xml"		xml DEFAULT NULL::xml
--,	"IsNew"		boolean DEFAULT '1'::boolean
--,	"ID"		uuid DEFAULT NULL::uuid
--,	"LpuTo"		text DEFAULT NULL::text
--)
--RETURNS void AS
--$BODY$
--Declare	_Result text;
--	_ContentEncoding text;
--	_Type text;
--	_OperationType text;
--	_OwnerID uuid;
--begin
--	_OperationType := 'Insert';
	
--	if "Xml" is null
--	then
--		--выясняем тип операции
--		if "IsNew" = '1'
--		then
--			_OperationType := 'Insert';
--		elsif "IsNew" = '0' and exists(select * from RDS.T_CANCEL_HOSPITALIZATION_OR_REFERRAL chr where chr.CancelHospitalizationOrReferralID = "ID")
--		then
--			_OperationType := 'Update';
--		elsif "IsNew" = '0' and not exists(select * from RDS.T_CANCEL_HOSPITALIZATION_OR_REFERRAL chr where hr.CancelHospitalizationOrReferralID = "ID")
--		then
--			_OperationType := 'Delete';
--			_Result := XMLELEMENT
--				  (
--					name "CANCEL_HOSPITALISATION_REFERRAL"
--				  ,	XMLELEMENT
--					(
--						name header
--					,	XMLFOREST
--						(
--							"LpuCode" 	as "LPU"
--						,	_OperationType	as "TYPE"
--						,	"ID"		as "ID"
--						,	"LpuTo"		as "LPU_TO"
--						)
--					)
--				  ); 
--		END if;

--		_ContentEncoding := 'utf-8';
--		_Type := 'CANCEL_HOSPITALISATION_REFERRAL';
--		_OwnerID := "ID";
	
--		if _OperationType <> 'Delete'
--		then
--			select 	
--			XMLELEMENT
--			(
--				name "CANCEL_HOSPITALISATION_REFERRAL"
--			,	XMLELEMENT
--				(
--					name header
--				,	XMLFOREST
--					(
--						"LpuCode"	as "LPU"
--					,	_OperationType	as "TYPE"
--					,	"ID"		as "ID"
--					,	"LpuTo"		as "LPU_TO"
--					)
--				)				
--			,	XMLELEMENT
--				(
--					name body
--				,	XMLFOREST
--					(
--						chr.ReasonCancel						as "REASON"
--					,	chr.CancellationSource						as "CANCEL_SOURSE"
--					,	chr.PatientID							as "PATIENT"
--					,	coalesce(hr.ReferralNumber, chr.ReferralNumber)			as "REFERRAL_NUMBER"
--					,	to_char(coalesce(hr.ReferralDate, chr.Date), 'YYYY-MM-DD')	as "DATE"
--					,	branch.BranchCode						as "BRANCH"
--					,	coalesce(br.LpuMain, chr.ReferralLpu)				as "REFERRAL_LPU"
--					,	to_char(chr.DateCancel, 'YYYY-MM-DD')				as "DATE_CANCEL"
--					,	0								as "CANCEL_TYPE"
--					,	chr.Descr							as "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
--			inner join 
--				RDS.T_BRANCH branch 
--				on branch.BranchID = chr.Branch
--			into _Result
--			where chr.CancelHospitalizationOrReferralID = "ID";
--		end if;

--		insert into RDS.T_RABBIT_SEND_OBJECT(RabbitSendObjectID, QueueName, Content, MessageID, Headers, ContentEncoding, Type, UserID, State, CreateDateTime, OwnerID)
--		select uuid_generate_v4(), s.ServerName, _Result, null, null, _ContentEncoding, _Type, null, 0, current_timestamp, _OwnerID
--		from RDS.T_SYNC_SERVERS s
--		where exists 
--		(
--			select * 
--			from RDS.T_SYNC_SERVERS s1
--			inner join 
--				RDS.T_SERVERS_LINK sl 
--				on sl.SyncServers = s1.SyncServersID
--			inner join 
--				RDS.T_SERVERS_LINK_LPU ll 
--				on ll.ServersLinkID = sl.ServersLinkID
--			inner join
--				RDS.T_LPU_MAIN lp 
--				on lp.MCOD = ll.LpuMain
--			where lp.MCOD = "LpuTo" 
--			and s1.SyncServersID = s.SyncServersID
--		);
--	end if;
--end;
--$BODY$
--  LANGUAGE plpgsql VOLATILE
--  COST 100;
--ALTER FUNCTION sp_SetCancelReferralXml(text, xml, boolean, uuid, text)
--  OWNER TO postgres;