if exists(select * from sysobjects where name='p_rz_ly' and xtype='p')
  drop procedure p_rz_ly
go
create procedure p_rz_ly
(					
  @psorgcode varchar(10),
  @psbillno varchar(20),
  @psparams varchar(200),
  @psmsg varchar(2000) out
)
as 
begin
  declare @sorgcode varchar(10)
  declare @org_orgtype varchar(1)
  declare @org_isdbdeploy varchar(1)
  declare @org_isconfirm varchar(1)
  declare @sedition varchar(20)
  declare @scurver varchar(1)
  declare @scurdate varchar(10)
  declare @scurtime varchar(8)
  declare @splace 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 @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 @costmode varchar(1)

  select @sedition=itemvalue from t_syscfg where section='SYSTEM' and itemname='Edition'
  select @sorgcode=orgcode,@splace=place from t_draw_head where billno=@psbillno
  select @org_orgtype=orgtype,@org_isdbdeploy=isdbdeploy,@org_isconfirm=isconfirm from t_shop where orgcode=@sorgcode
  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'
  select @costmode=isnull(itemvalue,'0') from t_syscfg where itemname='costmode'

  if @splace=''
  begin
    if @org_orgtype='2'
      set @splace='1'
    else
      set @splace='0'
  end

  if exists(select 1 from t_draw_body d,t_plu_org g 
            where d.plucode=g.plucode and billno=@psbillno and g.orgcode=@sorgcode and g.isright='1'
              and (case @costmode when '1' then d.jprice else d.netjprice end)<>(case @costmode when '1' then g.jprice else g.netjprice end))
  begin    
    update d set jprice=g.jprice,netjprice=g.netjprice,cost=round(g.jprice*counts,2),netcost=round(g.netjprice*counts,2)
      from t_draw_body d,t_plu_org g 
     where d.plucode=g.plucode and billno=@psbillno and g.orgcode=@sorgcode and g.isright='1'
       and (case @costmode when '1' then d.jprice else d.netjprice end)<>(case @costmode when '1' then g.jprice else g.netjprice end)
    
    update f set f.cost=d.cost,f.netcost=d.netcost
      from t_draw_head f,(select billno,sum(cost) cost,sum(netcost) netcost from t_draw_body group by billno) d 
     where f.billno=d.billno and f.billno=@psbillno
  end
  
  if exists(select 1 from t_draw_body d,t_plu_org g where d.plucode=g.plucode and billno=@psbillno and g.orgcode=@sorgcode and g.isright='1' and d.price<>g.price)
  begin    
    update d set price=g.price,total=round(counts*g.price,2)
      from t_draw_body d,t_plu_org g 
     where d.plucode=g.plucode and billno=@psbillno and g.orgcode=@sorgcode and g.isright='1' and d.price<>g.price
    
    update f set f.total=d.total
      from t_draw_head f,(select billno,sum(total) total from t_draw_body group by billno) d 
     where f.billno=d.billno and f.billno=@psbillno
  end

  declare cur cursor for select plucode,pluname,depcode,price,jtaxrate,cost as fscost,netcost as fsnetcost,total as fstotal,counts,cost,netcost,jprice,netjprice
                           from t_draw_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=o.jprice,
           @goods_netjprice=o.netjprice,
           @goods_cost=o.cost,
           @goods_netcost=o.netcost,
           @goods_tzamount=o.tzamount,
           @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 join t_plu g on o.plucode=g.plucode
    where o.plucode=@detail_plucode and o.orgcode=@sorgcode
    if @@rowcount=0 
    begin
      set @psmsg='商品['+@detail_pluname+']不存在'
      return(-1)
    end

    set @new_cost=@goods_cost-@detail_cost
    set @new_netcost=@goods_netcost-@detail_netcost
    set @new_tzamount=@goods_tzamount    
    
    update a set ccount=case when @splace='1' then ccount-@detail_count else ccount end,
                 gcount=case when @splace='0' then gcount-@detail_count else gcount end,
                 cost=@new_cost,netcost=@new_netcost,total=total-@fstotal,                 
                 @stock_plutype=b.plutype,
                 @jccount=gcount+ccount-@detail_count,
                 @jccost=@new_cost+tzamount,@jcnetcost=@new_netcost+round(tzamount/(1+@goods_jtaxrate/100),2),
                 @jctotal=total-@fstotal
      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'
    
    exec p_create_gbk_data @scurdate,@scurtime,@detail_plucode,@detail_depcode,@psbillno,'7',@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_draw_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_draw_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
  
  update t_draw_head set rzdate=@scurdate,rztime=@scurtime
  where billno=@psbillno

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