---- =============================================
---- Author:  Marat Zigangaraev
---- Create date: 16.10.2010
---- Description: Возвращает цену формы ЛС
---- =============================================
--CREATE OR REPLACE FUNCTION fn_GetDrugFormLim("Code" Integer -- код лекарственной формы (id T_DRUG_FORM)
--	,	"OnDate" TimeStamp = NULL
--	,	"KindOfLim" Integer = NULL)
--	RETURNS Integer AS
-- $$
-- Begin
--	RETURN 
--	(
--		Select l.Code
--		From akuz.T_DRUG_FORM_LIM l
--		Where l.DrugForm = "Code"
--		And l.DateBegin <= coalesce("OnDate", Current_TimeStamp::TIMESTAMP) 
--		And 
--		(
--			(
--				coalesce("KindOfLim", 0) = 0 AND l.Fed = '1'
--			) 
--			Or 
--			(
--				coalesce("KindOfLim", 0) = 1 AND l.Psn = '1'
--			) 
--			Or 
--			(
--				coalesce("KindOfLim",0) = 2 AND l.Reg = '1'
--			)
--		)
--		Order by l.DateBegin desc
--		Limit 1
--	);
--END $$
--language plpgsql stable;