Dim st As Date = Date.Now '用于计算时间
Dim s As String = e.Form.Controls("起始月").Value
If s <> "" Then
Tables("材料月结").StopRedraw() '停止重绘
DataTables("材料月结").DataRows.Clear
Dim drs_out As List(Of DataRow)
Dim dt_in As List(Of DataRow)
dt_in = DataTables("材料入库明细表").Select("材料编号 is not null", "材料编号,材料名称,规格型号,材料_大类,材料_中类,月份") '排序取出 入仓 全部数据
drs_out = DataTables("材料出库明细表").Select("材料编号 is not null", "材料编号,材料名称,规格型号,材料_大类,材料_中类,月份") '排序取出 入仓 全部数据
Dim drs_init As List(Of DataRow) = DataTables("材料期初库存明细表").Select("月份 <= " & s)
Dim r As Row
Dim sum_curr_money, sum_prev_money As Double
Dim sum_curr_count, sum_prev_count As Single
r = Tables("材料月结").AddNew
r("材料编号") = dt_in(0)("材料编号")
r("材料名称") = dt_in(0)("材料名称")
r("规格型号") = dt_in(0)("规格型号")
r("材料_大类") = dt_in(0)("材料_大类")
r("材料_中类") = dt_in(0)("材料_中类")
If dt_in(0)("月份") < s Then '之前月
sum_prev_count = sum_prev_count + dt_in(0)("数量")
sum_prev_money = sum_prev_money + dt_in(0)("不含税金额")
Else If dt_in(0)("月份") = s Then '本月份
sum_curr_money = sum_curr_money + dt_in(0)("不含税金额")
sum_curr_count = sum_curr_count + dt_in(0)("数量")
End If
For i As Single = 1 To dt_in.Count - 1 '写入型号和规格,同时统计入仓数量和金额
If dt_in(i)("材料编号") <> dt_in(i-1)("材料编号") OrElse dt_in(i)("材料名称") <> dt_in(i-1)("材料名称") OrElse dt_in(i)("规格型号") <> dt_in(i-1)("规格型号") OrElse dt_in(i)("材料_大类") <> dt_in(i-1)("材料_大类") OrElse dt_in(i)("材料_中类") <> dt_in(i-1)("材料_中类") Then '不同产品
r("本月入库_数量") = sum_curr_count
r("本月入库_不含税金额") = sum_curr_money
r("月初结存_数量") = sum_prev_count '暂存
r("月初结存_不含税金额") = sum_prev_money '暂存
r = Tables("材料月结").AddNew '插入新数据
r("材料编号") = dt_in(i)("材料编号")
r("材料名称") = dt_in(i)("材料名称")
r("规格型号") = dt_in(i)("规格型号")
r("材料_大类") = dt_in(i)("材料_大类")
r("材料_中类") = dt_in(i)("材料_中类")
sum_curr_money = 0
sum_curr_count = 0
sum_prev_count = 0
sum_prev_money = 0
End If
If dt_in(i)("月份") < s Then '之前月
sum_prev_count = sum_prev_count + dt_in(i)("数量")
sum_prev_money = sum_prev_money + dt_in(i)("不含税金额")
Else If dt_in(i)("月份") = s Then '本月份
sum_curr_money = sum_curr_money + dt_in(i)("不含税金额")
sum_curr_count = sum_curr_count + dt_in(i)("数量")
End If
Next
For Each dr As DataRow In drs_init
If DataTables("材料月结").find("材料名称 = '" & dr("材料名称") & "' And 规格型号 = '" & dr("规格型号") & "'") Is Nothing Then
Dim nr = Tables("材料月结").AddNew
nr("材料编号") = dr("材料编号")
nr("材料名称") = dr("材料名称")
nr("规格型号") = dr("规格型号")
nr("材料_大类") = dr("材料_大类")
nr("材料_中类") = dr("材料_中类")
End If
Next
For Each dr As DataRow In drs_out
If DataTables("材料月结").find("材料名称 = '" & dr("材料名称") & "' And 规格型号 = '" & dr("规格型号") & "'") Is Nothing Then
Dim nr = Tables("材料月结").AddNew
nr("材料编号") = dr("材料编号")
nr("材料名称") = dr("材料名称")
nr("规格型号") = dr("规格型号")
nr("材料_大类") = dr("材料_大类")
nr("材料_中类") = dr("材料_中类")
End If
Next
r("本月入库_数量") = sum_curr_count
r("本月入库_不含税金额") = sum_curr_money
r("月初结存_数量") = sum_prev_count '暂存
r("月初结存_不含税金额") = sum_prev_money '暂存
'MessageBox.Show("复制型号和规格耗时: " & (Date.Now - st).TotalSeconds & "秒")
'st = Date.Now '用于计算时间
For Each dr As DataRow In DataTables("材料月结").DataRows '填充统计数据
sum_curr_count = 0 '累计本月出仓数量
sum_prev_count = 0 '累计之前出仓数量
sum_curr_money = 0 '累计之前出仓金额
sum_prev_money = 0 '累计之前出仓金额
Dim filter As String = "材料名称 = '" & dr("材料名称") & "' And 规格型号 = '" & dr("规格型号") & "'"
drs_out = DataTables("材料出库明细表").Select(filter, "月份") '排序取出 出仓 全部数据
For Each dr_out As DataRow In drs_out
If dr_out("月份") < s Then
sum_prev_count = sum_prev_count + dr_out("数量")
sum_prev_money = sum_prev_money + dr_out("出库不含税金额")
Else If dr_out("月份") = s Then
sum_curr_count = sum_curr_count + dr_out("数量")
sum_curr_money = sum_curr_money + dr_out("出库不含税金额")
Else
Exit For
End If
Next
Dim qcsl As Single = DataTables("材料期初库存明细表").compute("sum(数量)", "材料名称 = '" & dr("材料名称") & "' And 规格型号 = '" & dr("规格型号") & "' and 月份 <= " & s)
Dim qcsl1 As Single = DataTables("材料期初库存明细表").compute("sum(不含税金额)", "材料名称 = '" & dr("材料名称") & "' And 规格型号 = '" & dr("规格型号") & "' and 月份 <= " & s)
Dim sum_in_prev_count As Single = dr("月初结存_数量") '从表中取出暂存数据
Dim sum_in_prev_money As Double = dr("月初结存_不含税金额") '从表中取出暂存数据
dr("本月出库_数量") = sum_curr_count
dr("本月出库_不含税金额") = sum_curr_money
dr("月初结存_数量") = sum_in_prev_count - sum_prev_count + qcsl
dr("月初结存_不含税金额") = sum_in_prev_money - sum_prev_money + qcsl1
dr("月末结存_数量") = dr("月初结存_数量") + dr("本月入库_数量") - dr("本月出库_数量")
dr("月末结存_不含税金额") = dr("月初结存_不含税金额") + dr("本月入库_不含税金额") - dr("本月出库_不含税金额")
dr("加权平均单价") = (dr("月初结存_不含税金额") + dr("本月入库_不含税金额"))/(dr("月初结存_数量") + dr("本月入库_数量"))
''dr("本月出库_不含税金额") = dr("加权平均单价") * dr("本月出库_数量")
Next
Tables("材料月结").ResumeRedraw()
End If
'MessageBox.Show("统计和写入耗时: " & (Date.Now - st).TotalSeconds & "秒")
'
Dim trv As WinForm.TreeView = e.Form.Controls("TreeView1")
trv.BuildTree("材料月结", "材料_大类|材料_中类|材料名称|规格型号")
trv.Nodes.Insert("显示所有行",0)