1
Jianw
9 天以前 70f29da38121b9a467841253e3268feb5df02902
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
--[[
    编码: AMS-90-10
    名称: 物料-导入
    作者:LZH
    日期:2025-4-17
    入口函数: Import
 
    功能说明:
 
    变更历史:
 
--]]
wms_base = require("wms_base")
 
local function value_sort(a, b) return a < b end
 
function Import(strLuaDEID)
    local nRet, strRetInfo, strNo
    -- 获取导入的数据, 返回 [{"attr":"xx","value":""},...]
    local row_data = {}
    nRet, row_data = m3.GetSysDataJson(strLuaDEID)
    if (nRet ~= 0 or strRetInfo == '') then lua.Error(strLuaDEID, debug.getinfo(1), "无法获取导入数据!") end
    -- lua.Debug(strLuaDEID, debug.getinfo(1), '修改条数:', row_data)
 
    local row_attrs
    local n, nCount, nRows, nIndex
 
    -- 导入是可分多页分批导入, 因此每次的起始行不一样
    nRet, strRetInfo = mobox.getGlobalAttrValue(strLuaDEID, "start_row")
    if (nRet ~= 0) then
        mobox.error(strLuaDEID, "获取批量导入全局参数 start_row 失败!" .. strRetInfo)
        return
    end
    local nStartRow = tonumber(strRetInfo)
    local err_msg_list = {} -- 用于写导入错误信息
    local str_err_msg
 
    -- 步骤1 获取从excel导入的一行数据
    nRow = nStartRow
    for row = 1, #row_data do
        row_attrs = row_data[row]
        nCount = #row_attrs
        -- 初始化 货位 对象
        local materail = m3.AllocObject(strLuaDEID, "SKU")
        nIndex = 1
        local value_array = {}
        strNo = ''
        str_err_msg = ''
        for n = 1, nCount do
            strAttr = row_attrs[n].attr
            strValue = row_attrs[n].value
            if (strAttr ~= '') then
                if (strAttr == "物料号") then
                    if (strValue == '') then
                        str_err_msg = strAttr .. "不能为空!"
                        goto err_msg_process
                    end
                    materail.item_code = strValue
                    --V1.2
                elseif (strAttr == "物料描述") then
                    if (strValue == '') then
                        str_err_msg = strAttr .. "不能为空!"
                        goto err_msg_process
                    end
                    materail.item_name = strValue
                elseif (strAttr == "存储库区") then
                    if (strValue == '') then
                        str_err_msg = strAttr .. "不能为空!"
                        goto err_msg_process
                    end
                    materail.udf01 = strValue
                elseif (strAttr == "材质") then
                    materail.udf02 = strValue
                elseif (strAttr == "ABC类型") then
                    if (strValue == '') then
                        str_err_msg = strAttr .. "不能为空!"
                        goto err_msg_process
                    end
                    materail.abc_type = strValue
                elseif (strAttr == "数量上限") then
                    materail.loading_limit = strValue
                elseif (strAttr == "单件重量") then
                    materail.weight = tonumber(strValue)
                    if (tonumber(strValue) == 50) then
                        materail.ctd_code = "CTD-001"
                    else
                        materail.ctd_code = "CTD-002"
                    end
                elseif (strAttr == "料箱重量") then
                    if (strValue == '') then
                        str_err_msg = strAttr .. "不能为空!"
                        goto err_msg_process
                    end
                    materail.load_capacity = strValue
                elseif (strAttr == "料箱规格") then
                    materail.cell_type = strValue
                end
            end
        end
 
        ::err_msg_process::
        if (str_err_msg ~= '') then
            local err_msg_row = {
                row_no = nRow,
                err_msg = str_err_msg
            }
            table.insert(err_msg_list, err_msg_row)
        else
            if (tonumber(materail.loading_limit) == nil) then
                materail.count_method = 'Limit'
                if (tonumber(materail.weight) == nil) then
                    local err_msg_row = {
                        row_no = nRow,
                        err_msg = "数量上限和单件重量必须有一个有值!"
                    }
                    table.insert(err_msg_list, err_msg_row)
                    goto back
                end
            else
                materail.count_method = 'Weight'
                if (tonumber(materail.weight) ~= nil) then
                    local err_msg_row = {
                        row_no = nRow,
                        err_msg = "数量上限和单件重量有且只有一个有值!"
                    }
                    table.insert(err_msg_list, err_msg_row)
                    goto back
                end
            end
            local strCondition = "S_CODE = '" .. materail.udf01 .. "'"
            nRet, id, strRetInfo = mobox.getDataObjAttrByKeyAttr(strLuaDEID, "Area", strCondition)
            if (nRet ~= 0) then
                local err_msg_row = {
                    row_no = nRow,
                    err_msg = "判断库区'" .. area_code .. "' 是否存在时失败!" .. strRetInfo
                }
                table.insert(err_msg_list, err_msg_row)
                goto back
            end
            -- lua.Debug(strLuaDEID, debug.getinfo(1), 'materail:', materail)
            -- 判断物料是否已经存在,如果已经存在刷新新的属性
            local strCondition = "S_ITEM_CODE = '" .. materail.item_code .. "'"
            nRet, id, strRetInfo = mobox.getDataObjAttrByKeyAttr(strLuaDEID, "SKU", strCondition)
            if (nRet > 1) then
                local err_msg_row = {
                    row_no = nRow,
                    err_msg = "导入物料信息失败: 在检查 inco = '" .. materail.item_code .. "'是否存在时失败!" .. strRetInfo
                }
                table.insert(err_msg_list, err_msg_row)
            end
            if (nRet == 1) then
                -- 不存在
                nRet, materail = m3.CreateDataObj(strLuaDEID, materail)
                if (nRet ~= 0) then
                    local err_msg_row = {
                        row_no = nRow,
                        err_msg = "序号='" .. strNo .. "'导入行在创建【物料】时失败!" .. materail
                    }
                    table.insert(err_msg_list, err_msg_row)
                end
            else
                -- 已经存在更新数据对象
                local update_data_obj = {
                    {
                        id = id,
                        attrs = {
                            { attr = "S_ITEM_CODE",     value = materail.item_code },
                            { attr = "S_ITEM_NAME",     value = materail.item_name },
                            { attr = "S_UDF01",         value = materail.udf01 },
                            { attr = "S_UDF02",         value = materail.udf02 },
                            { attr = "S_ABCTYPE",       value = materail.abc_type },
                            { attr = "N_LOADING_LIMIT", value = materail.loading_limit },
                            { attr = "F_LOAD_CAPACITY", value = materail.load_capacity },
                            { attr = "S_CELL_TYPE",      value = materail.cell_type },
                            { attr = "S_CTD_CODE",      value = materail.ctd_code },
                            { attr = "S_COUNT_METHOD",      value = materail.count_method },
                            { attr = "F_WEIGHT",        value = materail.weight }
                        }
                    }
                }
                nRet, strRetInfo = mobox.updateDataObj(strLuaDEID, "SKU", lua.table2str(update_data_obj))
                if (nRet ~= 0) then
                    local err_msg_row = {
                        row_no = nRow,
                        err_msg = "序号='" .. strNo .. "'导入行在更新【物料】属性失败!" .. strRetInfo
                    }
                    table.insert(err_msg_list, err_msg_row)
                end
            end
        end
        ::back::
        nRow = nRow + 1
    end
    lua.Debug(strLuaDEID, debug.getinfo(1), 'err_msg_list:', #err_msg_list)
    -- 把导入过程中的错误信息返回到前端
    local import_fail = {
        result = {
            fail = err_msg_list
        }
    }
 
    if (#err_msg_list > 0) then
        lua.Error(strLuaDEID, debug.getinfo(1), lua.table2str(import_fail))
    end
end