代码
'If DataTables.Contains("进货") = False Then
' DataTables.Load("进货|销售|退货")
'End If
With e.Form.Controls("日期")
If .Value Is Nothing Then
Return
End If
End With
Tables("统计_Table1").StopRedraw
Dim Filter As String
Dim curD As Date=e.Form.Controls("日期").Value '当前日期
Dim lastD As Date = curD.AddMonths(-1) '上一个月的日期
Dim lastD2 As New Date(lastD.Year, lastD.Month, Date.DaysInMonth(lastD.Year,lastD.Month)) '上个月的最后一天
Filter = Filter & "日期 > #" & lastD2 & "#" & " And 日期 <= #" & curD & "#"
Dim bd1 As New SQLGroupTableBuilder("统计表1","进货")
Dim dt1 As fxDataSource
bd1.Groups.AddDef("型号") '根据型号分组
bd1.Totals.AddDef("进货_数量","进货_数量") '对数量进行统计
bd1.Totals.AddDef("进货_金额","进货_金额") '对金额进行统计
bd1.Filter = Filter
dt1 = bd1.BuildDataSource()
Dim bd2 As New SQLGroupTableBuilder("统计表2","销售")
Dim dt2 As fxDataSource
bd2.Groups.AddDef("型号") '根据型号分组
bd2.Totals.AddDef("销售_数量","销售_数量") '对数量进行统计
bd2.Totals.AddDef("销售_金额","销售_金额") '对金额进行统计
bd2.Filter = Filter
dt2 = bd2.BuildDataSource()
Dim bd3 As New SQLGroupTableBuilder("统计表3","退货")
Dim dt3 As fxDataSource
bd3.Groups.AddDef("型号") '根据型号分组
bd3.Totals.AddDef("退货_数量","退货_数量") '对数量进行统计
bd3.Totals.AddDef("退货_金额","退货_金额") '对金额进行统计
bd3.Filter = Filter
dt3 = bd3.BuildDataSource()
dt1.Combine("型号",dt2,"型号") '将销售统计数据组合到进货统计数据
dt1.Combine("型号",dt3,"型号") '将退货统计数据组合到进货统计数据
Tables("统计_Table1").DataSource = dt1 '将统计结果绑定到Table
With DataTables("统计_Table1").DataCols '用表达式列计算库存数据
.Add("期初_数量",Gettype(Double),"","期初_数量")
.Add("期初_金额",Gettype(Double),"","期初_金额")
End With
Filter = "日期 <= #" & lastD2 & "#"
Dim dt_jh As DataTable
Dim dt_xs As DataTable
Dim dt_th As DataTable
Dim cmd As new SQLCommand
cmd.CommandText = "select * from {进货} where " & Filter
dt_jh = cmd.ExecuteReader
cmd.CommandText = "select * from {销售} where " & Filter
dt_xs = cmd.ExecuteReader
cmd.CommandText = "select * from {退货} where " & Filter
dt_th = cmd.ExecuteReader
For Each r As Row In Tables("统计_Table1").Rows
Filter = "型号 = '" & r("型号") & "'"
r("期初_数量") = dt_jh.Compute("sum(进货_数量)", filter) - dt_xs.Compute("sum(销售_数量)", filter) - dt_th.Compute("sum(退货_数量)", filter)
r("期初_金额") = dt_jh.Compute("sum(进货_金额)", filter) - dt_xs.Compute("sum(销售_金额)", filter) - dt_th.Compute("sum(退货_金额)", filter)
Next
With DataTables("统计_Table1").DataCols '用表达式列计算库存数据
.Add("库存_数量",Gettype(Double), "IsNull([期初_数量],0) + IsNull([进货_数量],0) - ISNULL([销售_数量],0) - ISNULL([退货_数量],0)","库存_数量")
.Add("库存_金额",Gettype(Double), "IsNull([期初_金额],0) + IsNull([进货_金额],0) - ISNULL([销售_金额],0) - ISNULL([退货_金额],0)","库存_金额")
End With
DataTables("统计_Table1").DataCols.Add("进货_单价", Gettype(Double), "(期初_金额+进货_金额)/(期初_数量+进货_数量)")
Tables("统计_Table1").SetColVisibleWidth("型号|80|期初_数量|80|期初_金额|80|进货_单价|80|进货_数量|80|进货_金额|80|销售_数量|80|销售_金额|80|退货_数量|80|退货_金额|80|库存_数量|80|库存_金额|80")
Tables("统计_Table1").DefaultRowHeight = 35
DataTables("统计_Table1").DataCols("期初_数量").SetFormat("#,###.00")
DataTables("统计_Table1").DataCols("期初_金额").SetFormat("#,###.00")
DataTables("统计_Table1").DataCols("进货_数量").SetFormat("#,###.00")
DataTables("统计_Table1").DataCols("进货_金额").SetFormat("#,###.00")
DataTables("统计_Table1").DataCols("销售_数量").SetFormat("#,###.00")
DataTables("统计_Table1").DataCols("销售_金额").SetFormat("#,###.00")
DataTables("统计_Table1").DataCols("退货_数量").SetFormat("#,###.00")
DataTables("统计_Table1").DataCols("退货_金额").SetFormat("#,###.00")
DataTables("统计_Table1").DataCols("库存_数量").SetFormat("#,###.00")
DataTables("统计_Table1").DataCols("库存_金额").SetFormat("#,###.00")
Tables("统计_Table1").ResumeRedraw