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

create procedure order_29_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_29  
set @maxordid = isnull(@maxordid , 0 ) 

if (  @stype='biz' and @sbizid=30 and ((@sord2tag>0 and @sord2tag=0) or (@sord2tag=0)))  
begin
 insert into order_29(ord_csny1,ord_jg,ord_kdgsj1,ord_lxfs,ord_ncuname,ord_qwxz,ord_sfzhm,ord_xb,ord_zpqd,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_csny,new.ord_jg,new.ord_ydgsj,new.ord_lxdh,new.ord_cuname,new.ord_qwxz,new.ord_sfzh,new.ord_xb,new.ord_yply,@cmpid ,1 ,@jbid ,@biztitle ,@bizstyle ,getdate(),getdate(),29,@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_30 new  where ord_id=@sordid 
end

insert into @ord(ordid,ordno) select ord_id,'' from order_29
where ord_cmpid=@cmpid and ord_id>@maxordid and ord_bizid=29 and ord_no=''  
declare status11 cursor LOCAL FORWARD_ONLY READ_ONLY for
select ord_id from order_29 , @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',29,@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_29 set ord_no=ordno from @ord where ord_id=ordid 
select top 1 @newordid=ordid from @ord  
set @newordid = isnull(@newordid,0) 
  if (@newcount=1 and  @stype='biz' and @sbizid=30)  
begin
insert into order_29_4(ord2_cuname,ord2_mslx,ord2_opinion,ord2_ordid,ord2_date,ord2_group,ord2_order,ord2_tags,ord2_tag,ord2_ftype,ord2_fbizid,ord2_fordid,ord2_ford2tag,ord2_ford2id,ord2_rtype,ord2_rbizid,ord2_rordid,ord2_rord2tag,ord2_rord2id) 
select new2.ord2_cuname,new2.ord2_mslx,new2.ord2_opinion,@newordid ,getdate() ,new2.ord2_group ,new2.ord2_order ,new2.ord2_tags ,4,'biz',new.ord_bizid ,new.ord_id ,6,new2.ord2_id ,case when new2.ord2_fordid=0 then 'biz' when new2.ord2_rordid>0 then new2.ord2_rtype else new2.ord2_ftype end ,case when new2.ord2_fordid=0 then new2.ord2_fbizid when new2.ord2_rordid>0 then new2.ord2_rbizid else new2.ord2_fbizid end ,case when new2.ord2_fordid=0 then new2.ord2_fordid when new2.ord2_rordid>0 then new2.ord2_rordid else new2.ord2_fordid end ,case when new2.ord2_fordid=0 then new2.ord2_tag when new2.ord2_rordid>0 then new2.ord2_rord2tag else new2.ord2_ford2tag end ,case when new2.ord2_fordid=0 then new2.ord2_ordid when new2.ord2_rordid>0 then new2.ord2_rordid else new2.ord2_ford2id end 
from order_30 new , order_30_6 new2 
where new.ord_id=@sordid and new.ord_id=new2.ord2_ordid and new2.ord2_tag=6
end
set @newordids=null 
select @newordids=ISNULL(@newordids+',','')+convert(varchar(16),ordid) from @ord 
set @newordids = isnull(@newordids,'')  
end