if exists(select * from sysobjects where name='p_rz_prepare_ps' and xtype='p')
  drop procedure p_rz_prepare_ps
go
create procedure p_rz_prepare_ps
(					
  @psorgcode varchar(10),
  @psbillno varchar(20),
  @psparams varchar(200),
  @psmsg varchar(2000) out
)
as 
begin
  declare @optaccepttimes varchar(1)
  declare @sacceptno varchar(20)
  declare @sbilltype varchar(1)
  declare @optupacceptrate numeric(19,4)
  declare @billcost numeric(19,4)
  declare @sorgcode varchar(10)
  declare @sshopcode varchar(10)

  select @sorgcode=orgcode,@sshopcode=shopcode from t_dist_head where billno=@psbillno

  select @psmsg=coalesce(@psmsg+char(13),'')+t_plu.plucode+'-'+t_plu.pluname from t_dist_body d,t_plu
   where d.plucode=t_plu.plucode and d.billno=@psbillno and (t_plu.plustatus='2' or t_plu.plustatus='9')
  if @psmsg<>''
  begin
    set @psmsg='以下商品未审核或已被淘汰，请查找原因'+char(13)+@psmsg
    return(-1)
  end
  
  select @psmsg=coalesce(@psmsg+char(13),'')+d.plucode from t_dist_body d,t_plu_org g
   where d.plucode=g.plucode and g.orgcode=@sorgcode and g.isright='1' and d.billno=@psbillno and d.price<>g.price
  if isnull(@psmsg,'')<>''
  begin
    set @psmsg='以下商品的售价已经改变，请查找原因'+char(13)+@psmsg
    return(-1)
  end

  select @psmsg=coalesce(@psmsg+char(13),'')+ltrim(plucode)+'-'+pluname from t_dist_body d
     where billno=@psbillno and (not exists(select plucode from t_plu where plucode=d.plucode)
           or not exists(select plucode from t_plu_org where plucode=d.plucode and orgcode=@sorgcode and isright='1'))
  if isnull(@psmsg,'')<>''
  begin
    set @psmsg='以下商品已不存在或在机构['+@sorgcode+']中已无经营权限，无法记账'+char(13)+@psmsg
    return(-1)
  end
  
  select @psmsg=coalesce(@psmsg+char(13),'')+ltrim(plucode)+'-'+pluname from t_dist_body d
   where billno=@psbillno and (not exists(select plucode from t_plu where plucode=d.plucode)
         or not exists(select plucode from t_plu_org where plucode=d.plucode and orgcode=@sshopcode and isright='1'))
  if isnull(@psmsg,'')<>''
  begin
    set @psmsg='以下商品已不存在或在机构['+@sshopcode+']中已无经营权限，无法记账'+char(13)+@psmsg
    return(-1)
  end
end	
go

if exists(select * from sysobjects where name='p_rz_ps' and xtype='p')
  drop procedure p_rz_ps
go
create procedure p_rz_ps
(					
  @psorgcode varchar(10),
  @psbillno varchar(20),
  @psparams varchar(200),
  @psmsg varchar(2000) out
)
as 
begin
  declare @sshopcode varchar(10)
  declare @sorgcode varchar(10)
  declare @org_orgtype varchar(1)
  declare @org_isdbdeploy varchar(1)
  declare @org_isconfirm varchar(1)
  declare @shop_orgtype varchar(1)
  declare @shop_isdbdeploy varchar(1)
  declare @shop_isconfirm varchar(1)
  declare @sedition varchar(20)
  declare @scurver varchar(1)
  declare @scurdate varchar(10)
  declare @scurtime varchar(8)
  declare @optplumultivendor varchar(10)
  declare @splace varchar(1)
  declare @bill_cost numeric(19,4)
  declare @svendorcode varchar(15)
  declare @svendorname varchar(100)
  declare @svendorstatus varchar(1)
  declare @svendorvaliddate varchar(20)
  declare @svendortype varchar(2)
  declare @svendorjymode varchar(1)
  declare @siszs varchar(1)
  declare @szsywjs varchar(10)
  declare @vendor_payable numeric(19,4)
  declare @vendor_prepaytotal numeric(19,4)
  declare @v_org_type varchar(1)
  declare @stock_plutype varchar(1)
  declare @detail_cost numeric(19,4)
  declare @detail_netcost numeric(19,4)
  declare @detail_total numeric(19,4)
  declare @detail_jprice numeric(19,4)
  declare @detail_netjprice numeric(19,4)
  declare @detail_count numeric(19,4)
  declare @detail_plucode varchar(20)
  declare @detail_depcode varchar(10)
  declare @detail_price numeric(19,4)
  declare @detail_jtaxrate numeric(19,4)
  declare @detail_pluname varchar(40)  
  declare @goods_jprice numeric(19,4)
  declare @goods_netjprice numeric(19,4)
  declare @goods_cost numeric(19,4)
  declare @goods_netcost numeric(19,4)
  declare @jy_netcost numeric(19,4)
  declare @goods_tzamount numeric(19,4)
  declare @goods_counts numeric(19,4)
  declare @goods_ccount numeric(19,4)
  declare @goods_gcount numeric(19,4)
  declare @goods_jtaxrate numeric(19,4)
  declare @goods_mngstock varchar(1)
  declare @sflg varchar(1)
  declare @new_jprice numeric(19,4)
  declare @new_netjprice numeric(19,4)
  declare @new_cost numeric(19,4)
  declare @new_netcost numeric(19,4)
  declare @new_tzamount numeric(19,4)
  declare @fscost numeric(19,4)
  declare @fsnetcost numeric(19,4)
  declare @fstotal numeric(19,4)
  declare @jccost numeric(19,4)
  declare @jcnetcost numeric(19,4)
  declare @jctotal numeric(19,4)
  declare @jccount numeric(19,4)
  declare @ssql varchar(2000)
  declare @sbranchno varchar(10)
  declare @detailcs_plucode varchar(20)
  declare @detailcs_colorcode varchar(10)
  declare @detailcs_sizecode varchar(10)
  declare @detailcs_cgcount numeric(19,4)
  declare @detailcs_ccounts numeric(19,4)
  declare @stateflg int
  declare @mngcsstock varchar(1)  
  declare @scalctype varchar(1)
  declare @fsjrate numeric(19,4)
  declare @saskno varchar(20)
  declare @fzpcount numeric(19,4)
  declare @fzpamount numeric(19,4)  
  declare @acceptno varchar(20)
  declare @chkstatus varchar(1)
  declare @susercode varchar(6)
  declare @susername varchar(10)
  declare @sshrcode varchar(6) 
  declare @sshrname varchar(10)
  declare @zerozp varchar(1)  
  declare @crtpsjspaybillbysale varchar(1)
  declare @vi_result integer
  declare @vs_message varchar(max)
  declare @vd_procdate datetime
  declare @ssameaccorg varchar(1)
  declare @finacode varchar(10)
  declare @stmp varchar(10)
  
  select @optplumultivendor=itemvalue from t_syscfg where section='SYSTEM' and itemname='PluMultiVendor'
  select @sedition=itemvalue from t_syscfg where section='SYSTEM' and itemname='Edition'
  select @sorgcode=orgcode,@sshopcode=shopcode,@saskno=askno,@fzpcount=zpcount,@fzpamount=zpamount,@susercode=usercode,@susername=username from t_dist_head where billno=@psbillno
  select @org_orgtype=orgtype,@org_isdbdeploy=isdbdeploy,@org_isconfirm=isconfirm from t_shop where orgcode=@sorgcode
  select @finacode=finacode from t_shop where orgcode=@sshopcode
  select @shop_orgtype=orgtype,@shop_isdbdeploy=isdbdeploy,@scalctype=isnull(calctype,'0'),@fsjrate=isnull(sjrate,0),@shop_isconfirm=IsNull(isconfirm,'0') from t_shop where orgcode=@sshopcode
  select @scurver=itemvalue from t_syscfg where section='SYSTEM' and itemname='VER'
  select @scurdate=convert(varchar(100),getdate(),23),@scurtime=convert(varchar(100),getdate(),108)
  select @sbranchno=itemvalue from t_syscfg where section='SYSTEM' and itemname='BranchNo'

  if @splace=''
  begin
    if @org_orgtype='2'
      set @splace='1'
    else
      set @splace='0'
  end
  --select @ssameaccorg=case when isnull(a.finacode,'')<>isnull(b.finacode,'') then '0' else '1' end from t_shop a,t_shop b where a.orgcode=@sorgcode and b.orgcode=@sshopcode

  exec p_get_finacode @sorgcode,@sshopcode,@stmp out,@ssameaccorg out ,@psmsg out
  if @psmsg <> ''
  begin
	return (-1)
  end
  set @scalctype = '0'
  if exists(select 1 from t_dist_body where billno=@psbillno and zpcount>=0)
  begin
    if @scalctype='1' or @scalctype='2'
    begin
      update a set a.zpprice=case when @scalctype='1' then b.jprice*(1+@fsjrate/100.00) when @scalctype='2' then b.price*(1+@fsjrate/100.00) else a.zpprice end,
                   a.zpamount=case when c.plutype='2' then 0 when @scalctype='1' then round(b.jprice*(1+@fsjrate/100.00)*zpcount,2) when @scalctype='2' then round(b.price*(1+@fsjrate/100.00)*zpcount,2) else a.zpamount end
        from t_dist_body a,t_plu_org b,t_plu c
       where a.plucode=b.plucode and a.plucode=c.plucode and a.billno=@psbillno and b.orgcode=@sorgcode and a.ispsprice='0'
       
      if @@error<>0 
      begin
        set @psmsg='更新配送价失败'
        return(-1)
      end
    end
	else
	begin
	  update a set a.zpprice=b.jprice,
                   a.zpamount= round(b.jprice*zpcount,2)
        from t_dist_body a,t_plu_org b,t_plu c
       where a.plucode=b.plucode and a.plucode=c.plucode and a.billno=@psbillno and b.orgcode=@sorgcode
    end
	update a set a.zpnetamount=round(case when @ssameaccorg='1' then a.zpamount/(1+a.jtaxrate/100.00) else a.zpamount/(1+a.staxrate/100.00) end,2)
	  from t_dist_body a,t_plu_org b
	  where a.plucode=b.plucode and a.billno=@psbillno
	    and b.orgcode=@sorgcode
	   
	if @@ERROR <> 0 
	begin
	  set @psMsg = '更新无税配送金额失败'
	  return(-1)
	end
    update a set a.jprice=b.jprice,a.netjprice=b.netjprice,a.price=b.price,
                 a.cost=case when c.plutype='2' then 0 else round(b.jprice*zpcount,2) end,
                 a.netcost=case when c.plutype='2' then 0 else round(round(b.jprice*zpcount,2)/(1+c.jtaxrate/100.00),2) end,
                 a.total=case when c.plutype='2' then 0 else round(b.price*zpcount,2) end
      from t_dist_body a,t_plu_org b,t_plu c
     where a.plucode=b.plucode and a.plucode=c.plucode and a.billno=@psbillno and b.orgcode=@sorgcode
    if @@error<>0 
    begin
      set @psmsg='更新进价信息失败'
      return(-1)
    end
    update a set zpcount=b.zpcount,zpamount=b.zpamount,zpnetamount=b.zpnetamount,cost=b.cost,netcost=b.netcost,total=b.total
      from t_dist_head a,(select billno,sum(zpcount) as zpcount,sum(zpamount) as zpamount,sum(zpnetamount) as zpnetamount,sum(cost) as cost,sum(netcost) as netcost,sum(total) as total from t_dist_body where billno=@psbillno group by billno) b
     where a.billno=b.billno and (a.zpcount<>b.zpcount or a.zpamount<>b.zpamount or a.zpnetamount<>b.zpnetamount or a.cost<>b.cost or a.netcost<>b.netcost or a.total<>b.total)
    if @@error<>0 
    begin
      set @psmsg='更新表头信息失败'
      return(-1)
    end
    if @shop_orgtype<>'2' and @shop_isdbdeploy='0'
    begin
      update a set supcode=b.supcode from t_dist_body a,t_plu b where a.billno=@psbillno and a.plucode=b.plucode
      if @@error<>0 
      begin
        set @psmsg='更新表体供应商信息失败'
        return(-1)
      end
    end
  end

  declare cur cursor for select plucode,pluname,depcode,price,jtaxrate,cost as fscost,netcost as fsnetcost,
                                total as fstotal,zpcount,cost,netcost,jprice,netjprice 
                           from t_dist_body where billno=@psbillno order by plucode,jprice
  open cur
  fetch next from cur into @detail_plucode,@detail_pluname,@detail_depcode,@detail_price,@detail_jtaxrate,@fscost,@fsnetcost,@fstotal,@detail_count,@detail_cost,@detail_netcost,@detail_jprice,@detail_netjprice
  while @@fetch_status=0
  begin
    set @sflg='0'
    select @goods_jprice=case when g.plutype='2' then 0 else o.jprice end ,
           @goods_netjprice=case when g.plutype='2' then 0 else o.netjprice end,
           @goods_cost=case when g.plutype='2' then 0 else o.cost end,
           @goods_netcost=case when g.plutype='2' then 0 else o.netcost end,
           @goods_tzamount=case when g.plutype='2' then 0 else o.tzamount end,
           @goods_counts=o.gcount+o.ccount,
           @goods_ccount=o.ccount,
           @goods_gcount=o.gcount,
           @goods_jtaxrate=g.jtaxrate,@goods_mngstock=o.mngstock
      from t_plu_org o,t_plu g where o.plucode=g.plucode and o.plucode=@detail_plucode and o.orgcode=@sorgcode
    if @@rowcount=0 
    begin
      set @psmsg='商品['+@detail_pluname+']不存在'
	  close cur
      deallocate cur
      return(-1)
    end

   -- if (@detail_count>0) and (not exists (select plucode from t_plu_org where plucode=@detail_plucode and isask='1' and orgcode=@sshopcode and isright='1'))
   -- begin
   --   set @psmsg='商品['+@detail_pluname+']在门店['+@sshopcode+']中不允许要货'
	  --close cur
   --   deallocate cur
   --   return(-1)
   -- end

    if @goods_mngstock<>'1'
    begin
      set @psmsg='商品['+@detail_pluname+']不管理库存，不能配送'
	  close cur
      deallocate cur
      return(-1)
    end

    if @detail_count<0
    begin
      if @goods_counts>0
      begin
        if @goods_tzamount<>0
        begin
          if (@goods_tzamount<0) and (@goods_tzamount<@detail_cost)
          begin
            set @new_tzamount=@goods_tzamount-@detail_cost
            set @new_cost=@goods_cost
            set @new_netcost=@goods_netcost
          end
          else
          begin
            set @new_tzamount=0
            set @new_cost=@goods_cost-@detail_cost+@goods_tzamount
            set @new_netcost=@goods_netcost-@detail_netcost+round(@goods_tzamount/(1+@goods_jtaxrate/100),2)
          end
        end
        else
        begin
          set @new_cost=@goods_cost-@detail_cost
          set @new_netcost=@goods_netcost-@detail_netcost
          set @new_tzamount=@goods_tzamount
        end

        set @new_jprice=round(@new_cost/(@goods_counts-@detail_count),4)
        set @new_netjprice=round(@new_netcost/(@goods_counts-@detail_count),4)
      end
      else
      begin
        select @new_jprice=@detail_jprice,@new_netjprice=@detail_netjprice
        set @new_cost=round(@new_jprice*(@goods_counts-@detail_count),2)
        set @new_netcost=round(@new_netjprice*(@goods_counts-@detail_count),2)
        set @new_tzamount=@goods_tzamount+@goods_cost-@detail_cost-@new_cost
        set @jy_netcost=@goods_netcost-@detail_netcost+round(@goods_tzamount/(1+@goods_jtaxrate/100),2)
        if @jy_netcost<>@new_netcost+round(@new_tzamount/(1+@goods_jtaxrate/100),2)
          set @new_netcost=@jy_netcost-round(@new_tzamount/(1+@goods_jtaxrate/100),2)
      end
    end
    else
    begin 
      if ((@detail_count>=@goods_counts) and (@detail_cost<=@goods_cost)) or ((@detail_count<=@goods_counts) and (@detail_cost>=@goods_cost))
      begin
        set @sflg='1'
        select @new_jprice=@detail_jprice,@new_netjprice=@detail_netjprice
        set @new_cost=round(@new_jprice*(@goods_counts-@detail_count),2)
        set @new_netcost=round(@new_netjprice*(@goods_counts-@detail_count),2)
        set @new_tzamount=@goods_tzamount+@goods_cost-@detail_cost-@new_cost
        set @jy_netcost=@goods_netcost-@detail_netcost+round(@goods_tzamount/(1+@goods_jtaxrate/100),2)
        if @jy_netcost<>@new_netcost+round(@new_tzamount/(1+@goods_jtaxrate/100),2)
          set @new_netcost=@jy_netcost-round(@new_tzamount/(1+@goods_jtaxrate/100),2)
      end
      else
      begin
        if @goods_tzamount<>0
        begin
          if (@goods_tzamount>0) and (@goods_tzamount>@detail_cost)
          begin
            set @new_tzamount=@goods_tzamount-@detail_cost
            set @new_cost=@goods_cost
            set @new_netcost=@goods_netcost
          end
          else
          begin
            set @new_tzamount=0
            set @new_cost=@goods_cost-@detail_cost+@goods_tzamount
            set @new_netcost=@goods_netcost-@detail_netcost+round(@goods_tzamount/(1+@goods_jtaxrate/100),2)
          end
        end
        else
        begin
          set @new_cost=@goods_cost-@detail_cost
          set @new_netcost=@goods_netcost-@detail_netcost
          set @new_tzamount=@goods_tzamount
        end

        set @new_jprice=round(@new_cost/(@goods_counts-@detail_count),4)
        set @new_netjprice=round(@new_netcost/(@goods_counts-@detail_count),4)
      end
    end

    select @zerozp=isnull(itemvalue,'0') from t_syscfg where section='SYSTEM' and itemname='ZeroZp'
    if (@zerozp<>'1') and (@detail_count>0) and (@goods_ccount<@detail_count)
    begin
      set @psmsg='商品['+@detail_plucode+']配送数量不能超过库存数量'
	  close cur
      deallocate cur
      return(-1)
    end
    
    update a set ccount=ccount-@detail_count,
                 jprice=@new_jprice,
                 netjprice=@new_netjprice,
                 tzamount=@new_tzamount,
                 cost=@new_cost,
                 netcost=@new_netcost,
                 total=case when b.plutype='2' then 0 else round(@detail_price*(@goods_counts-@detail_count),2) end,
                 newpsdate=case when @detail_count>0 then @scurdate else a.newpsdate end,
                 @stock_plutype=b.plutype,
                 @fstotal=case when b.plutype='2' then 0 else @fstotal end,
                 @jccount=gcount+ccount-@detail_count,
                 @jccost=@new_cost+@new_tzamount,
                 @jcnetcost=@new_netcost+round(@new_tzamount/(1+@goods_jtaxrate/100),2),
                 @jctotal=case when b.plutype='2' then 0 else round(@detail_price*(@goods_counts-@detail_count),2) end
      from t_plu_org a,t_plu b
     where a.plucode=b.plucode and a.orgcode=@sorgcode and a.plucode=@detail_plucode and isright='1'

    if @saskno<>''
    begin
      update t_ask_body set ypcount=case when ypcount+@detail_count>counts then counts else ypcount+@detail_count end,psdate=@scurdate
       where plucode=@detail_plucode and billno=@saskno
    end
    
    exec p_create_gbk_data @scurdate,@scurtime,@detail_plucode,@detail_depcode,@psbillno,'E',@detail_jprice,@detail_netjprice,@detail_price,@detail_count,@fscost,@fsnetcost,@fstotal,
                           @jccount,@jccost,@jcnetcost,@jctotal,@sorgcode,'','',@psmsg
    fetch next from cur into @detail_plucode,@detail_pluname,@detail_depcode,@detail_price,@detail_jtaxrate,@fscost,@fsnetcost,@fstotal,@detail_count,@detail_cost,@detail_netcost,@detail_jprice,@detail_netjprice
  end
  close cur
  deallocate cur
  
  select @mngcsstock=mngcsstock from t_shop where orgcode=@sorgcode
  if (@sedition='4') and (@mngcsstock='1')
  begin
    insert into t_plu_org_cs(plucode,orgcode,colorcode,sizecode,csbarcode,gcount,ccount,remark)
      select distinct a.plucode as plucode,@sorgcode as orgcode,a.colorcode as colorcode,a.sizecode as sizecode,g.csbarcode as csbarcode,0 as gcount,0 as ccount,'' as remark
        from t_dist_cs_body a,t_plu_cs g where a.plucode=g.plucode and a.colorcode=g.colorcode and a.sizecode=g.sizecode and a.billno=@psbillno 
             and not exists(select plucode from t_plu_org_cs o where o.plucode=a.plucode and o.colorcode=a.colorcode and o.sizecode=a.sizecode and o.orgcode=@sorgcode)

    update a set a.gcount=a.gcount,a.ccount=a.ccount-b.ccounts
      from t_plu_org_cs a,(select plucode,colorcode,sizecode,sum(ccounts) as ccounts from t_dist_cs_body where billno=@psbillno group by plucode,colorcode,sizecode) b
     where a.plucode=b.plucode and a.orgcode=@sorgcode and a.colorcode=b.colorcode and a.sizecode=b.sizecode

    update a set ypcount=case when ypcount+b.ccounts>a.ccounts then a.ccounts else ypcount+b.ccounts end
      from t_ask_cs_body a,(select plucode,colorcode,sizecode,sum(ccounts) as ccounts from t_dist_cs_body where billno=@psbillno group by plucode,colorcode,sizecode) b
     where a.billno=@saskno and a.plucode=b.plucode and a.colorcode=b.colorcode and a.sizecode=b.sizecode
  end
  
  --set @vd_procdate=getdate()
  --exec p_decbatchstk @psbillno,'ps',@susercode,@susername,@vd_procdate,@vi_result output,@vs_message output
  --if @vi_result<>1
  --begin
  --  select @psmsg=case when isnull(@vs_message,'')='' then '配送处理批次库存冲减失败' else @vs_message end 
  --  return(-1)
  --end
  
  if (@fzpcount>0) and (@shop_orgtype<>'2') and (@shop_isdbdeploy='1')
  begin
    select @chkstatus='0',@sshrcode='',@sshrname=''
    select @chkstatus=case when chkmode='0' then '0' else '1' end from t_bill_type where billcode='PJH'
    if @chkstatus='1'
    begin
      select @sshrcode=@susercode,@sshrname=@susername
    end
    exec f_get_billno 'jh',@sorgcode,'1',@acceptno output
    insert into t_accept_head(billno,orgcode,billtype,bizdate,lrdate,lrtime,depcode,depname,supcode,supname,
                              place,sqauremode,paydate,orderno,chkstatus,shrcode,shrname,usercode,username,zdrcode,zdrname,remark,askno)
      select @acceptno,shopcode,'3',@scurdate,@scurdate,@scurtime,depcode,depname,orgcode,(select supname from t_supplier where supcode=orgcode) as supname,
             '0',sqauremode,paydate,billno,@chkstatus,@sshrcode,@sshrname,@susercode,@susername,@susercode,@susername,remark,askno
        from t_dist_head where billno=@psbillno
    if @@error<>0
    begin
      set @psmsg='生成配送验收单表头失败'
      return(-1)
    end
    insert into t_accept_body(billno,plucode,serialno,barcode,pluname,cargono,spec,unit,punit,itemcnt,pcount,sglcount,
                              jprice,netjprice,ornjprice,ornnetjprice,price,counts,cost,netcost,total,jtaxrate,jxprice,cgcount,depcode,depname,remark)
      select @acceptno,a.plucode,a.serialno,a.barcode,a.pluname,a.cargono,a.spec,a.unit,a.punit,a.itemcnt,a.pcount,a.sglcount,
             a.zpprice,round(case when @ssameaccorg='1' then a.zpprice/(1+a.jtaxrate/100.0) else a.zpprice/(1+a.staxrate/100.0) end,4),
			 a.zpprice,round(case when @ssameaccorg='1' then a.zpprice/(1+a.jtaxrate/100.0) else a.zpprice/(1+a.staxrate/100.0) end,4),c.price,
             a.zpcount,a.zpamount,a.zpnetamount,
             case when b.plutype='2' then 0 else round(c.price*a.zpcount,2) end,
			 case when @ssameaccorg='1' then a.jtaxrate else a.staxrate end,
             case when b.plutype='2' then 0 else (round(c.price*a.zpcount,2)-round(a.zpprice*a.zpcount,2)) end,a.zpcount,a.depcode,a.depname,a.remark
       from t_dist_body a,t_plu b,t_plu_org c where billno=@psbillno
            and a.plucode=b.plucode and a.plucode=c.plucode and c.orgcode=@sshopcode and c.isright='1'
    if @@error<>0
    begin
      set @psmsg='生成配送验收单表体失败'
      return(-1)
    end
    insert into t_accept_cs_body(billno,serialno,plucode,csbarcode,colorcode,sizecode,ccounts,cgcount,remark)
      select @acceptno,serialno,plucode,csbarcode,colorcode,sizecode,ccounts,ccounts,remark from t_dist_cs_body where billno=@psbillno
    if @@error<>0
    begin
      set @psmsg='生成配送验收单色码明细失败'
      return(-1)
    end
    update a set counts=b.counts,cost=b.cost,netcost=b.netcost,total=b.total
      from t_accept_head a,(select billno,sum(counts) as counts,sum(cost) as cost,sum(netcost) as netcost,sum(total) as total from t_accept_body where billno=@acceptno group by billno) b
     where a.billno=b.billno
    if @@error<>0
    begin
      set @psmsg='更新配送验收单表头失败'
      return(-1)
    end
	update t_dist_head set diststate = '2'
     where billno=@psbillno
	if @shop_isconfirm='0' 
	begin
		exec p_rz_jh @sorgcode,@acceptno,'',@psmsg output
		if @psmsg<>'' or @@error<>0
		begin
		  select @psmsg=case when isnull(@psmsg,'')='' then '配送验收单记账失败' else @psmsg end 
		  return(-1)
		end
	end
  end
  
  set @crtpsjspaybillbysale='0'
  
  select @crtpsjspaybillbysale=isnull(itemvalue,'0') from t_syscfg where section='system' and itemname='crtpsjspaybillbysale' 

  if @fzpamount<>0 and @crtpsjspaybillbysale<>'1' and @shop_orgtype='1'
  begin
    insert into t_distpay_bill(billno,billtype,shopcode,orgcode,rzdate,sqauremode,zpamount,jsamount,zramount,cost,netcost,total,isfinished)
    select billno,case when @fzpamount>=0 then '0' else '1' end as billtype,
          shopcode,orgcode,@scurdate,sqauremode,zpamount,0,0,cost,netcost,total,'0'
    from t_dist_head 
    where billno=@psbillno
    if @@error<>0
    begin
      set @psmsg='生成门店结算单信息失败'
      return(-1)
    end
    update t_shop set zpamount=case when @fzpamount>0 then zpamount+@fzpamount else zpamount end,
                      zpthamount=case when @fzpamount<0 then zpthamount-@fzpamount else zpthamount end 
    where orgcode=@sshopcode
    if @@error<>0
    begin
      set @psmsg='更新门店配送收款信息失败'
      return(-1)
    end
  end

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

  if (@fzpamount<>0) and (@ssameaccorg <> '1')
  begin
    insert into t_pay_bill(orgcode,billtype,billno,bizdate,supcode,sqauremode,cost,paydate,jscost,zrcost,isfinished,total,contrno,finacode)
    select shopcode,case when zpamount > 0 then '6' else '7' end as billtype,billno,lrdate,'C2000',sqauremode,zpamount,paydate,0,0,
	       case when zpamount=0 then '1' else '0' end,total,'',@finacode
    from t_dist_head where billno=@psbillno
  end

  if @fzpcount>0
    update a set remark=left(b.barcode,200) from t_dist_body a,
      (select plucode,stuff((select ','+barcode from t_barcode_ref where plucode=t.plucode order by barcode for xml path('')),1,1,'') as barcode
      from t_barcode_ref t group by plucode) b
      where a.plucode=b.plucode and a.billno=@psbillno and isnull(b.barcode,'')<>''
  
  set @psmsg=''
  return(1)
end	
go