---- =============================================
---- Author:		Vitacore (с)
---- Create date: 2014-03-11
---- Description:	Выгрузка информации о госпитализации в RDS 
---- =============================================
--CREATE OR REPLACE FUNCTION sp_rds_motioninhospitalxml
--(
--	"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)
--,	"ObjectType" boolean DEFAULT '0'::boolean	-- 0 - Движение 1- выписка
--)
--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 "ObjectType" = '0'
--		then
--			--выясняем тип операции
--			IF "IsNew" = '1'
--			then
--				_OperationType := 'Insert';
--			ELSIF "IsNew" = '0' and exists(select* from akuz.T_MOVEMENTS_DIVISION_CARD mdc where mdc.MovementsDivisionCardID = "ID")
--			then
--				_OperationType := 'Update';
--			ELSIF "IsNew" = '0' and not exists(select* from akuz.T_MOVEMENTS_DIVISION_CARD mdc where mdc.MovementsDivisionCardID = "ID")
--			then
--				_OperationType := 'Delete';
--				_Result := XMLELEMENT(name "MOTION_IN_HOSPITAL", XMLFOREST(XMLFOREST("LpuCode" as "LPU", OperationTypeVar as "TYPE", "ID" as "ID") as header));			
--			END if;

--			_ContentEncoding := 'utf-8';
--			TypeVar := 'MOTION_IN_HOSPITAL';
--			_OwnerID := "ID";

--			IF _OperationType <> 'Delete'
--			then
--				select
--				XMLELEMENT
--				(
--					name "MOTION_IN_HOSPITAL"
--				,	XMLFOREST
--					(
--						XMLFOREST
--						(
--							"LpuCode" as "LPU"
--						,	_OperationType as "TYPE"
--						,	"ID" as "ID"
--						) as header
--					)
--				,	XMLFOREST
--					(
--						XMLFOREST
--						(
--							mdc.Movement as "HOSPITALISATION_ID"
--						,	br.BranchCode as "BRANCH"
--						,	to_char(mov.MovementDate, 'YYYY-MM-DD') as "HOSPITALISATION_DATE"
--						,	to_char(mdc.DateIn, 'YYYY-MM-DD') as "DATE_IN"
--						,	to_char(mdc.DateOut, 'YYYY-MM-DD') as "DATE_OUT"
--						,	v0.Id as "STRUCTURE_BED"
--						,	sb.Code as "V020_STRUCTURE_BED"
--						,	d.Code as "DIVISION"
--						,	mov.MedCardNumber as "MED_CARD_NUMBER"
--						) as body
--					)
--				)
--				from akuz.T_MOVEMENTS_DIVISION_CARD mdc
--				inner join 
--					akuz.T_STRUCTURE_BED sb 
--					on sb.StructureBedID = mdc.StructureBed
--				inner join 
--					akuz.T_V002_PROFILE v0 
--					on v0.Id = sb.V002Profile
--				--LPU
--				inner join 
--					akuz.T_DIVISION d 
--					on d.DivisionID = mdc.Division
--				inner join 
--					akuz.T_BRANCH br 
--					on br.LpuID = d.Branch
--				inner join 
--					akuz.T_LPU_MAIN lp 
--					on lp.LpuID = br.LpuMain
--				--patient
--				inner join 
--					akuz.T_MOVEMENT mov 
--					on mov.MovementID = mdc.Movement
--				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
--				into _Result
--				where mdc.MovementsDivisionCardID = "ID";
--			ELSE
--				--выясняем тип операции
--				IF "IsNew" = '1'
--				then
--					_OperationType := 'Insert';
--				ELSIF "IsNew" = '0' and exists(select* from akuz.T_MOVEMENT_RESULT mr where mr.MedicalRecordId = "ID")
--				then
--					_OperationType := 'Update';
--				ELSIF "IsNew" = '0' and not exists(select* from akuz.T_MOVEMENT_RESULT mr where mr.MedicalRecordId = "ID")
--				then
--					_OperationType := 'Delete';
--					_Result := XMLELEMENT(name "MOTION_IN_HOSPITAL", XMLFOREST(XMLFOREST("LpuCode" as "LPU", _OperationType as "TYPE", "ID" as "ID") as header));			
--				END if;

--				_ContentEncoding := 'utf-8';
--				TypeVar := 'MOTION_IN_HOSPITAL';
--				_OwnerID := "ID";

--				IF OperationTypeVar <> 'Delete'
--				then
--					select
--					XMLELEMENT
--					(
--						name "MOTION_IN_HOSPITAL"
--					,	XMLFOREST
--						(
--							XMLFOREST
--							(
--								"LpuCode" as "LPU"
--							,	_OperationType as "TYPE"
--							,	"ID" as "ID"
--										) as header
--						)
--					,	XMLFOREST
--						(
--							XMLFOREST
--							(
--								mr.MedicalcardRegistration as "HOSPITALISATION_ID"
--							,	br.BranchCode as "BRANCH"
--							,	to_char(mov.MovementDate, 'YYYY-MM-DD') as "HOSPITALISATION_DATE"
--							,	to_char(mr.DatIn, 'YYYY-MM-DD') as "DATE_IN"
--							,	to_char(mr.DatOut, 'YYYY-MM-DD') as "DATE_OUT"
--							,	v0.Id as "STRUCTURE_BED"
--							,	d.Code as "DIVISION"
--							,	mov.MedCardNumber as "MED_CARD_NUMBER"
--							,	case when mr.TreatmentOutcome = 102 then 1
--									when mr.TreatmentOutcome = 103 then 2
--									when mr.TreatmentOutcome = 104 then 3 
--								end as "OUTCOME"
--							) as body
--						)
--					)
--					from akuz.T_MOVEMENT_RESULT mr
--					inner join 
--						akuz.T_STRUCTURE_BED sb 
--						on sb.StructureBedID = mr.StructureBed
--					inner join 
--						akuz.T_V002_PROFILE v0 
--						on v0.Id = sb.V002Profile
--					--LPU
--					inner join 
--						akuz.T_DIVISION d 
--						on d.DivisionID = mr.Division
--					inner join 
--						akuz.T_BRANCH br 
--						on br.LpuID = d.Branch
--					inner join 
--						akuz.T_LPU_MAIN lp 
--						on lp.LpuID = br.LpuMain
--					--patient
--					inner join 
--						akuz.T_MOVEMENT mov 
--						on mov.MovementID = mr.MedicalcardRegistration
--					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
--					into _Result
--					where mr.MedicalRecordId = "ID"; --and d.IsRequestonDivision = 0 and d.IsRequestInHospital = 1;
--				END if;	
--			END if;
--		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_motioninhospitalxml(text, xml, boolean, uuid, varchar(255), boolean)
--  OWNER TO postgres;
