--CREATE OR REPLACE FUNCTION sp_rds_hospitalisationxml
--(
--	"LpuCode" text DEFAULT NULL::text
--,	"Xml" xml DEFAULT NULL::xml
--,	"IsNew" boolean DEFAULT '1'::boolean
--,	"ID" uuid DEFAULT NULL::uuid
--,	"QueueName" varchar(255) DEFAULT NULL::varchar(255)
--)
--RETURNS void AS
--$BODY$
--Declare	_Result text;
--	_ContentEncoding varchar(255);
--	TypeVar varchar(255);
--	_OperationType varchar(255);
--	_OwnerID UUID;
--Begin
--	IF "Xml" is null
--	then
--		--выясняем тип операции
--		if "IsNew" = '1'
--		then
--			_OperationType := 'Insert';
--		elsif "IsNew" = '0' and exists(select* from akuz.T_MEDICALCARD_REGISTRATION mcr where mcr.MovementID = "ID")
--		then
--			_OperationType := 'Update';
--		elsif "IsNew" = '0' and not exists(select* from akuz.T_MEDICALCARD_REGISTRATION mcr where mcr.MovementID = "ID")
--		then
--			_OperationType := 'Delete';
--			_Result := XMLELEMENT
--				  (
--					name "HOSPITALISATION"
--				  , 	xmlforest
--					(
--						xmlforest
--						(
--							"LpuCode" as "LPU"
--						,	_OperationType as "TYPE"
--						, 	"ID" as "ID"
--						) as header
--					)
--				  );
--		end if;

--		_ContentEncoding := 'utf-8';
--		TypeVar := 'HOSPITALISATION';
--		_OwnerID := "ID";
		
--		IF	_OperationType <> 'Delete'
--		then
--			select
--			XMLELEMENT
--			(
--				name "HOSPITALISATION"
--			,	XMLFOREST
--				(
--					XMLFOREST
--					(
--						"LpuCode" as "LPU"
--					,	_OperationType as "TYPE"
--					,	"ID" as "ID"
--					) as header
--				)
--			,	XMLFOREST
--				(
--					XMLFOREST
--					(
--						db.DirectionNumber as "REFERRAL_NUMBER"
--					,	to_char(db.DirectionDate, 'YYYY-MM-DD') as "REFERRAL_DATE"
--					,	lpDir.LpuCode as "REFERRAL_MO"
--					,	br.BranchCode as "BRANCH"
--					,	to_char(mov.MovementDate, 'YYYY-MM-DD') as "HOSPITALISATION_DATE"
--					,	to_char(mov.MovementTime, 'YYYY-MM-DD HH:MI:SS') as "HOSPITALISATION_TIME"
--					,	pol.PolicyForm as "POLICY_TYPE"
--					,	pol.Serial as "POLIS_SERIAL"
--					,	pol.Number as "POLIS_NUMBER"
--					,	Coalesce(i1.SmoCod, i2.SmoCod) as "SMO"
--					,	pat.FirstName as "FIRST_NAME"
--					,	pat.LastName as "LAST_NAME"
--					,	pat.FatherName as "FATHER_NAME"
--					,	pat.Sex as "SEX"
--					,	to_char(pat.Birthday, 'YYYY-MM-DD') as "BIRTHDAY"
--					,	v0.Id as "STRUCTURE_BED"
--					,	sb.code	 as "V020_STRUCTURE_BED"
--					,	d.Code as "DIVISION"
--					,	mov.MedCardNumber as "MED_CARD_NUMBER"
--					,	mov.Mkb as "MKB"
--					,	mov.Diagnosis as "DIAGNOSIS"
--					,	pat.PatientID as "PATIENT"
--					,	mov.MovementID as "ID"
--					,	CASE mdc.HospitalizationType
--							WHEN 13001 THEN 0
--							WHEN 13002 THEN 1
--							WHEN 13003 THEN 2
--							ELSE 0 
--						END as "FORM_MEDICAL_CARE"
--					) as body
--				)
--			)
--			from akuz.T_MOVEMENT mov 
--			inner join 
--				akuz.T_STRUCTURE_BED sb 
--				on sb.StructureBedID = mov.StructureBed
--			inner join 
--				akuz.T_V002_PROFILE v0 
--				on v0.Id = sb.V002Profile
--			--LPU
--			inner join 
--				akuz.T_DIVISION d 
--				on d.DivisionID = mov.Division
--			inner join 
--				akuz.T_BRANCH br 
--				on br.LpuID = d.Branch
--			--patient
--			inner join 
--				akuz.T_EVENT_BASE eb 
--				on eb.EventID = mov.EmergencyEvent
--			inner join 
--				akuz.T_AMBULANCE_CARD ac 
--				on ac.AmbulanceCardID = eb.AmbulanceCard
--			inner join 
--				akuz.T_PATIENT pat 
--				on pat.PatientID = ac.Patient
--			--polis
--			left join 
--				akuz.T_POLICY pol 
--				on pol.PolicyID = dbo.fn_GetOmsPolicy(pat.PatientID,mov.MovementDate)
--			left join 
--				akuz.T_INSURANCE_COMPANY ic 
--				on ic.OrganizationID = pol.InsuranceCompany
--			left join 
--				akuz.T_INSURER i1 
--				on i1.InsurerID = ic.Insurer
--			left join 
--				akuz.T_INSURER i2 
--				on i2.InsurerID = pol.Insurer
--			--referral
--			left join 
--				akuz.T_DIRECTION dir 
--				on dir.DirectionID = eb.Direction
--			left join 
--				akuz.T_DIRECTION_BASE db 
--				on db.DirectionID = dir.DirectionID
--			left join 
--				akuz.T_LPU_MAIN lpDir 
--				on lpDir.LpuID = db.DirectorLpu
--			inner join 
--				akuz.T_MEDICALCARD_REGISTRATION mdc 
--				on mdc.MovementID = mov.MovementID
--			into _Result
--			where mov.MovementID = "ID";			
--		END if;

--		INSERT INTO akuz.T_RABBIT_SEND_OBJECT(RabbitSendObjectID, QueueName, Content, MessageID, Headers, ContentEncoding, Type, UserID, State, CreateDateTime, OwnerID)
--		select uuid_generate_v4(), "QueueName", _Result, null, null, _ContentEncoding, TypeVar, null, 0, Current_TimeStamp::TIMESTAMP, _OwnerID;
--	END if;
--End;
--$BODY$
--  LANGUAGE plpgsql VOLATILE
--  COST 100;
--ALTER FUNCTION sp_rds_hospitalisationxml(text, xml, boolean, uuid, varchar(255))
--  OWNER TO postgres;
