﻿IF OBJECT_ID('[dbo].[infokey_ParseBar]', 'P') IS NOT NULL
    DROP PROCEDURE [dbo].[infokey_ParseBar]
    
GO

CREATE PROCEDURE [infokey_ParseBar]
@bar VarChar(13)   Output, 
@cou Decimal(10,3) Output,
@rty Float     = 1 Output,
@wght Bit		   Output
AS
-- ============================================================================
-- версия: 4.00.01
-- автор: Мефед ЕВ
-- дата: 13.11.2008
-- комментарий: процедура парсинга штрихкода
-- ============================================================================
Set NoCount On
Declare @pref VarChar(4)
Declare @Len Int
Declare @stmp AS VarChar(10)
Declare @Mobile_BarParse TABLE([bp_prefix] VarChar(4), [bp_codelen] Int)
Declare @Par VarChar(255)
Declare @I Int
Declare @Pos Int
Declare @Tmp VarChar(790)
Declare @Dlm Char(1)
Declare @qty Float
Declare @askq Int
SELECT @Par=Cast(dbo.GetParm('ImpWeightBarCodesList') AS VarChar(255))+Char(13)
Set @wght = 0
Set @Dlm = Char(13)
Set @Pos = 1
Set @I = CharIndex(@Dlm,@Par,@Pos)
If Len(@bar) > 11
Begin
	While (@I > 0 )
	Begin
		Set @Tmp = SubString(@Par,@Pos,@I-@Pos)
		Set @Pos = @I+2
		Set @I = CharIndex('=',@Tmp)
	 	If @I>0
		Begin
	 		INSERT INTO @Mobile_BarParse(bp_prefix,bp_codelen)
			Values (Upper(LEFT(@Tmp,@I-1)),RIGHT(@Tmp,Len(@Tmp)-@I) )			
		End
		Set @I = CharIndex(@Dlm,@Par,@Pos)
	End
	Declare bar_curs Cursor Fast_Forward FOR
		SELECT * FROM @Mobile_BarParse
	Open bar_curs
	Fetch Next FROM bar_curs INTO @pref,@Len
	While @@fetch_status = 0
	Begin
		If LEFT(@bar,Len(@pref)) = @pref
		Begin
			Set @stmp = SubString(@bar,@Len+1,12-@Len)
			Set @bar = LEFT(@bar,@Len)
			Set @cou = Cast( @stmp AS Decimal(10,3))
			Set @wght = 1
			Break
		End
		Fetch Next FROM bar_curs INTO @pref,@Len
	End
	Close bar_curs
	DeAllocate bar_curs
End
SELECT @qty=b.quantity,@rty=Case When b.regularity IS Null OR b.regularity=0 Then g.regularity Else b.regularity End,@askq=b.AskQuantity
FROM dbo.goodsbarcode4 b INNER JOIN dbo.goods4 g On g.[code]= b.goodscode
WHERE b.barcode = @bar
If @wght = 0
	Set @cou=Case When @askq = 1 Then 0 When @qty IS Null OR @qty = 0 Then 1 Else @qty End
Else
	Set @cou= @cou*IsNull(@rty,1)
