--do $$
--begin
--	if exists 
--	(
--		select * from pg_proc p
--		where p.proname = 'sp_rds_updatehospreferralxml'
--	)
--	then
--		drop function public.sp_rds_updatehospreferralxml(text, xml, bit, uuid, text, varchar(255));
--	end if;
--end;
--$$ language plpgsql;
---- =============================================
---- Author:		Vitacore (с)
---- Create date: 2014-03-01
---- Description:	Обновление даты госпитализации по направлению 
---- =============================================
--CREATE or Replace Function public.sp_RDS_UpdateHospReferralXml
--(
--	"LpuCode" text = null,
--	"Xml" xml = null,
--	"IsNew" bit = '1',
--	"ID" UUID = null,
--	"LpuTo" text = null,
--	"QueueName" varchar(255) = null
--)
--RETURNS Void As 
--$$
--Declare	Result text;
--			ContentEncodingVar varchar(255);
--			TypeVar varchar(255);
--			OperationTypeVar varchar(255);
--			OwnerIDVar UUID;
--Begin
--	OperationTypeVar := 'Update';

--	ContentEncodingVar := 'utf-8';
--	TypeVar := 'HOSPITALISATION_REFERRAL';
--	OwnerIDVar := "ID";

--	select
--	XMLELEMENT
--	(
--		name "HOSPITALISATION_REFERRAL"
--	,	XMLFOREST
--		(
--			XMLFOREST
--			(
--				"LpuCode" as "LPU"
--			,	OperationTypeVar as "TYPE"
--			,	"ID" as "ID"
--			,	"LpuTo" as "LPU_TO"
--			) as header
--		)
--	,	XMLFOREST
--		(
--			XMLFOREST
--			(
--				lp.LpuCode as "REFERRAL_LPU"
--			,	rhr.ReferralNumber as "REFERRAL_NUMBER"
--			,	to_char(rhr.ReferralDate, 'YYYY-MM-DD') as "REFERRAL_DATE"
--			,	to_char(rhr.HospitalisationDate, 'YYYY-MM-DD') as "HOSPITALISATION_DATE"
--			) as body
--		)
--	) 
--	from akuz.T_RDS_HOSPITALIZATION_REFERRAL rhr 
--	inner join 
--		akuz.T_BRANCH br 
--		on br.LpuID = rhr.BranchFrom		
--	inner join 
--		akuz.T_LPU_MAIN lp 
--		on lp.LpuID = br.LpuMain
--	into Result
--	where rhr.ID = "ID";
	
--	INSERT INTO akuz.T_RABBIT_SEND_OBJECT(RabbitSendObjectID, QueueName,Content,MessageID,Headers,ContentEncoding,Type,UserID,State,CreateDateTime,OwnerID)
--	select uuid_generate_v1(), "QueueName", Result, null, null, ContentEncodingVar, TypeVar, null, 0, Current_TimeStamp::TIMESTAMP, OwnerIDVar;
--End;
--$$ Language plpgsql;