--do $$
--begin
--	if exists 
--	(
--		select * from pg_proc p
--		where p.proname = 'sp_rds_cancelhospitalisationxml'
--	)
--	then
--		drop function public.sp_rds_cancelhospitalisationxml(text, xml, bit, uuid, varchar(255));
--	end if;
--end;
--$$ language plpgsql;
---- =============================================
---- Author:		Vitacore (с)
---- Create date: 2014-03-01
---- Description:	Выгрузка информации об анулировании госпитализации в RDS 
---- =============================================
--CREATE or Replace Function public.sp_RDS_CancelHospitalisationXml
--(
--	"LpuCode" text = null,
--	"Xml" xml = null,
--	"IsNew" bit = '1',
--	"ID" UUID = null,
--	"QueueName" varchar(255) = null
--)
--RETURNS Void As 
--$$
--Declare	Result text;
--			ContentEncodingVar varchar(255);
--			TypeVar varchar(255);
--			OperationTypeVar varchar(255);
--			OwnerIDVar UUID;
--Begin
--	IF "Xml" is null
--	then
--		--выясняем тип операции
--		IF "IsNew" = '1'
--		then
--			OperationTypeVar := 'Insert';
--		ELSIF "IsNew" = '0' and exists (select * from akuz.T_TAKE_OVER_HOSPITALISATION toh  where toh.MedicalRecordID = "ID")
--		then
--			OperationTypeVar = 'Update';
--		ELSIF "IsNew" = '0' and not exists (select * from akuz.T_TAKE_OVER_HOSPITALISATION toh where toh.MedicalRecordID = "ID")
--		then
--			OperationTypeVar := 'Delete';
--			Result := XMLELEMENT
--				  (
--					name "CANCEL_HOSPITALISATION"
--				  ,	XMLELEMENT
--					(
--						name header
--					,	XMLFOREST
--						(
--							"LpuCode" as "LPU"
--						,	OperationTypeVar as "TYPE"
--						,	"ID" as "ID"
--						)
--					)
--				  );
--		END if;

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

--		IF OperationTypeVar <> 'Delete'
--		then
--			select
--			XMLELEMENT
--			(
--				name "CANCEL_HOSPITALISATION"
--			,	XMLELEMENT
--				(
--					name header
--				,	XMLFOREST
--					(
--						"LpuCode" as "LPU"
--					,	OperationTypeVar as "TYPE"
--					,	"ID" as "ID"
--					)
--				)
--			,	XMLELEMENT
--				(
--					name body
--				,	XMLFOREST
--					(
--						mcr.MovementID as "HOSPITALISATION_ID"
--					,	CASE toh.DenialHospitalisation 
--							WHEN 22901 THEN 2
--							WHEN 22902 THEN 5
--							WHEN 22903 THEN 1
--							ELSE 4 
--						END as "REASON"
--					,	1 as "CANCEL_SOURSE"
--					,	br.BranchCode as "BRANCH"
--					,	case when toh.DateOver is null then toh.DateOver::varchar(10) else to_char(toh.DateOver::TimeStamp, 'YYYY-MM-DD') end as "DATE_CANCEL"
--					,	1 as "CANCEL_TYPE"
--					,	d.Code as "HOSPITALISATION_DIVISION"						
--					,	case when m.MovementDate is null then m.MovementDate::varchar(10) else to_char(m.MovementDate::TimeStamp, 'YYYY-MM-DD') end as "DATE"
--					,	m.MedCardNumber as "MED_CARD_NUMBER"
--					,	p.PatientID as "PATIENT"
--					)
--				)
--			)
--			from akuz.T_TAKE_OVER_HOSPITALISATION toh
--			inner join 
--				akuz.T_AMBULANCE_RECORD ar 
--				on ar.MedicalRecordID = toh.MedicalRecordID
--			inner join 
--				akuz.T_EVENT_BASE eb 
--				on eb.EventID = ar.AmbulanceEvent
--			inner join 
--				akuz.T_AMBULANCE_CARD ac 
--				on ac.AmbulanceCardID = eb.AmbulanceCard
--			inner join 
--				akuz.T_PATIENT p 
--				on p.PatientID = ac.Patient
--			inner join 
--				akuz.T_MOVEMENT m 
--				on m.EmergencyEvent = eb.EventID
--			inner join 
--				akuz.T_MEDICALCARD_REGISTRATION mcr 
--				on mcr.MovementID = m.MovementID
--			inner join 
--				akuz.T_DIVISION d 
--				on d.DivisionID = m.Division
--			inner join 
--				akuz.T_BRANCH br 
--				on br.LpuID = d.Branch
--			into Result
--			where toh.MedicalRecordID = "ID";
--		END if;

--		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 if;
--End;
--$$ Language plpgsql;