If LvReceived.Items.Count = 0 Then
MessageBox.Show("กรุณาเลือกรายการที่จะรับก่อนครับ!!!", "คำเตือน", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
If MessageBox.Show("คุณต้องการบันทึกการรับสินค้าใช่หรือไม่", "ผลการตรวจสอบ", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
Exit Sub
End If
With cn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
For i As Integer = 0 To LvReceived.Items.Count - 1
'/////Update จำนวนรับ ลง ตาราง OrderDetail
Dim UpRe As String = ""
UpRe = ("Update OrderDetail")
UpRe &= (" set NumberToReceived = NumberToReceived + " & LvReceived.Items(i).SubItems(4).Text & " where ProductID = @ProID And OrderID = @OrderID")
With cm
.CommandType = CommandType.Text
.CommandText = UpRe
.Connection = cn
.Parameters.Clear()
.Parameters.Add("@ProID", SqlDbType.NVarChar, 10).Value = LvReceived.Items(i).SubItems(1).Text
.Parameters.Add("@OrderID", SqlDbType.NVarChar, 10).Value = cbbShowOrder.SelectedValue
.ExecuteNonQuery()
End With
'///////Update จำนวนสินค้าที่รับ + ของเก่า ลงตาราง Product
Dim UpPro As String = ""
UpPro = ("Update Products")
UpPro &= (" set Stock = Stock + " & LvReceived.Items(i).SubItems(4).Text & " where ProductID = @ProID")
With cm
.CommandType = CommandType.Text
.CommandText = UpPro
.Connection = cn
.Parameters.Clear()
.Parameters.Add("@ProID", SqlDbType.NVarChar, 10).Value = LvReceived.Items(i).SubItems(1).Text
.ExecuteNonQuery()
End With
'/////Update ค้างรับ
Dim UpReceivedable As String = ""
UpReceivedable = ("Update OrderDetail")
UpReceivedable &= (" SET Receivable = " & LvReceived.Items(i).SubItems(5).Text & " where ProductID = @ProID And OrderID = @OrderID")
With cm
.CommandType = CommandType.Text
.CommandText = UpReceivedable
.Connection = cn
.Parameters.Clear()
.Parameters.Add("@ProID", SqlDbType.NVarChar, 10).Value = LvReceived.Items(i).SubItems(1).Text
.Parameters.Add("@OrderID", SqlDbType.NVarChar, 10).Value = cbbShowOrder.SelectedValue
.ExecuteNonQuery()
End With
Next
'////////////Update Status////////////////
Dim sql1 As String = ""
For K As Integer = 0 To LvOrders.Items.Count - 1
If LvOrders.Items(K).SubItems(7).Text = 0 Then
sql1 = "update Orders set StatusOrder='2' where OrderID ='" & cbbShowOrder.Text & "'"
With cm
.Parameters.Clear()
.CommandText = sql1
.Connection = cn
.ExecuteNonQuery()
End With
End If
Next
MessageBox.Show("บันทึกการรับเรียบร้อยแล้วครับ!!")
Dim sql As String = ""
Dim s As String = ""
sql = "select Receivable From OrderDetail Where Receivable = 0 "
sql &= " And OrderID = '" & cbbShowOrder.Text & "'"
da = New SqlDataAdapter(sql, cn)
ds = New DataSet
da.Fill(ds, "Re")
If ds.Tables("Re").Rows.Count <> 0 Then
s = "update Orders set StatusOrder='1' where OrderID ='" & cbbShowOrder.Text & "'"
With cm
.Parameters.Clear()
.CommandText = s
.Connection = cn
.ExecuteNonQuery()
End With
ElseIf ds.Tables("Re").Rows.Count = 0 Then
s = "update Orders set StatusOrder='2' where OrderID ='" & cbbShowOrder.Text & "'"
With cm
.Parameters.Clear()
.CommandText = s
.Connection = cn
.ExecuteNonQuery()
End With
End If