if exists(select * from sysobjects where name='p_rz_bs' and xtype='p')
  drop procedure p_rz_bs
go
create procedure p_rz_bs
(					
  @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 @scurdate varchar(10)
  declare @scurtime varchar(8)
  declare @splace 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 @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)  

  select @sorgcode=orgcode,@splace=place from t_scrap_head where billno=@psbillno
  select @org_orgtype=orgtype from t_shop where orgcode=@sorgcode
  select @scurdate=convert(varchar(100),getdate(),23),@scurtime=convert(varchar(100),getdate(),108)

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

  declare cur cursor for select plucode,pluname,depcode,jtaxrate,cost as fscost,netcost as fsnetcost,
                                total as fstotal,counts,cost,netcost,jprice,netjprice,price 
                           from t_scrap_body where billno=@psbillno order by plucode,jprice
  open cur
  fetch next from cur into @detail_plucode,@detail_pluname,@detail_depcode,@detail_jtaxrate,@fscost,@fsnetcost,@fstotal,@detail_count,@detail_cost,@detail_netcost,@detail_jprice,@detail_netjprice,@detail_price
  while @@fetch_status=0
  begin
    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           
      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+']不存在'
      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,
                 @jccount=gcount+ccount-@detail_count,
                 @jccost=@new_cost+tzamount,
                 @jcnetcost=@new_netcost+round(tzamount/(1+@goods_jtaxrate/100.00),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

    exec p_create_gbk_data @scurdate,@scurtime,@detail_plucode,@detail_depcode,@psbillno,'S',@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_jtaxrate,@fscost,@fsnetcost,@fstotal,@detail_count,@detail_cost,@detail_netcost,@detail_jprice,@detail_netjprice,@detail_price
  end
  close cur
  deallocate cur

  update t_scrap_head set rzdate=@scurdate,rztime=@scurtime
  where billno=@psbillno
  --exec p_sup_stock_out @psbillno,'BS',@psmsg
  set @psmsg=''
  return(1)
end	
go

if exists(select * from sysobjects where name='p_rz_prepare_bs' and xtype='p')
  drop procedure p_rz_prepare_bs
go
create procedure p_rz_prepare_bs
(
  @psorgcode varchar(10),
  @psbillno varchar(20),
  @psparams varchar(200),
  @psmsg varchar(2000) out
)
as
begin
  declare @irtncode int
  declare @splace varchar(1)
  declare @serrmsg varchar(2000)

  select @splace=place from t_scrap_head where billno=@psbillno
  exec @irtncode=p_check_plu_stock_rz @psorgcode,@psbillno,@splace,'t_scrap_head','t_scrap_body',@psparams,@serrmsg output
  if @@error <> 0
  begin
    set @psmsg='判断是否负库存失败：未知错误'
    return(-1)
  end
  else if @irtncode=-1
  begin
    set @psmsg='判断是否负库存失败：'+isnull(@serrmsg,'')
    return(-1)
  end
  else if isnull(@serrmsg,'')<>''
  begin
    set @psmsg=@serrmsg
  end

  set @psmsg=''
  return(1)
end

go