IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_bm_getorset]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_bm_getorset]
GO
 
CREATE  procedure [dbo].[mps_bm_getorset]
(
@cmpid int,
@doctype varchar(10) , --// biz cst equ prd doc 
@crmid int , -- 可以是bizid  ctid  ecid pcid dcid 
@ordid1 bigint,  -- ordid ccid equc_id prd_id doc_id
@useruid int,
@bmcode2 nvarchar(200),
@set int /*0=set 1 get 2 set 但只针对临*/, 
@flowno nvarchar(26) OUTPUT,
@res int = 0
)
as
set nocount on
declare @ordid bigint 
declare @dpid1 int 
declare @dpcode nvarchar(20) 
declare @depart nvarchar(50) 
declare @cucode nvarchar(30) 

set @ordid = @ordid1 
if (@ordid<=0) set @ordid = 0 
declare @branchid1 bigint 
select @branchid1=a.cu_bcid , @dpid1=a.cu_dpid,@dpcode=dp_code , @depart=a.cu_depart , @cucode=b.cu_code 
from  mps_login a, mps_depart,mps_employee b   where a.cu_id = @useruid and a.cu_dpid=dp_id and a.cu_id=b.cu_id 
set @branchid1 = isnull(@branchid1,0) 
declare @branchcode nvarchar(30)
if (@branchid1>0)
begin
  select  @branchcode = bc_code from mps_branch with(nolock) where bc_id=@branchid1 
end else 
begin
  select @branchcode = cmp_groupcode from ebs_kernel.dbo.pub_company  where cmp_id=@cmpid 
end
set @branchcode = isnull(@branchcode , '') 
set @branchcode = @branchcode+'      '
declare @mycrmid bigint
declare @bmcode1 varchar(300)  
set @mycrmid = @crmid 
set @bmcode1 = @bmcode2 

declare @s nvarchar(36)
declare @l int
declare @hasct int 
set @l = charindex('[CT]',@bmcode1) + charindex('[CTD]',@bmcode1) + charindex('[CTA]',@bmcode1)
if (@l>0) 
begin
  set @hasct = 0 
  set @bmcode1 = replace(@bmcode1,'[CT]','') 
  set @bmcode1 = replace(@bmcode1,'[CTD]','') 
  set @bmcode1 = replace(@bmcode1,'[CTA]','') 
end else 
begin
  set @hasct = 1 
end 
declare  @ct1or_1 int
set @ct1or_1 = 1  
set @l = charindex('[CTD]',@bmcode1)
if (@l>0) set @ct1or_1 = -1 

declare @bmcode nvarchar(200) 
set @bmcode = @bmcode1
declare @rordid bigint 
declare @rbizid int 
declare @fordid bigint 
declare @fbizid int 
declare @ordid0 bigint 
declare @bizid0 bigint 
declare @fld1 nvarchar(100)
declare @fld2 nvarchar(100)
declare @fld3 nvarchar(100)
declare @fldn1 varchar(30)
declare @fldn2 varchar(30)
declare @fldn3 varchar(30)
declare @ordnum int 
declare @no varchar(50) 
set @ordid0 = 0 
declare @sql nvarchar(2000) 
declare @db varchar(100) 
declare @tbid int 
if (@doctype='biz') 
begin
select @db=biz_database , @tbid=biz_table from mps_biztype where biz_id=@crmid 
if (@tbid>0) 
select @db=biz_database , @tbid=biz_id from mps_biztype where biz_id=@tbid  
if (@db>'') set @db=@db+'.dbo.' 
if (@tbid=0) set @db=@db+'order_'+ltrim(rtrim(str(@crmid))) else set @db=@db+'order_'+ltrim(rtrim(str(@tbid)))
end else 
if (@doctype='cst') 
begin
select @db=ct_database , @tbid=ct_tag from mps_csttype where ct_id=@crmid 
if (@db>'') set @db=@db+'.dbo.' 
if (@tbid=0)  set @db=@db+'mps_customer' 
else  set @db=@db+'mps_customer'+ltrim(rtrim(str(@tbid))) 
end else 
if (@doctype='doc')
begin
-- select  @tbid=dc_tag from mps_doctype where dc_id=@crmid 
set @db='mps_document' -- +ltrim(rtrim(str(@tbid))) 
end else 
if (@doctype='equ')
begin
select  @tbid=ec_tag from mps_equctg where ec_id=@crmid 
if (@tbid=0)  set @db='mps_equ' 
else set @db='mps_equ'+ltrim(rtrim(str(@tbid))) 
end else 
if (@doctype='prd')
begin
select  @tbid=pc_tag from mps_prdctg where pc_id=@crmid 
if (@tbid=0)  set @db='mps_prd' 
else set @db='mps_prd'+ltrim(rtrim(str(@tbid))) 
end  

-- 分析一下，系统将用到哪些字段来编码
declare @dt2 varchar(10) 
if (@doctype='biz') set @dt2='ORD_'  
if (@doctype='cst') set @dt2='CC_'  
if (@doctype='doc') set @dt2='DOC_'  
if (@doctype='equ') set @dt2='EQU_'  
if (@doctype='prd') set @dt2='PRD_'  

declare @flds1 varchar(300) 
declare @s2 varchar(300)
set @s2 = @bmcode 
set @flds1 = '' 
set @fld1=''
set @fld2=''
set @fld3=''
set @fldn1=''
set @fldn3=''
set @fldn2=''
declare @ic int 
set @ic=0
while (@s2>'')
begin
set @l=charindex('{'+@dt2,@s2) 
if (@l<0)
begin
set @s2=substring(@s2,@l+1,len(@s2)-@l)
set @l=charindex('}',@s2) 
set @s = substring(@s2,1,@l-1) 
set @s2=substring(@s2,@l+1,len(@s2)-@l)
set @l=charindex(':',@s) 
if (@l>0) set @s=substring(@s,1,@l-1) 
if (@s>'') 
begin
set @ic = @ic + 1 
set @flds1 = @flds1+',@fld'+ltrim(rtrim(str(@ic)))+'='+@s 
if (@ic=1) set @fldn1=@s
if (@ic=2) set @fldn2=@s
if (@ic=3) set @fldn3=@s
end
end else 
begin
set @s2=''
end
end
if (@ic<3) 
begin
set @ic= @ic +1 
set @flds1 = @flds1+',@fld'+ltrim(rtrim(str(@ic)))+'='''' ' 
if (@ic=1) set @fldn1=''
if (@ic=2) set @fldn2=''
if (@ic=3) set @fldn3=''
end
if (@ic<3) 
begin
set @ic= @ic +1 
set @flds1 = @flds1+',@fld'+ltrim(rtrim(str(@ic)))+'='''' ' 
if (@ic=1) set @fldn1=''
if (@ic=2) set @fldn2=''
if (@ic=3) set @fldn3=''
end
if (@ic<3) 
begin
set @ic= @ic +1 
set @flds1 = @flds1+',@fld'+ltrim(rtrim(str(@ic)))+'='''' ' 
if (@ic=1) set @fldn1=''
if (@ic=2) set @fldn2=''
if (@ic=3) set @fldn3=''
end

if (@doctype='biz')
begin
set @sql = ' select @ordnum=ord_noi,@no=ord_no,@rordid=ord_rordid , @rbizid=ord_rbizid,@fordid=ord_fordid , @fbizid=ord_fbizid '+@flds1+' from '+@db+' where ord_id=@ordid ' 
exec sp_executesql @sql, N'@ordnum int out,@no varchar(50) out,@rordid bigint out ,@rbizid int out ,@fordid bigint out ,@fbizid int out,@fld1 nvarchar(100) out ,@fld2 nvarchar(100) out , @fld3 nvarchar(100) out ,  @ordid bigint', @ordnum out,@no out,@rordid out,@rbizid out,@fordid out,@fbizid out,@fld1 ,@fld2,@fld3,@ordid 
set @rordid = isnull(@rordid,0) 
set @rbizid = isnull(@rbizid,0) 
set @fordid = isnull(@fordid,0) 
set @fbizid = isnull(@fbizid,0) 
set @no = isnull(@no,'') 
set @fbizid = isnull(@fbizid,0) 
set @fld1 = isnull(@fld1,'') 
set @fld2 = isnull(@fld2,'') 
set @fld3 = isnull(@fld3,'') 
end else 
if (@doctype='cst')
begin
set @sql = ' select @ordnum=0,@no=cc_code  '+@flds1+' from '+@db+' where cc_id=@ordid ' 
exec sp_executesql @sql, N'@ordnum int out ,@no varchar(50) out,@fld1 nvarchar(100) out ,@fld2 nvarchar(100) out , @fld3 nvarchar(100) out ,  @ordid bigint', @ordnum out,@no out,@fld1,@fld2,@fld3,@ordid 
set @ordnum = isnull(@ordnum,0) 
set @no = isnull(@no,'') 
set @fld1 = isnull(@fld1,'') 
set @fld2 = isnull(@fld2,'') 
set @fld3 = isnull(@fld3,'') 
end  

declare @i int

if ((@rordid>0) and (charindex('{RORDNO}',@bmcode)>0))
begin
  set @i = charindex('|',@bmcode) 
  set @i = charindex('|',@bmcode) 
  if (@i>0) 
  begin
  set @bmcode = substring(@bmcode , 1 , @i - 1)
  end 
  set @ordid0 = @rordid
  set @bizid0 = @rbizid 
  set @bmcode = replace(@bmcode,'{*}','{#}') 
  set @bmcode = replace(@bmcode,'{**}','{##}') 
  set @bmcode = replace(@bmcode,'{***}','{###}') 
  set @bmcode = replace(@bmcode,'{****}','{####}') 
  set @bmcode = replace(@bmcode,'{*****}','{#####}') 
  set @bmcode = replace(@bmcode,'{******}','{######}') 
  set @bmcode = replace(@bmcode,'{*******}','{#######}') 
  set @bmcode = replace(@bmcode,'{********}','{########}') 
  set @bmcode = replace(@bmcode,'{RORDNO}','{ORDNO}') 
end
if ((@fordid>0) and (charindex('{FORDNO}',@bmcode)>0))
begin
  set @i = charindex('|',@bmcode) 
  if (@i>0) 
  begin
  set @bmcode = substring(@bmcode , 1 , @i - 1)
  end 
  set @ordid0 = @fordid
  set @bizid0 = @fbizid 
  set @bmcode = replace(@bmcode,'{*}','{#}') 
  set @bmcode = replace(@bmcode,'{**}','{##}') 
  set @bmcode = replace(@bmcode,'{***}','{###}') 
  set @bmcode = replace(@bmcode,'{****}','{####}') 
  set @bmcode = replace(@bmcode,'{*****}','{#####}') 
  set @bmcode = replace(@bmcode,'{******}','{######}') 
  set @bmcode = replace(@bmcode,'{*******}','{#######}') 
  set @bmcode = replace(@bmcode,'{********}','{########}') 
  set @bmcode = replace(@bmcode,'{FORDNO}','{ORDNO}') 
end

if ((@rordid<=0) and (charindex('{RORDNO}',@bmcode)>0))
begin
  set @i = charindex('|',@bmcode) 
  if (@i>0) 
  begin
    set @bmcode = substring(@bmcode , @i + 1 , len(@bmcode)-@i)
  end
end
if ((@fordid<=0) and (charindex('{FORDNO}',@bmcode)>0))
begin
  set @i = charindex('|',@bmcode) 
  if (@i>0) 
  begin
  set @bmcode = substring(@bmcode , @i + 1 , len(@bmcode)-@i)
  end
end
 
if (@bmcode='') set @bmcode = '{YYYY}-{MM}-{****:M}'
/*完整的CODE : FLAG-{YYYY}-{MM}-{DD}-{DEPART}-{EMPCODE:2}-{*****:N/Y/M/D/G/E/GY/GM/GD/EY/EM/ED}*/
/*N表示order不清0 G=DEPART/GRUP E=EMPLOYEE Y=YEAR M=MONTH D=DAY*/

/* {****:XX{ord_ext1:2}{ord_ext2:1}} */


if (@ordid0>0)
begin
declare @sql2 nvarchar(2000) 
declare @db2 varchar(100) 
if (@doctype='biz') 
begin
select @db2=biz_database , @tbid=biz_table from mps_biztype where biz_id=@bizid0 
if (@tbid>0) select @db2=biz_database , @tbid=biz_id from mps_biztype where biz_id=@tbid 
if (@db2>'') set @db2=@db2+'.dbo.' 
if (@tbid=0) set @db2=@db2+'order_'+ltrim(rtrim(str(@bizid0))) else set @db2=@db2+'order_'+ltrim(rtrim(str(@tbid)))
end  
set @sql2='select @no=ord_no from '+@db2+' where ord_id=@ordid0' 
exec sp_executesql @sql2, N'@no varchar(50) out , @ordid0 bigint', @no out,@ordid0 
set @no = isnull(@no,'') 
 
   set @bmcode = replace(@bmcode , '{ORDNO}' , @no) 
   if (charindex('{RORDNO}',@bmcode2)>0)
   begin
  set @sql2='select @ordnum=max(ord_noi) from '+@db2+' where ord_rordid=@ordid0' 
exec sp_executesql @sql2, N'@ordnum int out , @ordid0 bigint', @ordnum out,@ordid0 
set @ordnum = isnull(@ordnum,0) 
   end else 
  begin
    set @sql2='select @ordnum=max(ord_noi) from '+@db2+' where ord_fordid=@ordid0' 
exec sp_executesql @sql2, N'@ordnum int out , @ordid0 bigint', @ordnum out,@ordid0 
set @ordnum = isnull(@ordnum,0) 
  end
  
   set @ordnum = @ordnum+1 
  
  set @s = '00000000000000'+ltrim(rtrim(str(@ordnum))) 
 
set @l = len(@s) 
set @bmcode = replace(@bmcode,'{########}',substring(@s,@l-7,8))
set @bmcode = replace(@bmcode,'{#######}',substring(@s,@l-6,7))
set @bmcode = replace(@bmcode,'{######}',substring(@s,@l-5,6))
set @bmcode = replace(@bmcode,'{#####}',substring(@s,@l-4,5))
set @bmcode = replace(@bmcode,'{####}',substring(@s,@l-3,4))
set @bmcode = replace(@bmcode,'{###}',substring(@s,@l-2,3))
set @bmcode = replace(@bmcode,'{##}',substring(@s,@l-1,2))
set @bmcode = replace(@bmcode,'{#}',substring(@s,@l,1))
end
set @bmcode = replace(@bmcode,'{ORDNO}','??')
if ((@ordid>0) and (charindex('{'+@dt2,@bmcode)>0)) -- 有数据库字段
begin
if (@fldn1>'' and charindex('{'+@fldn1,@bmcode)>0) 
begin
  set @bmcode = replace(@bmcode,'{'+@fldn1+'}',@fld1)
  set @bmcode = replace(@bmcode,'{'+@fldn1+':1}',substring(@fld1,1,1))
  set @bmcode = replace(@bmcode,'{'+@fldn1+':2}',substring(@fld1,1,2))
  set @bmcode = replace(@bmcode,'{'+@fldn1+':3}',substring(@fld1,1,3))
  set @bmcode = replace(@bmcode,'{'+@fldn1+':4}',substring(@fld1,1,4))
  set @bmcode = replace(@bmcode,'{'+@fldn1+':5}',substring(@fld1,1,5))
  set @bmcode = replace(@bmcode,'{'+@fldn1+':6}',substring(@fld1,1,6))
end
if (@fldn2>'' and charindex('{'+@fldn2,@bmcode)>0) 
begin
  set @bmcode = replace(@bmcode,'{'+@fldn2+'}',@fld2)
  set @bmcode = replace(@bmcode,'{'+@fldn2+':1}',substring(@fld2,1,1))
  set @bmcode = replace(@bmcode,'{'+@fldn2+':2}',substring(@fld2,1,2))
  set @bmcode = replace(@bmcode,'{'+@fldn2+':3}',substring(@fld2,1,3))
  set @bmcode = replace(@bmcode,'{'+@fldn2+':4}',substring(@fld2,1,4))
  set @bmcode = replace(@bmcode,'{'+@fldn2+':5}',substring(@fld2,1,5))
  set @bmcode = replace(@bmcode,'{'+@fldn2+':6}',substring(@fld2,1,6))
end
if (@fldn3>'' and charindex('{'+@fldn3,@bmcode)>0) 
begin
  set @bmcode = replace(@bmcode,'{'+@fldn3+'}',@fld3)
  set @bmcode = replace(@bmcode,'{'+@fldn3+':1}',substring(@fld3,1,1))
  set @bmcode = replace(@bmcode,'{'+@fldn3+':2}',substring(@fld3,1,2))
  set @bmcode = replace(@bmcode,'{'+@fldn3+':3}',substring(@fld3,1,3))
  set @bmcode = replace(@bmcode,'{'+@fldn3+':4}',substring(@fld3,1,4))
  set @bmcode = replace(@bmcode,'{'+@fldn3+':5}',substring(@fld3,1,5))
  set @bmcode = replace(@bmcode,'{'+@fldn3+':6}',substring(@fld3,1,6))
end
end


declare @extclearrule varchar(50) 
set @i=charindex('%',@bmcode)
if (@i>0) 
begin
  set @extclearrule = substring(@bmcode,@i+1,len(@bmcode)-@i) 
  set @bmcode = substring(@bmcode,1,@i-1) 
end else 
begin
  set @extclearrule = ''  
end 
declare @hascy int 
declare @hascm int 

if ((charindex('CY',@bmcode)>0))
begin
  set @hascy = 1 
end else 
begin
  set @hascy = 0 
end
if ((charindex('CM',@bmcode)>0))
begin
  set @hascm = 1 
end else 
begin
  set @hascm = 0 
end

declare @dpid bigint 
set @dpid = 0
declare @cuid bigint 
set @cuid = 0 
declare @branchid bigint 
set @branchid = 0 
declare @y int 
declare @m int
declare @d int
declare @cy int 
declare @cm int 
set @y = 0 
set @m = 0 
set @d = 0 
set @cy = 0 
set @cm = 0 



if ((@hascy=1) or (@hascm=1))
begin
  declare @acrq nvarchar(10) 
 
  select @acrq = convert(varchar(10),cmp_accountdate,120) from ebs_kernel.dbo.pub_company with(nolock) where cmp_id = @cmpid
  set @acrq = isnull(@acrq,'') 
  if (@acrq>'')
  begin
    if (@hascy=1) set @cy = convert(int,substring(@acrq,1,4)) 
    if (@hascm=1) set @cm = convert(int,substring(@acrq,6,2)) 
  end
end


if ((charindex('*:N',@bmcode)>0) or (charindex('*:',@bmcode)<=0)) 
begin
  set @branchid = 0
  set @dpid = 0 
  set @cuid = 0 
  set @y = 0 
  set @m = 0 
  set @d = 0 
  set @bmcode = replace(@bmcode,'*:N','*') 
end else 
if (charindex('*:Y',@bmcode)>0) 
begin
  set @branchid = 0 
  set @dpid = 0 
  set @cuid = 0 
  if (@hascy=1) 
  begin
    set @y = @cy  
  end else 
  begin
    set @y = year(getdate()) 
  end
  set @m = 0 
  set @d = 0 
  set @bmcode = replace(@bmcode,'*:Y','*') 
end else 
if (charindex('*:M',@bmcode)>0) 
begin
  set @branchid = 0 
  set @dpid = 0 
  set @cuid = 0 
  if (@hascy=1)
  begin
    set @y = @cy 
  end else 
  begin
    set @y = year(getdate()) 
  end  
  if (@hascm=1)
  begin
    set @m = @cm 
  end else
  begin
    set @m = month(getdate()) 
  end   
  set @d = 0 
  set @bmcode = replace(@bmcode,'*:M','*') 
end else 
if (charindex('*:D',@bmcode)>0) 
begin
  set @branchid = 0 
  set @dpid = 0 
  set @cuid = 0 
  if (@hascy=1)
  begin
    set @y = @cy 
  end else 
  begin
    set @y = year(getdate()) 
  end  
  if (@hascm=1)
  begin
    set @m = @cm 
  end else
  begin
    set @m = month(getdate()) 
  end 
  set @d = day(getdate()) 
  set @bmcode = replace(@bmcode,'*:D','*') 
end else
if (charindex('*:BY',@bmcode)>0) 
begin
  set @branchid = @branchid1 
  set @dpid = 0 
  set @cuid = 0 
  if (@hascy=1)
  begin
    set @y = @cy 
  end else 
  begin
    set @y = year(getdate()) 
  end 
  set @m = 0 
  set @d = 0 
  set @bmcode = replace(@bmcode,'*:BY','*') 
end else
if (charindex('*:BM',@bmcode)>0) 
begin
  set @branchid = @branchid1 
  set @dpid = 0 
  set @cuid = 0 
  if (@hascy=1)
  begin
    set @y = @cy 
  end else 
  begin
    set @y = year(getdate()) 
  end  
  if (@hascm=1)
  begin
    set @m = @cm 
  end else
  begin
    set @m = month(getdate()) 
  end  
  set @d = 0 
  set @bmcode = replace(@bmcode,'*:BM','*') 
end else
if (charindex('*:BD',@bmcode)>0) 
begin
  set @branchid = @branchid1 
  set @dpid = 0 
  set @cuid = 0 
  if (@hascy=1)
  begin
    set @y = @cy 
  end else 
  begin
    set @y = year(getdate()) 
  end  
  if (@hascm=1)
  begin
    set @m = @cm 
  end else
  begin
    set @m = month(getdate()) 
  end 
  set @d = day(getdate()) 
  set @bmcode = replace(@bmcode,'*:BD','*') 
end else
if (charindex('*:B',@bmcode)>0) 
begin
  set @branchid = @branchid1 
  set @dpid = 0 
  set @cuid = 0 
  set @y = 0 
  set @m = 0 
  set @d = 0 
  set @bmcode = replace(@bmcode,'*:B','*') 
end else
if (charindex('*:GY',@bmcode)>0) 
begin
  set @branchid = 0
  set @dpid = @dpid1 
  set @cuid = 0 
  if (@hascy=1)
  begin
    set @y = @cy 
  end else 
  begin
    set @y = year(getdate()) 
  end 
  set @m = 0 
  set @d = 0 
  set @bmcode = replace(@bmcode,'*:GY','*') 
end else
if (charindex('*:GM',@bmcode)>0) 
begin
  set @branchid = 0
  set @dpid = @dpid1 
  set @cuid = 0 
  if (@hascy=1)
  begin
    set @y = @cy 
  end else 
  begin
    set @y = year(getdate()) 
  end  
  if (@hascm=1)
  begin
    set @m = @cm 
  end else
  begin
    set @m = month(getdate()) 
  end  
  set @d = 0 
  set @bmcode = replace(@bmcode,'*:GM','*') 
end else
if (charindex('*:GD',@bmcode)>0) 
begin
  set @branchid = 0
  set @dpid = @dpid1 
  set @cuid = 0 
  if (@hascy=1)
  begin
    set @y = @cy 
  end else 
  begin
    set @y = year(getdate()) 
  end  
  if (@hascm=1)
  begin
    set @m = @cm 
  end else
  begin
    set @m = month(getdate()) 
  end 
  set @d = day(getdate()) 
  set @bmcode = replace(@bmcode,'*:GD','*') 
end else
if (charindex('*:G',@bmcode)>0) 
begin
  set @branchid = 0
  set @dpid = @dpid1 
  set @cuid = 0 
  set @y = 0 
  set @m = 0 
  set @d = 0 
  set @bmcode = replace(@bmcode,'*:G','*') 
end else
if (charindex('*:EY',@bmcode)>0) 
begin
  set @branchid = 0
  set @dpid = 0 
  set @cuid = @useruid 
  if (@hascy=1)
  begin
    set @y = @cy 
  end else 
  begin
    set @y = year(getdate()) 
  end 
  set @m = 0 
  set @d = 0 
  set @bmcode = replace(@bmcode,'*:EY','*') 
end else
if (charindex('*:EM',@bmcode)>0) 
begin
  set @branchid = 0
  set @dpid = 0 
  set @cuid = @useruid  
  if (@hascy=1)
  begin
    set @y = @cy 
  end else 
  begin
    set @y = year(getdate()) 
  end  
  if (@hascm=1)
  begin
    set @m = @cm 
  end else
  begin
    set @m = month(getdate()) 
  end 
  set @d = 0 
  set @bmcode = replace(@bmcode,'*:EM','*') 
end else
if (charindex('*:ED',@bmcode)>0) 
begin
  set @branchid = 0
  set @dpid = 0 
  set @cuid = @useruid  
  if (@hascy=1)
  begin
    set @y = @cy 
  end else 
  begin
    set @y = year(getdate()) 
  end  
  if (@hascm=1)
  begin
    set @m = @cm 
  end else
  begin
    set @m = month(getdate()) 
  end  
  set @d = day(getdate()) 
  set @bmcode = replace(@bmcode,'*:ED','*') 
end else
if (charindex('*:E',@bmcode)>0) 
begin
  set @branchid = 0
  set @dpid = 0 
  set @cuid = @useruid  
  set @y = 0 
  set @m = 0 
  set @d = 0 
  set @bmcode = replace(@bmcode,'*:G','*') 
end 
if (charindex('{BRCH',@bmcode1)>0) 
begin
set @bmcode = replace(@bmcode,'{BRCH}',rtrim(@branchcode)) 
set @bmcode = replace(@bmcode,'{BRCH:1}',rtrim(substring(@branchcode,1,1))) 
set @bmcode = replace(@bmcode,'{BRCH:2}',rtrim(substring(@branchcode,1,2))) 
set @bmcode = replace(@bmcode,'{BRCH:3}',rtrim(substring(@branchcode,1,3))) 
set @bmcode = replace(@bmcode,'{BRCH:4}',rtrim(substring(@branchcode,1,4))) 
end
if (charindex('{DEPART',@bmcode1)>0) 
begin
set @bmcode = replace(@bmcode,'{DEPART}',@depart) 
set @bmcode = replace(@bmcode,'{DEPART:1}',substring(@depart,1,1)) 
set @bmcode = replace(@bmcode,'{DEPART:2}',substring(@depart,1,2)) 
set @bmcode = replace(@bmcode,'{DEPART:3}',substring(@depart,1,3)) 
set @bmcode = replace(@bmcode,'{DEPART:4}',substring(@depart,1,4)) 
end
if (charindex('{DPCODE',@bmcode1)>0) 
begin
set @bmcode = replace(@bmcode,'{DPCODE}',@dpcode) 
set @bmcode = replace(@bmcode,'{DPCODE:1}',substring(@dpcode,1,1)) 
set @bmcode = replace(@bmcode,'{DPCODE:2}',substring(@dpcode,1,2)) 
set @bmcode = replace(@bmcode,'{DPCODE:3}',substring(@dpcode,1,3)) 
set @bmcode = replace(@bmcode,'{DPCODE:4}',substring(@dpcode,1,4)) 
end
if (charindex('{EMPCODE',@bmcode1)>0) 
begin
set @bmcode = replace(@bmcode,'{EMPCODE}',@cucode) 
set @bmcode = replace(@bmcode,'{EMPCODE:1}',substring(@cucode,1,1))
set @bmcode = replace(@bmcode,'{EMPCODE:2}',substring(@cucode,1,2))
set @bmcode = replace(@bmcode,'{EMPCODE:3}',substring(@cucode,1,3))
set @bmcode = replace(@bmcode,'{EMPCODE:4}',substring(@cucode,1,4))
end

set @s = ltrim(rtrim(str(@cy)))

set @bmcode = replace(@bmcode,'{CYYYY}',@s)
set @bmcode = replace(@bmcode,'{CYYY}',substring(@s,2,3))
set @bmcode = replace(@bmcode,'{CYY}',substring(@s,3,2))
set @bmcode = replace(@bmcode,'{CY}',substring(@s,4,1))


set @s = ltrim(rtrim(str(year(getdate()))))
set @bmcode = replace(@bmcode,'{YYYY}',@s)
set @bmcode = replace(@bmcode,'{YYY}',substring(@s,2,3))
set @bmcode = replace(@bmcode,'{YY}',substring(@s,3,2))
set @bmcode = replace(@bmcode,'{Y}',substring(@s,4,1))


set @s = ltrim(rtrim(str(@cm)))

set @bmcode = replace(@bmcode,'{CM}',@s)
if (len(@s)=1) set @s = '0'+@s 
set @bmcode = replace(@bmcode,'{CMM}',@s)


set @s = ltrim(rtrim(str(month(getdate()))))

set @bmcode = replace(@bmcode,'{M}',@s)
if (len(@s)=1) set @s = '0'+@s 
set @bmcode = replace(@bmcode,'{MM}',@s)

set @s = ltrim(rtrim(str(day(getdate()))))
set @bmcode = replace(@bmcode,'{D}',@s)
if (len(@s)=1) set @s = '0'+@s 
set @bmcode = replace(@bmcode,'{DD}',@s)


declare @order bigint 
declare @cbid  bigint
if (@set=0) 
begin
select @order = cb_order , @cbid = cb_id 
from mps_order_libary with(nolock)
where  
  cb_branchid=@branchid and 
cb_dpid=@dpid and  
cb_cuid=@cuid and 

cb_y=@y and 
cb_m=@m and 
cb_d=@d and 
cb_ext = @extclearrule and 
cb_crmid=@mycrmid
end else 
begin
  select @order = cb_order2 , @cbid = cb_id 
from mps_order_libary with(nolock)
where  
  cb_branchid=@branchid and 
cb_dpid=@dpid and  
cb_cuid=@cuid and 
cb_y=@y and 
cb_m=@m and 
cb_d=@d and 
cb_ext = @extclearrule and 
cb_crmid=@mycrmid
end


set @order = isnull(@order , 0) 
set @order = @order + 1 
set @cbid = isnull(@cbid,0) 

set @s = '00000000000000'+ltrim(rtrim(str(@order))) 
 
set @l = len(@s) 

if (charindex('{ORD_',@bmcode)>0) 
begin
set @bmcode = replace(@bmcode,'{*********}','?????????')
set @bmcode = replace(@bmcode,'{********}','????????')
set @bmcode = replace(@bmcode,'{*******}','???????')
set @bmcode = replace(@bmcode,'{******}','??????')
set @bmcode = replace(@bmcode,'{*****}','?????')
set @bmcode = replace(@bmcode,'{****}','????')
set @bmcode = replace(@bmcode,'{***}','???')
set @bmcode = replace(@bmcode,'{**}','??')
set @bmcode = replace(@bmcode,'{*}','?')
end else 
begin
set @bmcode = replace(@bmcode,'{*********}',substring(@s,@l-8,9))
set @bmcode = replace(@bmcode,'{********}',substring(@s,@l-7,8))
set @bmcode = replace(@bmcode,'{*******}',substring(@s,@l-6,7))
set @bmcode = replace(@bmcode,'{******}',substring(@s,@l-5,6))
set @bmcode = replace(@bmcode,'{*****}',substring(@s,@l-4,5))
set @bmcode = replace(@bmcode,'{****}',substring(@s,@l-3,4))
set @bmcode = replace(@bmcode,'{***}',substring(@s,@l-2,3))
set @bmcode = replace(@bmcode,'{**}',substring(@s,@l-1,2))
set @bmcode = replace(@bmcode,'{*}',substring(@s,@l,1))
end
declare @del table(objid bigint) 

declare @useoldno int 
declare @cobid bigint
set @cobid = 0
set @useoldno = 1 
if (@set=0) 
begin
  if (@cbid<=0)
  begin
    insert into mps_order_libary(cb_crmid,cb_type,cb_branchid,cb_dpid,cb_cuid,cb_y,cb_m,cb_d,cb_ext,cb_order)
    values(@mycrmid,@doctype,@branchid,@dpid,@cuid,@y,@m,@d,@extclearrule,1) 
    set @hasct = 1 
  end else 
  begin
    if ((@hasct=0) and (@cbid>0))
    begin
      if (exists (select * from mps_order_pool with(nolock) where cob_cbid=@cbid)) 
      begin
        select top 1 @bmcode = cob_no ,@cobid =cob_id from mps_order_pool with(nolock) where cob_cbid=@cbid 
        set @useoldno = 0 
     delete mps_order_pool with(rowlock) where cob_id=@cobid 
      end  
    end 
    if (@useoldno=1) 
    begin
update mps_order_libary  with(rowlock)  set cb_order = @order 
    where cb_id = @cbid 
end 
  end 
  if (@ordid>0) 
  begin
  if (@doctype='biz')
  begin
  if (@hasct=1) 
  begin
      set @sql='update '+@db+' set ord_finished=case  when ord_finished=0 and charindex(''临'',@bmcode)<=0 then 1 else ord_finished end ,  ord_no = @bmcode , ord_noi = @ordnum , ord_cbid=0 where ord_id=@ordid '
      exec sp_executesql @sql, N'@bmcode varchar(50) ,@ordnum int ,@ordid bigint', @bmcode ,@ordnum ,@ordid 
    end else 
    begin
  set @sql='update '+@db+' set ord_finished=case  when ord_finished=0 and charindex(''临'',@bmcode)<=0 then 1 else ord_finished end , ord_no = @bmcode , ord_noi = @ordnum , ord_cbid=@ct1or_1*@cbid where ord_id=@ordid '
  exec sp_executesql @sql, N'@bmcode varchar(50) ,@ordnum int ,@ordid bigint,@ct1or_1 int,@cbid bigint', @bmcode ,@ordnum ,@ordid ,@ct1or_1,@cbid 
    end
    delete @del 
    insert into @del(objid) select jb_id from mps_job   where jb_cmpid=@cmpid and jb_ordid=@ordid and jb_ordno<>@bmcode and jb_crmid=@crmid 
    if (exists (select * from @del)) 
    update mps_job  set jb_ordno = @bmcode from @del where jb_id=objid 
 
  if (@hasct=1) 
  begin
  declare @ordidtemp bigint
  set @sql='select @ordidtemp=ord_id  from '+@db+' where ord_id>@ordid and ord_bizid=@crmid '
      exec sp_executesql @sql, N'@ordidtemp bigint out ,@ordid bigint,@crmid int', @ordidtemp out ,@ordid ,@crmid  
  set @ordidtemp =  isnull(@ordidtemp,0) 
  if (@ordidtemp=0) 
  begin
    update mps_order_libary with(rowlock)  set cb_order2 = @order where cb_id = @cbid
  end 
  end 
  end else 
  if (@doctype='cst')
  begin
  set @sql='update '+@db+' set cc_code = @bmcode  where cc_id=@ordid '
        exec sp_executesql @sql, N'@bmcode varchar(50) ,@ordnum int ,@ordid bigint', @bmcode ,@ordnum ,@ordid 
  end else 
  if (@doctype='doc')
  begin
  set @sql='update '+@db+' set doc_no = @bmcode  where doc_id=@ordid '
        exec sp_executesql @sql, N'@bmcode varchar(50) ,@ordnum int ,@ordid bigint', @bmcode ,@ordnum ,@ordid 
  end else 
  if (@doctype='prd')
  begin
  set @sql='update '+@db+' set prd_code = @bmcode  where prd_id=@ordid '
        exec sp_executesql @sql, N'@bmcode varchar(50) ,@ordnum int ,@ordid bigint', @bmcode ,@ordnum ,@ordid 
      end else 
  if (@doctype='equ')
  begin
  set @sql='update '+@db+' set equ_code = @bmcode  where equ_id=@ordid '
        exec sp_executesql @sql, N'@bmcode varchar(50) ,@ordnum int ,@ordid bigint', @bmcode ,@ordnum ,@ordid 
  end
  end
end else 
begin
  if (@set=2) 
  begin
  if (@cbid<=0)
  begin
insert into mps_order_libary(cb_crmid,cb_type,cb_branchid,cb_dpid,cb_cuid,cb_y,cb_m,cb_d,cb_ext,cb_order2)
values(@mycrmid,@doctype,@branchid,@dpid,@cuid,@y,@m,@d,@extclearrule,1) 
  end else 
  begin
    if ((@hasct=0) and (@cbid>0))
    begin
      if (exists (select * from mps_order_pool  where cob_cbid=@cbid)) 
      begin
        select top 1 @bmcode = cob_no from mps_order_pool  where cob_cbid=@cbid 
        set @useoldno = 0 
      end   
    end  
    if (@useoldno=1)
    begin
update mps_order_libary  set cb_order2 = @order where cb_id = @cbid 
 end 
  end 
end else 
begin
  if ((@hasct=0) and (@cbid>0))
    begin
      if (exists (select * from mps_order_pool where cob_cbid=@cbid)) 
      begin
        select top 1 @bmcode = cob_no from mps_order_pool  where cob_cbid=@cbid 
      end   
    end
end 
end
if (@set<>0)
begin
  set @bmcode = @bmcode +'(临)'
end
if (@res=0) 
begin
  select @bmcode flow_no 
end
select @flowno = @bmcode 
set nocount off