Option Strict Off
Imports System.Data.SqlClient
Public Class 人員行政物料領用記錄表
    Dim conn As New SqlConnection
    Dim da As New SqlDataAdapter
    Dim cmd As New SqlCommand
    Dim ds As New DataSet : Dim ds1 As New DataSet : Dim ds2 As New DataSet
    Dim dr As SqlDataReader
    Dim 編號 As String
    Dim KKKJJ As String : Dim KKKJJ2 As String : Dim KKKJJ3 As String : Dim KKK3 As String : Dim KKK4 As String
    Dim XXCC As String : Dim DTP As String
    Private Sub Set_人員清單()
        DataGridView2.DataSource = Nothing : ds1.Clear()
        DataGridView2.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView2.ColumnHeadersHeight = 25
        DataGridView2.AllowUserToAddRows = False
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 人事資料表.員工編號, 使用者權限管理表.姓名 AS 系統登入姓名, 人事資料表.姓名, 人事資料表.入職日期, 人事資料表.離職日期
                FROM 使用者權限管理表 LEFT OUTER JOIN
                            人事資料表 ON 使用者權限管理表.員工編號 = 人事資料表.員工編號 AND 
                            使用者權限管理表.姓名 = 人事資料表.系統登入姓名
                WHERE (使用者權限管理表.CC49 = 0) AND (人事資料表.員工編號 IS NOT NULL) AND (人事資料表.離職日期 LIKE '在職')
                ORDER BY 使用者權限管理表.職稱"
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds1) : DataGridView2.DataSource = ds1.Tables(0) : conn.Close() : Set_grid1()
    End Sub
    Private Sub Set_grid1()
        DataGridView2.Columns(0).Visible = False : DataGridView2.Columns(1).Width = 150 : DataGridView2.Columns(2).Visible = False : DataGridView2.Columns(3).Visible = False
        DataGridView2.Columns(4).Visible = False
    End Sub
    Private Sub Set_清單()
        DataGridView1.DataSource = Nothing : ds.Clear()
        DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView1.ColumnHeadersHeight = 25
        DataGridView1.AllowUserToAddRows = False
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 流水號, 人員, 日期, 物料, 數量, 單位 FROM 人員行政物料領用紀錄表 WHERE (人員 LIKE N'" & XXCC & "') " & KKKJJ3 & "
                ORDER BY 日期"
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds) : DataGridView1.DataSource = ds.Tables(0) : conn.Close() : Set_grid()
    End Sub
    Private Sub Set_grid()
        DataGridView1.Columns(0).Width = 150 : DataGridView1.Columns(1).Width = 150 : DataGridView1.Columns(2).Width = 150 : DataGridView1.Columns(3).Width = 934
        DataGridView1.Columns(4).Width = 150 : DataGridView1.Columns(5).Width = 150
        DataGridView1.EditMode = DataGridViewEditMode.EditOnEnter
    End Sub
    Private Sub ComboBox1下拉表單資料載入()
        conn.Close()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT  年份 FROM 年份清單 ORDER BY 年份 DESC"
        cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
        ComboBox1.Items.Clear()
        While (dr.Read()) : ComboBox1.Items.Add(dr("年份")) : End While
        conn.Close()
    End Sub
    Private Sub ComboBox3下拉表單資料載入()
        conn.Close()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT  行政物料 FROM 人員行政物料表 ORDER BY 行政物料"
        cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
        ComboBox3.Items.Clear()
        While (dr.Read()) : ComboBox3.Items.Add(dr("行政物料")) : End While
        conn.Close()
    End Sub
    Private Sub Set_日期格式轉換()
        DTP = Format(Today(), "yyyy/MM/dd")
    End Sub
    Private Sub 人員行政物料領用記錄表_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.MdiParent = WINPROFIT_ERP_SYS : Me.WindowState = 2 : Me.AutoScroll = True
        ComboBox1下拉表單資料載入() : ComboBox3下拉表單資料載入()
        Set_人員清單()
        ComboBox1.Text = Year(Today) : ComboBox2.SelectedIndex = Month(Today) - 1
        KKKJJ3 = "AND (日期 LIKE N'" & KKK3 & "/" & KKK4 & "%') "
    End Sub
    Private Sub DataGridView2_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView2.CellClick
        If e.RowIndex = -1 Then : Else
            XXCC = DataGridView2.Rows(e.RowIndex).Cells("姓名").Value
            KKKJJ3 = "AND (日期 LIKE N'" & KKK3 & "/" & KKK4 & "%') "
            Set_清單()
        End If
    End Sub
    Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        If e.RowIndex = -1 Then : Else
            TextBox4.Text = DataGridView1.Rows(e.RowIndex).Cells("流水號").Value
        End If
    End Sub
    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        If ComboBox3.Text = "" Or XXCC = "" Then
            MsgBox("沒有選擇物料或人員")
        Else
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "SELECT TOP (1) 流水號 FROM 人員行政物料領用紀錄表 ORDER BY 流水號 DESC"
            cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
            If dr.Read() Then : 編號 = dr("流水號").ToString : Else : 編號 = "MR00000000" : End If
            conn.Close()
            Dim NUM1 As Integer
            NUM1 = Double.Parse(Strings.Right(編號, 8)) + 1
            If NUM1 < 10 Then : 編號 = "MR" & "0000000" & NUM1
            ElseIf NUM1 > 9 And NUM1 < 100 Then : 編號 = "MR" & "000000" & NUM1
            ElseIf NUM1 > 99 And NUM1 < 1000 Then : 編號 = "MR" & "00000" & NUM1
            ElseIf NUM1 > 999 And NUM1 < 10000 Then : 編號 = "MR" & "0000" & NUM1
            ElseIf NUM1 > 9999 And NUM1 < 100000 Then : 編號 = "MR" & "000" & NUM1
            ElseIf NUM1 > 99999 And NUM1 < 100000 Then : 編號 = "MR" & "00" & NUM1
            ElseIf NUM1 > 999999 And NUM1 < 1000000 Then : 編號 = "MR" & "0" & NUM1
            ElseIf NUM1 > 9999999 Then : 編號 = "MR" & NUM1
            End If
            Set_日期格式轉換()
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "INSERT INTO 人員行政物料領用紀錄表 (流水號, 人員, 日期, 物料, 數量, 單位) " &
                   "VALUES (N'" & 編號 & "',N'" & XXCC & "',N'" & DTP & "',N'" & ComboBox3.Text & "','0','')"
            cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
            Set_清單()
        End If
    End Sub
    Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click
        Dim aa As MsgBoxResult
        aa = MsgBox("確定要刪除該筆資料?", MsgBoxStyle.OkCancel)
        If AA = MsgBoxResult.Ok Then
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "DELETE FROM 人員行政物料領用紀錄表 WHERE (流水號 = '" & TextBox4.Text & "')"
            cmd.CommandText = SQL1 : cmd.Connection = conn : cmd.ExecuteNonQuery() : conn.Close()
            MsgBox("刪除完成") : Set_清單()
        End If
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        For i As Integer = 0 To DataGridView1.Rows.Count - 1
            資料數 = DataGridView1.Rows.Count : MyModule1.進度條()
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "UPDATE 人員行政物料領用紀錄表 SET  數量 = '" & DataGridView1.Rows(i).Cells("數量").Value & "', 單位 = '" & DataGridView1.Rows(i).Cells("單位").Value & "' " &
                   "WHERE (流水號 LIKE N'" & DataGridView1.Rows(i).Cells("流水號").Value & "')"
            cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
        Next : WINPROFIT_ERP_SYS.ToolStripProgressBar1.Value = 0 : 最終數 = 0
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        清單修改.Close() : 行政物料 = True : 清單修改.ShowDialog() : ComboBox3下拉表單資料載入() : 行政物料 = False
    End Sub
    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        KKK3 = ComboBox1.Text
        KKKJJ3 = "AND (日期 LIKE N'" & KKK3 & "/" & KKK4 & "%') "
    End Sub
    Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
        KKK4 = ComboBox2.Text
        KKKJJ3 = "AND (日期 LIKE N'" & KKK3 & "/" & KKK4 & "%') "
    End Sub
End Class