Option Strict Off
Imports System.Data.SqlClient
Imports System.Net
Public Class 驗貨報告圖片導入資料庫
    Public Property Credentials As ICredentials
    ReadOnly conn As New SqlConnection
    ReadOnly da As New SqlDataAdapter
    ReadOnly cmd As New SqlCommand
    ReadOnly ds As New DataSet
    Dim dr As SqlDataReader
    Dim DTP As String
    Private Sub Set_DGV1載入前設定()
        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
    End Sub
    Private Sub Set_DGV1載入後設定()
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds) : DataGridView1.DataSource = ds.Tables(0) : conn.Close()
    End Sub
    Private Sub Set_清單1()
        Set_DGV1載入前設定()
        SQL1 = "SELECT      訂單控制表.訂單流水號, 訂單控制表.年份季度, 訂單控制表.客戶訂單號, 
                            訂單控制表.GOODBOX AS 型體名稱, 訂單控制表.型體號碼, 訂單控制表.APMA確認 AS 顏色, 
                            訂單控制表.訂單數量, 訂單控制表.CFMSPEC AS 訂單屬性, 訂單控制表.工廠回復交期 AS 工廠交期, 
                            METOO選擇按鈕.選擇, 訂單控制表.資料流水號
                FROM        訂單控制表 LEFT OUTER JOIN
                            METOO選擇按鈕 ON 訂單控制表.訂單流水號 = METOO選擇按鈕.對應
                WHERE      (訂單控制表.CXL = 0 OR
                            訂單控制表.CXL IS NULL) AND (訂單控制表.訂單數量 - 訂單控制表.出貨數量 <> 0) AND (訂單控制表.驗貨報告圖片 LIKE '')
                ORDER BY   訂單控制表.訂單流水號, 訂單控制表.客戶訂單號, 型體名稱"
        Set_DGV1載入後設定()
    End Sub
    Private Sub Set_grid1()
        DataGridView1.Columns(0).FillWeight = 120 : DataGridView1.Columns(1).FillWeight = 70 : DataGridView1.Columns(2).FillWeight = 100 : DataGridView1.Columns(3).FillWeight = 120
        DataGridView1.Columns(4).FillWeight = 80 : DataGridView1.Columns(5).FillWeight = 80 : DataGridView1.Columns(6).FillWeight = 80 : DataGridView1.Columns(7).FillWeight = 80
        DataGridView1.Columns(8).FillWeight = 80 : DataGridView1.Columns(9).FillWeight = 50 : DataGridView1.Columns(10).Visible = False

        DataGridView1.Columns(6).DefaultCellStyle.Format = "#,##0"
        DataGridView1.Columns(6).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        DataGridView1.Columns(6).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        For I As Integer = 0 To DataGridView1.Rows.Count - 1
            DataGridView1("選擇", I).Value = False
        Next
    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
        Target1 = Target & AA(13) & "/"
        Set_清單1() : Set_grid1()
        TextBox1.Enabled = False : TextBox2.Enabled = False : TextBox5.Enabled = False : TextBox6.Enabled = False
        TextBox3.Visible = False : TextBox7.Visible = False
        WebBrowser1.Url = New Uri(Target1)
        If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
            WebBrowser2.Navigate(FolderBrowserDialog1.SelectedPath)
        End If
    End Sub
    Private Sub DataGridView1_RowPostPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles DataGridView1.RowPostPaint
        Dim linePen As New Pen(Color.Blue, 2)
        If e.RowIndex = DataGridView1.Rows.Count - 1 Then
            Exit Sub
        Else
            If DataGridView1(0, e.RowIndex).Value.ToString <> DataGridView1(0, e.RowIndex + 1).Value.ToString Then
                Dim startX As Integer = IIf(DataGridView1.RowHeadersVisible, DataGridView1.RowHeadersWidth, 0)
                Dim startY As Integer = e.RowBounds.Top + e.RowBounds.Height - 1
                Dim endX As Integer = startX + DataGridView1.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) - DataGridView1.HorizontalScrollingOffset
                e.Graphics.DrawLine(linePen, startX, startY, endX, startY)
                Exit Sub
            End If
        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
            TextBox1.Text = DataGridView1("客戶訂單號", e.RowIndex).Value.ToString : TextBox2.Text = DataGridView1("型體名稱", e.RowIndex).Value.ToString
            TextBox6.Text = DataGridView1("型體號碼", e.RowIndex).Value.ToString : TextBox5.Text = DataGridView1("顏色", e.RowIndex).Value.ToString
            If DataGridView1("選擇", e.RowIndex).Value = True Then : DataGridView1("選擇", e.RowIndex).Value = False : Else : DataGridView1("選擇", e.RowIndex).Value = True : End If
        End If
    End Sub
    Private Sub Button31_Click(sender As Object, e As EventArgs) Handles Button31.Click
        PA = InputBox("請輸入要查詢的資料")
        Set_DGV1載入前設定()
        SQL1 = "SELECT      訂單控制表.訂單流水號, 訂單控制表.年份季度, 訂單控制表.客戶訂單號, 
                            訂單控制表.GOODBOX AS 型體名稱, 訂單控制表.型體號碼, 訂單控制表.APMA確認 AS 顏色, 
                            訂單控制表.訂單數量, 訂單控制表.CFMSPEC AS 訂單屬性, 訂單控制表.工廠回復交期 AS 工廠交期, 
                            METOO選擇按鈕.選擇, 訂單控制表.資料流水號
                FROM        訂單控制表 LEFT OUTER JOIN
                            METOO選擇按鈕 ON 訂單控制表.訂單流水號 = METOO選擇按鈕.對應
                WHERE      (訂單控制表.CXL = 0 OR
                            訂單控制表.CXL IS NULL) AND (訂單控制表.訂單數量 - 訂單控制表.出貨數量 <> 0) AND 
                           (訂單控制表.驗貨報告圖片 LIKE '') AND (訂單流水號 LIKE '%" & PA & "%' OR 訂單控制表.年份季度 LIKE '%" & PA & "%' OR 
                            訂單控制表.客戶訂單號 LIKE '%" & PA & "%' OR 訂單控制表.型體號碼 LIKE '%" & PA & "%' OR 訂單控制表.生產工廠 LIKE '%" & PA & "%' OR 
                           訂單控制表.GOODBOX LIKE '%" & PA & "%' OR 訂單控制表.APMA確認 LIKE '%" & PA & "%')
                ORDER BY   訂單控制表.訂單流水號, 訂單控制表.客戶訂單號, 型體名稱"
        Set_DGV1載入後設定() : Set_grid1()
        PA = ""
    End Sub
    Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
        If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox4.Text = "" Then
            MsgBox("檔案名稱或資料不可空白")
        Else
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "SELECT 客戶訂單號 FROM 驗貨報告圖庫 WHERE 客戶訂單號 LIKE '" & TextBox1.Text & "' AND 型體名稱 LIKE '" & TextBox2.Text & "'"
            cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
            If dr.Read() Then
                MsgBox("資料重複，請從新輸入或檢查")
                conn.Close() : dr.Close()
            Else
                conn.Close()
                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 : TextBox3.Text = dr("驗貨報告編號").ToString : Else : TextBox3.Text = "0" : End If
                conn.Close()
                Dim NUM1 As Integer
                NUM1 = Double.Parse(Strings.Right(TextBox3.Text, 8)) + 1
                If NUM1 < 10 Then : TextBox3.Text = "IR" & "0000000" & NUM1
                ElseIf NUM1 > 9 And NUM1 < 100 Then : TextBox3.Text = "IR" & "000000" & NUM1
                ElseIf NUM1 > 99 And NUM1 < 1000 Then : TextBox3.Text = "IR" & "00000" & NUM1
                ElseIf NUM1 > 999 And NUM1 < 10000 Then : TextBox3.Text = "IR" & "0000" & NUM1
                ElseIf NUM1 > 9999 And NUM1 < 100000 Then : TextBox3.Text = "IR" & "000" & NUM1
                ElseIf NUM1 > 99999 And NUM1 < 100000 Then : TextBox3.Text = "IR" & "00" & NUM1
                ElseIf NUM1 > 999999 And NUM1 < 1000000 Then : TextBox3.Text = "IR" & "0" & NUM1
                ElseIf NUM1 > 9999999 Then : TextBox3.Text = "IR" & NUM1
                End If

                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "INSERT INTO 驗貨報告圖庫 (客戶訂單號, 型體名稱, 驗貨報告編號, 操作人, 驗貨報告) SELECT '" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & gUserName & "', " &
                       "BULKCOLUMN FROM OPENROWSET ( BULK '" & FTP實體位置 & AA(13) & "\" & TextBox4.Text & "', SINGLE_BLOB) AS EMPLOYEEPICTURE"
                cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()

                DTP = Format(DateTimePicker1.Value, "yyyy/MM/dd")
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "UPDATE 訂單控制表 SET 驗貨時間 = '" & DTP & "', 驗貨報告圖片 = '" & TextBox3.Text & "' WHERE (客戶訂單號 = '" & TextBox1.Text & "' AND GOODBOX = '" & TextBox2.Text & "')"
                cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
                '---FTP方式刪除檔案----------------------------------------------------------------------------------------------------------------------------------------------------------------------
                Dim frq As FtpWebRequest, frp As FtpWebResponse, fcr As NetworkCredential
                frq = CType(WebRequest.Create(New Uri(Target1 & TextBox4.Text)), FtpWebRequest)
                fcr = New NetworkCredential(FTP帳號, FTP密碼)
                frq.Credentials = fcr
                frq.Method = WebRequestMethods.Ftp.DeleteFile
                frq.UseBinary = True
                frp = CType(frq.GetResponse, FtpWebResponse)
                frp.Close()
                WebBrowser1.Refresh()
                '----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                TextBox1.Text = "" : TextBox2.Text = "" : TextBox4.Text = ""
                Set_清單1() : Set_grid1()
                MsgBox("新增完成")
            End If
        End If
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox4.Text = "" Then
            MsgBox("檔案名稱或資料不可空白")
        Else
            conn.Close()
            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 : TextBox3.Text = dr("驗貨報告編號").ToString : Else : TextBox3.Text = "0" : End If
            conn.Close()
            Dim NUM1 As Integer
            NUM1 = Double.Parse(Strings.Right(TextBox3.Text, 8)) + 1
            If NUM1 < 10 Then : TextBox3.Text = "IR" & "0000000" & NUM1
            ElseIf NUM1 > 9 And NUM1 < 100 Then : TextBox3.Text = "IR" & "000000" & NUM1
            ElseIf NUM1 > 99 And NUM1 < 1000 Then : TextBox3.Text = "IR" & "00000" & NUM1
            ElseIf NUM1 > 999 And NUM1 < 10000 Then : TextBox3.Text = "IR" & "0000" & NUM1
            ElseIf NUM1 > 9999 And NUM1 < 100000 Then : TextBox3.Text = "IR" & "000" & NUM1
            ElseIf NUM1 > 99999 And NUM1 < 100000 Then : TextBox3.Text = "IR" & "00" & NUM1
            ElseIf NUM1 > 999999 And NUM1 < 1000000 Then : TextBox3.Text = "IR" & "0" & NUM1
            ElseIf NUM1 > 9999999 Then : TextBox3.Text = "IR" & NUM1
            End If

            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "INSERT INTO 驗貨報告圖庫 (客戶訂單號, 型體名稱, 驗貨報告編號, 操作人, 驗貨報告) SELECT '" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & gUserName & "', " &
                       "BULKCOLUMN FROM OPENROWSET ( BULK '" & FTP實體位置 & AA(13) & "\" & TextBox4.Text & "', SINGLE_BLOB) AS EMPLOYEEPICTURE"
            cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()

            DTP = Format(DateTimePicker1.Value, "yyyy/MM/dd")
            For I As Integer = 0 To DataGridView1.Rows.Count - 1
                If DataGridView1("選擇", I).Value = True Then
                    If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                    SQL1 = "UPDATE 訂單控制表 SET 驗貨時間 = '" & DTP & "', 驗貨報告圖片 = '" & TextBox3.Text & "' WHERE (資料流水號 = '" & DataGridView1("資料流水號", I).Value & "')"
                    cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
                End If
            Next
            '---FTP方式刪除檔案----------------------------------------------------------------------------------------------------------------------------------------------------------------------
            Dim frq As FtpWebRequest, frp As FtpWebResponse, fcr As NetworkCredential
            frq = CType(WebRequest.Create(New Uri(Target1 & TextBox4.Text)), FtpWebRequest)
            fcr = New NetworkCredential(FTP帳號, FTP密碼)
            frq.Credentials = fcr
            frq.Method = WebRequestMethods.Ftp.DeleteFile
            frq.UseBinary = True
            frp = CType(frq.GetResponse, FtpWebResponse)
            frp.Close()
            WebBrowser1.Refresh()
            '----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
            TextBox1.Text = "" : TextBox2.Text = "" : TextBox4.Text = ""
            Set_清單1() : Set_grid1()
            MsgBox("新增完成")
        End If
    End Sub
    Private Sub TextBox4_DragEnter(sender As Object, e As DragEventArgs) Handles TextBox4.DragEnter
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            Dim files As String()
            Try
                files = CType(e.Data.GetData(DataFormats.FileDrop), String())
                TextBox4.Text = files(files.Length - 1)
            Catch ex As Exception
                MessageBox.Show(ex.Message)
                Return
            End Try
        End If

        Dim STR2 As Integer = 0
        TextBox7.Text = TextBox4.Text
        If TextBox4.Text = "" Then
        Else
            For i As Integer = 0 To 9999
                Dim STR1 As Integer = Strings.Len(TextBox7.Text)
                If Strings.Right((TextBox7.Text), 1) <> "\" Then
                    STR1 -= 1 : STR2 += 1
                    If STR1 = 0 Then
                        TextBox7.Text = ""
                        TextBox4.Text = ""
                        i = 9999
                    Else
                        TextBox7.Text = Strings.Left((TextBox7.Text), STR1)
                    End If
                Else
                    TextBox7.Text = Strings.Right((TextBox4.Text), STR2)
                    TextBox4.Text = TextBox7.Text
                    i = 9999
                End If
            Next
        End If
    End Sub
    Private Sub TextBox4_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles TextBox4.DragDrop
        Dim files As Array = e.Data.GetData(DataFormats.FileDrop)
        For Each file As String In files
            TextBox4.AppendText(file + Environment.NewLine)
        Next
    End Sub
    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
            WebBrowser2.Navigate(FolderBrowserDialog1.SelectedPath)
        End If
    End Sub
End Class