1
Jianw
2025-07-09 f6f5e6b632d6649386a380558d84003f3de7ec6c
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
--[[
    编码: GK-84-20
    名称: 导入国科入库任务
    作者:HAN   
    日期:2025-7-1 
    入口函数: Import
    来源:
         国科
 
    功能说明:
        通过导入国科入库任务创建 码盘单 和 码盘明细
 
    变更历史:
 
--]]
wms_cntr = require( "wms_container" )
wms_pallet = require( "wms_palletizing" )
 
local function set_cell_limit_def( cell_type, cell_limit, avl_cell_set, cell_set)
    if cell_limit > 0 then
        table.insert( avl_cell_set, cell_type )
        local cell_limit = {
            cell_type = cell_type,
            loading_limit = cell_limit
        }
        table.insert( cell_set.cell_def, cell_limit )
    end
end
 
function Import(strLuaDEID)
    local nRet, strRetInfo, strNo
    -- 获取导入的数据, 返回 [{"attr":"xx","value":""},...]
 
    m3.PrintLuaDEInfo( strLuaDEID )
 
    local row_data = {}
    nRet, row_data = m3.GetSysDataJson(strLuaDEID)
    if (nRet ~= 0 or strRetInfo == '') then 
        lua.Stop( strLuaDEID, "无法获取导入数据!")
        return
    end
 
    local factory
    nRet, factory = wms_base.Get_sConst2( "WMS_Default_Factory" )
    if ( nRet ~= 0 ) then
        lua.Stop( strLuaDEID, "系统无法获取常量'WMS_Default_Factory'")
        return
    end      
    local wh_code
    nRet, wh_code = wms_base.Get_sConst2( "WMS_Default_Warehouse" )
    if ( nRet ~= 0 ) then
        lua.Stop( strLuaDEID, "系统无法获取常量'WMS_Default_Warehouse'")
        return
    end   
 
 
    local row_attrs
    local n, nCount, nRows, sku, cntr_spec, ctd_code
  
    -- 导入是可分多页分批导入, 因此每次的起始行不一样
    nRet, strRetInfo = mobox.getGlobalAttrValue( strLuaDEID, "start_row" )
    if ( nRet ~= 0 ) then
        mobox.error( strLuaDEID, "获取批量导入全局参数 start_row 失败!"..strRetInfo )
        return
    end    
    local nStartRow = tonumber( strRetInfo )
    local str_err_msg
    local task_detail, item_detail
    local cntr = {}
    local inbound_no_set = {}
 
    -- 步骤1 获取从excel导入的一行数据
    nRow = nStartRow
    for row = 1, #row_data do
        row_attrs = row_data[row]
        nCount = #row_attrs
 
        cell_limit_set = {
            A = 0, B = 0, C = 0, D = 0, E = 0, F = 0
        }
        task_detail = m3.AllocObject( strLuaDEID, "GK_IN_TASK_Detail" )
        item_detail = m3.AllocObject2( strLuaDEID, "INB_Pallet_Detail" )
        -- 初始化 SKU 对象
        sku = m3.AllocObject(strLuaDEID,"SKU")
        cntr_spec = ''
        ctd_code = ''
        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
                    task_detail.io_no = strValue 
                    find = false
                    for _, no in ipairs( inbound_no_set ) do
                        if strValue == no then
                            find = true
                            break
                        end
                    end
                    if not find then
                        table.insert( inbound_no_set, strValue )
                    end                    
                elseif (strAttr == "行号") then
                    if (strValue == '') then 
                        str_err_msg = strAttr .. "不能为空!"
                        goto err_msg_process
                    end
                    task_detail.row_no = lua.Get_NumAttrValue( strValue )  
 
                elseif (strAttr == "商品编码") then
                    if (strValue == '') then 
                        str_err_msg = strAttr .. "不能为空!"
                        goto err_msg_process
                    end
                    sku.item_code = strValue
                    task_detail.item_code = strValue
                    item_detail.S_ITEM_CODE = strValue
 
                elseif (strAttr == "商品名称") then
                    if (strValue == '') then 
                        str_err_msg = strAttr .. "不能为空!"
                        goto err_msg_process
                    end
                    sku.item_name = strValue  
                    task_detail.item_name = strValue
                    item_detail.S_ITEM_NAME = strValue
 
                elseif (strAttr == "重量") then
                    sku.weight = lua.StrToNumber( strValue )
                elseif (strAttr == "体积") then
                    sku.volume = lua.StrToNumber( strValue )
                elseif (strAttr == "适配料格") then
                    if (strValue == '') then 
                        str_err_msg = strAttr .. "不能为空!"
                        goto err_msg_process
                    end
                    sku.cell_type = strValue
                elseif (strAttr == "数量") then
                    task_detail.qty = lua.Get_NumAttrValue( strValue ) 
                    item_detail.F_QTY = task_detail.qty
 
                elseif (strAttr == "A") then
                    cell_limit_set.A =lua.StrToNumber( strValue )
                elseif (strAttr == "B") then
                    cell_limit_set.B =lua.StrToNumber( strValue )
                elseif (strAttr == "C") then
                    cell_limit_set.C =lua.StrToNumber( strValue )
                elseif (strAttr == "D") then
                    cell_limit_set.D =lua.StrToNumber( strValue )
                elseif (strAttr == "E") then
                    cell_limit_set.E =lua.StrToNumber( strValue )
                elseif (strAttr == "F") then
                    cell_limit_set.F =lua.StrToNumber( strValue )
 
                elseif (strAttr == "产品线") then
                    sku.udf01 = strValue 
                    item_detail.S_UDF01 = strValue
                elseif (strAttr == "商品状态") then
                    sku.item_state = strValue    
                    task_detail.item_state = strValue 
                    item_detail.S_ITEM_STATE = strValue  
   
                elseif (strAttr == "货主") then
                    sku.storer = strValue    
                    task_detail.storer = strValue
                    item_detail.S_STORER = strValue
                                   
                elseif (strAttr == "批次") then
                    task_detail.batch_no = strValue
                    item_detail.S_BATCH_NO = strValue  
       
                elseif (strAttr == "容器类型") then
                    if (strValue == '') then 
                        str_err_msg = strAttr .. "不能为空!"
                        goto err_msg_process
                    end
                    sku.ctd_code = strValue
                    ctd_code = strValue
                elseif (strAttr == "有效期") then
                    strValue = string.gsub( strValue, "/","-")
                    task_detail.exp_date = strValue
                    item_detail.D_EXP_DATE = strValue 
 
                elseif (strAttr == "周转箱号") then
                    if (strValue == '') then 
                        str_err_msg = strAttr .. "不能为空!"
                        goto err_msg_process
                    end
                    task_detail.cntr_code = strValue 
                    item_detail.S_CNTR_CODE = strValue 
 
                elseif (strAttr == "料格类型") then
                    if (strValue == '') then 
                        str_err_msg = strAttr .. "不能为空!"
                        goto err_msg_process
                    end
                    cntr_spec = strValue 
                elseif (strAttr == "料格编码") then
                    if (strValue == '') then 
                        str_err_msg = strAttr .. "不能为空!"
                        goto err_msg_process
                    end
                    task_detail.cell_no = strValue
                    item_detail.S_CELL_NO = strValue 
                                   
                end
            end
        end
 
        ::err_msg_process::
        if ( str_err_msg ~= '' ) then
            local err_msg_row = {
                row_no = nRow,
                err_msg = str_err_msg
            }
            lua.Stop( strLuaDEID, "第"..nRow.."数据不正确 --> "..str_err_msg )
            return
        else
            -- 判断导入的数据是否完整
            if task_detail.cntr_code == '' then
                lua.Stop( strLuaDEID, "第"..nRow.."数据不正确 --> 周转箱号不能为空!")
                return
            end
            if cntr_spec == '' then
                lua.Stop( strLuaDEID, "第"..nRow.."数据不正确 --> 料格类型不能为空!")
                return
            end
            if ctd_code == '' then
                lua.Stop( strLuaDEID, "第"..nRow.."数据不正确 --> 容器类型不能为空!")
                return
            end            
            if task_detail.cell_no == '' then
                lua.Stop( strLuaDEID, "第"..nRow.."数据不正确 --> 料格编码不能为空!")
                return
            end
 
            -- 判断料箱是否存在, 不存在需要创建料箱,是否是空料箱,如果不是空料箱要报错
            nRet, cntr = wms_cntr.GetInfo( strLuaDEID, task_detail.cntr_code )
            if nRet ~= 0 then
                lua.Stop( strLuaDEID, "第"..nRow.."获取容器对象时发生错误 --> "..cntr)
                return
            end       
            if cntr == '' then
                -- 料箱不存在,新建一个
                cntr = m3.AllocObject( strLuaDEID, "Container" ) 
                cntr.type = "Cell_Box"
                cntr.spec = cntr_spec
                cntr.code = task_detail.cntr_code
                cntr.ctd_code = ctd_code
                nRet, cntr = m3.CreateDataObj( strLuaDEID, cntr )
                if nRet ~= 0 then
                    lua.Stop( strLuaDEID, cntr )
                    return
                end 
            else
                if  cntr.empty_full ~= 0 then
                    lua.Stop( strLuaDEID, "料箱'"..task_detail.cntr_code.."'已经存在,并且非空" )
                    return
                end   
                if cntr.position ~= '' then 
                    lua.Stop( strLuaDEID, "料箱'"..task_detail.cntr_code.."'已经和货位'"..cntr.position.."'绑定" )
                    return
                end                                                                  
            end
 
            local avl_cell_set = {}
            local cell_set = {
                ctd_code = sku.ctd_code,
                cell_def = {}
            }
 
            local cell_type = "A"
            for n = 1, 6 do
                cell_limit = cell_limit_set[cell_type]
                set_cell_limit_def( cell_type, cell_limit, avl_cell_set, cell_set)
                cell_type = lua.IncrementalChar(cell_type)
            end
 
            sku.available_spec = lua.table2str( avl_cell_set )
            sku.sku_grid_parm = "["..lua.table2str( cell_set ).."]"
            sku.count_method = "Limit"
            nRet, strRetInfo = m3.CreateDataObj( strLuaDEID, sku, 1 )
            if nRet ~= 0 then
                lua.Stop( strLuaDEID, strRetInfo )
                return
            end
            nRet, strRetInfo = m3.CreateDataObj( strLuaDEID, task_detail )
            if nRet ~= 0 then
                lua.Stop( strLuaDEID, strRetInfo )
                return
            end 
 
            -- 增加码盘
            local detail_item_data = {}
            table.insert( detail_item_data, item_detail )
            nRet, strRetInfo = wms_pallet.Add_INB_Pallet_Detail( strLuaDEID, cntr.code, detail_item_data )
            if nRet ~= 0 then
                lua.Stop( strLuaDEID, strRetInfo )
                return
            end                     
        end
        nRow = nRow + 1
    end
 
    local gk_in_task
    for _, no in ipairs( inbound_no_set ) do
        gk_in_task = m3.AllocObject(strLuaDEID,"GK_IN_TASK")  
        gk_in_task.no = no     
        gk_in_task.factory = factory
        gk_in_task.wh_code = wh_code
        nRet, strRetInfo = m3.CreateDataObj( strLuaDEID, gk_in_task )
        if nRet ~= 0 then
            lua.Stop( strLuaDEID, strRetInfo )
            return
        end     
    end    
 end