Imports System.Data
Imports System.Data.OleDb
Public Class advance_payment
    Dim dr As OleDbDataReader
    Dim cmd As New OleDbCommand
    Dim gSQL As String
    Dim total As Integer
    Private Sub advance_payment_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Call connectdatabase()
        Call Cleartext()
        Call SetGrid()
        Call GetDocNo()
    End Sub
    Private Sub PictureBox5_Click(sender As Object, e As EventArgs) Handles PictureBox5.Click
        wel.Show()
        Hide()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        total = numday.Text * numage.Text
        Label12.Text = total

    End Sub
    Private Sub eno_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles eno.KeyPress
        If e.KeyChar = Chr(13) Then
            gSQL = "Select * From Employeee Where Employee_ID = '" & eno.Text & "'"
            With cmd
                .CommandType = CommandType.Text
                .CommandText = gSQL
                .Connection = cnn
                dr = .ExecuteReader()
            End With
            dr.Read()
            If dr.HasRows Then
                ename.Text = dr.Item("Employee_name").ToString
            Else
                MessageBox.Show("ไม่พบข้อมูลลูกค้า")
            End If
            dr.Close()

        End If
    End Sub

    Private Sub PictureBox3_Click(sender As Object, e As EventArgs) Handles PictureBox3.Click
        gSQL = "Insert Into AdvancePayment Values ("
        gSQL += "'" & TextBox1.Text & "',"
        gSQL += "'" & TextBox2.Text & "',"
        gSQL += "'" & eno.Text & "',"
        gSQL += "'" & ename.Text & "',"
        gSQL += "'" & DateTimePicker1.Text & "',"
        gSQL += "'" & money.Text & "')"
        With cmd
            .CommandType = CommandType.Text
            .CommandText = gSQL
            .Connection = cnn
            .ExecuteNonQuery()
        End With
        MessageBox.Show("เพิ่มรายการใหม่แล้ว", "เพิ่มขอ้มูล", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Call UpdateDocNo()
        Call Cleartext()
        Call GetDocNo()

    End Sub

    Private Sub Cleartext()
        txtSearch.Text = ""
        TextBox1.Text = ""
        TextBox2.Text = ""
        eno.Text = ""
        ename.Text = ""
        DateTimePicker1.Text = ""
        money.Text = ""
        GetDocNo()
    End Sub

    Private Sub PictureBox4_Click(sender As Object, e As EventArgs) Handles PictureBox4.Click
        Dim dt As New DataTable
        Dim cmd As New OleDbCommand
        gSQL = "Select AdPaym_ID,AdPaym_week,Employee_name,AdPaym_Date,AdPaym_Net From AdvancePayment Where Employee_name  Like '%" & txtSearch.Text & "%'"

        With cmd
            .CommandType = CommandType.Text
            .CommandText = gSQL
            .Connection = cnn
            dr = .ExecuteReader
        End With

        If dr.HasRows Then
            dt.Load(dr)
            DataGridView1.DataSource = dt
            SetGrid()

        End If
        dr.Close()
    End Sub
    Private Sub SetGrid()
        If DataGridView1.RowCount > 0 Then
            DataGridView1.Columns(0).HeaderText = "เลขที่การเบิกเงินล่วงหน้า"
            DataGridView1.Columns(1).HeaderText = "วีคที่เบิก"
            DataGridView1.Columns(2).HeaderText = "ชื่อพนักงาน"
            DataGridView1.Columns(2).Width = 150
            DataGridView1.Columns(3).HeaderText = "วันที่เบิก"
            DataGridView1.Columns(4).HeaderText = "จำนวนเงิน"


        End If
    End Sub
    Private Sub GetDocNo()
        gSQL = "Select * From RunDoc Where DocNo = 'ADP'"
        With cmd
            .CommandType = CommandType.Text
            .CommandText = gSQL
            .Connection = cnn
            dr = .ExecuteReader()
        End With
        dr.Read()
        If dr.HasRows Then
            TextBox1.Text = dr.Item("RunNo") + 1
        Else

        End If
        dr.Close()
    End Sub
    Private Sub UpdateDocNo()
        gSQL = "Update RunDoc Set RunNo = '" & TextBox1.Text & "'"
        gSQL += " Where DocNo = 'ADP'"
        With cmd
            .CommandType = CommandType.Text
            .CommandText = gSQL
            .Connection = cnn
            .ExecuteNonQuery()
        End With
    End Sub

    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        advSr.Show()
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        adSR.Show()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        frmadv.Show()

    End Sub


End Class