ListView3.Items.Clear()
Dim row As Integer = 0
Dim ProOrder_Bom, fg_fg_Bom As String
'วนลูปแถวทั้งหมดของ ListView
For row = 0 To Me.ListView2.Items.Count - 1
' If row = 5 Then
'Exit For
' End If
ProOrder_Bom = Me.ListView2.Items(row).SubItems(2).Text
fg_fg_Bom = Me.ListView2.Items(row).SubItems(9).Text
'FG_FG = fg_fg_Bom
sql = "SELECT Count(Pro_Order)AS C_ProOrder, Pro_Order"
sql &= " FROM MB51"
sql &= " WHERE Plnt='" & txt_plant.Text & "' and MvT=261 and Material NOT BETWEEN '10000000' AND '19999999' and Pro_Order='" & ProOrder_Bom & "'"
'sql &= " Order by Plnt asc, Pro_Order asc, Material asc, Batch asc"
sql &= " GROUP BY Pro_Order"
Dim cn As New SqlConnection(strConxx)
If cn.State = ConnectionState.Open Then cn.Close()
cn.Open()
Dim comx As New SqlCommand(sql, cn)
Dim dr_xz As SqlDataReader
dr_xz = comx.ExecuteReader
Dim k_xz As Integer = 0
If dr_xz.HasRows <> 0 Then
dr_xz.Read()
k_xz = dr_xz.Item("C_ProOrder").ToString()
'MsgBox(dr_xz.Item("C_ProOrder").ToString())
Else
k_xz = 0
End If
'''''''''''''''''''''''''''''''เริ่ม เงือนไข'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If k_xz > 0 Then
sql = "SELECT Pro_Order, Plnt, Material, Description, Mat_Doc, Batch, Qty_in_UnE, EUn, Postg_Date"
sql &= " FROM MB51"
sql &= " WHERE Plnt='" & txt_plant.Text & "' and MvT=261 and Material NOT BETWEEN '10000000' AND '19999999' and Pro_Order='" & ProOrder_Bom & "'"
sql &= " Order by Plnt asc, Pro_Order asc, Material asc, Batch asc"
If cn.State = ConnectionState.Open Then cn.Close()
cn.Open()
Dim com As New SqlCommand(sql, cn)
'com.CommandTimeout = 0
Dim dr_x As SqlDataReader
dr_x = com.ExecuteReader
Dim k_x As Integer = 0
If dr_x.HasRows <> 0 Then
Do While dr_x.Read()
k_x = k_x + 1
If k_x = 1 Then
Child_name = dr_x.Item("Material").ToString()
Child_Description = dr_x.Item("Description").ToString()
Child_ProOrder = dr_x.Item("Pro_Order").ToString()
Child_Plant = dr_x.Item("Plnt").ToString()
Child_MatDoc = dr_x.Item("Mat_Doc").ToString()
Child_Batch = dr_x.Item("Batch").ToString()
Child_Qty = dr_x.Item("Qty_in_UnE").ToString()
Child_EUn = dr_x.Item("EUn").ToString()
Child_PostingDate = Fdate.dtformatCurrentInfo_with_ddmmyyyy(dr_x.Item("Postg_Date").ToString())
Qty_Sum_Child = Child_Qty
Postg_Date_Sum_Child = Child_PostingDate
If k_x = k_xz Then
check_12 = Mid(Child_name, 1, 1)
If check_12 = "1" Or check_12 = "2" Then
Check_FGx = "0"
Else
Check_FGx = "1"
End If
Dim newItem As New ListViewItem(New String() {Child_name, Child_Description, Child_ProOrder, Child_Plant, Child_MatDoc, Child_Batch, Qty_Sum_Child, Child_EUn, Postg_Date_Sum_Child, fg_fg_Bom, Check_FGx})
Me.ListView3.Items.Add(newItem)
End If
'''''''''''''''''''''''''''''''''''''''''''
Else
Child_namex = dr_x.Item("Materail").ToString()
Child_Descriptionx = dr_x.Item("Description").ToString()
Child_ProOrderx = dr_x.Item("Pro_Order").ToString()
Child_Plantx = dr_x.Item("Plnt").ToString()
Child_MatDocx = dr_x.Item("Mat_Doc").ToString()
Child_Batchx = dr_x.Item("Batch").ToString()
Child_Qtyx = dr_x.Item("Qty_in_UnE").ToString()
Child_EUnx = dr_x.Item("EUn").ToString()
Child_PostingDatex = Fdate.dtformatCurrentInfo_with_ddmmyyyy(dr_x.Item("Postg_Date").ToString())
If Child_namex = Child_name And Child_Batchx = Child_Batch And Child_ProOrderx = Child_ProOrder Then
Qty_Sum_Child = Qty_Sum_Child + Child_Qtyx
If Child_PostingDatex <= Child_PostingDate Then
Postg_Date_Sum_Child = Child_PostingDatex
End If
If k_x = k_xz Then
check_12 = Mid(Child_name, 1, 1)
If check_12 = "1" Or check_12 = "2" Then
Check_FGx = "0"
Else
Check_FGx = "1"
End If
Dim newItem As New ListViewItem(New String() {Child_name, Child_Description, Child_ProOrder, Child_Plant, Child_MatDoc, Child_Batch, Qty_Sum_Child, Child_EUn, Postg_Date_Sum_Child, fg_fg_Bom, Check_FGx})
Me.ListView3.Items.Add(newItem)
End If
Child_name = Child_namex
Child_Description = Child_Descriptionx
Child_ProOrder = Child_ProOrderx
Child_Plant = Child_Plantx
Child_MatDoc = Child_MatDocx
Child_Batch = Child_Batch
Child_Qty = Child_Qtyx
Child_EUn = Child_EUnx
Child_PostingDate = Child_PostingDatex
Else
If k_x = k_xz Then
check_12 = Mid(Child_name, 1, 1)
If check_12 = "1" Or check_12 = "2" Then
Check_FGx = "0"
Else
Check_FGx = "1"
End If
Dim newItem As New ListViewItem(New String() {Child_name, Child_Description, Child_ProOrder, Child_Plant, Child_MatDoc, Child_Batch, Qty_Sum_Child, Child_EUn, Postg_Date_Sum_Child, fg_fg_Bom, Check_FGx})
Me.ListView3.Items.Add(newItem)
End If
End If '''''''''''''''''''''''''''''''''''''''''''''''
End If
Loop
End If
cn.Close()
End If
'''''''''''''''''''''''''''''''''''''''''''''''''''ในเงื่อนใข จบ'''''''''''''''''''''''''''''''''''''
cn.Close()
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Next