if exists(select * from sysobjects where name='p_rz_prepare_sj' and xtype='p')
  drop procedure p_rz_prepare_sj
go
create procedure p_rz_prepare_sj
(					
  @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_sj_circle_proc' and xtype='p')
  drop procedure p_rz_sj_circle_proc
go
create procedure p_rz_sj_circle_proc
(					
  @psorgcode varchar(10),
  @psbillno varchar(20),
  @psproctype varchar(1),
  @psmsg varchar(2000) out
)
as 
begin
  declare @detail_plucode varchar(20)
  declare @detail_pluname varchar(40)
  declare @detail_plutype varchar(1)
  declare @detail_depcode varchar(10)
  declare @detail_serialno numeric(19,0)
  declare @detail_jtaxrate numeric(19,4)
  declare @detail_newprice numeric(19,4)
  declare @detail_newvipprice numeric(19,4)
  declare @detail_newpfprice numeric(19,4)
  declare @detail_newpsprice numeric(19,4)
  declare @detail_oldprice numeric(19,4)
  declare @detail_newtotal numeric(19,4)
  declare @detail_pricediff numeric(19,4)
  declare @detail_jprice numeric(19,4)
  declare @detail_netjprice numeric(19,4)
  declare @detail_counts numeric(19,4)
  declare @detail_totaldiff numeric(19,4)
  declare @detail_jccost numeric(19,4)
  declare @detail_jcnetcost numeric(19,4)
  declare @detail_jctotal numeric(19,4)
  declare @stock_newtotal numeric(19,4)
  declare @stock_cost numeric(19,4)
  declare @stock_netcost numeric(19,4)
  declare @stock_tzamount numeric(19,4)
  declare @pack_packcode varchar(20)
  declare @pack_plucounts numeric(19,4) 
  declare @scurdate varchar(10)
  declare @scurtime varchar(8)
  declare @sinfo varchar(8000)
  declare @ssql varchar(8000)
  declare @isupdgoods varchar(1)

  select @isupdgoods='1'
  select @scurdate=convert(varchar(100),getdate(),120),@scurtime=convert(varchar(100),getdate(),108)
  select @isupdgoods=isnull(itemvalue,'1') from t_syscfg where section='SYSTEM' and itemname='BothChange'

  declare cur cursor for select d.plucode,d.pluname,d.serialno,d.newprice,d.newvipprice,d.newpfprice,d.newpsprice,d.oldprice,d.newtotal,d.depcode,d.jprice,d.netjprice,d.counts,d.totaldiff
    from t_adjprice_body d where billno=@psbillno order by d.plucode,d.serialno                         
  open cur
  fetch next from cur into @detail_plucode,@detail_pluname,@detail_serialno,@detail_newprice,@detail_newvipprice,@detail_newpfprice,@detail_newpsprice,
     @detail_oldprice,@detail_newtotal,@detail_depcode,@detail_jprice,@detail_netjprice,@detail_counts,@detail_totaldiff
  while @@fetch_status=0
  begin
    set @detail_pricediff=@detail_newprice-@detail_oldprice
    set @stock_newtotal=@detail_newtotal

    select @stock_cost=case when a.plutype='2' then 0 else b.cost end,
           @stock_netcost=case when a.plutype='2' then 0 else b.netcost end,
           @stock_tzamount=case when a.plutype='2' then 0 else b.tzamount end,
           @detail_jtaxrate=a.jtaxrate,@detail_plutype=a.plutype 
    from t_plu a,t_plu_org b 
    where a.plucode=@detail_plucode and a.plucode=b.plucode and b.orgcode=@psorgcode and b.isright='1'

    if @detail_newprice is not null 
    begin
      if @isupdgoods='1'
      begin
        set @ssql='update t_plu set price='+cast(@detail_newprice as varchar)+' where plucode='''+@detail_plucode+''''
        exec (@ssql)
        if @@error<>0 
        begin
          set @psmsg='更新商品零售价失败'
          return(-1)    
        end 
      end 
      update t_plu_org set price=@detail_newprice,sjdate=convert(varchar(10),getdate(),120),chgdate=convert(varchar(20),getdate(),120),total=@stock_newtotal 
        where plucode=@detail_plucode and orgcode=@psorgcode and isright='1'
      if @@error<>0 
      begin
        set @psmsg='更新机构商品零售价失败'
        return(-1)    
      end     
    end

    if @detail_newvipprice is not null 
    begin
      if @isupdgoods='1'
      begin
        update t_plu set vipprice=@detail_newvipprice where plucode=@detail_plucode
        if @@error<>0 
        begin
          set @psmsg='更新商品会员价失败'
          return(-1)    
        end 
      end

      update t_plu_org set vipprice=@detail_newvipprice,sjdate=convert(varchar(10),getdate(),120),chgdate=convert(varchar(20),getdate(),120) 
        where plucode=@detail_plucode and orgcode=@psorgcode and isright='1'
      if @@error<>0 
      begin
        set @psmsg='更新机构商品会员价失败'
        return(-1)    
      end     
    end

    if @detail_newpfprice is not null 
    begin
      if @isupdgoods='1'
      begin
        update t_plu set pfprice=@detail_newpfprice where plucode=@detail_plucode
        if @@error<>0 
        begin
          set @psmsg='更新商品批发价失败'
          return(-1)    
        end 
      end

      update t_plu_org set pfprice=@detail_newpfprice where plucode=@detail_plucode and orgcode=@psorgcode and isright='1'
      if @@error<>0 
      begin
        set @psmsg='更新机构商品批发价失败'
        return(-1)    
      end     
    end

    if @detail_newpsprice is not null 
    begin
      if @isupdgoods='1'
      begin
        update t_plu set psprice=case when plutype='2' then 0 else @detail_newpsprice end where plucode=@detail_plucode
        if @@error<>0 
        begin
          set @psmsg='更新商品配送价失败'
          return(-1)    
        end     
      end
      update t_plu_org set psprice=case when @detail_plutype='2' then 0 else @detail_newpsprice end 
        where plucode=@detail_plucode and orgcode=@psorgcode and isright='1'
      if @@error<>0 
      begin
        set @psmsg='更新机构商品配送价失败'
        return(-1)    
      end 
    end

    if @detail_newprice is not null 
    begin
      update t_plupack set pluprice=@detail_newprice where plucode=@detail_plucode
      if @@error<>0 
      begin
        set @psmsg='更新商品包中单品的零售价失败'
        return(-1)    
      end    
    end

    if (@psproctype='0') and (@detail_newprice is not null)
    begin
      declare curplupack cursor for select packcode,counts from t_plupack where plucode=@detail_plucode
      open curplupack
      fetch next from curplupack into @pack_packcode,@pack_plucounts
      while @@fetch_status=0
      begin   
        update t_plupack set packtotal=packtotal+round(@pack_plucounts*@detail_pricediff,2) where packcode=@pack_packcode        
        if @@error<>0 
        begin
          set @psmsg='更新商品包零售价失败'
          return(-1)    
        end  

        update t_plu set packprice=packprice+round(@pack_plucounts*@detail_pricediff,2),chgdate=convert(varchar(20),getdate(),120) where plucode=@pack_packcode
        if @@error<>0 
        begin
          set @psmsg='更新商品零售价失败'
          return(-1)    
        end 

        fetch next from curplupack into @pack_packcode,@pack_plucounts
      end
      close curplupack
      deallocate curplupack        
    end

    set @detail_jccost=@stock_cost+@stock_tzamount
    set @detail_jcnetcost=@stock_netcost+round(@stock_tzamount/(1+@detail_jtaxrate/100.00),2)
    set @detail_jctotal=@stock_newtotal
    if @detail_newprice is  null
      set @detail_newprice=@detail_oldprice 
    exec p_create_gbk_data @scurdate,@scurtime,@detail_plucode,@detail_depcode,@psbillno,'8',
                           @detail_jprice,@detail_netjprice,@detail_newprice,@detail_counts,0,0,@detail_totaldiff,
                           @detail_counts,@detail_jccost,@detail_jcnetcost,@detail_jctotal,@psorgcode,'','',@sinfo out
    if @@error<>0 
    begin
      set @psmsg='生成商品台账失败'
      return(-1)    
    end 

    fetch next from cur into @detail_plucode,@detail_pluname,@detail_serialno,@detail_newprice,@detail_newvipprice,@detail_newpfprice,@detail_newpsprice,
       @detail_oldprice,@detail_newtotal,@detail_depcode,@detail_jprice,@detail_netjprice,@detail_counts,@detail_totaldiff
  end
  close cur
  deallocate cur
end
go

if exists(select * from sysobjects where name='p_rz_sj' and xtype='p')
  drop procedure p_rz_sj
go
create procedure p_rz_sj
(					
  @psorgcode varchar(10),
  @psbillno varchar(20),
  @psparams varchar(200),
  @psmsg varchar(2000) out
)
as
begin
  declare @sorgcode varchar(10)
  declare @scurdate varchar(10)
  declare @scurtime varchar(8)
  declare @sver varchar(1)
  declare @sorgtype varchar(1)
  declare @sisdbdeploy varchar(1)
  declare @supdlastpfprice varchar(1)
  declare @ssql varchar(8000)
  declare @sinfo varchar(8000)
  declare @irtn int
  declare @isplan varchar(1)
  declare @sxdate varchar(10)
  declare @sxtime varchar(8)
  declare @isexec varchar(1)
  declare @ischgprc varchar(1)
  declare @svalue varchar(200)
  declare @iindex int
  declare @ivalueno int

  set @supdlastpfprice=''
  set @isexec=''  
  set @iindex=1
  set @ivalueno=0
  set @svalue=''

  while @iindex<=len(@psparams)
  begin
    if substring(@psparams,@iindex,1)<>';'
    begin
      set @svalue=@svalue+substring(@psparams,@iindex,1)
    end
    else
    begin
      set @ivalueno=@ivalueno+1
      if @ivalueno=1
        set @supdlastpfprice=@svalue
      set @svalue=''
    end  
    set @iindex=@iindex+1
    if @iindex>len(@psparams)
    begin
      set @ivalueno=@ivalueno+1
      if @ivalueno=2
        set @isexec=@svalue        
    end
  end  
  
  set @scurdate=convert(varchar(10),getdate(),120)
  set @scurtime=convert(varchar(8),getdate(),108)
  select @sver=itemvalue from t_syscfg where section='SYSTEM' and itemname='VER'	
  select @sorgcode=orgcode,@isplan=isplan,@sxdate=sxdate,@sxtime=sxtime from t_adjprice_head where billno=@psbillno
  select @sorgtype=orgtype,@sisdbdeploy=isdbdeploy from t_shop where orgcode=@sorgcode
  
  if (@isplan='1' and @isexec='0')
    set @ischgprc='0' 
  else
    set @ischgprc='1'

  if @ischgprc='1'
  begin    
    if (@sver='0') or (@sver<>'0') and ((@sorgtype='2') or (@sisdbdeploy='1'))
    begin
      set @ssql='update a set counts=b.gcount+b.ccount,
                   total=case when c.plutype=''2'' then 0 else round(oldprice*(b.gcount+b.ccount),2) end,
                   newtotal=isnull(case when c.plutype=''2'' then 0 else round(newprice*(b.gcount+b.ccount),2) end,0),
                   totaldiff=isnull(case when c.plutype=''2'' then 0 else round(newprice*(b.gcount+b.ccount),2)-round(oldprice*(b.gcount+b.ccount),2) end,0)
                   from t_adjprice_body a,t_plu_org b,t_plu c
                   where a.plucode=b.plucode and a.plucode=c.plucode and billno='''+@psbillno+''' and b.gcount+b.ccount<>counts 
                   and b.orgcode='''+@sorgcode+''' and b.isright=''1'''
      exec (@ssql)
      if @@error<>0
      begin
        set @psmsg='更新售价调整单表体失败'
        return(-1)    
      end  

      set @ssql='update a set oldprice=b.price,
                   total=case when c.plutype=''2'' then 0 else round(b.price*counts,2) end,
                   totaldiff=newtotal-case when c.plutype=''2'' then 0 else round(b.price*counts,2) end
                   from t_adjprice_body a,t_plu_org b,t_plu c
                   where a.plucode=b.plucode and a.plucode=c.plucode and billno='''+@psbillno+''' and oldprice<>b.price 
                   and b.orgcode='''+@sorgcode+''' and b.isright=''1'''
      exec (@ssql)
      if @@error<>0 
      begin
        set @psmsg='更新售价调整单表体失败'
        return(-1)    
      end  

      set @ssql='update a set counts=b.counts,total=b.total,newtotal=b.newtotal,totaldiff=b.totaldiff,rzdate='''+@scurdate+''',rztime='''+@scurtime+''',
                   exectime=convert(varchar(20),getdate(),120)
                   from t_adjprice_head a,(select billno,sum(counts) as counts,sum(total) as total,
                   sum(newtotal) as newtotal,sum(totaldiff) as totaldiff from t_adjprice_body where billno='''+@psbillno+''' group by billno) b
                   where a.billno=b.billno and a.billno='''+@psbillno+''''
      exec (@ssql)
      if @@error<>0 
      begin
        set @psmsg='更新售价调整单表头失败'
        return(-1)    
      end 

      exec p_rz_sj_circle_proc @sorgcode,@psbillno,'0',@psmsg out
      if @@error<>0 
      begin
        set @psmsg='执行售价记账循环处理失败 '+@psmsg
        return(-1)    
      end  
    end
    else
    begin
      exec p_rz_sj_circle_proc @sorgcode,@psbillno,'1',@psmsg out
      if @@error<>0 
      begin
        set @psmsg='执行售价记账循环处理失败 '+@psmsg
        return(-1)    
      end     
    end
    
    if @supdlastpfprice='1'
    begin
      set @ssql='update old set pfsaleno='''+@psbillno+''',rzdate='''+@scurdate+''',
                   pfprice=new.newpfprice,pfcount=0,zpcount=0,pftotal=0 
                   from t_whsle_cust_info old,
                   (select plucode,newpfprice from t_adjprice_body where billno='''+@psbillno+''' and newpfprice is not null) new 
                   where old.plucode=new.plucode'
      exec (@ssql)
      if @@error<>0 
      begin
        set @psmsg='修改上次批发价失败'
        return(-1)    
      end 
    end

    update t_pos set pluflg='1' where orgcode=@sorgcode
    if @@error<>0 
    begin
      set @psmsg='更新收银机下发商品标志失败'
      return(-1)    
    end      
  end

  if ((@isplan='1' and @isexec='0') or (@isplan='0')) and (@sorgtype<>'2')
  begin
    set @ssql='insert into t_label_prn_task(plucode,pluname,barcode,prncount,
                 orgcode,oldprice,price,vipprice,unit,spec,prodarea,grade,prntype,srctype,remark)
               select b.plucode,c.pluname,c.barcode,1,a.orgcode,d.price,
                  case when b.newprice is not null then b.newprice else d.price end,
                  case when b.newvipprice is not null then b.newvipprice else d.vipprice end,
                  b.unit,b.spec,c.prodarea,c.grade,''1'',''1'',b.billno
               from t_adjprice_head a,t_adjprice_body b,t_plu c,t_plu_org d
               where a.billno=b.billno and b.plucode=c.plucode
                 and b.plucode=d.plucode and a.orgcode=d.orgcode and d.isright=''1''
                 and a.billno='''+@psbillno+'''
               union
               select b.plucode,r.pluname,r.barcode,1,a.orgcode,d.price,
                  case when b.newprice is not null then b.newprice else d.price end,
                  case when b.newvipprice is not null then b.newvipprice else d.vipprice end,
                  b.unit,r.spec,c.prodarea,c.grade,''1'',''1'',b.billno
               from t_adjprice_head a,t_adjprice_body b,t_plu c,t_barcode_ref r,t_plu_org d
               where a.billno=b.billno and b.plucode=c.plucode and b.plucode=r.plucode
                 and b.plucode=d.plucode and a.orgcode=d.orgcode and d.isright=''1''
                 and a.billno='''+@psbillno+''''
    exec (@ssql)
    if @@error<>0 
    begin
      set @psmsg='增加标价签打印任务失败'
      return(-1)    
    end
  end
  
  set @psmsg=''
  return(1)
end	
go

if exists(select * from sysobjects where name='p_adjust_auto_rz' and xtype='p')
  drop procedure p_adjust_auto_rz
go
create procedure p_adjust_auto_rz
(					
  @psbillno varchar(20)
)
as 
begin
  declare @sisplan varchar(1)
  declare @ssxdate varchar(10)
  declare @ssxtime varchar(8)
  declare @srzdate varchar(10)
  declare @sexectime varchar(20)
  declare @scurdate varchar(20)
  declare @smsg varchar(200)
  declare @irtn int   

  select @scurdate=convert(varchar(20),getdate(),120)
  select @sisplan=isplan,@ssxdate=sxdate,@ssxtime=sxtime,@sexectime=exectime,@srzdate=rzdate 
  from t_adjprice_head where billno=@psbillno
  
  if @srzdate=''
  begin
    set @smsg=@psbillno+'单号不存在或未记账'
    raiserror(@smsg,16,1)
    return(-1)
  end
  
  if @sisplan<>'1'
  begin
    set @smsg=@psbillno+'不是计划调价单'
    raiserror(@smsg,16,1)
    return(-1)
  end 
  
  if @sexectime<>''
  begin
    set @smsg=@psbillno+'已执行了新价格'
    raiserror(@smsg,16,1)
    return(-1)
  end
  
  exec @irtn=p_rz_sj '',@psbillno,'1;1',@smsg out
  if @@error<>0 or @irtn<>1
  begin
    set @smsg=@psbillno+'记账失败：'+@smsg
    raiserror(@smsg,16,1)
    return(-1)  
  end
  
  return(1)
end
go


 