if exists(select * from sysobjects where name='p_rz_prepare_xh' and xtype='p')
  drop procedure p_rz_prepare_xh
go
create procedure p_rz_prepare_xh
(
  @psorgcode varchar(10),
  @psbillno varchar(20),
  @psparams varchar(200),
  @psmsg varchar(2000) out
)
as
begin
  declare @sorgcode varchar(10)
  declare @scustcode varchar(15)
  declare @ssqauremode varchar(1)
  declare @syqflag varchar(1)
  declare @sstr varchar(1000)
  declare @ismngcredit varchar(1)
  declare @billamount numeric(19,4)
  declare @pfamount numeric(19,4)
  declare @prepay numeric(19,4)
  declare @ystotal numeric(19,4)
  declare @spackcode varchar(20)
  declare @splucode varchar(20)
  declare @fcounts numeric(19,4)
  declare @splutype varchar(1)
  declare @fkycount numeric(19,4)
  declare @fbillcounts numeric(19,4)
  declare @serrmsg varchar(200)
  declare @sbillno varchar(20)
  declare @splace varchar(1)
  declare @irtncode int
  declare @pfquoteno varchar(20)
  declare @pfquotemultsend varchar(1)

  set @psmsg=''

  select @sbillno=refbillno,@splace=place,@sorgcode=orgcode,@scustcode=custcode,@ssqauremode=sqauremode,@ystotal=ystotal,@pfquoteno=pfquoteno from t_whsale_head where billno=@psbillno

  select @pfquotemultsend=isnull(itemvalue,'1') from t_syscfg where section='whlsale' and itemname='pfquotemultsend'

  select @syqflag=yqflag,@pfamount=pftotal-thtotal-fktotal,@prepay=prepay from t_customer where custcode=@scustcode
  if @@rowcount=0 
  begin
    set @psmsg='客户已经不存在'
    return(-1)
  end

  if (@ssqauremode='1') and (@syqflag='0')
  begin
    set @psmsg='客户不允许延期收款'
    return(-1)
  end

  select @sstr=coalesce(@sstr+char(13),'')+rtrim(d.plucode)+'-'+rtrim(d.pluname) from t_whsale_body d 
    where d.billno=@psbillno and not exists(select plucode from t_plu where plucode=d.plucode and depcode=d.depcode) 
  if isnull(@sstr,'')<>''
  begin
    set @psmsg='已经不存在或部门发生了变化'+char(13)+@sstr
    return(-1)
  end 

  if exists(select * from t_whsale_head where isnull(@sbillno,'')<>'' and refbillno=@sbillno and isnull(rzdate,'')<>'')
  begin
    set @psmsg='该批发销售单对应的要货单存在已经记账的批发销售单，请检查'
    return(-1)
  end

  if @pfquotemultsend<>'1' and isnull(@pfquoteno,'')<>''
  begin
    if exists(select 1 from t_whsale_head where isnull(@pfquoteno,'')<>'' and pfquoteno=@pfquoteno and isnull(rzdate,'')<>'')
    begin
      set @psmsg='该批发销售单对应的批发订单已发货，请检查'
      return(-1)
    end
  end

  exec @irtncode=p_check_plu_stock_rz @sorgcode,@psbillno,@splace,'t_whsale_head','t_whsale_body',@psparams,@psmsg output
  if @irtncode=-1 
    return(-1)

  if @psmsg<>''
    return(-2) 

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

if exists(select * from sysobjects where name='p_rz_xh' and xtype='p')
  drop procedure p_rz_xh
go
create procedure p_rz_xh
(					
  @psorgcode varchar(10),
  @psbillno varchar(20),
  @psparams varchar(200),
  @psmsg varchar(2000) out
)
as 
begin
  declare @sorgcode varchar(10)
  declare @scustcode varchar(15)
  declare @szdrcode varchar(6)
  declare @ssqauremode varchar(1)
  declare @splace varchar(1)
  declare @srzdate varchar(10)
  declare @ispfpriceflag varchar(1)
  declare @org_orgtype varchar(1)
  declare @org_isdbdeploy varchar(1)
  declare @scurver varchar(1)
  declare @scurdate varchar(10)
  declare @scurtime varchar(8)
  declare @custbook_name varchar(20)
  declare @sedition varchar(20)
  declare @ssql varchar(8000)
  declare @sinfo varchar(8000)
  declare @irtn int
  declare @detail_jtaxrate int
  declare @detail_cost numeric(19,4)
  declare @detail_netcost numeric(19,4)
  declare @detail_total numeric(19,4)
  declare @detail_plucode varchar(20)
  declare @detail_depcode varchar(10)
  declare @detail_pfprice numeric(19,4)
  declare @detail_zrtotal numeric(19,4)
  declare @detail_pftotal numeric(19,4)
  declare @detail_counts numeric(19,4)
  declare @detail_zpcounts numeric(19,4)
  declare @detail_serialno numeric(19,0)
  declare @detail_plutype varchar(1)
  declare @detail_mngstock varchar(1)
  declare @detail_jymode varchar(1)
  declare @detail_ornpftotal numeric(19,4)
  declare @detail_ystotal numeric(19,4)
  declare @detail_pludsctotal numeric(19,4)
  declare @detail_dsccode varchar(20)
  declare @detail_authno varchar(6)
  declare @detail_sharetotal numeric(19,4)
  declare @goods_netjprice numeric(19,4)
  declare @goods_jprice numeric(19,4)
  declare @goods_price numeric(19,4)
  declare @stock_gcount numeric(19,4)
  declare @stock_ccount numeric(19,4)
  declare @stock_cost numeric(19,4)
  declare @stock_netcost numeric(19,4)
  declare @stock_total numeric(19,4)
  declare @stock_tzamount numeric(19,4)
  declare @new_cost numeric(19,4)
  declare @new_netcost numeric(19,4)
  declare @new_total numeric(19,4)
  declare @stock_csgcount numeric(19,4)
  declare @stock_csccount numeric(19,4)
  declare @csdetail_ccounts numeric(19,4)
  declare @csdetail_plucode varchar(20)
  declare @csdetail_colorcode varchar(10)
  declare @csdetail_sizecode varchar(10)
  declare @csdetail_zpcounts numeric(19,4)
  declare @packplu_plucode varchar(20)
  declare @packplu_pluname varchar(40)
  declare @packplu_barcode varchar(20)
  declare @packplu_depcode varchar(10)
  declare @packplu_depname varchar(20)
  declare @packplu_cargono varchar(20)
  declare @packplu_spec varchar(40)
  declare @packplu_prodarea varchar(30)
  declare @packplu_unit varchar(4)
  declare @packplu_jtaxrate int
  declare @packplu_price numeric(19,4)
  declare @packplu_jprice numeric(19,4)
  declare @packplu_netjprice numeric(19,4)
  declare @packplu_counts numeric(19,4)
  declare @packplu_zpcounts numeric(19,4)
  declare @packplu_plutotal numeric(19,4)
  declare @packplu_pftotal numeric(19,4)
  declare @packplu_zrtotal numeric(19,4)
  declare @packplu_pfprice numeric(19,4)
  declare @packplu_pfcounts numeric(19,4)
  declare @packplu_ornpftotal numeric(19,4)
  declare @packplu_ornpfprice numeric(19,4)
  declare @packplu_ystotal numeric(19,4)
  declare @packplu_pludsctotal numeric(19,4)
  declare @packplu_sharetotal numeric(19,4)
  declare @fscscount numeric(19,4)  
  declare @packplucount int
  declare @packplutotalsum numeric(19,4)
  declare @i int
  declare @sumpftotal numeric(19,4)
  declare @sumzrtotal numeric(19,4)
  declare @sumornpftotal numeric(19,4)
  declare @sumystotal numeric(19,4)
  declare @sumpludsctotal numeric(19,4)
  declare @sumsharetotal numeric(19,4)
  declare @fscount 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 @billystotal numeric(19,4)
  declare @billno varchar(20)
  declare @pfquoteno varchar(20)
  declare @goods_iszfcode varchar(1)
  declare @packplu_iszfcode varchar(1)
  declare @packplu_zmcode varchar(20)

  select @sedition=itemvalue from t_syscfg where section='SYSTEM' and itemname='Edition'
  select @sorgcode=orgcode,@scustcode=custcode,@splace=place,@szdrcode=zdrcode,@ssqauremode=sqauremode,@srzdate=isnull(rzdate,''),@billno=refbillno,@pfquoteno=pfquoteno
    from t_whsale_head where billno=@psbillno

  select @ispfpriceflag=pfpriceflag from t_customer where custcode=@scustcode
  select @org_orgtype=orgtype,@org_isdbdeploy=isdbdeploy from t_shop where orgcode=@sorgcode
  select @scurver=itemvalue from t_syscfg where section='SYSTEM' and itemname='VER'
  select @scurdate=convert(varchar(100),getdate(),120),@scurtime=convert(varchar(100),getdate(),108)
  set @custbook_name='t_whcbk_yyyymm'

  declare cur cursor for select d.plucode,d.serialno,g.plutype,d.counts,d.zpcount,d.pftotal,d.zrtotal,d.ornpftotal,d.ystotal,
                                d.pludsctotal,d.dsccode,d.authno,d.sharetotal,isnull(g.iszfcode,'0')
                           from t_whsale_body d,t_plu g where billno=@psbillno and d.plucode=g.plucode order by d.plucode,d.serialno
  open cur
  fetch next from cur into @detail_plucode,@detail_serialno,@detail_plutype,@detail_counts,@detail_zpcounts,@detail_pftotal,@detail_zrtotal
                         ,@detail_ornpftotal,@detail_ystotal,@detail_pludsctotal,@detail_dsccode,@detail_authno,@detail_sharetotal,@goods_iszfcode
  while @@fetch_status=0
  begin
    if (@detail_plutype<>'3') and (@goods_iszfcode<>'2')
    begin	
       insert into t_whsale_body2(billno,serialno,plucode,packcode,barcode,pluname,cargono,spec,
                                  prodarea,punit,pcount,unit,price,jprice,netjprice,ornpfprice,zkrate,pfprice,counts,zpcount,
                                  pftotal,zrtotal,cost,netcost,total,jtaxrate,tag,depname,depcode,remark,itemcnt,sglcount,
                                  ornpftotal,ystotal,pludsctotal,dsccode,authno,sharetotal)
         select billno,serialno,plucode,'' packcode,barcode,pluname,cargono,spec,
                prodarea,punit,pcount,unit,price,jprice,netjprice,ornpfprice,zkrate,pfprice,counts,zpcount,
                pftotal,zrtotal,cost,netcost,total,jtaxrate,tag,depname,depcode,remark,itemcnt,sglcount,
                ornpftotal,ystotal,pludsctotal,dsccode,authno,sharetotal
           from t_whsale_body d
          where billno=@psbillno and plucode=@detail_plucode and serialno=@detail_serialno
    end
    else if (@detail_plutype<>'3') and  (@goods_iszfcode='2')
    begin
      insert into t_whsale_body2(billno,serialno,plucode,packcode,barcode,pluname,cargono,spec,
                                 prodarea,punit,pcount,unit,price,jprice,netjprice,ornpfprice,zkrate,pfprice,counts,zpcount,
                                 pftotal,zrtotal,cost,netcost,total,jtaxrate,tag,depname,depcode,remark,itemcnt,sglcount,
                                 ornpftotal,ystotal,pludsctotal,dsccode,authno,sharetotal)
        select billno,serialno,p.innercode,'' packcode,barcode,pluname,cargono,spec,
               prodarea,punit,pcount,unit,price,jprice,netjprice,ornpfprice,zkrate,pfprice,d.counts,zpcount,
               pftotal,zrtotal,cost,netcost,total,jtaxrate,tag,depname,depcode,remark,itemcnt,sglcount,
               ornpftotal,ystotal,pludsctotal,dsccode,authno,sharetotal
          from t_whsale_body d,(select plucode,innercode from t_plu where iszfcode='2') p 
         where billno=@psbillno and d.plucode=@detail_plucode and serialno=@detail_serialno and p.plucode=d.plucode 
    end
    else if @detail_plutype='3'
    begin      
      set @i=1
      set @sumpftotal=0
      set @sumzrtotal=0
      set @sumornpftotal=0
      set @sumystotal=0
      set @sumpludsctotal=0
      set @sumsharetotal=0

      select @packplucount=count(*),@packplutotalsum=sum(pluprice*counts) from t_plupack where packcode=@detail_plucode and packtype='0'	

      declare curpackplu cursor static for select g.plucode,g.pluname,g.barcode,g.depcode,d.depname,g.cargono,g.spec,g.prodarea,g.unit,g.jtaxrate,o.price,
                                                  case when isnull(iszfcode,'0')='2' then g.jprice else o.jprice end as jprice,
                                                  case when isnull(iszfcode,'0')='2' then g.netjprice else o.netjprice end as netjprice,
                                                  p.counts,(p.counts * p.pluprice),isnull(iszfcode,'0')  
                                             from t_plupack p,t_plu_org o,t_plu g left join t_dept d on g.depcode=d.depcode
                                            where o.orgcode=@sorgcode and p.packcode=@detail_plucode and p.plucode=g.plucode and p.plucode=o.plucode and p.packtype='0'           
      open curpackplu
      
      --set @packplucount=@@cursor_rows --包内商品总数，注意这里使用静态游标以获得游标记录数
      fetch next from curpackplu into @packplu_plucode,@packplu_pluname,@packplu_barcode,@packplu_depcode,@packplu_depname,
                                      @packplu_cargono,@packplu_spec,@packplu_prodarea,@packplu_unit,@packplu_jtaxrate,
                                      @packplu_price,@packplu_jprice,@packplu_netjprice,@packplu_counts,@packplu_plutotal,@packplu_iszfcode
      while @@fetch_status=0
      begin
        if @i<@packplucount
        begin
          set @packplu_pftotal=round(@detail_pftotal*@packplu_plutotal/@packplutotalsum,2)
          set @packplu_zrtotal=round(@detail_zrtotal*@packplu_plutotal/@packplutotalsum,2)
          set @packplu_ornpftotal=round(@detail_ornpftotal*@packplu_plutotal/@packplutotalsum,2)
          set @packplu_ystotal=round(@detail_ystotal*@packplu_plutotal/@packplutotalsum,2)
          set @packplu_pludsctotal=round(@detail_pludsctotal*@packplu_plutotal/@packplutotalsum,2)
          set @packplu_sharetotal=round(@detail_sharetotal*@packplu_plutotal/@packplutotalsum,2)
          set @sumpftotal=@sumpftotal+@packplu_pftotal
          set @sumzrtotal=@sumzrtotal+@packplu_zrtotal
          set @sumornpftotal=@sumornpftotal+@packplu_ornpftotal
          set @sumystotal=@sumystotal+@packplu_ystotal
          set @sumpludsctotal=@sumpludsctotal+@packplu_pludsctotal
          set @sumsharetotal=@sumsharetotal+@packplu_sharetotal
        end
        else
        begin
          set @packplu_pftotal=round(@detail_pftotal-@sumpftotal,2)
          set @packplu_zrtotal=round(@detail_zrtotal-@sumzrtotal,2)
          set @packplu_ornpftotal=round(@detail_ornpftotal-@sumornpftotal,2)
          set @packplu_ystotal=round(@detail_ystotal-@sumystotal,2)
          set @packplu_pludsctotal=round(@detail_pludsctotal-@sumpludsctotal,2) 
          set @packplu_sharetotal=round(@detail_sharetotal-@sumsharetotal,2)
        end
        set @packplu_pfcounts=@packplu_counts*@detail_counts
        set @packplu_zpcounts=@packplu_counts*@detail_zpcounts
        if @packplu_pfcounts<>0 
        begin
          set @packplu_pfprice=round(@packplu_pftotal/@packplu_pfcounts,4)
          set @packplu_ornpfprice=round(@packplu_ornpftotal/@packplu_pfcounts,4)
        end
        else
        begin
          set @packplu_pfprice=0
          set @packplu_ornpfprice=0
        end

        if @packplu_iszfcode='2' 
        begin  
          select @packplu_zmcode=innercode from t_plu where plucode=@packplu_plucode
          if exists (select plucode from t_whsale_body2 where billno=@psbillno and serialno=@detail_serialno and plucode=@packplu_zmcode)
          begin
            update t_whsale_body2 set counts=counts+@packplu_pfcounts,zpcount=zpcount+@packplu_zpcounts,pftotal=pftotal+@packplu_pftotal,
                                      zrtotal=zrtotal+@packplu_zrtotal,cost=cost+round(@packplu_jprice*@packplu_pfcounts,2),netcost=netcost+round(@packplu_netjprice*@packplu_pfcounts,2),
                                      total=total+round(@packplu_price*@packplu_pfcounts,2),sglcount=sglcount+@packplu_pfcounts,ornpftotal=ornpftotal+@packplu_ornpftotal,
                                      ystotal=ystotal+@packplu_ystotal,pludsctotal=pludsctotal+@packplu_pludsctotal,sharetotal=sharetotal+@packplu_sharetotal 
             where billno=@psbillno and serialno=@detail_serialno and plucode=@packplu_zmcode
            set @i=@i+1
          end
          else
          begin         
            insert into t_whsale_body2(billno,serialno,plucode,packcode,barcode,pluname,cargono,spec,
                                       prodarea,punit,pcount,unit,price,jprice,netjprice,zkrate,pfprice,counts,zpcount,
                                       pftotal,zrtotal,cost,netcost,total,jtaxrate,tag,depname,depcode,remark,itemcnt,sglcount,
                                       ornpftotal,ornpfprice,ystotal,pludsctotal,dsccode,authno,sharetotal) 
              values(@psbillno,@detail_serialno,@packplu_zmcode,@detail_plucode,@packplu_barcode,@packplu_pluname,@packplu_cargono,@packplu_spec,
                     @packplu_prodarea,'',0,@packplu_unit,@packplu_price,@packplu_jprice,@packplu_netjprice,0,@packplu_pfprice,
                     @packplu_pfcounts,@packplu_zpcounts,@packplu_pftotal,@packplu_zrtotal,
                     round(@packplu_jprice*@packplu_pfcounts,2),round(round(@packplu_jprice*@packplu_pfcounts,2)/(1+@packplu_jtaxrate/100.0),2),round(@packplu_price*@packplu_pfcounts,2),
                     @packplu_jtaxrate,'',@packplu_depname,@packplu_depcode,'',0,@packplu_pfcounts,
                     @packplu_ornpftotal,@packplu_ornpfprice,@packplu_ystotal,@packplu_pludsctotal,@detail_dsccode,@detail_authno,@packplu_sharetotal)
            set @i=@i+1
          end
        end
        else
        begin                  
            insert into t_whsale_body2(billno,serialno,plucode,packcode,barcode,pluname,cargono,spec,
                           prodarea,punit,pcount,unit,price,jprice,netjprice,zkrate,pfprice,counts,zpcount,
                           pftotal,zrtotal,cost,netcost,total,jtaxrate,tag,depname,depcode,remark,itemcnt,sglcount,
                           ornpftotal,ornpfprice,ystotal,pludsctotal,dsccode,authno,sharetotal)
            values(@psbillno,@detail_serialno,@packplu_plucode,@detail_plucode,@packplu_barcode,@packplu_pluname,@packplu_cargono,@packplu_spec,
                           @packplu_prodarea,'',0,@packplu_unit,@packplu_price,@packplu_jprice,@packplu_netjprice,0,@packplu_pfprice,
                           @packplu_pfcounts,@packplu_zpcounts,@packplu_pftotal,@packplu_zrtotal,
                           round(@packplu_jprice*@packplu_pfcounts,2),round(round(@packplu_jprice*@packplu_pfcounts,2)/(1+@packplu_jtaxrate/100.0),2),round(@packplu_price*@packplu_pfcounts,2),
                           @packplu_jtaxrate,'',@packplu_depname,@packplu_depcode,'',0,@packplu_pfcounts,
                           @packplu_ornpftotal,@packplu_ornpfprice,@packplu_ystotal,@packplu_pludsctotal,@detail_dsccode,@detail_authno,@packplu_sharetotal)
            set @i=@i+1
        end
        fetch next from curpackplu into @packplu_plucode,@packplu_pluname,@packplu_barcode,@packplu_depcode,@packplu_depname,
                                        @packplu_cargono,@packplu_spec,@packplu_prodarea,@packplu_unit,@packplu_jtaxrate,
                                        @packplu_price,@packplu_jprice,@packplu_netjprice,@packplu_counts,@packplu_plutotal,@packplu_iszfcode
      end
      close curpackplu
      deallocate curpackplu   	

      update a set cost=b.cost,jprice=round(b.cost/(a.counts+zpcount),4),
                   netcost=b.netcost,netjprice=round(b.netcost/(a.counts+zpcount),4),
                   total=b.total,price=round(b.total/(a.counts+zpcount),2)
        from t_whsale_body a,(select billno,serialno,packcode,sum(cost) cost,sum(netcost) netcost,sum(total) total 
             from t_whsale_body2 where billno=@psbillno and isnull(packcode,'')<>'' group by billno,serialno,packcode) b  
       where a.billno=@psbillno and a.billno=b.billno and a.plucode=b.packcode and a.serialno=b.serialno       	
    end
    --else
    --begin 
    --  set @psmsg='单据表体中商品类型不正确'
    --  return(-1)       		
    --end

    fetch next from cur into @detail_plucode,@detail_serialno,@detail_plutype,@detail_counts,@detail_zpcounts,@detail_pftotal,@detail_zrtotal,
                             @detail_ornpftotal,@detail_ystotal,@detail_pludsctotal,@detail_dsccode,@detail_authno,@detail_sharetotal,@goods_iszfcode
  end
  close cur
  deallocate cur  

  declare cur cursor for select d.plucode,d.serialno,d.counts,d.zpcount,d.pfprice,d.pftotal,d.depcode,d.jtaxrate,o.mngstock,isnull(v2.jymode,v1.jymode) as jymode
                          from t_whsale_body2 d join t_plu_org o  on d.plucode = o.plucode and o.orgcode = @sorgcode
						      join t_plu p on d.plucode = p.plucode
						      left join t_supplier v1 on p.supcode = v1.supcode
							  left join dbo.t_supplier v2 on o.supcode = v2.supcode 
						 where billno=@psbillno 
						 order by d.plucode,d.serialno
  open cur
  fetch next from cur into @detail_plucode,@detail_serialno,@detail_counts,@detail_zpcounts,@detail_pfprice,@detail_pftotal,@detail_depcode,
                           @detail_jtaxrate,@detail_mngstock,@detail_jymode
  while @@fetch_status=0
  begin  
    set @fscount=@detail_counts+@detail_zpcounts		
    
    select @goods_netjprice=netjprice,@goods_jprice=jprice,@goods_price=price,
           @stock_gcount=gcount,@stock_ccount=ccount,@stock_cost=cost,@stock_netcost=netcost,
           @stock_total=total,@stock_tzamount=tzamount 
    from t_plu_org where plucode=@detail_plucode and orgcode=@sorgcode
    
    if @scurver='0'
    begin
      if @splace='0'
        set @stock_gcount=@stock_gcount-@fscount
      else
        set @stock_ccount=@stock_ccount-@fscount
    end    
    else
    begin 
      if @org_orgtype='2' 	
        set @stock_ccount=@stock_ccount-@fscount
      else if (@org_orgtype<>'2' and @org_isdbdeploy='1')
        set @stock_gcount=@stock_gcount-@fscount    
    end
    
    set @jccount=@stock_gcount+@stock_ccount
    
    set @new_cost=round(@goods_jprice*@jccount,2) 
    set @new_netcost=round(@goods_netjprice*@jccount,2)
    set @new_total=round(@goods_price*@jccount,2) 
    
    set @detail_cost=@stock_cost-@new_cost
    set @detail_netcost=@stock_netcost-@new_netcost;
    set @detail_total=@stock_total-@new_total    

    set @jccost=@new_cost+@stock_tzamount
    set @jcnetcost=@new_netcost+@stock_tzamount/(1+@detail_jtaxrate/100.00)
    set @jctotal=@new_total

    update t_plu_org set ccount=@stock_ccount,gcount=@stock_gcount,cost=@new_cost,netcost=@new_netcost,total=@new_total
     where plucode=@detail_plucode and orgcode=@sorgcode and @detail_mngstock = '1'
    
    update t_whsale_body2 set jprice=@goods_jprice,netjprice=@goods_netjprice,price=@goods_price,
           cost=@detail_cost,netcost=@detail_netcost,total=@detail_total
     where billno=@psbillno and serialno=@detail_serialno and plucode=@detail_plucode

    update a set cost=b.cost,jprice=round(b.cost/(a.counts+zpcount),4),
                   netcost=b.netcost,netjprice=round(b.netcost/(a.counts+zpcount),4),
                   total=b.total,price=round(b.total/(a.counts+zpcount),2)
    from t_whsale_body a,(select billno,serialno,packcode,sum(cost) cost,sum(netcost) netcost,sum(total) total 
         from t_whsale_body2 where billno=@psbillno and isnull(packcode,'')<>'' group by billno,serialno,packcode) b  
    where a.billno=@psbillno and a.billno=b.billno and a.plucode=b.packcode and a.serialno=b.serialno
    if @detail_jymode = '2'
	begin
	  select @goods_jprice = 0,@goods_netjprice=0,@detail_cost=0,@detail_netcost=0,@detail_total=0,@jccost=0,@jcnetcost=0,@jctotal=0
	  if @detail_mngstock = '0'
	  begin
	    select @fscount=0,@jccount =0
	  end
	end
    exec p_create_gbk_data @scurdate,@scurtime,@detail_plucode,@detail_depcode,@psbillno,'A',
                           @goods_jprice,@goods_netjprice,@goods_price,@fscount,@detail_cost,@detail_netcost,@detail_total,
                           @jccount,@jccost,@jcnetcost,@jctotal,@sorgcode,'','',@sinfo out

    fetch next from cur into @detail_plucode,@detail_serialno,@detail_counts,@detail_zpcounts,@detail_pfprice,@detail_pftotal,@detail_depcode,@detail_jtaxrate,@detail_mngstock,@detail_jymode   
  end
  close cur
  deallocate cur 
  if @sedition='4'
  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_whsale_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=case when @splace='0' then a.gcount-b.ccounts-b.zpcounts else a.gcount end,
                 a.ccount=case when @splace='0' then a.ccount else a.ccount-b.ccounts-b.zpcounts end
    from t_plu_org_cs a,(select plucode,colorcode,sizecode,sum(ccounts) as ccounts,sum(zpcounts) as zpcounts from t_whsale_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
  end
  
  if (@ispfpriceflag='1')  
  begin	
    delete a from t_whsle_cust_info a,t_whsale_body b
     where b.billno=@psbillno and a.custcode=@scustcode and a.plucode=b.plucode and b.counts>0

    insert into t_whsle_cust_info (pfsaleno,custcode,rzdate,zdrcode,sqauremode,plucode,pfprice,pfcount,zpcount,pftotal) 
    select distinct b.billno,@scustcode,@scurdate,@szdrcode,@ssqauremode,b.plucode,b.pfprice,b.counts,b.zpcount,b.pftotal 
    from t_whsale_body b,(select plucode,max(serialno) as serialno from t_whsale_body where billno=@psbillno group by plucode) a 
    where b.billno=@psbillno and b.counts>0 and b.plucode=a.plucode and b.serialno=a.serialno
  end 
 
  update g set newxsdate=@scurdate,yjdate=case when yjdate is null or yjdate='' then @scurdate else yjdate end
    from t_plu_org g,t_whsale_head f,t_whsale_body2 d
   where f.billno=d.billno and f.billno=@psbillno and g.orgcode=f.orgcode and g.plucode=d.plucode and g.isright='1' and d.ystotal<>0       

  update a set cost=b.cost,netcost=b.netcost,total=b.total 
  from t_whsale_head a,(select sum(IsNull(cost,0)) cost,sum(IsNull(netcost,0)) netcost,sum(IsNUll(total,0)) total from t_whsale_body2 where billno=@psbillno) b
  where billno=@psbillno

  update a set a.pftotal=a.pftotal+(b.ystotal) from t_customer a,t_whsale_head b where a.custcode=b.custcode and a.custcode=@scustcode and b.billno=@psbillno

  set @ssql='insert into  '+@custbook_name+' (bookdate,booktime,bizdate,custcode,billtype,billno,username,ywname,ctotal,ysjytotal,prepayjytotal,orgcode,tag,accdate)'
           +' select '''+@scurdate+''','''+@scurtime+''',f.bizdate,f.custcode,''0'',f.billno,f.username,f.zdrname,(f.ystotal) as ctotal,
                     c.pftotal-c.thtotal-c.fktotal as ysjytotal,prepay as prepayjytotal,'''+@sorgcode+''' as orgcode,''0'' as tag,'''' as accdate  
                from t_customer c,t_whsale_head f
               where c.custcode=f.custcode and c.custcode='''+@scustcode+''' and f.billno='''+@psbillno+'''' 
  exec(@ssql)
  
  select @billystotal=b.ystotal from t_customer a,t_whsale_head b where a.custcode=b.custcode and b.billno=@psbillno

  update t_whsale_head set rzdate=@scurdate,rztime=@scurtime where billno=@psbillno
  update t_whsale_head set isfinished='1' where billno=@psbillno and ystotal=0

  if isnull(@billno,'')<>''
    update t_ask_head set state='1' where billno=@billno 

  if isnull(@pfquoteno,'')<>''
    update t_whquote_head set state='1' where billno=@pfquoteno

  --exec p_sup_stock_out @psbillno,'XH',@psmsg
  set @psmsg=''
  return(1)
end	
go
