Public Class frmTransferItems

    Private Sub frmTransferItems_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        reloadTransfer()

    End Sub
    Private Sub reloadTransfer()
        cbo_fill(cboLocation, "Location", "LocationID", "tbllocation WHERE Location !='None'")
        cbo_fill(cboCompSet, "ComputerSet", "CompSetID", "tblcompset WHERE ComputerSet != 'None'")
        sql = "SELECT AssignedID,`Barcode`,`Description`,`Location`, `ComputerSet`,AssignedOnhandQty as 'AssignedQuantity' FROM `tblassigneditems`  a,`tbllocation` l,`tblcompset` c WHERE a.`LocationID`=l.`LocationID`  AND a.`CompSetID`=c.`CompSetID` AND AssignedOnhandQty > 0"
        'sql = "SELECT ItemID,`Barcode`, i.`Description`,`Location`, `ComputerSet` FROM `tblbrand` b,`tblitems` i, `tblparts` p, `tbllocation` l,tblcompset c WHERE b.`BrandID`=i.`BrandID` AND i.`PartsID`=p.`PartsID` AND i.`LocationID`=l.`LocationID` AND i.CompSetID=c.CompSetID AND Status='Good'"
        reloadDtg(sql, dtgList)
        dtgList.Columns(0).Visible = False
        cleartext(GroupBox1)
        cleartext(GroupBox2)

    End Sub

    Private Sub dtgList_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtgList.Click
        Try
            sql = "SELECT * " &
            " FROM  `tblassigneditems` a,  `tbllocation` l,tblcompset c " _
             & "WHERE a.`LocationID`=l.`LocationID` AND a.CompSetID=c.CompSetID AND AssignedID=" & dtgList.CurrentRow.Cells(0).Value
            reloadtxt(sql)

            With dt.Rows(0)
                txtBarcode.Text = .Item("Barcode")
                txtItems.Text = .Item("Description")
                txtParts.Text = .Item("Parts")
                txtLocated.Text = .Item("Location")
                txtCompSet.Text = .Item("ComputerSet")
                txtRemainingQty.Text = .Item("AssignedOnhandQty")
            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

            'With dtgList.CurrentRow
            '    txtBarcode.Text = .Cells(1).Value
            '    'txtBrand.Text = .Item("Brand")
            '    txtItems.Text = .Cells(2).Value
            '    'txtParts.Text = .Item("Parts")
            '    txtLocated.Text = .Cells(3).Value
            '    txtCompSet.Text = .Cells(4).Value
            '    txtRemainingQty.Text = .Cells(5).Value
            'End With

            'If txtCompSet.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 computer parts you want to transfer.", MsgBoxStyle.Exclamation)
            Return
        End If


        If txtTransferQty.Text = "" Or txtTransferQty.Text = "0" Then
            MsgBox("The quantity you want to transfer is not valid.", MsgBoxStyle.Exclamation)
            Return
        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
            MsgBox(txtItems.Text & " already exist in the computer set", MsgBoxStyle.Exclamation)
        Else



            If txtLocated.Text = cboLocation.Text And txtParts.Text = "None" Then

                'sql = "UPDATE `tblassigneditems` SET `AssignedOnhandQty`=`AssignedOnhandQty`+'" & txtTransferQty.Text & "' WHERE Parts='None' AND `Barcode`='" & txtBarcode.Text & "'"
                'updates(sql)
                MsgBox("Equipment cannot be transfer due to the same location.", MsgBoxStyle.Exclamation)
            Else

                sql = "UPDATE `tblassigneditems` SET `AssignedOnhandQty`=`AssignedOnhandQty`-'" & txtTransferQty.Text & "' WHERE `AssignedID`=" & dtgList.CurrentRow.Cells(0).Value
                updates(sql)

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

                sql = "INSERT INTO  `tbltransfer` (AssignedID,`Barcode`, `Item`,	TransferedQuantity,  `LocationFROM`, `LocationTO`, `PCFrom`, `PCTo`, `TransferDate`) " &
                " VALUES (" & dtgList.CurrentRow.Cells(0).Value & ",'" & txtBarcode.Text & "','" & txtItems.Text & "','" & txtTransferQty.Text & "' ,'" & txtLocated.Text & "','" & cboLocation.Text & "','" & txtCompSet.Text & "','" & cboCompSet.Text & "',DATE(NOW()))"
                create(sql)



                sql = "SELECT * FROM tblassigneditems WHERE Parts = 'None' and Barcode ='" & txtBarcode.Text & "' AND `LocationID`=" & cboLocation.SelectedValue
                reloadtxt(sql)

                If dt.Rows.Count > 0 Then
                    sql = "UPDATE `tblassigneditems` SET `AssignedOnhandQty`=`AssignedOnhandQty`+'" & txtTransferQty.Text & "' WHERE Parts='None' AND `Barcode`='" & txtBarcode.Text & "' AND `LocationID`=" & cboLocation.SelectedValue
                    updates(sql)
                Else
                    sql = "INSERT INTO `tblassigneditems` (`Barcode`, `Description`, `AssignedQuantity`,`AssignedOnhandQty`,`Parts`, `LocationID`,`CompSetID`) " &
                       " VALUES ('" & txtBarcode.Text & "','" & txtItems.Text & "',0,'" & txtTransferQty.Text & "','" & txtParts.Text & "','" & cboLocation.SelectedValue & "'," & compset & ")"
                    create(sql)
                End If
                MsgBox("Equipment has been transferred.")
            End If


            reloadTransfer()


        End If
    End Sub

    Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
        sql = "SELECT AssignedID,`Barcode`,`Description`,`Location`, `ComputerSet`,AssignedOnhandQty as 'AssignedQuantity' FROM `tblassigneditems`  a,`tbllocation` l,`tblcompset` c WHERE a.`LocationID`=l.`LocationID`  AND a.`CompSetID`=c.`CompSetID` AND AssignedOnhandQty > 0 " &
        " AND (Barcode Like '%" & txtSearch.Text & "%' OR Description Like '%" & txtSearch.Text & "%')"
        reloadDtg(sql, dtgList)
        dtgList.Columns(0).Visible = False

    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub txtTransferQty_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtTransferQty.KeyUp
        If Val(txtRemainingQty.Text) < Val(txtTransferQty.Text) Then
            MsgBox("The quantity you want to transfer is greater than the actual quantity!", MsgBoxStyle.Exclamation)
            txtTransferQty.Text = ""
        End If
    End Sub


End Class