Imports System.IO
Imports System.Text

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'Dim ar As New ArrayList
        Dim blean_Rtn As Boolean
        blean_Rtn = Read_CsvFile()
        Dim strFileName = CsvFolder & Csv_Dates
        Dim dt_Datas As DataTable = New DataTable
        Dim dt_Product As DataTable = New DataTable
        Dim dt_OrderData As DataTable = New DataTable
        Dim objcsv As New ReadDatasCsv(strFileName) 'Csvファイルをテーブルに格納
        Dim dtDis As DataTable = Nothing
        Dim dv As DataView = Nothing
        Dim dv2 As DataView = Nothing
        Dim dv3 As DataView = Nothing
        Dim dtDis2 As DataTable = Nothing
        Dim dtDis3 As DataTable = Nothing
        Dim sel_row As DataRow()
        Dim sel_row4 As DataRow()
        Dim data_row As DataRow
        Dim strwkarray()
        Dim strsyohincode As String
        Dim strarray(8) As String
        Dim intbkarray(8) As Integer
        Dim strkeycode As String = ""
        Dim strwkstr As String = ""
        Dim strwkkey As String = ""
        Dim strbranch As String = ""
        Dim strbranch2 As String = ""
        Dim iloopcnt As Integer = 1
        Dim inumcnt As Integer = 0
        Dim iLoopidx As Integer = 0
        Dim iarraycnt As Integer = 0
        Dim strkey_code As String = ""
        Dim lproduc_order As Long = 0
        Dim lproduc_order2 As Long = 0
        Dim products_order As Long = 0
        Dim products_order2 As Long = 0
        If MsgBox("【通常分】取込処理を行います。" & vbCrLf & "よろしいですか？", vbYesNo + vbInformation, gMsgTitle) = vbNo Then
            Return
        End If
        'フォームのカーソルを待機状態にする
        Me.Cursor = Cursors.WaitCursor
        dt_Datas = objcsv.Read_Start()
        '作成した DatatTable に対する DataView を生成
        dv = New DataView(dt_Datas)
        dtDis = dv.ToTable(True, "key_syohincode")


        'dt_Datas.DefaultView.Sort = "key_syohincode"
        'strkeycode = dtDis.Rows(0).Item("syohincode").ToString()
        '製品情報ワークテーブルの作成
        '項目定義
        dt_Product.Columns.Add("key_code", Type.GetType("System.String")) '0
        dt_Product.Columns.Add("all_num", Type.GetType("System.Int32"))
        dt_Product.Columns.Add("branch_num", Type.GetType("System.Int32"))
        dt_Product.Columns.Add("branch_num_m", Type.GetType("System.Int32"))
        dt_Product.Columns.Add("branch_num_a", Type.GetType("System.Int32")) '
        dt_Product.Columns.Add("branch_num_n", Type.GetType("System.Int32"))
        dt_Product.Columns.Add("branch_num_nh", Type.GetType("System.Int32"))
        dt_Product.Columns.Add("branch_num_h", Type.GetType("System.Int32"))
        dt_Product.Columns.Add("branch_num_e", Type.GetType("System.Int32"))
        dt_Product.Columns.Add("branch_num_x", Type.GetType("System.Int32"))
        dt_Product.Columns.Add("branch_num_xx", Type.GetType("System.Int32"))
        dt_Product.Columns.Add("branch_order1", Type.GetType("System.Int32")) '枝番なし
        dt_Product.Columns.Add("branch_order2", Type.GetType("System.Int32")) 'm
        dt_Product.Columns.Add("branch_order3", Type.GetType("System.Int32")) 'a
        dt_Product.Columns.Add("branch_order4", Type.GetType("System.Int32")) 'n
        dt_Product.Columns.Add("branch_order5", Type.GetType("System.Int32")) 'nh
        dt_Product.Columns.Add("branch_order6", Type.GetType("System.Int32")) 'h
        dt_Product.Columns.Add("branch_order7", Type.GetType("System.Int32")) 'e
        dt_Product.Columns.Add("branch_order8", Type.GetType("System.Int32")) 'x
        dt_Product.Columns.Add("branch_order9", Type.GetType("System.Int32")) 'xx
        dt_Product.Columns.Add("key_order", Type.GetType("System.Int32"))     '商品順位
        dt_Product.Columns.Add("name", Type.GetType("System.String"))

        data_row = dt_Product.NewRow
        Do Until iloopcnt > dtDis.Rows.Count
            strsyohincode = dtDis.Rows(iloopcnt - 1).ItemArray(0)
            If Omake_Dic.ContainsKey(strsyohincode) = False Then
                sel_row = dt_Datas.Select("key_syohincode = '" & strsyohincode & "'")
                inumcnt = 0
                Do Until inumcnt = sel_row.Count
                    strkeycode = sel_row(inumcnt).ItemArray(1)
                    If InStr(strkeycode, "-") Then  '枝番有
                        strwkarray = Split(strkeycode, "-")
                        iarraycnt = UBound(strwkarray)
                        strwkstr = strwkarray(iarraycnt)
                        'strbranch
                        Select Case strwkstr
                            Case "m"
                                strbranch = "m"
                                strkeycode = Mid(strkeycode, 1, Len(strkeycode) - 2)
                            Case "a"
                                strbranch = "a"
                                strkeycode = Mid(strkeycode, 1, Len(strkeycode) - 2)
                            Case "n"
                                strbranch = "n"
                                strkeycode = Mid(strkeycode, 1, Len(strkeycode) - 2)
                            Case "nh"
                                strbranch = "nh"
                                strkeycode = Mid(strkeycode, 1, Len(strkeycode) - 2)
                            Case "h"
                                strbranch = "h"
                                strkeycode = Mid(strkeycode, 1, Len(strkeycode) - 2)
                            Case "e"
                                strbranch = "e"
                                strkeycode = Mid(strkeycode, 1, Len(strkeycode) - 2)
                            Case "x"
                                strbranch = "x"
                                strkeycode = Mid(strkeycode, 1, Len(strkeycode) - 2)
                            Case "xx"
                                strbranch = "xx"
                                strkeycode = Mid(strkeycode, 1, Len(strkeycode) - 2)
                            Case Else
                                strbranch = ""
                        End Select
                    End If
                    If inumcnt = 0 Then
                        data_row(1) = 0
                        data_row(2) = 0
                        data_row(3) = 0
                        data_row(4) = 0
                        data_row(5) = 0
                        data_row(6) = 0
                        data_row(7) = 0
                        data_row(8) = 0
                        data_row(9) = 0
                        data_row(10) = 0
                    End If
                    Select Case strbranch
                        Case "m"
                            data_row(3) = data_row(3) + 1
                        Case "a"
                            data_row(4) = data_row(4) + 1
                        Case "n"
                            data_row(5) = data_row(5) + 1
                        Case "nh"
                            data_row(6) = data_row(6) + 1
                        Case "h"
                            data_row(7) = data_row(7) + 1
                        Case "e"
                            data_row(8) = data_row(8) + 1
                        Case "x"
                            data_row(9) = data_row(9) + 1
                        Case "xx"
                            data_row(10) = data_row(10) + 1
                        Case Else
                            data_row(2) = data_row(2) + 1
                    End Select
                    inumcnt = inumcnt + 1
                Loop
                strarray(0) = Format(data_row(2), "0000000") & "_99 "
                strarray(1) = Format(data_row(3), "0000000") & "_8m  "
                strarray(2) = Format(data_row(4), "0000000") & "_7a "
                strarray(3) = Format(data_row(5), "0000000") & "_6n "
                strarray(4) = Format(data_row(6), "0000000") & "_5nh"
                strarray(5) = Format(data_row(7), "0000000") & "_4h "
                strarray(6) = Format(data_row(8), "0000000") & "_3e "
                strarray(7) = Format(data_row(9), "0000000") & "_2x "
                strarray(8) = Format(data_row(10), "0000000") & "_1xx"

                Array.Sort(strarray)
                Array.Reverse(strarray)
                For iLoopidx = 0 To UBound(strarray)
                    Select Case Mid(strarray(iLoopidx), 10, 2)
                        Case "9 "
                            data_row（11) = iLoopidx + 1
                        Case "m "
                            data_row（12) = iLoopidx + 1
                        Case "a "
                            data_row（13) = iLoopidx + 1
                        Case "n "
                            data_row（14) = iLoopidx + 1
                        Case "nh"
                            data_row（15) = iLoopidx + 1
                        Case "h "
                            data_row（16) = iLoopidx + 1
                        Case "e "
                            data_row（17) = iLoopidx + 1
                        Case "x "
                            data_row（18) = iLoopidx + 1
                        Case "xx"
                            data_row（19) = iLoopidx + 1
                    End Select
                Next
                data_row(0) = strsyohincode
                data_row(1) = inumcnt
                data_row(20) = 0
                data_row(21) = sel_row(inumcnt - 1).ItemArray(7)
                dt_Product.Rows.Add(data_row)
                data_row = dt_Product.NewRow
            End If
            inumcnt = 0
            iloopcnt = iloopcnt + 1
        Loop
        dtDis.Dispose()
        dtDis = Nothing
        'sel_row = Nothing

        Dim wkdv = New DataView(dt_Product)
        Dim strjyunum As String
        Dim str_syohincode(2) As String
        '個数の順にソート
        wkdv.Sort = "all_num DESC"
        dt_Product = wkdv.ToTable
        '商品ごとの順位を設定
        iloopcnt = 0
        Do Until iloopcnt = dt_Product.Rows.Count - 1
            dt_Product.Rows(iloopcnt).Item("key_order") = iloopcnt + 1
            iloopcnt = iloopcnt + 1
        Loop
        wkdv.Dispose()
        wkdv = Nothing
        dv.Dispose()
        dv = Nothing
        '枝番ごとの総合番号設定　商品の順位 * 10 + 枝番の順位
        '受注番号 
        '作成した DatatTable に対する DataView を生成
        dv2 = New DataView(dt_Datas)
        'OrderData TABLE 作成
        dt_OrderData.Columns.Add("order_num", Type.GetType("System.String"))         '0:受注番号
        dt_OrderData.Columns.Add("kessai", Type.GetType("System.String"))            '1: 決済方法
        dt_OrderData.Columns.Add("haiso", Type.GetType("System.String"))             '2: 配送方法
        dt_OrderData.Columns.Add("memo", Type.GetType("System.String"))              '3: 出荷指示メモ
        dt_OrderData.Columns.Add("ondo", Type.GetType("System.String"))              '4: 温度帯
        dt_OrderData.Columns.Add("send_count_hitoketa", Type.GetType("System.Int32")) '5: 送付件数（1桁固定）
        dt_OrderData.Columns.Add("send_count_all", Type.GetType("System.Int32"))     '6:送付件数（全件）
        dt_OrderData.Columns.Add("product_count", Type.GetType("System.Int32"))      '7: 商品点数表示
        dt_OrderData.Columns.Add("order_send_num", Type.GetType("System.String"))     '8: 受注番号送付先番号
        dt_OrderData.Columns.Add("yuusenNO", Type.GetType("System.String"))           '9:優先商品1 OR 2 
        dt_OrderData.Columns.Add("products", Type.GetType("System.String"))         ' 10: 第一優先商品
        dt_OrderData.Columns.Add("priority", Type.GetType("System.String"))           '11:優先度
        dt_OrderData.Columns.Add("out_order", Type.GetType("System.String"))          '12: 指示書出力の順
        dt_OrderData.Columns.Add("product_name", Type.GetType("System.String"))       '13:商品名 
        dt_OrderData.Columns.Add("branch", Type.GetType("System.String"))             '14:枝番 
        dt_OrderData.Columns.Add("kubun", Type.GetType("System.Int32"))             '15:区分 
        dtDis2 = dv2.ToTable(True, "soufunumber")
        iloopcnt = 1
        inumcnt = 1
        inumcnt = 1
        strwkkey = ""
        Dim mRow As DataRow = dt_OrderData.NewRow()
        Dim pet(2)
        Dim drip(2)
        Dim honsya(2)


        Dim idx As Integer = 0
        Dim ikubun As Integer
        Dim wkikubun As Integer
        Dim wkikubun2 As Integer
        Dim isoufu_cnt As Integer = 0
        Dim ipet_cnt As Integer = 0
        Dim idrip_cnt As Integer = 0
        Dim ihon_cnt As Integer = 0
        Dim wkitemcode As String = ""
        Dim wkitemcode2 As String = ""
        Dim strhaisou As String = ""
        Dim strdoukon As String = ""
        Dim strondo As String = ""
        Dim strkessai As String = ""
        Dim strdaibiki As String = ""
        Dim strmemo As String = ""
        Dim strnum1 As String = ""
        Dim strten As String = ""
        Dim strpriority As String = ""
        Dim pre_strjyutu As String = ""
        Dim iadd1 As Integer = 0
        Dim iadd2 As Integer = 0

        Do Until iloopcnt > dtDis2.Rows.Count   '受注送付番号ごと
            strjyunum = dtDis2.Rows(iloopcnt - 1).ItemArray(0)
            'soufunumber
            sel_row = dt_Datas.Select("soufunumber = '" & strjyunum & "'")
            isoufu_cnt = 0
            ipet_cnt = 0
            idrip_cnt = 0
            ihon_cnt = 0
            inumcnt = 1
            Do Until inumcnt > sel_row.Count
                idx = 0
                strwkkey = sel_row(inumcnt - 1).ItemArray(10)
                wkitemcode = sel_row(inumcnt - 1).ItemArray(12) '品番コード
                '除外品番でない  2021/02/04 データ件数が異なるため今はコメント
                'If Jogai_Dic.ContainsKey(wkitemcode) = False And
                'Omake_Dic.ContainsKey(wkitemcode) = False Then
                If Pet_Dic.ContainsKey(wkitemcode) And ipet_cnt < 2 Then
                    If ipet_cnt = 0 Then
                        pet(ipet_cnt) = sel_row(inumcnt - 1).ItemArray(1)
                        ipet_cnt = ipet_cnt + 1
                    ElseIf pet(ipet_cnt) <> sel_row(inumcnt - 1).ItemArray(1) And ipet_cnt = 1 Then　'重複商品か
                        pet(ipet_cnt) = sel_row(inumcnt - 1).ItemArray(1)
                        ipet_cnt = ipet_cnt + 1
                    End If
                ElseIf Drip_Dic.ContainsKey(wkitemcode) And idrip_cnt < 2 Then
                    If idrip_cnt = 0 Then
                        drip(idrip_cnt) = sel_row(inumcnt - 1).ItemArray(1)
                        idrip_cnt = idrip_cnt + 1
                    ElseIf drip(idrip_cnt) <> sel_row(inumcnt - 1).ItemArray(1) And idrip_cnt = 1 Then '重複商品か
                        drip(idrip_cnt) = sel_row(inumcnt - 1).ItemArray(1)
                        idrip_cnt = idrip_cnt + 1
                    End If
                ElseIf ihon_cnt < 2 Then
                    If ihon_cnt = 0 Then
                        honsya(ihon_cnt) = sel_row(inumcnt - 1).ItemArray(1)
                        ihon_cnt = ihon_cnt + 1
                    ElseIf honsya(ihon_cnt) <> sel_row(inumcnt - 1).ItemArray(1) And ihon_cnt = 1 Then '重複商品か
                        honsya(ihon_cnt) = sel_row(inumcnt - 1).ItemArray(1)
                        ihon_cnt = ihon_cnt + 1
                    End If
                End If
                inumcnt = inumcnt + 1
            Loop
            '優先商品のチェック
            str_syohincode(1) = ""
            ikubun = 0       'リキッドのみ0→D/Cのみ1→リキッド+D/C2→リキッドと本社3→D/Cと本社4→本社6
            If ipet_cnt > 0 Then ' 出荷元がリキッドの場合:0 ドリップの場合:1    本社の場合:2
                str_syohincode(0) = pet(0)
                ikubun = 0
                If ipet_cnt = 2 Then  'リキッドのみ0
                    str_syohincode(1) = pet(1)
                    ikubun = 0
                ElseIf idrip_cnt > 0 Then 'リキッド+D/C2
                    str_syohincode(1) = drip(0)
                    ikubun = 2
                ElseIf ihon_cnt > 0 Then  'リキッドと本社3
                    str_syohincode(1) = honsya(0)
                    ikubun = 3
                End If
            ElseIf idrip_cnt > 0 Then
                str_syohincode(0) = drip(0)
                ikubun = 1
                If idrip_cnt = 2 Then  'D/Cのみ1
                    str_syohincode(1) = drip(1)
                    ikubun = 1
                ElseIf ihon_cnt > 0 Then  'D/Cと本社4
                    str_syohincode(1) = honsya(0)
                    ikubun = 4
                End If
            ElseIf ihon_cnt > 0 Then
                str_syohincode(0) = honsya(0)
                ikubun = 6
                If ihon_cnt = 2 Then  '本社6
                    str_syohincode(1) = honsya(1)
                    ikubun = 6
                End If
            End If
            '第一優先商品
            '初期値
            mRow(1) = ""        '1: 決済方法
            mRow(2) = ""          '2: 配送方法
            mRow(3) = ""         '3: 出荷指示メモ
            mRow(4) = ""          '4: 温度帯
            inumcnt = 1
            iadd1 = 0
            iadd2 = 0

            Do Until inumcnt > sel_row.Count
                If str_syohincode(0) = sel_row(inumcnt - 1).ItemArray(1) And iadd1 = 0 Then
                    '配送方法
                    '決済方法
                    mRow(0) = strjyunum   ' 受注送付番号
                    mRow(1) = sel_row(inumcnt - 1).ItemArray(5)           '1: 決済方法
                    mRow(2) = sel_row(inumcnt - 1).ItemArray(4)            '2: 配送方法
                    mRow(3) = sel_row(inumcnt - 1).ItemArray(6)           '3: 出荷指示メモ
                    mRow(4) = sel_row(inumcnt - 1).ItemArray(3)           '4: 温度帯
                    mRow(5) = sel_row(inumcnt - 1).ItemArray(8)           '5: 送付件数（1桁固定） 
                    If sel_row(inumcnt - 1).ItemArray(8) > 9 Then
                        mRow(5) = 9
                    End If
                    mRow(6) = sel_row(inumcnt - 1).ItemArray(8)           '6:送付件数（全件） 
                    mRow(7) = sel_row(inumcnt - 1).ItemArray(9)          '7: 商品点数表示
                    mRow(8) = sel_row(inumcnt - 1).ItemArray(10)          '8: 受注番号送付先番号
                    mRow(9) = "1"                                              '9:優先商品1 OR 2 
                    mRow(10) = sel_row(inumcnt - 1).ItemArray(1)         ' 10: 第一優先商品
                    mRow(11) = ""                                           '11:優先度
                    mRow(12) = ""                                           '12: 指示書出力の順
                    mRow(13) = sel_row(inumcnt - 1).ItemArray(7)         '13:商品名 
                    mRow(14) = sel_row(inumcnt - 1).ItemArray(13)         '14:枝番 
                    mRow(15) = ikubun                                        '15:区分 
                    dt_OrderData.Rows.Add(mRow)
                    mRow = dt_OrderData.NewRow '新しい行をセット
                    iadd1 = iadd1 + 1
                ElseIf str_syohincode(1) <> "" Then '第二優先商品が存在する
                    If str_syohincode(1) = sel_row(inumcnt - 1).ItemArray(1) And iadd2 = 0 Then
                        '1件目のレコードから取得
                        '配送方法
                        '決済方法
                        mRow(0) = strjyunum   ' 受注送付番号
                        mRow(1) = sel_row(inumcnt - 1).ItemArray(5)           '1: 決済方法
                        mRow(2) = sel_row(inumcnt - 1).ItemArray(4)            '2: 配送方法
                        mRow(3) = sel_row(inumcnt - 1).ItemArray(6)           '3: 出荷指示メモ
                        mRow(4) = sel_row(inumcnt - 1).ItemArray(3)           '4: 温度帯
                        mRow(5) = sel_row(inumcnt - 1).ItemArray(8)           '5: 送付件数（1桁固定） 
                        If sel_row(inumcnt - 1).ItemArray(8) > 9 Then
                            mRow(5) = 9
                        End If
                        mRow(6) = sel_row(inumcnt - 1).ItemArray(8)           '6:送付件数（全件） 
                        mRow(7) = sel_row(inumcnt - 1).ItemArray(9)          '7: 商品点数表示
                        mRow(8) = sel_row(inumcnt - 1).ItemArray(10)          '8: 受注番号送付先番号
                        mRow(9) = "2"                                          '9:優先商品1 OR 2 
                        mRow(10) = sel_row(inumcnt - 1).ItemArray(1)         ' 10: 第一優先商品
                        mRow(11) = ""                                           '11:優先度
                        mRow(12) = ""                                           '12: 指示書出力の順
                        mRow(13) = sel_row(inumcnt - 1).ItemArray(7)         '13:商品名 
                        mRow(14) = sel_row(inumcnt - 1).ItemArray(13)        '14:枝番 
                        mRow(15) = ikubun                                       '15:区分 
                        dt_OrderData.Rows.Add(mRow)
                        mRow = dt_OrderData.NewRow '新しい行をセット
                        iadd2 = iadd1 + 1
                    End If
                End If
                inumcnt = inumcnt + 1
            Loop
            inumcnt = 1
            iloopcnt = iloopcnt + 1
        Loop
        sel_row = Nothing
        wkitemcode = ""
        strwkkey = ""
        iloopcnt = 1

        Dim sel_row3 As DataRow()
        dv3 = New DataView(dt_OrderData)
        dtDis3 = dv3.ToTable(True, "order_send_num")
        iloopcnt = 1
        inumcnt = 1

        Do Until iloopcnt > dtDis3.Rows.Count   '受注番号ごと
            inumcnt = 1
            strjyunum = dtDis3.Rows(iloopcnt - 1).ItemArray(0)
            sel_row3 = dt_OrderData.Select("order_send_num = '" & strjyunum & "'")
            If sel_row3.Count > 0 Then
                strwkkey = sel_row3(0).ItemArray(8)      '8: 受注番号送付先番
                strjyunum = sel_row3(0).ItemArray(0)     '0: 受注番号
                strkeycode = sel_row3(0).ItemArray(10)   '10: 第一優先商品
                strbranch = sel_row3(0).ItemArray(14)    '14:枝番
                wkikubun = sel_row3(0).ItemArray(15)     '15:区分
                If strbranch <> "" Then
                    wkitemcode = Mid(strkeycode, 1, Len(strkeycode) - Len(strbranch) - 1)
                Else
                    wkitemcode = strkeycode
                End If
                sel_row4 = dt_Product.Select("key_code = '" & wkitemcode & "'")
                If sel_row4.Count > 0 Then
                    lproduc_order = 0
                    products_order = (wkikubun * 1000000000000)
                    Select Case strbranch
                        Case ""
                            lproduc_order = sel_row4(0).ItemArray（11)
                        Case "m"
                            lproduc_order = sel_row4(0).ItemArray（12)
                        Case "a"
                            lproduc_order = sel_row4(0).ItemArray（13)
                        Case "n"
                            lproduc_order = sel_row4(0).ItemArray（14)
                        Case "nh"
                            lproduc_order = sel_row4(0).ItemArray（15)
                        Case "h"
                            lproduc_order = sel_row4(0).ItemArray（16)
                        Case "e"
                            lproduc_order = sel_row4(0).ItemArray（17)
                        Case "x"
                            lproduc_order = sel_row4(0).ItemArray（18)
                        Case "xx"
                            lproduc_order = sel_row4(0).ItemArray（19)
                    End Select
                    products_order = products_order + (sel_row4(0).ItemArray(20) * 1000000000) 'key_order
                    products_order = products_order + (lproduc_order * 10000000)       'branch_order
                    products_order = products_order + (sel_row4(0).ItemArray(6) * 10)
                End If
                If sel_row3.Count = 2 Then
                    '第二優先商品有
                    strkeycode = sel_row3(1).ItemArray(10)   '10: 第2優先商品
                    strbranch2 = sel_row3(1).ItemArray(14)    '14:枝番
                    wkikubun2 = sel_row3(1).ItemArray(15)     '15:区分
                    'wkitemcode 
                    If strbranch2 <> "" Then
                        wkitemcode2 = Mid(strkeycode, 1, Len(strkeycode) - Len(strbranch2) - 1)
                    End If
                    sel_row4 = dt_Product.Select("key_code = '" & wkitemcode2 & "'")
                    If sel_row4.Count > 0 Then
                        lproduc_order2 = 0
                        Select Case strbranch2
                            Case ""
                                lproduc_order2 = sel_row4(0).ItemArray（11)
                            Case "m"
                                lproduc_order2 = sel_row4(0).ItemArray（12)
                            Case "a"
                                lproduc_order2 = sel_row4(0).ItemArray（13)
                            Case "n"
                                lproduc_order2 = sel_row4(0).ItemArray（14)
                            Case "nh"
                                lproduc_order2 = sel_row4(0).ItemArray（15)
                            Case "h"
                                lproduc_order2 = sel_row4(0).ItemArray（16)
                            Case "e"
                                lproduc_order2 = sel_row4(0).ItemArray（17)
                            Case "x"
                                lproduc_order2 = sel_row4(0).ItemArray（18)
                            Case "xx"
                                lproduc_order2 = sel_row4(0).ItemArray（19)
                        End Select
                        '第二優先商品の出荷場所 * (10 ** 6) +
                        '第二優先商品の商品総合番号 * (10 ** 2) +
                        '第二優先商品の個数 * (10 ** 0) + (10 ** 8)
                        products_order2 = products_order2 + ((sel_row4(0).ItemArray(20) * 10) + lproduc_order2) * 100   'key_order*10+branch_order
                        products_order2 = products_order2
                        products_order = products_order + (wkikubun2 * 1000000)
                        'products_order2 = products_order2 * 100
                        products_order = products_order + products_order2 + sel_row3(1).ItemArray(6) + 100000000
                    End If
                    '配送方法
                    Select Case Trim(sel_row3(0).ItemArray(2))
                        Case "ゆうパック"
                            strhaisou = "0"
                        Case "追跡ゆうメール"
                            strhaisou = "1"
                        Case "ヤマト運輸"
                            strhaisou = "2"
                        Case "宅配便"
                            strhaisou = "3"
                        Case Else   'エラー プログラム終了
                            ' "配送方法が不適切なレコードです。処理を終了します（受注番号：#{@order_num　strwkkey}）"
                            MsgBox("配送方法が不適切なレコードです。処理を終了します（受注番号：" & strjyunum, vbOKOnly + vbCritical, gMsgTitle)
                            'カーソルを通常に戻す
                            Me.Cursor = Cursors.Default
                            Exit Sub
                    End Select
                    '同梱フラグ 第一優先商品の個数が1以外 または 第二優先商品が存在する
                    If sel_row3(0).ItemArray(6) <> 1 Or sel_row3.Count = 2 Then '個数
                        strdoukon = "1"
                    ElseIf strbranch = "" Or strbranch = "m" Or
                        strbranch = "n" Or strbranch = "nh" Then
                        strdoukon = "0"
                    Else
                        strdoukon = "1"
                    End If
                    '温度 
                    If sel_row3(0).ItemArray(4) = "冷凍" Then
                        strondo = "1"
                    Else
                        strondo = "0"
                    End If
                    '1: 決済方法
                    If (sel_row3(0).ItemArray(1) = "コンビニ" Or sel_row3(0).ItemArray(1) = "コンビニ決済") And
                            sel_row3(0).ItemArray(1) <> "コンビニ別送" Then '出荷指示メモ
                        strkessai = "0"
                    ElseIf (sel_row3(0).ItemArray(1) = "コンビニ" Or sel_row3(0).ItemArray(1) = "コンビニ決済") And
                            sel_row3(0).ItemArray(1) <> "コンビニ別送" Then '出荷指示メモ
                        strkessai = "1"
                    Else
                        strkessai = "8"
                    End If
                    '代引き
                    If strhaisou = "2" And sel_row3(0).ItemArray(1) = "代金引換" Then
                        strdaibiki = "1"
                    Else
                        strdaibiki = "0"
                    End If
                    'memo
                    If InStr(sel_row3(0).ItemArray(3), "領収") > 0 Then
                        strmemo = "2"
                        If InStr(sel_row3(0).ItemArray(3), "同梱") > 0 Then
                            strmemo = "0"
                        End If
                    ElseIf InStr(sel_row3(0).ItemArray(3), "納品書") > 0 Then
                        strmemo = "3"
                        If InStr(sel_row3(0).ItemArray(3), "納品書同梱") > 0 Then
                            strmemo = "1"
                        End If
                    ElseIf InStr(sel_row3(0).ItemArray(3), "まで") > 0 Then
                        strmemo = "4"
                    ElseIf InStr(sel_row3(0).ItemArray(3), "質問中") > 0 Then
                        strmemo = "5"
                    ElseIf InStr(sel_row3(0).ItemArray(3), "のし") > 0 Then
                        strmemo = "5"
                    ElseIf Len(sel_row3(0).ItemArray(3)) > 0 Then
                        strmemo = "7"
                    ElseIf Len(sel_row3(0).ItemArray(3)) = 0 Then
                        strmemo = "9"
                    End If
                    strnum1 = Trim(Str(sel_row3(0).ItemArray(5)))          '5: 送付件数（1桁固定） 
                    strten = Trim(Str(sel_row3(0).ItemArray(6)))           '6:送付件数（全件） 
                    '配送　strhaisou　{haiso}#{strdaibiki}#{ondo}#{kessai}#{memo}#{get_send_count_order}#{@kubun}#{@doukon_flag}#{products_order}#{@product_count}
                    strpriority = strhaisou & strdaibiki & strondo & strkessai & strmemo & Format(Val(strnum1), "0") & Format(Val(wkikubun), "0") &
                              Format(Val(products_order), "0000000000000") & Format(Val(strten), "0")
                    'Next
                    '更新
                    sel_row3(0)("priority") = strpriority
                    sel_row3(0)("out_order") = Trim(Str(products_order))
                End If
            End If
            sel_row4 = Nothing
            sel_row3 = Nothing
            iloopcnt = iloopcnt + 1
        Loop
        'out_order.csv 出力 
        blean_Rtn = objcsv.Out_Order_Put(dt_OrderData)

        'order_list.txt 　出力
        blean_Rtn = objcsv.Order_List_Put(dt_OrderData)

        'sougouNo.txt     出力

        dt_Product.Dispose()
        dt_Product = Nothing
        dt_Datas.Dispose()
        dt_Datas = Nothing
        dt_OrderData.Dispose()
        dt_OrderData = Nothing
        '辞書ファイルクリア
        Jogai_Dic.Clear()
        Pet_Dic.Clear()
        Omake_Dic.Clear()
        Drip_Dic.Clear()

        If blean_Rtn = True Then
            MsgBox("取込処理が正常に終了しました。", vbOKOnly + vbInformation, gMsgTitle)
        Else
            MsgBox("取込処理に失敗しました。", vbOKOnly + vbCritical, gMsgTitle)
        End If
        'フォームのカーソルを元に戻す
        Me.Cursor = Cursors.Default
        Dim Writer As New IO.StreamWriter("C:sample.txt")

        For Each strBuff As String In (strpriority)
            Writer.WriteLine(strpriority)
        Next strBuff
        Writer.Close()




    End Sub



    Private Function iPutcnt() As Integer
        Throw New NotImplementedException()
    End Function
End Class