Option Strict Off
Public Class 倉儲入庫
    ReadOnly ds As New DataSet : ReadOnly ds1 As New DataSet : ReadOnly ds2 As New DataSet : ReadOnly ds3 As New DataSet
    Dim 首次開啟 As Boolean = False : Dim 選擇 As String : Dim NUM1 As Integer : Dim EDR As Integer = 0 : Dim ESTR As String
    Private Sub Set_清單()
        銷售單號_dgv.DataSource = Nothing : ds1.Clear()
        銷售單號_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        銷售單號_dgv.ColumnHeadersHeight = 40
        銷售單號_dgv.AllowUserToAddRows = False
        銷售單號_dgv.RowTemplate.Height = 30
        SQL_倉儲入庫_銷售單號()
        da.Fill(ds1) : 銷售單號_dgv.DataSource = ds1.Tables(0) : conn.Close()

        銷售單號_dgv.Columns(0).FillWeight = 50

        銷售單號_dgv.Columns("銷售單號").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        銷售單號_dgv.Columns("銷售單號").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter

        銷售單號_dgv.Columns(0).ReadOnly = True
    End Sub
    Private Sub Set_清單1()
        供應商_dgv.DataSource = Nothing : ds.Clear()
        供應商_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        供應商_dgv.ColumnHeadersHeight = 40
        供應商_dgv.AllowUserToAddRows = False
        供應商_dgv.RowTemplate.Height = 30
        SQL_倉儲入庫_供應商()
        da.Fill(ds) : 供應商_dgv.DataSource = ds.Tables(0) : conn.Close()

        供應商_dgv.Columns(0).FillWeight = 50 : 供應商_dgv.Columns(1).FillWeight = 80

        供應商_dgv.Columns("供應商").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        供應商_dgv.Columns("供應商").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        供應商_dgv.Columns("編號").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        供應商_dgv.Columns("編號").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter

        供應商_dgv.Columns(0).ReadOnly = True : 供應商_dgv.Columns(1).ReadOnly = True
    End Sub
    Private Sub Set_清單3()
        進貨日期_dgv.DataSource = Nothing : ds3.Clear()
        進貨日期_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        進貨日期_dgv.ColumnHeadersHeight = 40
        進貨日期_dgv.AllowUserToAddRows = False
        進貨日期_dgv.RowTemplate.Height = 30
        SQL_倉儲入庫_進廠日()
        da.Fill(ds3) : 進貨日期_dgv.DataSource = ds3.Tables(0) : conn.Close()

        進貨日期_dgv.Columns(0).FillWeight = 50

        進貨日期_dgv.Columns("預計進廠").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        進貨日期_dgv.Columns("預計進廠").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter

        進貨日期_dgv.Columns(0).ReadOnly = True
    End Sub
    Private Sub Set_清單2()
        採購清單_dgv.DataSource = Nothing : ds2.Clear()
        採購清單_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        採購清單_dgv.ColumnHeadersHeight = 40
        採購清單_dgv.AllowUserToAddRows = False
        採購清單_dgv.RowTemplate.Height = 30
        If 小間距_ch.Checked = False And 中間距_ch.Checked = False And 大間距_ch.Checked = True Then
            採購清單_dgv.RowTemplate.Height = 95
        ElseIf 小間距_ch.Checked = False And 中間距_ch.Checked = True And 大間距_ch.Checked = False Then
            採購清單_dgv.RowTemplate.Height = 60
        ElseIf 小間距_ch.Checked = True And 中間距_ch.Checked = False And 大間距_ch.Checked = False Then
            採購清單_dgv.RowTemplate.Height = 25
        End If

        SQL_倉儲入庫_採購明細()
        da.Fill(ds2) : 採購清單_dgv.DataSource = ds2.Tables(0) : conn.Close()

        採購清單_dgv.Columns(0).Visible = False : 採購清單_dgv.Columns(1).FillWeight = 80 : 採購清單_dgv.Columns(2).Visible = False : 採購清單_dgv.Columns(3).FillWeight = 80
        採購清單_dgv.Columns(4).FillWeight = 80 : 採購清單_dgv.Columns(5).FillWeight = 200 : 採購清單_dgv.Columns(6).Visible = False : 採購清單_dgv.Columns(7).FillWeight = 100
        採購清單_dgv.Columns(8).Visible = False : 採購清單_dgv.Columns(9).FillWeight = 160 : 採購清單_dgv.Columns(10).Visible = False : 採購清單_dgv.Columns(11).FillWeight = 95
        採購清單_dgv.Columns(12).FillWeight = 145 : 採購清單_dgv.Columns(13).Visible = False : 採購清單_dgv.Columns(14).FillWeight = 45 : 採購清單_dgv.Columns(15).Visible = False
        採購清單_dgv.Columns(16).Visible = False : 採購清單_dgv.Columns(17).Visible = False : 採購清單_dgv.Columns(18).Visible = False : 採購清單_dgv.Columns(19).Visible = False
        採購清單_dgv.Columns(20).FillWeight = 80 : 採購清單_dgv.Columns(21).Visible = False : 採購清單_dgv.Columns(22).FillWeight = 45 : 採購清單_dgv.Columns(23).Visible = False
        採購清單_dgv.Columns(24).Visible = False

        採購清單_dgv.Columns("數量").DefaultCellStyle.Format = "#,##0" : 採購清單_dgv.Columns("成本").DefaultCellStyle.Format = "#,##0" : 採購清單_dgv.Columns("金額").DefaultCellStyle.Format = "#,##0"
        採購清單_dgv.Columns("數量").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        採購清單_dgv.Columns("數量").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        採購清單_dgv.Columns("成本").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        採購清單_dgv.Columns("成本").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        採購清單_dgv.Columns("金額").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        採購清單_dgv.Columns("金額").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter

        採購清單_dgv.Columns(1).ReadOnly = True : 採購清單_dgv.Columns(3).ReadOnly = True : 採購清單_dgv.Columns(4).ReadOnly = True : 採購清單_dgv.Columns(5).ReadOnly = True
        採購清單_dgv.Columns(7).ReadOnly = True : 採購清單_dgv.Columns(9).ReadOnly = True : 採購清單_dgv.Columns(11).ReadOnly = True : 採購清單_dgv.Columns(12).ReadOnly = True
        採購清單_dgv.Columns(13).ReadOnly = True : 採購清單_dgv.Columns(14).ReadOnly = True : 採購清單_dgv.Columns(15).ReadOnly = True : 採購清單_dgv.Columns(16).ReadOnly = True
        採購清單_dgv.Columns(17).ReadOnly = True : 採購清單_dgv.Columns(18).ReadOnly = True : 採購清單_dgv.Columns(19).ReadOnly = True : 採購清單_dgv.Columns(20).ReadOnly = True
        採購清單_dgv.Columns(21).ReadOnly = True : 採購清單_dgv.Columns(23).ReadOnly = True

        採購清單_dgv.EditMode = DataGridViewEditMode.EditOnEnter
        資料筆數_tb.Text = 採購清單_dgv.Rows.Count

        For I As Integer = 0 To 採購清單_dgv.Rows.Count - 1
            If Val(採購清單_dgv.Rows(I).Cells("數量").Value) < 0 Then
                採購清單_dgv.Rows(I).Cells("數量").Value = Val(採購清單_dgv.Rows(I).Cells("數量").Value) * -1
            End If
        Next
    End Sub
    Private Sub 倉儲入庫_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.MdiParent = YD_ERP_SYS : Me.WindowState = 2 : Me.AutoScroll = True
        大間距_ch.Checked = True
        Set_清單() : Set_清單1() : Set_清單3()
        開始日期_dtp.Value = Today() : 入庫選擇1 = False : 入庫選擇2 = True
        DTP1 = Format(開始日期_dtp.Value, "yyyy/MM/dd") : Set_清單2()
        If YD_ERP_SYS.Size.Width > 1440 And YD_ERP_SYS.Size.Height > 900 Then
            字體_NUD.Value = 9 : Set_清單2()
        ElseIf YD_ERP_SYS.Size.Width <= 1440 And YD_ERP_SYS.Size.Height <= 900 Then
            字體_NUD.Value = 7.6 : Set_清單2()
        End If
        首次開啟 = True
    End Sub
    Private Sub 一般性申請採購單_SizeChanged(sender As Object, e As EventArgs) Handles MyBase.SizeChanged
        If 首次開啟 = True Then
            If YD_ERP_SYS.Size.Width > 1440 And YD_ERP_SYS.Size.Height > 900 Then
                字體_NUD.Value = 9 : Set_清單2()
            ElseIf YD_ERP_SYS.Size.Width <= 1440 And YD_ERP_SYS.Size.Height <= 900 Then
                字體_NUD.Value = 7.6 : Set_清單2()
            End If
        End If
    End Sub
    Private Sub 採購清單_dgv_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles 採購清單_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            物料圖1_pb.Image = Nothing : PA1 = 採購清單_dgv.Rows(e.RowIndex).Cells("料號").Value : SQL_物料檔案_是否有圖()
            If dr.Read() Then
                If dr("是否有圖") = True Then
                    物料圖1_pb.BackgroundImage = Nothing
                    PA = 採購清單_dgv.Rows(e.RowIndex).Cells("料號").Value : SQL_物料圖庫_圖片顯示() '--------------------圖片顯示
                    While dr.Read() = True
                        Dim unused As Byte() = New Byte(-1) {}
                        Dim bytes As Byte() = DirectCast(dr.Item("圖片"), Byte())
                        Dim oStream As New MemoryStream(bytes)
                        物料圖1_pb.Image = Bitmap.FromStream(oStream)
                    End While
                    conn.Close()
                    物料圖1_pb.SizeMode = 4
                Else
                    物料圖1_pb.BackgroundImage = My.Resources.底圖1 : 物料圖1_pb.BackgroundImageLayout = 3
                    物料圖1_pb.Image = Nothing
                End If
            End If : conn.Close()
        End If
    End Sub
    Private Sub 銷售單號_dgv_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles 銷售單號_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            入庫選擇1 = False : 入庫選擇2 = False : PA = 銷售單號_dgv("銷售單號", e.RowIndex).Value.ToString : 選擇 = PA : Set_清單2()
        End If
    End Sub
    Private Sub 進貨日期_dgv_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles 進貨日期_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            入庫選擇1 = False : 入庫選擇2 = True : DTP1 = 進貨日期_dgv("預計進廠", e.RowIndex).Value.ToString : 選擇 = DTP1 : Set_清單2()
            開始日期_dtp.Value = 進貨日期_dgv("預計進廠", e.RowIndex).Value.ToString
        End If
    End Sub
    Private Sub 供應商_dgv_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles 供應商_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            入庫選擇1 = True : 入庫選擇2 = False : PA = 供應商_dgv("編號", e.RowIndex).Value.ToString : 選擇 = PA : Set_清單2()
        End If
    End Sub
    Private Sub 窄間距_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 小間距_ch.Click
        小間距_ch.Checked = True : 中間距_ch.Checked = False : 大間距_ch.Checked = False
        If 入庫選擇1 = False And 入庫選擇2 = True Then : DTP1 = 選擇 : Set_清單2() : Else : PA = 選擇 : Set_清單2() : End If
    End Sub
    Private Sub 中間距_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 中間距_ch.Click
        小間距_ch.Checked = False : 中間距_ch.Checked = True : 大間距_ch.Checked = False
        If 入庫選擇1 = False And 入庫選擇2 = True Then : DTP1 = 選擇 : Set_清單2() : Else : PA = 選擇 : Set_清單2() : End If
    End Sub
    Private Sub 大間距_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 大間距_ch.Click
        小間距_ch.Checked = False : 中間距_ch.Checked = False : 大間距_ch.Checked = True
        If 入庫選擇1 = False And 入庫選擇2 = True Then : DTP1 = 選擇 : Set_清單2() : Else : PA = 選擇 : Set_清單2() : End If
    End Sub
    Private Sub 前一天_bt_Click(sender As Object, e As EventArgs) Handles 前一天_bt.Click
        開始日期_dtp.Value = 開始日期_dtp.Value.AddDays(-1) : 入庫選擇1 = False : 入庫選擇2 = True
        DTP1 = Format(開始日期_dtp.Value, "yyyy/MM/dd") : Set_清單2()
    End Sub
    Private Sub 後一天_bt_Click(sender As Object, e As EventArgs) Handles 後一天_bt.Click
        開始日期_dtp.Value = 開始日期_dtp.Value.AddDays(+1) : 入庫選擇1 = False : 入庫選擇2 = True
        DTP1 = Format(開始日期_dtp.Value, "yyyy/MM/dd") : Set_清單2()
    End Sub
    Private Sub 全部選擇_bt_Click(sender As Object, e As EventArgs) Handles 全部選擇_bt.Click
        For i As Integer = 0 To 採購清單_dgv.Rows.Count - 1 : 採購清單_dgv.Rows(i).Cells("已入庫").Value = True : Next i
    End Sub
    Private Sub 全部取消_bt_Click(sender As Object, e As EventArgs) Handles 全部取消_bt.Click
        For i As Integer = 0 To 採購清單_dgv.Rows.Count - 1 : 採購清單_dgv.Rows(i).Cells("已入庫").Value = False : Next i
    End Sub
    Private Sub Set_日期格式轉換()
        DTP = Format(開始日期_dtp.Value, "yyyy/MM/dd") : DTP1 = Format(開始日期_dtp.Value, "yyyyMMdd")
    End Sub
    Private Sub 確認入庫_bt_Click(sender As Object, e As EventArgs) Handles 確認入庫_bt.Click
        Dim 確認有資料 As Boolean
        For i As Integer = 0 To 採購清單_dgv.Rows.Count - 1
            If 採購清單_dgv.Rows(i).Cells("已入庫").Value = True Then
                確認有資料 = True : Exit For
            End If
        Next i

        If 確認有資料 = False Then
            MsgBox("入庫資料選擇最少要有一筆")
        Else
            Dim aa1 As MsgBoxResult = MsgBox("請確定選擇的入庫資料是否正確?", MsgBoxStyle.OkCancel)
            If aa1 = MsgBoxResult.Ok Then
                Set_日期格式轉換()
                For i As Integer = 1 To 999
                    NUM1 = 0 + i
                    If NUM1 < 10 Then : 調庫單號_tb.Text = "WH-" & DTP1 & "-00" & NUM1
                    ElseIf NUM1 > 9 And NUM1 < 100 Then : 調庫單號_tb.Text = "WH-" & DTP1 & "-0" & NUM1
                    ElseIf NUM1 > 99 Then : 調庫單號_tb.Text = "WH-" & DTP1 & "-" & NUM1
                    End If
                    PA1 = 調庫單號_tb.Text
                    SQL_倉儲明細執行單號_執行單號驗證()
                    If dr.Read() Then : Else : i = 999 : conn.Close() : End If : conn.Close()
                Next
                SQL_倉儲明細執行單號_新增() : conn.Close()
                資料數 = 採購清單_dgv.Rows.Count
                For i As Integer = 0 To 採購清單_dgv.Rows.Count - 1
                    MyModule1.進度條()
                    If 採購清單_dgv.Rows(i).Cells("已入庫").Value = True Then
                        SQL_倉儲明細表_最後一筆資料()
                        If dr.Read() Then : EDR = Double.Parse(Strings.Right(dr("資料流水號").ToString, 9)) : Else : EDR = 0 : End If
                        conn.Close() : EDR += 1
                        If EDR < 10 Then : ESTR = "WH" & "00000000" & EDR
                        ElseIf EDR > 9 And EDR < 100 Then : ESTR = "WH" & "0000000" & EDR
                        ElseIf EDR > 99 And EDR < 1000 Then : ESTR = "WH" & "000000" & EDR
                        ElseIf EDR > 999 And EDR < 10000 Then : ESTR = "WH" & "00000" & EDR
                        ElseIf EDR > 9999 And EDR < 100000 Then : ESTR = "WH" & "0000" & EDR
                        ElseIf EDR > 99999 And EDR < 100000 Then : ESTR = "WH" & "000" & EDR
                        ElseIf EDR > 999999 And EDR < 1000000 Then : ESTR = "WH" & "00" & EDR
                        ElseIf EDR > 9999999 And EDR < 10000000 Then : ESTR = "WH" & "0" & EDR
                        ElseIf EDR > 99999999 Then : ESTR = "WH" & EDR : End If

                        Set_日期格式轉換()
                        PA3 = ESTR : PA4 = 調庫單號_tb.Text
                        PA5 = 採購清單_dgv.Rows(i).Cells("倉級").Value.ToString : PA6 = 採購清單_dgv.Rows(i).Cells("倉儲狀態").Value.ToString
                        PA2 = 採購清單_dgv.Rows(i).Cells("料號").Value.ToString : NU1 = 採購清單_dgv.Rows(i).Cells("數量").Value : NU2 = 0
                        PA1 = 採購清單_dgv.Rows(i).Cells("備註").Value.ToString : PA = 採購清單_dgv.Rows(i).Cells("銷售單號").Value.ToString
                        PA7 = 採購清單_dgv.Rows(i).Cells("物料狀態").Value.ToString : PA8 = 採購清單_dgv.Rows(i).Cells("訂製狀態").Value.ToString
                        PA9 = gUserName : PA10 = 採購清單_dgv.Rows(i).Cells("資料流水號").Value.ToString
                        SQL_倉儲明細表_倉儲入庫() : SQL_倉儲入庫_採購控制表修改() : conn.Close()
                    End If
                Next : YD_ERP_SYS.ToolStripProgressBar1.Value = 0 : 最終數 = 0
            End If
            Set_清單() : Set_清單1() : Set_清單2() : Set_清單3()
            MsgBox("選定資料入庫完成")
        End If
    End Sub
    Private Sub 重新整理_tsm_Click(sender As Object, e As EventArgs) Handles 重新整理_tsm.Click
        Set_清單() : Set_清單1() : Set_清單3()
    End Sub
    Private Sub 字體_NUD_ValueChanged(sender As Object, e As EventArgs) Handles 字體_NUD.ValueChanged
        採購清單_dgv.RowsDefaultCellStyle.Font = New System.Drawing.Font("微軟正黑體", 字體_NUD.Value)
        進貨日期_dgv.RowsDefaultCellStyle.Font = New System.Drawing.Font("微軟正黑體", 字體_NUD.Value)
        供應商_dgv.RowsDefaultCellStyle.Font = New System.Drawing.Font("微軟正黑體", 字體_NUD.Value)
        銷售單號_dgv.RowsDefaultCellStyle.Font = New System.Drawing.Font("微軟正黑體", 字體_NUD.Value)
    End Sub
End Class