IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[order_88_newbizbysrc]') AND type = 'P')
DROP PROCEDURE [dbo].[order_88_newbizbysrc]
GO

create procedure order_88_newbizbysrc (
    @cmpid int , 
    @bcid int ,
    @branch nvarchar(50),
    @dpid int ,
    @depart nvarchar(50),
    @createcuid int ,
    @cuname nvarchar(50),
    @jbid bigint ,
    @bizstyle int,
    @biztitle nvarchar(100),  
    @bizcode nvarchar(100),
    @stype varchar(10) ,
    @sbizid int ,
    @sordid bigint ,
    @sord2tag int ,
    @sord2id bigint, 
    @newordids varchar(2000) output 
   ) 
  as 
  begin 
set @jbid = isnull(@jbid,0) 
if (@jbid=-999999999) set @jbid=0
declare @ordno varchar(50)
  declare @maxordid bigint 
  declare @ord table(ordid bigint , ordno varchar(50)) 
  declare @newcount int 
  set @newcount = 0 
  declare @newordid bigint
  
select @maxordid = max(ord_id) from order_88  
set @maxordid = isnull(@maxordid , 0 ) 

if (  @stype='biz' and @sbizid=86 and ((@sord2tag>0 and @sord2tag=0) or (@sord2tag=0)))  
begin
 insert into order_88(ord_createcuid,ord_createdepart,ord_createdpid,ord_creator,ord_cuid,ord_custname,ord_depart,ord_dpid,ord_dpmc,ord_empname,ord_fl,ord_fordid,ord_lfzsf,ord_lxdh,ord_ly,ord_yysj,ord_zxnr,ord_zxsj,ord_cmpid,ord_finished,ord_fjbid,ord_type,ord_style,ord_date,ord_updated,ord_bizid,ord_bcid,ord_branch,ord_ftype,ord_fbizid,ord_ford2tag,ord_ford2id,ord_rtype,ord_rbizid,ord_rordid,ord_rord2tag,ord_rord2id) 
select new.ord_createcuid,new.ord_createdepart,new.ord_createdpid,new.ord_creator,new.ord_cuid,new.ord_custname,new.ord_depart,new.ord_dpid,new.ord_dpmc,new.ord_empname,new.ord_fl,new.ord_fordid,new.ord_lfzsf,new.ord_lxdh,new.ord_ly,new.ord_yysj,new.ord_zxnr,new.ord_zxsj,@cmpid ,1 ,@jbid ,@biztitle ,@bizstyle ,getdate(),getdate(),88,@bcid ,@branch ,'biz',new.ord_bizid ,0,0 ,case when new.ord_fordid=0 then 'biz' when new.ord_rordid>0 then new.ord_rtype  else new.ord_ftype end ,case when new.ord_fordid=0 then new.ord_bizid when new.ord_rordid>0 then new.ord_rordid else new.ord_fbizid end ,case when new.ord_fordid=0 then new.ord_id when new.ord_rordid>0 then new.ord_rordid else new.ord_fordid end ,case when new.ord_fordid=0 then 0 when new.ord_rordid>0 then new.ord_rord2tag  else new.ord_ford2tag end ,case when new.ord_fordid=0 then 0 when new.ord_rordid>0 then new.ord_rord2id else new.ord_ford2id end 
  from order_86 new  where ord_id=@sordid 
end

if (  @stype='biz' and @sbizid=230 and ((@sord2tag>0 and @sord2tag=0) or (@sord2tag=0)))  
begin
 insert into order_88(ord_custname,ord_dpmc,ord_fl,ord_lfzsf,ord_lxdh,ord_ly,ord_zxnr,ord_zxsj,ord_cmpid,ord_finished,ord_fjbid,ord_type,ord_style,ord_date,ord_updated,ord_bizid,ord_bcid,ord_branch,ord_createdpid,ord_dpid,ord_createcuid,ord_cuid,ord_createdepart,ord_depart,ord_creator,ord_empname,ord_ftype,ord_fbizid,ord_fordid,ord_ford2tag,ord_ford2id,ord_rtype,ord_rbizid,ord_rordid,ord_rord2tag,ord_rord2id) 
select new.ord_creator,new.ord_dpmc,new.ord_fl,new.ord_bm,new.ord_lxdh,new.ord_ly,new.ord_wgnr,new.ord_zxsj,@cmpid ,1 ,@jbid ,@biztitle ,@bizstyle ,getdate(),getdate(),88,@bcid ,@branch ,@dpid ,@dpid ,@createcuid ,@createcuid ,@depart ,@depart ,@cuname ,@cuname ,'biz',new.ord_bizid ,new.ord_id ,0,0 ,case when new.ord_fordid=0 then 'biz' when new.ord_rordid>0 then new.ord_rtype  else new.ord_ftype end ,case when new.ord_fordid=0 then new.ord_bizid when new.ord_rordid>0 then new.ord_rordid else new.ord_fbizid end ,case when new.ord_fordid=0 then new.ord_id when new.ord_rordid>0 then new.ord_rordid else new.ord_fordid end ,case when new.ord_fordid=0 then 0 when new.ord_rordid>0 then new.ord_rord2tag  else new.ord_ford2tag end ,case when new.ord_fordid=0 then 0 when new.ord_rordid>0 then new.ord_rord2id else new.ord_ford2id end 
  from order_230 new  where ord_id=@sordid 
end

insert into @ord(ordid,ordno) select ord_id,'' from order_88
where ord_cmpid=@cmpid and ord_id>@maxordid and ord_bizid=88 and ord_no=''  
declare status11 cursor LOCAL FORWARD_ONLY READ_ONLY for
select ord_id from order_88 , @ord where ord_id=ordid  
open status11
FETCH NEXT FROM status11 INTO @newordid 
WHILE (@@FETCH_STATUS = 0)
BEGIN
set @newcount = @newcount + 1
exec mps_bm_getorset @cmpid,'biz',88,@newordid,@createcuid,@bizcode,2,@ordno output,1 
update @ord set ordno=@ordno where ordid=@newordid 
FETCH NEXT FROM status11 INTO @newordid 
END
CLOSE status11
DEALLOCATE status11   
update order_88 set ord_no=ordno from @ord where ord_id=ordid 
select top 1 @newordid=ordid from @ord  
set @newordid = isnull(@newordid,0) 
set @newordids=null 
select @newordids=ISNULL(@newordids+',','')+convert(varchar(16),ordid) from @ord 
set @newordids = isnull(@newordids,'')  
end