
-- old
USE [p543_v2.2]
GO
/****** Object:  UserDefinedFunction [dbo].[mart_Get543]    Script Date: 18.04.2019 11:00:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[mart_Get543] (@isDeb int, @cDeb varchar(20), @cCodeService varchar(20), @cCred varchar(20), @cZatr varchar(255)) RETURNS int WITH
EXEC AS
	CALLER AS
BEGIN
	DECLARE @graf      nvarchar(255)
	DECLARE @ResultVar int
	set @ResultVar = 999
	set @graf      = '-'
	if (@cCodeService = '2.3.'
	and
	@isDeb = 1)
	begin
		if (@cCred like '25%')
		begin
			set @ResultVar = 22
			RETURN @ResultVar
		end
		if (@cCred like '26%')
		begin
			set @ResultVar = 23
			RETURN @ResultVar
		end
		if (@cCred like '28%')
		begin
			set @ResultVar = 22
			RETURN @ResultVar
		end
		select top 1
			@graf = t6.[ГрафаОтчета]
		from
			[dbo].[Tab6_zatraty_20_1c] t6
		where
			t6.Код    =@cZatr
		if @@ROWCOUNT = 1
		begin
			select top 1
				@ResultVar=rr.id
			from
				[dbo].[rep_shbl] rr
			where
				rr.[no_str] = @graf
			RETURN @ResultVar
		end
		if (@cCred like '60%')
		begin
			set @ResultVar = 21
			RETURN @ResultVar
		end
		set @ResultVar = 23
		RETURN @ResultVar
	end
	if (@cCodeService = '2.3.'
	and
	@isDeb = 0)
	begin
		--- 2.3.8
		if (@cDeb like '20%'
		and
		(
			@cCred like '40%'
			or
			@cCred like '43%'
			or
			@cCred like '90%'
		)
		)
		begin
			set @ResultVar = 26
			RETURN @ResultVar
		end
		select top 1
			@graf = t6.[ГрафаОтчета]
		from
			[dbo].[Tab6_zatraty_20_1c] t6
		where
			t6.Код    =@cZatr
		if @@ROWCOUNT = 1
		begin
			select top 1
				@ResultVar=rr.id
			from
				[dbo].[rep_shbl] rr
			where
				rr.[no_str] = @graf
			if @ResultVar   = 24
			begin
				set @ResultVar = 25
				RETURN @ResultVar
			end
			RETURN @ResultVar
		end
		if (@cDeb like '20%'
		and
		@cCred like '10%')
		begin
			set @ResultVar = 19
			RETURN @ResultVar
		end
	end
	if (@cDeb like '888%')
	begin
		SET @ResultVar =
		CASE
		WHEN @cDeb = '888.01' THEN
			2
		WHEN @cDeb = '888.02' THEN
			3
		WHEN @cDeb = '888.03' THEN
			19
		WHEN @cDeb = '888.04' THEN
			20
		WHEN @cDeb = '888.05' THEN
			21
		WHEN @cDeb = '888.06' THEN
			22
		WHEN @cDeb = '888.07' THEN
			23
		WHEN @cDeb = '888.08' THEN
			28
		WHEN @cDeb = '888.09' THEN
			30
		WHEN @cDeb = '888.10' THEN
			31
		WHEN @cDeb = '888.11' THEN
			32
		WHEN @cDeb = '888.12' THEN
			34
		WHEN @cDeb = '888.13' THEN
			36
		WHEN @cDeb = '888.14' THEN
			29
			ELSE 999
		END
		RETURN @ResultVar
	end
	select top 1
		@ResultVar=id
	from
		[dbo].[rep_shbl]
	where
		[no_str] = @cCodeService
	-- Исключения по строкам!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	-- 31118,002
	if @ResultVar = 2
	and
	@isDeb = 1
	and
	@cCred not like '51%'
	begin
		set @ResultVar = 999
		RETURN @ResultVar
	end
	if @ResultVar = 2
	and
	@isDeb = 1
	and
	@cCred not like '55%'
	begin
		set @ResultVar = 999
		RETURN @ResultVar
	end
	if @ResultVar = 2
	and
	@isDeb = 0
	and
	@cDeb like '51%'
	and
	@cCred not like '62%'
	begin
		set @ResultVar = 999
		RETURN @ResultVar
	end
	if @ResultVar = 2
	and
	@isDeb = 0
	and
	@cDeb like '55%'
	and
	@cCred not like '62%'
	begin
		set @ResultVar = 999
		RETURN @ResultVar
	end
	if @ResultVar = 2
	and
	@isDeb = 0
	and
	(
		@cDeb like '62%'
	)
	and
	(
		@cCred like '62%'
		or
		@cCred like '60%'
	)
	begin
		set @ResultVar = 999
		RETURN @ResultVar
	end
	-- 31118,003
	if @ResultVar = 5
	and
	@isDeb = 1
	and
	(
		@cDeb like '60%'
	)
	and
	(
		@cCred like '62%'
	)
	begin
		set @ResultVar = 999
		RETURN @ResultVar
	end
	if @ResultVar = 5
	and
	@cDeb like '60%'
	and
	@cDeb = @cCred
	begin
		set @ResultVar = 999
		RETURN @ResultVar
	end
	if @ResultVar = 10
	and
	@cDeb like '60%'
	and
	@cDeb = @cCred
	begin
		set @ResultVar = 999
		RETURN @ResultVar
	end
	if @ResultVar  = 18
	set @ResultVar = 999
	RETURN @ResultVar
END