Dim StartDate As WinForm.DateTimePicker = e.Form.Controls("StartDate")
Dim EndDate As WinForm.DateTimePicker = e.Form.Controls("EndDate")
If StartDate.Value IsNot Nothing And EndDate.Value IsNot Nothing Then
For Each dr As DataRow In DataTables("产品收发存").DataRows
dr("期初_数量") = DataTables("产品入库明细").SQLCompute("Sum(数量)","[产品代码] = '" & dr("产品代码") & "' and 工序 = '完工入库' and 日期 < '" & StartDate.Value & "'") + DataTables("产品入库明细").Compute("Sum(数量)","[产品代码] = '" & dr("产品代码") & "' and 工序 = '外协入库' and 日期 < '" & StartDate.Value & "'") - DataTables("出库明细").Compute("Sum(实交数量)","[产品代码] = '" & dr("产品代码") & "' and 制单日期 < '" & StartDate.Value & "'")
dr("期初_重量") = DataTables("产品入库明细").SQLCompute("Sum(数量*单件重量)","[产品代码] = '" & dr("产品代码") & "' and 工序 = '完工入库' and 日期 < '" & StartDate.Value & "'") + DataTables("产品入库明细").Compute("Sum(数量*单件重量)","[产品代码] = '" & dr("产品代码") & "' and 工序 = '外协入库' and 日期 < '" & StartDate.Value & "'") - DataTables("出库明细").Compute("Sum(重量)","[产品代码] = '" & dr("产品代码") & "' and 制单日期 < '" & StartDate.Value & "'")
dr("期初_金额") = DataTables("产品入库明细").SQLCompute("Sum(数量*单价)","[产品代码] = '" & dr("产品代码") & "' and 工序 = '完工入库' and 日期 < '" & StartDate.Value & "'") + DataTables("产品入库明细").Compute("Sum(数量*单价)","[产品代码] = '" & dr("产品代码") & "' and 工序 = '外协入库' and 日期 < '" & StartDate.Value & "'") - DataTables("出库明细").Compute("Sum(金额)","[产品代码] = '" & dr("产品代码") & "' and 制单日期 < '" & StartDate.Value & "'")
dr("入库_数量") = DataTables("产品入库明细").SQLCompute("Sum(数量)","[产品代码] = '" & dr("产品代码") & "' and 工序 = '完工入库' and 日期 >= '" & StartDate.Value & "' and 日期 <= '" & EndDate.Value & "'") + DataTables("产品入库明细").SQLCompute("Sum(数量)","[产品代码] = '" & dr("产品代码") & "' and 工序 = '外协入库' and 日期 >= '" & StartDate.Value & "' and 日期 <= '" & EndDate.Value & "'")
dr("入库_重量") = DataTables("产品入库明细").SQLCompute("Sum(数量*单件重量)","[产品代码] = '" & dr("产品代码") & "' and 工序 = '完工入库' and 日期 >= '" & StartDate.Value & "' and 日期 <= '" & EndDate.Value & "'") + DataTables("产品入库明细").SQLCompute("Sum(数量*单件重量)","[产品代码] = '" & dr("产品代码") & "' and 工序 = '外协入库' and 日期 >= '" & StartDate.Value & "' and 日期 <= '" & EndDate.Value & "'")
dr("入库_金额") = DataTables("产品入库明细").SQLCompute("Sum(数量*单价)","[产品代码] = '" & dr("产品代码") & "' and 工序 = '完工入库' and 日期 >= '" & StartDate.Value & "' and 日期 <= '" & EndDate.Value & "'") + DataTables("产品入库明细").SQLCompute("Sum(数量*单价)","[产品代码] = '" & dr("产品代码") & "' and 工序 = '外协入库' and 日期 >= '" & StartDate.Value & "' and 日期 <= '" & EndDate.Value & "'")
dr("出库_数量") = DataTables("出库明细").SQLCompute("Sum(实交数量)","[产品代码] = '" & dr("产品代码") & "' and 制单日期 >= '" & StartDate.Value & "' and 制单日期 <= '" & EndDate.Value & "'")
dr("出库_重量") = DataTables("出库明细").SQLCompute("Sum(实交数量*单件重量)","[产品代码] = '" & dr("产品代码") & "' and 制单日期 >= '" & StartDate.Value & "' and 制单日期 <= '" & EndDate.Value & "'")
dr("出库_金额") = DataTables("出库明细").SQLCompute("Sum(实交数量*单价)","[产品代码] = '" & dr("产品代码") & "' and 制单日期 >= '" & StartDate.Value & "' and 制单日期 <= '" & EndDate.Value & "'")
dr("期末_数量") = dr("期初_数量") + dr("入库_数量") - dr("出库_数量")
dr("期末_重量") = dr("期初_重量") + dr("入库_重量") - dr("出库_重量")
dr("期末_金额") = dr("期初_金额") + dr("入库_金额") - dr("出库_金额")
Next
Else
For Each dr As DataRow In DataTables("产品收发存").DataRows
dr("入库_数量") = DataTables("产品入库明细").SQLCompute("Sum(数量)","[产品代码] = '" & dr("产品代码") & "' and 工序 = '完工入库'") + DataTables("产品入库明细").SQLCompute("Sum(数量)","[产品代码] = '" & dr("产品代码") & "' and 工序 = '外协入库'")
dr("入库_重量") = DataTables("产品入库明细").SQLCompute("Sum(数量*单件重量)","[产品代码] = '" & dr("产品代码") & "' and 工序 = '完工入库'") + DataTables("产品入库明细").SQLCompute("Sum(数量*单件重量)","[产品代码] = '" & dr("产品代码") & "' and 工序 = '外协入库'")
dr("入库_金额") = DataTables("产品入库明细").SQLCompute("Sum(数量*单价)","[产品代码] = '" & dr("产品代码") & "' and 工序 = '完工入库'") + DataTables("产品入库明细").SQLCompute("Sum(数量*单价)","[产品代码] = '" & dr("产品代码") & "' and 工序 = '外协入库'")
dr("出库_数量") = DataTables("出库明细").SQLCompute("Sum(实交数量)","[产品代码] = '" & dr("产品代码") & "'")
dr("出库_重量") = DataTables("出库明细").SQLCompute("Sum(实交数量*单件重量)","[产品代码] = '" & dr("产品代码") & "'")
dr("出库_金额") = DataTables("出库明细").SQLCompute("Sum(实交数量*单价)","[产品代码] = '" & dr("产品代码") & "'")
dr("期末_数量") = dr("入库_数量") - dr("出库_数量")
dr("期末_重量") = dr("入库_重量") - dr("出库_重量")
dr("期末_金额") = dr("入库_金额") - dr("出库_金额")
Next
End If