Public Class frmDamage


    Private Sub relaodDamage()

        With txtSearchBarcode
            .Clear()
            .Focus()
        End With
        cleartext(GroupBox1)
        txtDamageQty.Text = ""
        cbo_fill(cboLocation, "Location", "LocationID", "tbllocation")
        cbo_fill(cboCompSet, "ComputerSet", "CompSetID", "tblcompset")
        cboLocation.Enabled = False
        cboCompSet.Enabled = False

    End Sub

    Private Sub frm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        relaodDamage()
    End Sub

    Private Sub btnTransfer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTransfer.Click
        'MsgBox(locationid)

        If txtBarcode.Text = "" Then
            MsgBox("Select the damaged equipment.", MsgBoxStyle.Exclamation)
            Return
        End If

        'sql = "UPDATE `tblitems` SET `Status`='Damaged',RecievedDate = Now() WHERE `ItemID`=" & dtgList.CurrentRow.Cells(0).Value
        'updates(sql)

        sql = "SELECT * FROM tblitems WHERE Barcode='" & txtBarcode.Text & "'"
        reloadtxt(sql)
        If dt.Rows.Count > 0 Then
            If txtLocated.Text = "None" Then
                sql = "UPDATE `tblitems` SET OnhandQty=OnhandQty-'" & Val(txtDamageQty.Text) & "' WHERE `Barcode`='" & txtBarcode.Text & "'"
                updates(sql)
            ElseIf txtLocated.Text <> "None" And txtCompSet.Text = "None" Then
                sql = "Update `tblassigneditems` set `AssignedOnhandQty`=`AssignedOnhandQty`-'" & Val(txtDamageQty.Text) & "'  WHERE `Barcode`='" & txtBarcode.Text & "' AND `LocationID`=" & cboLocation.SelectedValue & " AND CompSetID=1"
                updates(sql)
            ElseIf txtLocated.Text <> "None" And txtCompSet.Text <> "None" Then
                sql = "Update `tblassigneditems` set `AssignedOnhandQty`=`AssignedOnhandQty`-'" & Val(txtDamageQty.Text) & "'  WHERE `Barcode`='" & txtBarcode.Text & "' AND `LocationID`=" & cboLocation.SelectedValue & " AND CompSetID =" & cboCompSet.SelectedValue
                updates(sql)
            End If

            With dt.Rows(0)
                sql = "INSERT INTO `tbldamage` (`Barcode`, `Description`, `DamagedQuantity`, `AssignedArea`, `DateDamaged`) " &
                        "Values ('" & .Item("Barcode") & "','" & .Item("Description") & "',1,'" & txtLocated.Text &
                        "',Now())"
                create(sql)
            End With
        End If

        MsgBox("items already damaged.")
        relaodDamage()

        Return

    End Sub



    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub
    Dim locationid As Integer = 0
    Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click

        Try
            Dim qty As Integer = 0
            Dim location As String = "None"
            Dim comset As String = "None"

            locationid = cboLocation.SelectedValue

            If cboLocation.Text = "None" Then
                sql = "SELECT * " &
                " FROM  `tblitems` i,tblparts p " _
                 & " WHERE i.PartsID=p.PartsID AND OnhandQty AND Barcode='" & txtSearchBarcode.Text & "'"
                reloadtxt(sql)
                If dt.Rows.Count > 0 Then
                    qty = dt.Rows(0).Item("OnhandQty")
                    location = "None"
                    comset = "None"
                Else
                    MsgBox("No record found!", MsgBoxStyle.Exclamation)
                    relaodDamage()
                End If
            Else
                sql = "SELECT * " &
                  " FROM  `tblassigneditems` a,  `tbllocation` l,tblcompset c " _
                   & " WHERE a.`LocationID`=l.`LocationID` AND a.CompSetID=c.CompSetID AND AssignedOnhandQty>0 AND Barcode='" & txtSearchBarcode.Text & "' AND l.`LocationID`=" & cboLocation.SelectedValue & " AND a.CompSetID=" & cboCompSet.SelectedValue
                reloadtxt(sql)

                If dt.Rows.Count > 0 Then
                    qty = dt.Rows(0).Item("AssignedOnhandQty")
                    location = dt.Rows(0).Item("Location")
                    comset = dt.Rows(0).Item("ComputerSet")
                Else
                    MsgBox("No record found!", MsgBoxStyle.Exclamation)
                    relaodDamage()
                End If
            End If
            cleartext(GroupBox1)
            If dt.Rows.Count > 0 Then
                With dt.Rows(0)
                    txtBarcode.Text = .Item("Barcode")
                    txtItems.Text = .Item("Description")
                    txtParts.Text = .Item("Parts")
                    txtLocated.Text = location
                    txtCompSet.Text = comset
                    txtActualQty.Text = qty
                End With
                With txtDamageQty
                    .Clear()
                    .Focus()
                End With
            End If

        Catch ex As Exception
            Me.Text = ex.Message
        End Try
    End Sub

    Private Sub txtSearchBarcode_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSearchBarcode.KeyPress
        'Try
        '    sql = "SELECT * FROM `tblitems` WHERE `Barcode`='" & txtSearchBarcode.Text & "'"
        '    MsgBox(sql.ToString())
        '    reloadtxt(sql)
        '    If dt.Rows.Count > 0 Then
        '        If dt.Rows(0).Item("PartsID") = 1 Then
        '            cboCompSet.Enabled = False
        '            cboLocation.Enabled = True
        '        Else
        '            cboLocation.Enabled = True
        '            cboCompSet.Enabled = True
        '        End If
        '    Else
        '        MsgBox("No record found!", MsgBoxStyle.Exclamation)
        '        'relaodDamage()
        '    End If
        'Catch ex As Exception
        '    Me.Text = ex.Message
        'End Try
    End Sub


    Private Sub txtDamageQty_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtDamageQty.KeyPress
        numbersOnly(e)
    End Sub

    Private Sub txtDamageQty_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDamageQty.TextChanged
        If Val(txtActualQty.Text) < Val(txtDamageQty.Text) Then
            MsgBox("The quantity you want to save is greater than the actual quantity!", MsgBoxStyle.Exclamation)
            txtDamageQty.Text = ""
        ElseIf txtDamageQty.Text = "0" Then
            MsgBox("Quantity cannot be 0!", MsgBoxStyle.Exclamation)
            txtDamageQty.Text = ""
        End If
    End Sub

    Private Sub txtSearchBarcode_TextChanged(sender As Object, e As EventArgs) Handles txtSearchBarcode.TextChanged
        Try
            sql = "SELECT * FROM `tblitems` WHERE `Barcode`='" & txtSearchBarcode.Text & "'"
            reloadtxt(sql)
            If dt.Rows.Count > 0 Then
                If dt.Rows(0).Item("PartsID") = 1 Then
                    cboCompSet.Enabled = False
                    cboLocation.Enabled = True
                Else
                    cboLocation.Enabled = True
                    cboCompSet.Enabled = True
                End If
            Else
                MsgBox("No record found!", MsgBoxStyle.Exclamation)
                'relaodDamage()
            End If
        Catch ex As Exception
            Me.Text = ex.Message
        End Try
    End Sub
End Class