DataTables("月结").DataRows.Clear()
Dim lr As DataRow
For Each vals As String() In DataTables("明细").GetValues("材料|月")
Dim dr As DataRow = DataTables("月结").AddNew()
dr("材料") = vals(0)
dr("月") = vals(1)
If lr Is Nothing OrElse lr("材料") <> dr("材料") Then
Dim qr As DataRow = DataTables("期初").Find("材料='" & vals(0) & "'")
If qr IsNot Nothing Then
dr("上月结存_数量") = qr("期初数量")
dr("上月结存_金额") = qr("期初金额")
End If
Else
dr("上月结存_数量") = lr("本月结存_数量")
dr("上月结存_金额") = lr("本月结存_金额")
End If
Dim filter As String = "材料='" & vals(0) & "' And 月='" & vals(1) & "'"
dr("本月入仓_数量") = DataTables("明细").Compute("Sum(入仓数量)", filter)
dr("本月出仓_数量") = DataTables("明细").Compute("Sum(入仓金额)", filter)
dr("本月出仓_数量") = DataTables("明细").Compute("Sum(出仓数量)", filter)
dr("本月结存_数量") = dr("上月结存_数量") + dr("本月入仓_数量") - dr("本月出仓_数量")
dr("本月加权平均单价") = (dr("上月结存_金额") + dr("本月入仓_金额")) /(dr("上月结存_数量")+dr("本月入仓_数量"))
dr("本月出仓_金额") =dr("本月出仓_数量") * dr("本月加权平均单价")
dr("本月结存_金额") = dr("本月加权平均单价") * dr("本月结存_数量")
lr = dr
Next