此主题相关图片如下:1.png
Dim cmd As new SQLCommand
cmd.C
Dim qc As Date = args(0) '当前期初日期
Dim s As String
Dim dt,dt1 As DataTable
s = "select _Identify,期初日期,期初名称,备注 fr om 期初名称 where 期初日期<= '" & qc & "'"
cmd.CommandText = s
dt = cmd.ExecuteReader
s = "select _Identify,仓库地点,产品线,库存类型,仓库属性,条码,BOM编码,产品型号,数量,采购合同号,入库日期 fr om (select d._Identify,rkid,仓库地点,产品线,库存类型,仓库属性,d.条码,BOM编码,产品型号,isnull(d.数量,0)-isnull(b.数量,0) as 数量,采购合同号,入库日期 fr om (select 入库表._identify,c.rkid,仓库地点,产品线,库存类型,仓库属性,条码,BOM编码,产品型号,数量,采购合同号,入库日期 fr om (select * fr om 入库条码明细) as c left join 入库表 on c.rkid = 入库表._identify where 审核 = 'true') as d left join (select 出库表._identify,a.ckid,出库日期,条码,数量 fr om (select * f rom 出库条码明细) as a left join 出库表 on a.ckid = 出库表._identify where 审核 = 'true' and 出库日期 <='" & qc & "') as b on d.条码 = b.条码 ) as e where 数量 > 0 and 入库日期<= '" & qc & "'"
cmd.CommandText = s
dt1 = cmd.ExecuteReader
cmd.CommandText = "select _Identify,qcid,仓库地点,产品线,库存类型,仓库属性,条码,BOM编码,产品型号,数量,采购合同号,入库日期 fro m 期初表 where 入库日期 <='" & qc & "'" 'And qcid = " & dt.DataRows(0)("_Identify")
Dim sql As String = cmd.CommandText
Dim dt2 As DataTable = cmd.ExecuteReader
If dt.DataRows.Count > 0 Then '如果还没有期初表,有入库了,可以把库存数据作为期初表,然后才可以出库
For h As Integer = 0 To dt.DataRows.Co unt - 1
If h = 0 Then '第一个期初表
If dt1.DataRows.Count > 0 Then
If dt2.DataRows.Count <= 0 Then
For j As Integer = 0 To dt1.DataRows.Count-1
Dim dr1 As DataRow = dt1.DataRows(j)
s = "ins ert in to 期初表(qcid,仓库地点,产品线,库存类型,仓库属性,条码,BOM编码,产品型号,数量,采购合同号,入库日期) values (" & dt.DataRows(0)("_Identify") & ",'" & dr1("仓库地点") & "','" & dr1("产品线") & "','" & dr1("库存类型") & "','" & dr1("仓库属性") & "','" & dr1("条码") & "','" & dr1("BOM编码") & "','" & dr1("产品型号") & "'," & dr1("数量") & ",'" & dr1("采购合同号") & "','" & dr1("入库日期") & "' )"
cmd.CommandText = s
cmd.ExecuteNonQuery
Next
End If
End If
Else ' H > 0 不是第一个期初表
If dt2.DataRows.Count > 0 Then '期初表有数据
Dim y As Integer = h - 1
Dim dr As DataRow = dt.DataRows(h) '当前期初表期初表
Dim dr2 As DataRow = dt.DataRows(y) '当前期初表的上一个期初表
Dim d As Date
Do While dr2("期初日期") < d <= qc 'd在上个期初日期和当前期初日期之间,以d为条件查找入库出库数据,计算当前期初表
s = "select * fr om (select 入库日期,采购合同号,产品线,库存类型,仓库属性,BOM编码,产品型号,仓库地点,条码, su m(条码数量) as 库存 fr om (select 入库日期,采购合同号,产品线,库存类型,仓库属性,BOM编码,产品型号,仓库地点,条码,su m(数量) as 条码数量 fr om 期初表 whe re qcid = " & dr2("_Identify") & "grou p by _Identify,qcid,入库日期,采购合同号,产品线,库存类型,仓库属性,BOM编码,产品型号,仓库地点,条码 un ion all Select 入库日期,采购合同号,产品线,库存类型,仓库属性,BOM编码,产品型号,仓库地点,条码,su m(数量) As 条码数量 fr om (Select * fr om 入库条码明细) As a left join 入库表 on a.rkid = 入库表._identify whe re 入库日期 >'" & dr2("期初日期") & "'and 入库日期 <='" & qc & "' gro up by 入库日期,采购合同号,产品线,库存类型,仓库属性,BOM编码,产品型号,仓库地点,条码 un ion all Select 入库日期,采购合同号,产品线,库存类型,仓库属性,BOM编码,产品型号,仓库地点,条码,-数量 fr om (Select * fr om 出库条码明细) As a left join 出库表 on a.ckid = 出库表._identify whe re 出库日期<='" & qc & "'And 出库日期 > '" & dr2("期初日期") & "') As c gro up by 入库日期,采购合同号,产品线,库存类型,仓库属性,BOM编码,产品型号,仓库地点,条码) As e wh ere 库存 > 0"
cmd.CommandText = s
Dim dt3 As DataTable = cmd.ExecuteReader
If dt3.DataRows.Count > 0 Then
For n As Integer = 0 To dt3.DataRows.Count - 1
Dim dr3 As DataRow = dt3.DataRows(n)
s = "ins ert into 期初表(qcid,仓库地点,产品线,库存类型,仓库属性,条码,BOM编码,产品型号,数量,采购合同号,入库日期) values (" & dr("_Identify") & ",'" & dr3("仓库地点") & "','" & dr3("产品线") & "','" & dr3("库存类型") & "','" & dr3("仓库属性") & "','" & dr3("条码") & "','" & dr3("BOM编码") & "','" & dr3("产品型号") & "'," & dr3("数量") & ",'" & dr3("采购合同号") & "','" & dr3("入库日期") & "' )"
Next
End If
Loop
End If
End If
Next
End If