if exists(select * from sysobjects where name ='p_nosameacc_th' and xtype='p')
  drop procedure p_nosameacc_th
go
create procedure p_nosameacc_th
(
  @psbillno varchar(20),
  @psmsg varchar(2000) out
)
as
  declare @swarecode varchar(10)
  declare @swarename varchar(100)
  declare @sshopcode varchar(10)
  declare @sfinacode varchar(10)
  declare @thno varchar(20)
  declare @psno varchar(20)
  declare @psthno varchar(20)
  declare @scurdate varchar(10)
  declare @scurtime varchar(8)
  declare @susercode varchar(10)
  declare @susername varchar(10)
begin
  select @swarecode = 'C009'
  select @swarename=orgname from t_shop where orgcode = @swarecode
  select @scurdate=convert(varchar(100),getdate(),23),@scurtime=convert(varchar(100),getdate(),108)
  select @sshopcode=orgcode,@susercode=usercode,@susername = username from t_return_head where billno = @psbillno
  select @sfinacode = finacode from t_shop where orgcode = @sshopcode

  --1、生成 C009无采购退货
  while 1=1
  begin
  exec f_get_billno 'TH', @swarecode, '1', @thno output
  if exists(select 1 from t_return_head where billno = @thno)
    continue
  else
    break
  end
  if @@ERROR <> 0 
  begin
    set @psMsg = '获取退货单单据号失败'
    return(-1)
  end

  insert into t_return_body(billno,serialno,plucode,pluname,barcode,cargono,spec,unit,punit,itemcnt,pcount,sglcount,price,jprice,netjprice,
                        counts,orncounts,cost,netcost,total,jxprice,jtaxrate,kindcd,remark)
  select @thno,serialno,plucode,pluname,barcode,cargono,spec,unit,punit,itemcnt,pcount,sglcount,price,jprice,netjprice,
         counts,orncounts,cost,netcost,total,jxprice,jtaxrate,kindcd,remark
  from t_return_body where billno = @psbillno

  insert into t_return_head(billno,orgcode,bizdate,lrdate,lrtime,contrno,supcode,supname,place,oldbillno,chkstatus,billtype,
                            counts,cost,netcost,total,usercode,username,zdrcode,zdrname,remark)
  select @thno,@swarecode,@scurdate,@scurdate,@scurtime,contrno,supcode,supname,'1'as place,@psbillno,chkstatus,'0',
         counts,cost,netcost,total,usercode,username,zdrcode,zdrname,'跨财务主体直送退货生成'
  from t_return_head where billno = @psbillno

  update t_return_head 
     set rzdate = @scurdate,
         rztime = @scurtime
  where billno = @thno

  --2、生成门店到C009的配送退货单
 -- while 1=1
 -- begin
  --exec f_get_billno 'PS', @swarecode, '1', @psno output
  --if exists(select 1 from t_dist_head where billno = @psno)
  --  continue
  --else
  --  break
 -- end
 -- if @@ERROR <> 0 
 -- begin
 --   set @psMsg = '获取配送单单据号失败'
 --   return(-1)
 -- end
  set @psno = @psbillno
  insert into t_dist_body(billno,serialno,plucode,pluname,barcode,supcode,cargono,spec,unit,punit,itemcnt,pcount,sglcount,price,jprice,netjprice,
                          cost,netcost,zpamount,zpnetamount,
                          ispsprice,zpprice,zpcount,total,jtaxrate,staxrate,depcode,depname,remark)
  select @psno,serialno,a.plucode,a.pluname,a.barcode,b.supcode,a.cargono,a.spec,a.unit,punit,a.itemcnt,-1*pcount,-1*sglcount,a.price,a.jprice,a.netjprice,
         -1*cost,-1*netcost,-1*cost as zpamount,-1*round(cost/(1+b.staxrate/100.00),2) as zpnetamount,
         '0' as ispsprice,a.jprice as zpprice,-1*counts,-1*total,a.jtaxrate,b.staxrate,a.depcode,a.depname,a.remark
  from t_return_body a,t_plu b
  where billno = @psbillno and a.plucode = b.plucode
  
  insert into t_dist_head(billno,askno,orgcode,shopcode,depcode,depname,lrdate,lrtime,sqauremode,paydate,chkstatus,
                          zpcount,zpamount,zpnetamount,cost,netcost,total,usercode,username,zdrcode,zdrname,remark)
  select @psno,'',@swarecode,@sshopcode,'' as depcode,'' depname,@scurdate,@scurtime,'1' as sqauremode,@scurdate,'1' as chkstatus,
        sum(d.zpcount),sum(d.zpamount),sum(d.zpnetamount),sum(d.cost),sum(d.netcost),sum(d.total),@susercode,@susername,@susercode,@susername,'跨财务主体直送退货生成'
  from t_dist_body d
  where d.billno = @psno

  update t_dist_head 
     set rzdate = @scurdate,
         rztime = @scurtime
  where billno = @psno

  insert into t_pay_bill(orgcode,billtype,billno,bizdate,supcode,sqauremode,cost,paydate,jscost,zrcost,isfinished,total,contrno,finacode)
  select shopcode,'7',billno,lrdate,'C2000',sqauremode,zpamount,paydate,0,0,case when zpamount=0 then '1' else '0' end,total,'',@sfinacode
    from t_dist_head where billno=@psno
  
  --3、生成门店的门店退仓单
  while 1=1
  begin
    exec f_get_billno 'PTH', @swarecode, '1', @psthno output
    if exists(select 1 from t_return_head where billno = @psthno)
      continue
    else
      break
  end
  if @@ERROR <> 0 
  begin
    set @psMsg = '获取门店退仓单单据号失败'
    return(-1)
  end

  insert into t_return_head(billno,orgcode,bizdate,lrdate,lrtime,contrno,supcode,supname,place,oldbillno,chkstatus,billtype,
                            counts,cost,netcost,total,usercode,username,zdrcode,zdrname,remark)
  select @psthno,@sshopcode,@scurdate,@scurdate,@scurtime,contrno,@swarecode,@swarename,place,@psbillno,chkstatus,'3',
         counts,cost,netcost,total,usercode,username,zdrcode,zdrname,'跨财务主体直送退货生成'
  from t_return_head where billno = @psbillno
  if @@error<>0
  begin
    set @psmsg='生成门店退仓单表头失败'
    return(-1)
  end
  insert into t_return_body(billno,serialno,plucode,pluname,barcode,cargono,spec,unit,punit,itemcnt,pcount,sglcount,price,jprice,netjprice,
                        counts,orncounts,cost,netcost,total,jxprice,jtaxrate,kindcd,remark)
  select @psthno,serialno,plucode,pluname,barcode,cargono,spec,unit,punit,itemcnt,pcount,sglcount,price,jprice,netjprice,
         counts,orncounts,cost,netcost,total,jxprice,jtaxrate,kindcd,remark
  from t_return_body where billno = @psbillno
  if @@error<>0
  begin
    set @psmsg='生成门店退仓单表体失败'
    return(-1)
  end

  update t_return_head 
    set rzdate = @scurdate,
        rztime = @scurtime
  where billno = @psthno

  set @psmsg = ''
  return (1)  
end
go