if exists(select * from sysobjects where name='p_rz_prepare_jz' and xtype='p')
  drop procedure p_rz_prepare_jz
go
create procedure p_rz_prepare_jz
(					
  @psorgcode varchar(10),
  @psbillno varchar(20),
  @psparams varchar(200),
  @psmsg varchar(2000) out
)
as 
begin
  set @psmsg=''
  return(1) 
end
go

if exists(select * from sysobjects where name='p_rz_jz' and xtype='p')
  drop procedure p_rz_jz
go
create procedure p_rz_jz
(					
  @psorgcode varchar(10),
  @psbillno varchar(20),
  @psparams varchar(200),
  @psmsg varchar(2000) out
)
as
  declare @sorgcode varchar(10) 
  declare @sisdbdeploy varchar(1)
  declare @sorgtype varchar(1)
  declare @sver varchar(1)
  declare @sdate varchar(10)
  declare @bookdate varchar(10)
  declare @booktime varchar(8)
  declare @ssql varchar(8000)
begin
  set @psmsg=''
  select @sver=itemvalue from t_syscfg where section='SYSTEM' and itemname='VER'
  select @sorgcode=orgcode from t_damage_head where billno=@psbillno
  select @sisdbdeploy=isdbdeploy,@sorgtype=orgtype from t_shop where orgcode=@sorgcode
  select @sdate=convert(varchar(10),getdate(),20)
  if @@error <> 0
  begin
    set @psmsg='商品减值单记账初始化变量失败'	
    return -1
  end
  
  if @sver='0' or @sisdbdeploy='1'
  begin
    if ((@sver='0') or (@sver='1' and @sisdbdeploy='1'))
    begin
      if exists(select name from tempdb..sysobjects where name='##tmpjz_d' and xtype='u')
         drop table ##tmpjz_d
      create table ##tmpjz_d
         (billno varchar(20),plucode varchar(20),jprice numeric(19,4),netjprice numeric(19,4),newjprice numeric(19,4),newnetjprice numeric(19,4),
          tzcount numeric(19,4),cjcost numeric(19,4),netcjcost numeric(19,4),stock_counts numeric(19,4),stock_cost numeric(19,4),
          tzcjcost numeric(19,4),nettzcjcost numeric(19,4),g_jtaxrate numeric(19,4),g_jprice numeric(19,4),g_tzamount numeric(19,4))
        
     insert into ##tmpjz_d(billno,plucode,jprice,netjprice,newjprice,newnetjprice,tzcount,cjcost,netcjcost,
                             stock_counts,stock_cost,tzcjcost,nettzcjcost,g_jtaxrate,g_jprice,g_tzamount)
     select billno,d.plucode,d.jprice,d.netjprice,d.newjprice,d.newnetjprice,d.jzcounts,d.cjcost,d.netcjcost,
                             o.gcount+o.ccount,o.cost,0 tzcjcost,0 tznetcjcost,g.jtaxrate,o.jprice,o.tzamount
     from t_damage_body d join t_plu_org o on d.plucode=o.plucode join t_plu g on d.plucode=g.plucode 
     where d.billno=@psbillno and o.orgcode=@sorgcode

     update d set jprice=o.jprice,
                    netjprice=o.netjprice,
                    jzcounts=o.gcount+o.ccount, 
                    cost=o.cost,
                    netcost=o.netcost,
                    cjcost=round(d.newjprice*(o.gcount+o.ccount)-o.cost,2),
                    netcjcost=round(d.newnetjprice*(o.gcount+o.ccount)-o.netcost,2), 
                    newcost=round(d.newjprice*(o.gcount+o.ccount),2),
                    newnetcost=round(d.newnetjprice*(o.gcount+o.ccount),2) 
        from t_damage_body d join t_plu_org o on d.plucode=o.plucode and d.billno=@psbillno and o.orgcode=@sorgcode
        where o.isright='1'

     --update g set jprice=d.newjprice,
     --               netjprice=d.newnetjprice
     --   from t_plu g join t_damage_body d on g.plucode=d.plucode and d.billno=@psbillno

     update o set jprice=d.newjprice,
                    netjprice=d.newnetjprice, 
                    --ojprice=d.newjprice,
                    --hjprice=case when hjprice>d.newjprice then hjprice else d.newjprice end,
                    --ljprice=case when ljprice<d.newjprice then ljprice else d.newjprice end,
                    cost=round(d.newjprice*(o.gcount+o.ccount),2),
                    netcost=o.netcost+round(tmp.g_tzamount/(1+g_jtaxrate/100.00),2)+d.netcjcost-round(o.tzamount/(1+g_jtaxrate/100.00),2)
        from t_damage_body d join t_plu_org o on d.plucode=o.plucode and d.billno=@psbillno and o.orgcode=@sorgcode
        join ##tmpjz_d tmp on d.plucode=tmp.plucode
        where o.isright='1'

      select @bookdate=convert(varchar(10),getdate(),20),@booktime=convert(varchar(8),getdate(),8)
  
      update t_gbk_yyyymm set jprice=round((fscost+d.cjcost)/(fscount+0),4),
                              netjprice=round((fsnetcost+d.netcjcost)/(fscount+0),4),
                              price=o.price,
                              fscost=fscost+d.cjcost,
                              fsnetcost=fsnetcost+d.netcjcost,
                              jccount=o.gcount+o.ccount,
                              jccost=o.cost+o.tzamount,
                              jcnetcost=o.netcost+round(o.tzamount/(1+d.jtaxrate/100.00),2),
                              jctotal=o.total,
                              tag='',
                              accdate=''
        from  t_gbk_yyyymm gbk join t_damage_body d on gbk.plucode=d.plucode and d.billno=@psbillno
              join t_plu_org o on d.plucode=o.plucode and o.orgcode=@sorgcode
       where bookdate=@bookdate and booktime=@booktime and gbk.plucode=d.plucode and gbk.depcode=d.depcode 
             and d.billno=@psbillno and billtype='9' and gbk.orgcode=@sorgcode
      if @@error<>0
      begin
        set @psmsg='更新商品台账失败'
        return -1
      end 
      
      insert into t_gbk_yyyymm
               (bookdate,booktime,plucode,depcode,billno,billtype,jprice,netjprice,price,fscount,
               fscost,fsnetcost,fstotal,jccount,jccost,jcnetcost,jctotal,tag,orgcode,accdate)
      select @bookdate,@booktime,d.plucode,d.depcode,@psbillno,'9',o.jprice,o.netjprice,o.price,0,d.cjcost,d.netcjcost,0,
             o.gcount+o.ccount,o.cost+o.tzamount,o.netcost+round(o.tzamount/(1+d.jtaxrate/100.00),2),o.total,'',@sorgcode,''
        from t_damage_body d join t_plu_org o on d.plucode=o.plucode and d.billno=@psbillno and o.orgcode=@sorgcode
       where not exists(select plucode from t_gbk_yyyymm where bookdate=@bookdate and booktime=@booktime and plucode=d.plucode
             and depcode=d.depcode and  billno=@psbillno and billtype='9' and orgcode=@sorgcode)
      if @@error<>0
      begin
        set @psmsg='插入商品台账失败'
        return -1
      end
       
      update a set cost=b.cost,
                    netcost=b.netcost,
                    cjcost=b.cjcost,
                    netcjcost=b.netcjcost,
                    newcost=b.newcost,
                    newnetcost=b.newnetcost,
                    jzcounts=b.jzcounts 
        from t_damage_head a,
            (select sum(cost) cost,sum(netcost) netcost,sum(cjcost) cjcost,sum(netcjcost) netcjcost,
                    sum(newcost) newcost,sum(newnetcost) newnetcost,sum(jzcounts) jzcounts 
                from t_damage_body where billno=@psbillno) b 
        where (a.billno=@psbillno)
    end
  end


  if exists(select a.plucode from t_damage_body a,t_plu b where a.plucode=b.plucode and isnull(b.iszfcode,'0')='1' and a.billno=@psbillno)
  begin
    update a set jprice=b.jprice,netjprice=b.netjprice from t_plu_org a,
      (select plucode,jprice,netjprice,orgcode from t_plu_org where plucode in (select plucode from t_damage_body where billno=@psbillno)) b,
      (select plucode,innercode from t_plu where iszfcode='2') c 
    where a.plucode=c.plucode and b.plucode=c.innercode and a.orgcode=b.orgcode
  end

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

