来这个项目主要是佩特来公司各部门及各代理商使用的系统,这个系统其中的一下功能就是统计代理商费用。费用的统计放在了费用池(传统方式统计代理商费用就叫费用池)数据表中,代理商可以根据费用池的中的金额购买东西,费用池中的钱来自于代理商每次填写的维修鉴定单中。
下面看一下这部分的数据表结构:
下面的存储过程是汇总材料费及其他费用表的费用并汇总到费用池信息表的存储过程,这段代码有点长,加入了各种判断:
-- =============================================-- author: 马兆娟-- create date: 2014-7-20 15:32:16-- description: 统计材费及其他费用汇总到费用池-- =============================================create procedure [dbo].[proc_wxjd_commitfee] -- add the parameters for the stored procedure here @wxjdid int, --维修鉴定单id @dlsid int --代理商idasdeclare @err1 int, --声明变量,事务使用 @err2 int, --声明变量,事务使用 @glf decimal, --管理费 @gsf decimal, --工时费 @xj decimal, --小计 @totalmoney decimal, --总金额 @count int, --整型数据 @ljglf decimal, --零件管理费 @ljgsf decimal, --零件工时费 @qtfy decimal, --其他费用 @wxjdtotalmoney decimal --总金额begin begin transaction --开启事务 select @count=0; --给变量赋值 --下面统计其他费用 select @count=count(*) from t_dls_wxjd_cost_qtfy where fid=@wxjdid if @count>0 --判断其他费用表是否已写入其他费用值,下面给小计赋值 begin select @xj = case sum(xj) when null then 0 else sum(xj) end from t_dls_wxjd_cost_qtfy where fid=@wxjdid; end else begin select @xj=0; end --下面是统计材料费 select @count=0; select @count=count(*) from t_dls_wxjd_cost_clf where fid =@wxjdid if @count>0 --判断材料费表是否已写入材料费,下面给管理费、工时费赋值 begin select @glf = case sum(glf) when null then 0 else sum(glf) end ,@gsf = case sum(gsf) when null then 0 else sum(gsf) end from t_dls_wxjd_cost_clf where fid =@wxjdid end else begin select @glf=0; select @gsf=0; end --下面给总金额赋值 select @totalmoney = @xj + @glf + @gsf; --print @totalmoney; --下面将从其他费用及材料费中统计的金额写入费用池 select @count =0; select @count=count(*) from t_feeexist where dlsid = @dlsid; if @count>0 --判断费用池表是否已写入某代理商费用,如果已写入过代理商费用,则需向代理商各项费用上添加统计的费用 begin select @ljglf=ljglf,@ljgsf=ljgsf,@qtfy=qtfy,@wxjdtotalmoney=wxjdtotalmoney from t_feeexist where dlsid = @dlsid; select @ljglf=@ljglf+@glf; select @ljgsf=@ljgsf+@gsf; select @qtfy=@qtfy+@xj; select @wxjdtotalmoney=@wxjdtotalmoney+@totalmoney; update t_feeexist set ljglf=@ljglf,ljgsf=@ljgsf,qtfy=@qtfy,wxjdtotalmoney=@wxjdtotalmoney where dlsid = @dlsid; end else --第一次向费用池写入某代理商费用,添加新记录 begin insert into t_feeexist(dlsid,ljglf,ljgsf,qtfy,wxjdtotalmoney) values(@dlsid,@glf,@gsf,@xj,@totalmoney); end set @err1=@@error --更新维修鉴定表是否已提交到费用池字段 update t_dls_wxjd set isfyc='是' where id=@wxjdid set @err2=@@error --判断是否出错 if (@err1=0 and @err2=0 ) commit transaction --提交事务 else rollback transaction --事务回滚end
统计费用就简单的介绍到这里了,这里主要记录的关键点就是如何汇总各表的数据及存储过程中对数值进行判断!
