--do $$
--begin
--	if exists 
--	(
--		select * from pg_proc p
--		where p.proname = 'sp_rds_perenatalcardxml'
--	)
--	then
--		drop function public.sp_rds_perenatalcardxml(text, uuid, bytea, varchar(255), varchar(255));
--	end if;
--end;
--$$ language plpgsql;
---- =============================================
---- Author:		Vitacore (с)
---- Create date: 2014-08-11
---- Description:	Выгрузка информации об обменных картах
---- =============================================
--CREATE or Replace Function public.sp_RDS_PerenatalCardXml
--(
--	"LpuCode" text = null
--,	"ID" UUID = null
--,	"Content" bytea = null
--,	"OperationType" varchar(255) = 'Insert'
--,	"QueueName" varchar(255) = 'RDSMotherhood'	
--)
--RETURNS Void As 
--$$
--Declare	Result text;
--			ContentEncodingVar varchar(255);
--			TypeVar varchar(255);
--			OwnerIDVar UUID;
--Begin
--	if not exists(select * from akuz.T_PERINATAL_CARD pc where pc.FileID = "ID")
--	then
--		"OperationType" := 'Delete';
--		Result := xmlelement
--					(
--						name "PERINATAL_CARD"
--					,	xmlforest
--						(
--							xmlforest
--							(
--								"LpuCode" as "LPU"
--							,	"ID" as "ID"			
--							,	"OperationType" as "TYPE"
--							) as header
--						)
--					);
--	end if;

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

--	if "OperationType" <> 'Delete'
--	then
--		Result := xmlelement
--					(
--						name "PERINATAL_CARD"
--					,	(
--							select
--							xmlagg
--							(
--								xmlforest
--								(
--									xmlforest
--									(
--										"LpuCode" as "LPU"
--									,	"ID" as "ID"
--									,	pc.MotherhoodEvent as "ID_PASSPORT"
--									,	"OperationType" as "TYPE"
--									) as header
--								,	xmlforest
--									(
--										to_char(mrp.IssueDate, 'YYYY-MM-DD') as "ISSUE_DATE"
--									,	to_char(mrp.CreateDate, 'YYYY-MM-DD') as "CREATION_DATE"
--									,	p.Code || ' ' || w.LastName || ' ' || w.FirstName || ' ' || w.FatherName as "POST"
--									,	lp.LpuCode as "CONSULTATION"
--									,	f.Name as "FILE_NAME"
--									,	"Content" as "CONTENT"
--									) as body
--								)
--							)
--							from akuz.T_PERINATAL_CARD pc 
--							inner join 
--								akuz.T_MEDICAL_RECORDS_PRINT mrp 
--								on mrp.FileID = pc.FileID 
--							inner join 
--								akuz.T_POST p 
--								on p.ResourceID = mrp.Post
--							inner join 
--								akuz.T_WORKER w 
--								on w.WorkerID = p.Worker
--							left join 
--								akuz.T_LPU_MAIN lp 
--								on lp.LpuID = pc.LpuMain
--							inner join 
--								vclib.T_FILE f 
--								on f.FileID = pc.FileID
--							where pc.FileID = "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;
--$$ Language plpgsql;