--do $$
--begin
--	if exists 
--	(
--		select * from pg_proc p
--		where p.proname = 'sp_rds_motherhoodeventxml'
--	)
--	then
--		drop function public.sp_rds_motherhoodeventxml(text, uuid, bit);
--	end if;
--end;
--$$ language plpgsql;
---- =============================================
---- Author:		Vitacore (с)
---- Create date: 2014-08-11
---- Description:	Выгрузка информации о индивидуальной карте беременной
---- =============================================
--CREATE or Replace Function public.sp_RDS_MotherhoodEventXml
--(
--	"LpuCode" text = null
--,	"ID" UUID = null
--,	"IsNew" bit = '0'
--)
--RETURNS Void As 
--$$
--Declare	Result text;
--			QueueNameVar varchar(255);
--			ContentEncodingVar varchar(255);
--			TypeVar varchar(255);
--			OperationTypeVar text;
--			OwnerIDVar UUID;
--Begin
--	if "IsNew" = '1'
--	then
--		OperationTypeVar := 'Insert';
--	elsif "IsNew" = '0' and exists (select * from akuz.T_MOTHERHOOD_EVENT me where me.EventId = "ID")
--	then
--		OperationTypeVar := 'Update';
--	elsif "IsNew" = '0' and not exists (select * from akuz.T_MOTHERHOOD_EVENT me where me.EventId = "ID")
--	then
--		OperationTypeVar := 'Delete';
--		Result := XMLELEMENT
--			  (
--				name "MOTHERHOOD_EVENT"
--			  ,	XMLFOREST
--				(
--					XMLFOREST
--					(
--						"LpuCode" as "LPU"
--					,	OperationTypeVar as "TYPE"
--					,	"ID" as "ID"
--					) as header
--				)
--			  );		
--	end if;

--	QueueNameVar := 'RDSMotherhood';
--	ContentEncodingVar := 'utf-8';
--	TypeVar := 'MOTHERHOOD_EVENT';
--	OwnerIDVar := "ID";

--	if OperationTypeVar <> 'Delete'
--	then
--		select
--		XMLELEMENT
--		(
--			name "MOTHERHOOD_EVENT"
--		,	XMLFOREST
--			(
--				XMLFOREST
--				(
--					"LpuCode" as "LPU"
--				,	OperationTypeVar as "TYPE"
--				,	"ID" as "ID"
--				) as header
--			)
--		,	XMLFOREST
--			(
--				XMLFOREST
--				(
--					Cast(DATE_PART('YEAR', eb.EventDate) as varchar(4)) || me.PassportNumber as "PASSPORT"
--				,	p.LastName as "LAST_NAME"
--				,	p.FirstName as "FIRST_NAME"
--				,	p.FatherName as "FATHER_NAME"
--				,	to_char(p.Birthday, 'YYYY-MM-DD') as "BIRTHDAY"
--				,	p.SNILS as "SNILS"
--				,	p.PatientID as "PATIENT_ID"
--				,	to_char(eb.EventDate, 'YYYY-MM-DD') as "EVENT_DATE"
--				,	to_char(eb.EventDate, 'YYYY-MM-DD') as "PREGNANCY_DATE"
--				,	to_char(me.LastDateMenstruationBegin, 'YYYY-MM-DD') as "LAST_MENSTRUATION"
--				,	to_char(me.FetalMovementsDate, 'YYYY-MM-DD') as "FETAL_MOVEMENTS_DATE"
--				) as body
--			)
--		)
--		from akuz.T_MOTHERHOOD_EVENT me
--		join akuz.T_EVENT_BASE eb on eb.EventID = me.EventID
--		join akuz.T_AMBULANCE_CARD ac on ac.AmbulanceCardID = eb.AmbulanceCard
--		join akuz.T_PATIENT p on p.PatientId = ac.Patient 
--		into Result
--		where me.EventId = "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(), QueueNameVar, Result, null, null, ContentEncodingVar, TypeVar, null, 0, Current_TimeStamp::TIMESTAMP, OwnerIDVar;
--End;
--$$ Language plpgsql;