IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[order_239_dolinkdata_2962]') AND type = 'P')
DROP PROCEDURE [dbo].[order_239_dolinkdata_2962]
GO
create procedure order_239_dolinkdata_2962(
@cmpid int ,
@useruid int ,
@bizid int ,
@ordbcid int,
@ordid bigint,
@jbid bigint,
@ftype varchar(10),
@fbizid int,
@fordid bigint,
@ford2tag int ,
@ford2id bigint,
@rtype varchar(10),
@rbizid int,
@rordid bigint,
@rord2tag int ,
@rord2id bigint 
) 
as 
begin 
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED 
declare @operation varchar(50) 
declare @errorinfo nvarchar(4000)  
declare @newobjid bigint 
declare @ident varchar(40) 
declare @newsql nvarchar(max) 
declare @condition nvarchar(4000) 
declare @authority varchar(200) 
create table #mptypes(mpt varchar(20)) 
declare @bcid int 
set @bcid=@ordbcid 
declare @i int,@curInd int,@strlen int,@tmps varchar(100) ,@srcs varchar(2000) 



set @operation = 'continue' 
set @errorinfo='' 
set @newobjid = 0 
set @ident=convert(varchar(100),convert(decimal(18,11),CURRENT_TIMESTAMP)) 
set @newsql = '' 
set @authority = '' 
set @condition = '' 
-- 以下是用户扩展的程序  ---- 

-- 以上是用户扩展的程序  ---- 
 
set @srcs = @authority  
set @authority=@srcs  
select @curInd=1,@strlen=LEN(@srcs)  
while (@curInd<=@strlen)  
begin  
select @i=charindex(',',@srcs,@curInd)  
if (@i>0)  
begin  
select @tmps=SUBSTRING(@srcs,@curInd,@i-@curInd)  
set @curInd=@i+1  
end else if (@curInd<=@strlen) 
begin  
select @tmps=SUBSTRING(@srcs,@curInd,@strlen-@curInd+1)  
set @curInd=@strlen+1  
end  
if (@tmps>'') insert into #mptypes(mpt) values(@tmps)  
end 
if (@operation = 'continue') 
begin 

set @newsql='
if (@newobjid<=0) 
begin 
insert into mps_customer(cc_cmpid,cc_bcid,cc_branch,cc_dpid,cc_depart,cc_tag,cc_caption,cc_ctid,cc_type,cc_code,cc_createcuid,cc_creator,cc_cuid,cc_cuname,cc_name) 
select distinct @cmpid,cu_bcid,cu_branch,cu_dpid,cu_depart,0,ct_caption,0,'''',@ident,cu_id,cu_callname,cu_id,cu_callname,''未录入'' from mps_login,mps_csttype where ct_cmpid=@cmpid and ct_tag=0 and  cu_id=@useruid 
select top 1 @newobjid = cc_id from mps_customer where cc_cmpid=@cmpid and cc_code=@ident order by cc_id desc 
set @newobjid = ISNULL(@newobjid,0) 
delete mps_customer_users where ccp_ccid=@newobjid 
if (not exists (select 1  from mps_customer_users  where ccp_type=''cstview'' and ccp_ccid=@newobjid and ccp_cuid=@useruid)) insert into mps_customer_users(ccp_cuid,ccp_ccid,ccp_type,ccp_otid) values(@useruid,@newobjid,''cstview'',-1) 
if (not exists (select 1 from mps_customer_users   where ccp_type=''cstedit'' and ccp_ccid=@newobjid and ccp_cuid=@useruid)) insert into mps_customer_users(ccp_cuid,ccp_ccid,ccp_type,ccp_otid) values(@useruid,@newobjid,''cstedit'',-1) 
end 
if (@authority>'''') 
begin 
set @authority = '',''+@authority+'',''  
insert into mps_customer_users(ccp_cuid,ccp_ccid,ccp_otid,ccp_type) 
select distinct ordp_cuid,@newobjid,0,''cstview'' 
from order_239_users  
where ordp_ordid=@ordid and  @authority like ''%,''+ordp_type +'',%'' and  
not exists (select 1 from mps_customer_users where ccp_cuid=ordp_cuid and ccp_ccid=@newobjid and ccp_type=''cstview'') 
update  mps_customer set cc_tag=ct_tag,cc_caption=ct_caption,cc_type=ct_type from mps_csttype where cc_id=@newobjid and cc_ctid=ct_id 
update  mps_customer set cc_depart=dp_depart,cc_bcid=dp_bcid  from mps_depart where cc_id=@newobjid and cc_dpid=dp_id 
update  mps_customer set cc_branch=bc_company  from mps_groupbranch where cc_id=@newobjid and cc_bcid=bc_id 
update  mps_customer set cc_creator=cu_name  from mps_employee where cc_id=@newobjid and cc_createcuid=cu_id 
update  mps_customer set cc_cuname=cu_name  from mps_employee where cc_id=@newobjid and cc_cuid=cu_id 
end 

update old set  old.cc_activetime=getdate() ,  old.cc_whordno=new.ord_no ,  old.cc_whordid=new.ord_id ,  old.cc_name=new.ord_khxm ,  old.cc_mobile=new.ord_hm ,  old.cc_ctid=5
from order_239 new ,mps_customer old 
where  cc_id=@newobjid and  new.ord_id=@ordid 

'
if (@condition>'') set @newsql=@newsql+' and ('+@condition+') ' 

exec sp_executesql @newsql, N'@cmpid int,@useruid int,@bizid int,@ordbcid int,@ordid bigint,@jbid bigint,@ftype varchar(10),@fbizid int,@fordid bigint,@ford2tag int ,@ford2id bigint,@rtype varchar(10),@rbizid int,@rordid bigint,@rord2tag int ,@rord2id bigint,@newobjid bigint,@ident varchar(40),@authority varchar(300),@bcid int ', @cmpid,@useruid,@bizid,@ordbcid,@ordid,@jbid,@ftype,@fbizid,@fordid,@ford2tag,@ford2id,@rtype,@rbizid,@rordid,@rord2tag,@rord2id,@newobjid,@ident,@authority,@bcid 
end 

--- 下面这段程序，是完成帐务处理------

--- 帐务处理完毕 ------

--- 下面这段程序，是完成物资帐务处理------

--- 物资帐务处理完毕 ------

drop table #mptypes 


end