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 NUM1 As Integer : Dim EDR As Integer = 0 : Dim ESTR As String
    Private Sub Set_清單()
        物料清單_dgv.DataSource = Nothing : ds.Clear()
        物料清單_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        物料清單_dgv.ColumnHeadersHeight = 40
        物料清單_dgv.AllowUserToAddRows = False
        物料清單_dgv.RowTemplate.Height = 25

        PA1 = "" : PA2 = "" : PA2 = 供應商編碼_cb.Text : PA1 = 查詢_tb.Text
        SQL_盤盈盤損申請_物料清單()
        da.Fill(ds) : 物料清單_dgv.DataSource = ds.Tables(0) : conn.Close()
        物料清單_dgv.Columns(0).FillWeight = 90 : 物料清單_dgv.Columns(1).FillWeight = 100 : 物料清單_dgv.Columns(2).FillWeight = 250
        物料清單_dgv.Columns(3).FillWeight = 60 : 物料清單_dgv.Columns(4).Visible = False
        物料清單_dgv.Columns(5).FillWeight = 40 : 物料清單_dgv.Columns(6).Visible = False

        物料清單_dgv.Columns(0).ReadOnly = True : 物料清單_dgv.Columns(1).ReadOnly = True : 物料清單_dgv.Columns(2).ReadOnly = True
        物料清單_dgv.Columns(3).ReadOnly = True : 物料清單_dgv.Columns(4).ReadOnly = True : 物料清單_dgv.Columns(5).ReadOnly = True

        物料清單_dgv.Columns("庫存").DefaultCellStyle.Format = "#,##0"
        物料清單_dgv.Columns("庫存").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        物料清單_dgv.Columns("庫存").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter

        資料筆數_tb.Text = 物料清單_dgv.Rows.Count
        For i As Integer = 0 To 物料清單_dgv.Rows.Count - 1
            資料數 = 物料清單_dgv.Rows.Count : MyModule1.進度條()
            If 物料清單_dgv("庫存", i).Value.ToString = "" Then : 物料清單_dgv("庫存", i).Value = 0 : End If
            If 物料清單_dgv("未出庫", i).Value.ToString = "" Then : 物料清單_dgv("未出庫", i).Value = 0 : End If
        Next : YD_ERP_SYS.ToolStripProgressBar1.Value = 0 : 最終數 = 0
    End Sub
    Private Sub Set_清單2()
        調庫單據_dgv.DataSource = Nothing : ds2.Clear()
        調庫單據_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        調庫單據_dgv.ColumnHeadersHeight = 25
        調庫單據_dgv.AllowUserToAddRows = False
        調庫單據_dgv.RowTemplate.Height = 25

        SQL_盤盈盤損申請_單據()
        da.Fill(ds2) : 調庫單據_dgv.DataSource = ds2.Tables(0) : conn.Close()
        調庫單據_dgv.Columns(0).FillWeight = 200 : 調庫單據_dgv.Columns(1).Visible = False : 調庫單據_dgv.Columns(2).Visible = False
        調庫單據_dgv.Columns(0).ReadOnly = True : 調庫單據_dgv.Columns(1).ReadOnly = True : 調庫單據_dgv.Columns(2).ReadOnly = True
    End Sub
    Private Sub Set_清單1()
        調庫明細_dgv.DataSource = Nothing : ds1.Clear()
        調庫明細_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        調庫明細_dgv.ColumnHeadersHeight = 40
        調庫明細_dgv.AllowUserToAddRows = False
        調庫明細_dgv.RowTemplate.Height = 35

        PA20 = 調庫單號_tb.Text
        SQL_盤盈盤損申請_明細()
        da.Fill(ds1) : 調庫明細_dgv.DataSource = ds1.Tables(0) : conn.Close()
        調庫明細_dgv.Columns(0).Visible = False : 調庫明細_dgv.Columns(1).FillWeight = 100 : 調庫明細_dgv.Columns(2).FillWeight = 100 : 調庫明細_dgv.Columns(3).FillWeight = 75
        調庫明細_dgv.Columns(4).FillWeight = 200 : 調庫明細_dgv.Columns(5).FillWeight = 100 : 調庫明細_dgv.Columns(6).FillWeight = 100 : 調庫明細_dgv.Columns(7).FillWeight = 50
        調庫明細_dgv.Columns(8).FillWeight = 50 : 調庫明細_dgv.Columns(9).FillWeight = 75 : 調庫明細_dgv.Columns(10).FillWeight = 100 : 調庫明細_dgv.Columns(11).FillWeight = 35
        調庫明細_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(1).ReadOnly = True : 調庫明細_dgv.Columns(2).ReadOnly = True : 調庫明細_dgv.Columns(3).ReadOnly = True : 調庫明細_dgv.Columns(4).ReadOnly = True
        調庫明細_dgv.Columns(5).ReadOnly = True : 調庫明細_dgv.Columns(6).ReadOnly = True : 調庫明細_dgv.Columns(9).ReadOnly = True : 調庫明細_dgv.Columns(11).ReadOnly = True
        調庫明細_dgv.EditMode = DataGridViewEditMode.EditOnEnter : 資料筆數1_tb.Text = 調庫明細_dgv.Rows.Count
    End Sub
    Private Sub Set_清單3()
        區域庫存_dgv.DataSource = Nothing : ds3.Clear()
        區域庫存_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        區域庫存_dgv.ColumnHeadersHeight = 25
        區域庫存_dgv.AllowUserToAddRows = False
        區域庫存_dgv.RowTemplate.Height = 25

        PA20 = 料號_tb.Text : SQL_倉名_物料分配查詢()
        da.Fill(ds3) : 區域庫存_dgv.DataSource = ds3.Tables(0) : conn.Close()
        區域庫存_dgv.Columns(0).FillWeight = 200 : 區域庫存_dgv.Columns(1).FillWeight = 80
        區域庫存_dgv.Columns(0).ReadOnly = True : 區域庫存_dgv.Columns(1).ReadOnly = True
        區域庫存_dgv.Columns("可用").DefaultCellStyle.Format = "#,##0"
        區域庫存_dgv.Columns("可用").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        區域庫存_dgv.Columns("可用").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
    End Sub
    Private Sub 供應商_下拉表單資料載入()
        BL1 = False : PA3 = "" : PA4 = ""
        If 地區_cb.Text = "" Then : PA3 = ""
        ElseIf 地區_cb.Text = "北部" Then : PA3 = "2"
        ElseIf 地區_cb.Text = "南部" Then : PA3 = "3"
        ElseIf 地區_cb.Text = "材料" Then : PA3 = "9"
        ElseIf 地區_cb.Text = "調貨" Then : PA3 = "Y"
        End If
        SQL_盤盈盤損申請_供應商_下拉清單() : 供應商_cb.Items.Clear() : 供應商編碼_cb.Items.Clear()
        While (dr.Read()) : 供應商_cb.Items.Add(dr("簡稱")) : 供應商編碼_cb.Items.Add(dr("編號")) : End While : conn.Close()
    End Sub
    Private Sub 倉儲狀態_cb_下拉表單資料載入()
        If 需要全部庫存區域清單_ch.Checked = True Then
            SQL_倉儲狀態_下拉清單()
        Else
            If 料號_tb.Text = "" Then
                SQL_倉儲狀態_下拉清單()
            Else
                PA20 = 料號_tb.Text : SQL_倉儲狀態_下拉清單_有條件()
            End If
        End If
        倉儲狀態_cb.Items.Clear() : 儲態編號_cb.Items.Clear()
        While (dr.Read()) : 倉儲狀態_cb.Items.Add(dr("狀態")) : 儲態編號_cb.Items.Add(dr("編碼")) : End While : conn.Close()
        If 倉儲狀態_cb.Items.Count = 0 Then
            SQL_倉儲狀態_下拉清單()
            While (dr.Read()) : 倉儲狀態_cb.Items.Add(dr("狀態")) : 儲態編號_cb.Items.Add(dr("編碼")) : End While : conn.Close()
        End If
    End Sub
    Private Sub 訂製狀態_cb_下拉表單資料載入()
        SQL_訂製狀態_下拉清單() : 訂製狀態_cb.Items.Clear() : 訂製編碼_cb.Items.Clear()
        While (dr.Read()) : 訂製狀態_cb.Items.Add(dr("訂製")) : 訂製編碼_cb.Items.Add(dr("編碼")) : End While : conn.Close()
        If 需要全部庫存區域清單_ch.Checked = True Then
            SQL_訂製狀態_下拉清單()
        Else
            If 料號_tb.Text = "" Then
                SQL_訂製狀態_下拉清單()
            Else
                PA20 = 料號_tb.Text : SQL_訂製狀態_下拉清單_有條件()
            End If
        End If
        訂製狀態_cb.Items.Clear() : 訂製編碼_cb.Items.Clear()
        While (dr.Read()) : 訂製狀態_cb.Items.Add(dr("訂製")) : 訂製編碼_cb.Items.Add(dr("編碼")) : End While : conn.Close()
        If 訂製狀態_cb.Items.Count = 0 Then
            SQL_訂製狀態_下拉清單()
            While (dr.Read()) : 訂製狀態_cb.Items.Add(dr("訂製")) : 訂製編碼_cb.Items.Add(dr("編碼")) : End While : conn.Close()
        End If
    End Sub
    Private Sub 指定倉庫_cb_下拉表單資料載入()
        If 需要全部庫存區域清單_ch.Checked = True Then
            SQL_倉名_下拉清單()
        Else
            If 料號_tb.Text = "" Then
                SQL_倉名_下拉清單()
            Else
                PA20 = 料號_tb.Text : SQL_倉名_下拉清單_有條件()
            End If
        End If
        指定倉庫_cb.Items.Clear()
        While (dr.Read()) : 指定倉庫_cb.Items.Add(dr("倉名")) : End While : conn.Close()
        If 指定倉庫_cb.Items.Count = 0 Then
            SQL_倉名_下拉清單()
            While (dr.Read()) : 指定倉庫_cb.Items.Add(dr("倉名")) : End While : conn.Close()
        End If
    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
        料號_tb.Enabled = False : 供應商編碼_tb.Visible = False '--------------------控件隱藏、操作許可設定與其他基本顯示設定
        指定倉庫_cb_下拉表單資料載入() : 訂製狀態_cb_下拉表單資料載入() : 倉儲狀態_cb_下拉表單資料載入()
        首次開啟 = False : Set_清單() : Set_清單2() : 首次開啟 = True '--------------------資料讀取

        If YD_ERP_SYS.Size.Width > 1440 And YD_ERP_SYS.Size.Height > 900 Then
            字體_NUD.Value = 9
        ElseIf YD_ERP_SYS.Size.Width <= 1440 And YD_ERP_SYS.Size.Height <= 900 Then
            字體_NUD.Value = 7.6
        End If
    End Sub
    Private Sub 盤盈盤損申請_SizeChanged(sender As Object, e As EventArgs) Handles MyBase.SizeChanged
        If YD_ERP_SYS.Size.Width > 1440 And YD_ERP_SYS.Size.Height > 900 Then
            字體_NUD.Value = 9
        ElseIf YD_ERP_SYS.Size.Width <= 1440 And YD_ERP_SYS.Size.Height <= 900 Then
            字體_NUD.Value = 8
        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
            指定倉庫_cb.Text = 區域庫存_dgv.Rows(e.RowIndex).Cells("倉名").Value
        End If
    End Sub
    Private Sub 調庫單據_dgv_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles 調庫單據_dgv.CellClick
        If 開立庫存調整單_bt.Enabled = True Then
            If e.RowIndex = -1 Then : Else
                調庫單號_tb.Text = 調庫單據_dgv.Rows(e.RowIndex).Cells("執行單號").Value : 申請人_tb.Text = 調庫單據_dgv.Rows(e.RowIndex).Cells("申請人").Value
                申請日期_dtp.Value = 調庫單據_dgv.Rows(e.RowIndex).Cells("申請日期").Value
                Set_清單1()
            End If
        End If
    End Sub
    Private Sub 調庫明細_dgv_CellEndEdit(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles 調庫明細_dgv.CellEndEdit
        If (Val(調庫明細_dgv.Rows(e.RowIndex).Cells("盤盈").Value) = 0 And Val(調庫明細_dgv.Rows(e.RowIndex).Cells("盤損").Value) = 0) Or
           (Val(調庫明細_dgv.Rows(e.RowIndex).Cells("盤盈").Value) <> 0 And Val(調庫明細_dgv.Rows(e.RowIndex).Cells("盤損").Value) <> 0) Then
            MsgBox("盤營與盤損不能同時為零或同時有數量")
            Set_清單1()
            Exit Sub
        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
            項次_tb.Text = 調庫明細_dgv(0, e.RowIndex).Value.ToString
            物料圖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 首次開啟 = True Then
            If e.RowIndex = -1 Then : Else
                料號_tb.Text = 物料清單_dgv("料號", e.RowIndex).Value.ToString : 供應商編碼_tb.Text = 物料清單_dgv("供應商", e.RowIndex).Value.ToString
                品名_tb.Text = 物料清單_dgv("品名", e.RowIndex).Value.ToString : 最大庫存量_tb.Text = 物料清單_dgv("最低庫存量", e.RowIndex).Value.ToString
                庫存量_tb.Text = Val(物料清單_dgv("庫存", e.RowIndex).Value.ToString)
                可申請_tb.Text = Val(物料清單_dgv("庫存", e.RowIndex).Value.ToString) - Val(物料清單_dgv("未出庫", e.RowIndex).Value.ToString)

                物料圖_pb.Image = Nothing : PA1 = 料號_tb.Text : SQL_物料檔案_是否有圖()
                If dr.Read() Then
                    If dr("是否有圖") = True Then
                        物料圖_pb.BackgroundImage = Nothing
                        PA = 料號_tb.Text : 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)
                            物料圖_pb.Image = Bitmap.FromStream(oStream)
                        End While
                        conn.Close()
                        物料圖_pb.SizeMode = 4
                    Else
                        物料圖_pb.BackgroundImage = My.Resources.底圖1 : 物料圖_pb.BackgroundImageLayout = 3
                        物料圖_pb.Image = Nothing
                    End If
                End If : conn.Close()

                指定倉庫_cb_下拉表單資料載入()
                訂製狀態_cb_下拉表單資料載入() : 倉儲狀態_cb_下拉表單資料載入()
            End If
        End If
    End Sub
    Private Sub 物料清單_dgv_RowPostPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles 物料清單_dgv.RowPostPaint
        Dim linePen As New Pen(Color.Blue, 2)
        If e.RowIndex = 物料清單_dgv.Rows.Count - 1 Then : Exit Sub : Else
            If 物料清單_dgv("供應商", e.RowIndex).Value.ToString <> 物料清單_dgv("供應商", e.RowIndex + 1).Value.ToString Then
                Dim startX As Integer = IIf(物料清單_dgv.RowHeadersVisible, 物料清單_dgv.RowHeadersWidth, 0) : Dim startY As Integer = e.RowBounds.Top + e.RowBounds.Height - 1
                Dim endX As Integer = startX + 物料清單_dgv.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) - 物料清單_dgv.HorizontalScrollingOffset
                e.Graphics.DrawLine(linePen, startX, startY, endX, startY) : Exit Sub
            End If
        End If
    End Sub
    Private Sub 地區_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 地區_cb.SelectedIndexChanged
        If 地區_cb.Text = "" And 查詢_tb.Text = "" Then
            供應商_cb.Items.Clear() : 供應商編碼_cb.Items.Clear() : Set_清單()
        ElseIf 地區_cb.Text = "" Then
            查詢_tb.Text = "" : 供應商_cb.Items.Clear() : 供應商編碼_cb.Items.Clear()
        ElseIf 地區_cb.Text <> "" Then
            查詢_tb.Text = "" : 供應商_下拉表單資料載入()
        End If
    End Sub
    Private Sub 供應商_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 供應商_cb.SelectedIndexChanged
        If 地區_cb.Text <> "" Then
            查詢_tb.Text = "" : 供應商編碼_cb.SelectedIndex = 供應商_cb.SelectedIndex
        End If
    End Sub
    Private Sub 供應商編碼_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 供應商編碼_cb.SelectedIndexChanged
        If 地區_cb.Text <> "" Then
            查詢_tb.Text = "" : 供應商_cb.SelectedIndex = 供應商編碼_cb.SelectedIndex : 供應商編碼_tb.Text = 供應商編碼_cb.Text : Set_清單()
        End If
    End Sub
    Private Sub 清空_bt_Click(sender As Object, e As EventArgs) Handles 清空_bt.Click
        查詢_tb.Text = "" : 地區_cb.Text = ""
    End Sub
    Private Sub 查詢_tb_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles 查詢_tb.KeyPress
        If e.KeyChar = Chr(13) Then
            供應商_cb.Items.Clear() : 供應商編碼_cb.Items.Clear()
            PA = 查詢_tb.Text : Set_清單() : 地區_cb.Text = ""
        End If
    End Sub
    Private Sub 指定倉庫_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 指定倉庫_cb.SelectedIndexChanged
        PA6 = 指定倉庫_cb.Text : SQL_指定倉儲_資料讀取()
        If dr.Read() Then : 倉庫級別_tb.Text = dr("倉級") : 倉庫編號_tb.Text = dr("倉名編號") : End If : conn.Close()
    End Sub
    Private Sub 倉儲狀態_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 倉儲狀態_cb.SelectedIndexChanged
        儲態編號_cb.SelectedIndex = 倉儲狀態_cb.SelectedIndex
    End Sub
    Private Sub 儲態編號_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 儲態編號_cb.SelectedIndexChanged
        倉儲狀態_cb.SelectedIndex = 儲態編號_cb.SelectedIndex
    End Sub
    Private Sub 訂製狀態_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 訂製狀態_cb.SelectedIndexChanged
        訂製編碼_cb.SelectedIndex = 訂製狀態_cb.SelectedIndex
    End Sub
    Private Sub 訂製編碼_cb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 訂製編碼_cb.SelectedIndexChanged
        訂製狀態_cb.SelectedIndex = 訂製編碼_cb.SelectedIndex
    End Sub
    Private Sub 盤損數_nud_ValueChanged(sender As Object, e As EventArgs) Handles 盤損數_nud.ValueChanged
        If 盤損數_nud.Value > 0 Then
            盤盈數_nud.Value = 0
            If 盤損數_nud.Value > Val(庫存量_tb.Text) Then
                MsgBox("盤損數量不可能大於目前的庫存量!!")
                盤損數_nud.Value = 0
            End If
        End If
    End Sub
    Private Sub 盤盈數_nud_ValueChanged(sender As Object, e As EventArgs) Handles 盤盈數_nud.ValueChanged
        If 盤盈數_nud.Value > 0 Then
            盤損數_nud.Value = 0
        End If
    End Sub
    Private Sub Set_日期格式轉換()
        DTP = Format(申請日期_dtp.Value, "yyyy/MM/dd") : DTP1 = Format(Today(), "yyyyMMdd")
    End Sub
    Private Sub 開立庫存調整單_bt_Click(sender As Object, e As EventArgs) Handles 開立庫存調整單_bt.Click
        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_倉儲明細執行單號_新增() : Set_清單2() : Set_清單1() : conn.Close()
        申請人_tb.Text = gUserName
        開立庫存調整單_bt.Enabled = False : 開立庫存調整單_tsm.Enabled = False
    End Sub
    Private Sub 選定內容加入庫存調整申請單_bt_Click(sender As Object, e As EventArgs) Handles 選定內容加入庫存調整申請單_bt.Click
        If 調庫單號_tb.Text = "" Then
            MsgBox("請先案開立庫存調整單建立新單號或是選擇未審核的庫存調整單!!")
        Else
            If 料號_tb.Text = "" Then
                MsgBox("請選擇物料，調整庫存需要指定物料!!")
            Else
                If 儲態編號_cb.Text = "" Then
                    MsgBox("倉儲狀態是一定要選擇的!!")
                Else
                    If 訂製狀態_cb.Text = "" Then
                        MsgBox("物品的訂製狀態也是一定要選擇的!!")
                    Else
                        If 盤損數_nud.Value > Val(庫存量_tb.Text) Then
                            MsgBox("盤損數量不可能小於庫存數量!!")
                            盤損數_nud.Value = 0
                        Else
                            If 盤損數_nud.Value = 0 And 盤盈數_nud.Value = 0 Then
                                MsgBox("盤盈盤損最少選一種，兩者都為零是不容許的!!")
                            Else
                                PA1 = 調庫單號_tb.Text : PA2 = 料號_tb.Text : PA3 = 倉庫編號_tb.Text
                                SQL_倉儲明細表_確認單據是否申請()
                                If dr.Read() Then
                                    conn.Close() : MsgBox("該料號已經在本單據中申請過了，如要修改，請直接到單據中尋找並修改。")
                                Else
                                    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 = 倉庫級別_tb.Text : PA6 = 倉庫編號_tb.Text : NU1 = 盤盈數_nud.Value : NU2 = 盤損數_nud.Value
                                    PA7 = 儲態編號_cb.Text : PA8 = 訂製編碼_cb.Text : PA9 = 申請人_tb.Text
                                    If 盤盈數_nud.Value = 0 And 盤損數_nud.Value > 0 Then
                                        SQL_盤盈盤損申請_倉儲明細表_盤損出庫新增()
                                    ElseIf 盤盈數_nud.Value > 0 And 盤損數_nud.Value = 0 Then
                                        SQL_盤盈盤損申請_倉儲明細表_盤盈入庫新增()
                                    End If : conn.Close()
                                    Set_清單1() : Set_清單2() : Set_清單()
                                End If
                            End If
                        End If
                    End If
                End If
            End If
        End If
    End Sub
    Private Sub 存檔_bt_Click(sender As Object, e As EventArgs) Handles 存檔_bt.Click
        If 調庫單號_tb.Text = "" Or 料號_tb.Text = "" Then
            MsgBox("沒有選擇庫存調整單據!!")
        Else
            For i As Integer = 0 To 調庫明細_dgv.Rows.Count - 1
                資料數 = 調庫明細_dgv.Rows.Count : MyModule1.進度條()
                PA10 = 調庫明細_dgv.Rows(i).Cells("備註").Value : PA11 = 調庫明細_dgv.Rows(i).Cells("資料流水號").Value
                NU1 = 調庫明細_dgv.Rows(i).Cells("盤盈").Value : NU2 = 調庫明細_dgv.Rows(i).Cells("盤損").Value
                SQL_倉儲明細表_出入庫及修改() : conn.Close()
            Next
            MsgBox("存檔完成!!") : YD_ERP_SYS.ToolStripProgressBar1.Value = 0 : 最終數 = 0
            Set_清單1() : Set_清單2()
            開立庫存調整單_bt.Enabled = True : 開立庫存調整單_tsm.Enabled = True
        End If
    End Sub
    Private Sub 刪除請購單上點選資料_bt_Click(sender As Object, e As EventArgs) Handles 刪除單據上點選的資料_bt.Click
        If 調庫單號_tb.Text = "" Then
            MsgBox("沒有選擇庫存調整單據!!")
        Else
            If 項次_tb.Text = "" Then
                MsgBox("沒有選擇要刪除的明細!!")
            Else
                Dim aa1 As MsgBoxResult = MsgBox("你確定要刪除這筆資料?", MsgBoxStyle.OkCancel)
                If aa1 = MsgBoxResult.Ok Then
                    PA1 = 項次_tb.Text : SQL_倉儲明細表_刪除二() : conn.Close()
                    Set_清單1()
                End If
            End If
        End If
    End Sub
    Private Sub 刪除_bt_Click(sender As Object, e As EventArgs) Handles 刪除_bt.Click
        Dim aa1 As MsgBoxResult = MsgBox("你確定要刪除這張庫存調整單?", MsgBoxStyle.OkCancel)
        If aa1 = MsgBoxResult.Ok Then
            PA1 = 調庫單號_tb.Text : SQL_倉儲明細表_刪除() : SQL_倉儲明細執行單號_刪除() : conn.Close()
            調庫單號_tb.Text = "" : 申請人_tb.Text = "" : 項次_tb.Text = ""
            調庫明細_dgv.DataSource = Nothing : ds1.Clear()
            Set_清單2()
        End If
    End Sub
    Private Sub 需要全部庫存區域清單_ch_CheckedChanged(sender As Object, e As EventArgs) Handles 需要全部庫存區域清單_ch.CheckedChanged
        指定倉庫_cb_下拉表單資料載入()
    End Sub
    '-----------------------------滑鼠右鍵--------------------------------------------------------------------------------------------------------------------------------------
    Private Sub 重新整理_tsm_Click(sender As Object, e As EventArgs) Handles 重新整理_tsm.Click
        清空_bt.PerformClick() : Set_清單2() : Set_清單1()
    End Sub
    Private Sub 開立庫存調整單_tsm_Click(sender As Object, e As EventArgs) Handles 開立庫存調整單_tsm.Click
        開立庫存調整單_bt.PerformClick()
    End Sub
    Private Sub 選定內容加入庫存調整申請單_tsm_Click(sender As Object, e As EventArgs) Handles 選定內容加入庫存調整申請單_tsm.Click
        選定內容加入庫存調整申請單_bt.PerformClick()
    End Sub
    Private Sub 存檔_tsm_Click(sender As Object, e As EventArgs) Handles 存檔_tsm.Click
        存檔_bt.PerformClick()
    End Sub
    Private Sub 刪除庫存調整單據上點選的資料_tsm_Click(sender As Object, e As EventArgs) Handles 刪除庫存調整單據上點選的資料_tsm.Click
        刪除單據上點選的資料_bt.PerformClick()
    End Sub
    Private Sub 刪除庫存調整單據_tsm_Click(sender As Object, e As EventArgs) Handles 刪除庫存調整單據_tsm.Click
        刪除_bt.PerformClick()
    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