---- =============================================
---- Author:		Dmitry
---- Create date: 05.07.2010
---- Description:	Возвращает DrugRequestList
---- =============================================
--CREATE OR REPLACE FUNCTION fn_GetRecipeDrugRequestList("RecipeID" UUID, "OnDate" TimeStamp = NULL)
--RETURNS UUID AS
-- $$
-- declare	_Mnn int;
--		_Trn int;
--		_MedicationForms int;
--		_DoseView int;
--		_Dose varchar(255);
--		_DoseCount int;
--		_Doses decimal(31,10);
--		_VolumeView int;
--		_Weigth decimal(31,10);
--		_WeightView int;
--		_Volume decimal(31,10);
-- Begin
--	select into
--		_Mnn, _Trn, _MedicationForms, _DoseView, _Dose, _DoseCount, _Doses, _VolumeView, _Weigth, _WeightView, _Volume
--		rf.Mnn, rf.Trn, r.MedicationForms, rf.RFDoseView, r.Dose, r.DoseCount, r.Doses, r.VolumeView, r.Weigth, r.WeightView, r.Volume
--	from akuz.T_RECIPE_FEDERAL rf
--	inner join 
--		akuz.T_RECIPE r 
--		On rf.MedicalRecordID = r.MedicalRecordID
--	where rf.MedicalRecordID = "RecipeID"
--	Limit 1;

--	return
--	(
--		select dr.BaseDrugRequestItemID
--		from akuz.T_BASE_DRUG_REQUEST_ITEM dr
--		inner join 
--			akuz.T_DRUG_REQUEST_LIST drl 
--			on drl.BaseDrugRequestItemID = dr.BaseDrugRequestItemID
--		where ("OnDate" IS NULL OR dr.BeginDate = "OnDate") 
--		and drl.Mnn = _Mnn
--		And (drl.Trn Is Null OR drl.Trn = _Trn)
--		and dr.MedicationForms = _MedicationForms
--		and drl.DoseView = _DoseView
--		and drl.Dose = _Dose
--		and drl.DoseCount = _DoseCount
--		and drl.Doses = _Doses
--		and 
--		(
--			(drl.Volume IS NULL AND _Volume IS NULL) OR drl.Volume = _Volume
--		)
--		and 
--		(
--			(drl.VolumeView IS NULL AND _VolumeView IS NULL) OR drl.VolumeView = _VolumeView
--		)
--		and 
--		(
--			(drl.Weigth IS NULL AND _Weigth IS NULL) OR drl.Weigth = _Weigth
--		)
--		and 
--		(
--			(drl.WeightView IS NULL AND _WeightView IS NULL) OR drl.WeightView = _WeightView
--		)
--		order by dr.BeginDate desc, drl.Trn Desc
--		Limit 1
--	);
	
--END $$
--language plpgsql stable;