IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fn_GetRecipeDrugFormLim]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[fn_GetRecipeDrugFormLim]
GO
--SET ANSI_NULLS ON
--GO
--SET QUOTED_IDENTIFIER ON
--GO
---- =============================================
---- Author:		Marat Zigangaraev
---- Create date: 16.10.2010
---- Description:	Возвращает цену рецепта
---- =============================================
--CREATE FUNCTION [dbo].[fn_GetRecipeDrugFormLim] 
--(
-- @RecipeID uniqueidentifier -- id рецепта
--)
--RETURNS int
--AS
--BEGIN
--  Declare @code int,
--  @dateend datetime,
--  @recipedate datetime

--       Select Top 1 @code = l.Code, @dateend = l.DateEnd, @recipedate = r.RecipeDate
--      From [AKUZ].T_RECIPE r WITH(NOLOCK)
--      left Join [AKUZ].T_DRUG_FORM_LIM l WITH(NOLOCK) On l.DrugForm = r.DrugFormCode
--      left Join [AKUZ].T_BENEFITS b WITH(NOLOCK) On b.BenefitsID = r.Benefits
--      left Join [VCLib].T_ENUMS e WITH(NOLOCK) On e.EnumID = b.BenefitViews
--      Where r.MedicalRecordID = @RecipeID And l.DateBegin <= r.RecipeDate And
--   ((e.Code=1 And l.FED=1) Or 
--    (e.Code=6 And l.PSN=1) Or
--    ((e.Code=2 Or e.Code=3) And l.REG=1))
--      Order by l.DateBegin desc

--        if (@dateend is null Or @dateend >= @recipedate) return @code
--        return null
--END
--GO
