Public Class frmAssignItems
    Private Sub reloadAssignedItems()
        Try
            cbo_fill(cboLocation, "Location", "LocationID", "tbllocation WHERE Location !='None'")
            cbo_fill(cboCompSet, "ComputerSet", "CompSetID", "tblcompset")

            sql = "SELECT ItemID,`Barcode`, i.`Description`,OnhandQty as 'Quantity' FROM `tblbrand` b,`tblitems` i, `tblparts` p WHERE b.`BrandID`=i.`BrandID` AND i.`PartsID`=p.`PartsID` AND OnhandQty != 0"
            reloadDtg(sql, dtgList)
            dtgList.Columns(0).Visible = False
            cleartext(GroupBox1)
            txtAssignQty.Text = ""
        Catch ex As Exception

        End Try
    End Sub
    Private Sub frmAssignItems_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        reloadAssignedItems()
    End Sub

    Private Sub dtgList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtgList.Click
        Try
            txtAssignQty.Focus()
            sql = "SELECT *,i.Description as ItemDesc " &
            " FROM `tblbrand` b,`tblitems` i, `tblparts` p  " _
             & "WHERE b.`BrandID`=i.`BrandID` AND i.`PartsID`=p.`PartsID` " _
             & " AND OnhandQty > 0 AND ItemID=" & dtgList.CurrentRow.Cells(0).Value
            reloadtxt(sql)

            With dt.Rows(0)
                txtBarcode.Text = .Item("Barcode")
                txtBrand.Text = .Item("Brand")
                txtItems.Text = .Item("ItemDesc")
                txtParts.Text = .Item("Parts")
                'txtLocated.Text = .Item("Location")
                'txtCompSet.Text = .Item("ComputerSet")
                txtRemainingQty.Text = .Item("OnhandQty")
            End With

            If txtParts.Text = "None" Then
                cboCompSet.Enabled = False
                cboCompSet.Text = "None"
            Else
                cboCompSet.Enabled = True
                cbo_fill(cboCompSet, "ComputerSet", "CompSetID", "tblcompset WHERE ComputerSet != 'None'")
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnTransfer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTransfer.Click
        If txtBarcode.Text = "" Then
            MsgBox("Select the equipment you want to assign.", MsgBoxStyle.Exclamation)
            Return
        End If
        If txtAssignQty.Text = "" Or txtAssignQty.Text = "0" Then
            MsgBox("The assign quantity is not valid.", MsgBoxStyle.Exclamation)
            txtAssignQty.Focus()
            Return
        End If
        If txtParts.Text = "RAM" Then
            If Val(txtAssignQty.Text) < 1 Or Val(txtAssignQty.Text) > 4 Then
                MsgBox("Assigned quantity for RAM is not valid!", MsgBoxStyle.Exclamation)
                Return
            End If
        End If



        sql = "SELECT *  FROM `tblassigneditems`  " _
           & " WHERE `LocationID`=" & cboLocation.SelectedValue & " AND  CompSetID=" & cboCompSet.SelectedValue &
           " AND Barcode ='" & txtBarcode.Text & "'"
        reloadtxt(sql)
        If dt.Rows.Count > 0 Then

            Select Case dt.Rows(0).Item("Parts")
                Case "RAM"
                    Dim totalram As Integer
                    totalram = Val(txtAssignQty.Text) + Val(dt.Rows(0).Item("AssignedQuantity"))
                    MsgBox(totalram)
                    If totalram < 1 Or totalram > 4 Then
                        MsgBox("Assigned quantity for RAM is not valid!", MsgBoxStyle.Exclamation)
                    Else
                        sql = "UPDATE `tblassigneditems` SET `AssignedQuantity`='" & totalram & "' WHERE `AssignedID`=" & dt.Rows(0).Item("AssignedID")
                        updates(sql)

                        sqledit = "UPDATE `tblitems` SET   `OnhandQty`=`OnhandQty` - '" & txtAssignQty.Text & "' WHERE Barcode='" & txtBarcode.Text & "'"
                        updates(sqledit)

                        MsgBox("Equipment have been assigned.")
                    End If
                Case Else
                    MsgBox(txtParts.Text & " already exist in the computer set", MsgBoxStyle.Exclamation)
            End Select
        Else

            Dim compset As Integer = 0
            If cboCompSet.Text = "None" Then
                compset = 1
            Else
                compset = cboCompSet.SelectedValue
            End If

            sql = "INSERT INTO `tblassigneditems` (`Barcode`, `Description`, `AssignedQuantity`,`AssignedOnhandQty`,`Parts`, `LocationID`,`CompSetID`) " &
            " VALUES ('" & txtBarcode.Text & "','" & txtItems.Text & "','" & txtAssignQty.Text & "','" & txtAssignQty.Text & "','" & txtParts.Text & "','" & cboLocation.SelectedValue & "'," & compset & ")"
            create(sql)

            sqledit = "UPDATE `tblitems` SET   `OnhandQty`=`OnhandQty` - '" & txtAssignQty.Text & "' WHERE Barcode='" & txtBarcode.Text & "'"
            updates(sqledit)

            'sql = "UPDATE `tblitems` SET `LocationID`=" & cboLocation.SelectedValue & ",`CompSetID`=" & compset & ",`Remarks`='Transfered' WHERE `ItemID`=" & dtgList.CurrentRow.Cells(0).Value
            'updates(sql)


            MsgBox("Equipment have been assigned.")

            reloadAssignedItems()
        End If
    End Sub
    Private Sub txtAssignQty_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtAssignQty.KeyUp
        If Val(txtRemainingQty.Text) < Val(txtAssignQty.Text) Then
            MsgBox("The assign quantity is greater than the actual quantity!", MsgBoxStyle.Exclamation)
            txtAssignQty.Text = ""
        End If
    End Sub

    Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
        sql = "SELECT ItemID,`Barcode`, i.`Description`,OnhandQty as 'Quantity' FROM `tblbrand` b,`tblitems` i, `tblparts` p WHERE b.`BrandID`=i.`BrandID` AND i.`PartsID`=p.`PartsID` AND OnhandQty > 0 " &
        " AND (Barcode Like '%" & txtSearch.Text & "%' Or  i.`Description` Like '%" & txtSearch.Text & "%')"
        reloadDtg(sql, dtgList)
        dtgList.Columns(0).Visible = False
    End Sub
End Class