1
Jianw
10 天以前 88e26a2a960dbbc148332772448b79b9877102d8
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
--[[
    编码: GK-56-21
    名称: 预分配容器明细-条码
    作者:HAN  
    日期:2025-1-29
 
    级别:固定 (说明本段代码在项目中不太会变化)
    
    函数: BarCodeChange
 
    功能:
        -- 根据输入的周转箱号 GK_IN_TASK_Detail 表查询, 获取入库单明细中的货品编码,批次号等信息
 
 
    更改记录:
 
 
--]]
wms_wh   = require( "wms_wh" )
wms_cntr = require( "wms_container" )
gk_base  = require( "gk_base" )
 
function BarCodeChange ( strLuaDEID ) 
    local nRet, strRetInfo
 
    m3.PrintLuaDEInfo( strLuaDEID )
 
    -- 获取周转箱号
    nRet, strRetInfo = mobox.getCurEditDataObjAttr( strLuaDEID, "BarCode","ScanType","Prompt","TrayCode","F_ACT_QTY","S_ITEM_CODE") 
    if ( nRet ~= 0 )  then 
        wms_base.Notice_Audio( strLuaDEID, "获取当前编辑属性失败! "..strRetInfo, "error.mp3" ) 
        return
    end 
    local obj_attrs = json.decode( strRetInfo ) 
    local bar_code = lua.Get_StrAttrValue( obj_attrs[1].value )  
    local scan_type = lua.Get_StrAttrValue( obj_attrs[2].value ) 
    local prompt = lua.Get_StrAttrValue( obj_attrs[3].value )  
    local cur_tray_code = lua.Get_StrAttrValue( obj_attrs[4].value ) 
    local act_qty = lua.Get_NumAttrValue( obj_attrs[5].value )  
    local item_code = lua.Get_StrAttrValue( obj_attrs[6].value )
 
    if bar_code == '' then return end
 
    local parameter
    nRet, parameter = m3.Get_3053_PanelParameter( strLuaDEID, "组盘输入" )
    if nRet ~= 0 then
        wms_base.Notice_Audio( strLuaDEID, parameter, "error.mp3" ) 
        return        
    end
    local station = parameter.station or ''  
    if ( station == "") then
        wms_base.Notice_Audio( strLuaDEID, "'组盘输入'面板必须有 station 参数!", "error.mp3")
        return         
    end
    local groupID = "Station:"..station
 
    local wms_url
    nRet, wms_url = wms_base.Get_sConst2( "网站URL" ) 
    if ( nRet ~= 0 ) then
        wms_base.Notice_Audio( strLuaDEID, "系统无法获取常量'网站URL'", "error.mp3" ) 
        return
    end    
 
    local success
    local action = {}
    if prompt == "请扫周转箱号" then
        -- 扫周转箱的处理程序
        local tray_code = bar_code
        local sku_list = {}
        nRet, sku_list = gk_base.Set_CurTrayBoxDetail( strLuaDEID, station, tray_code )
        if ( nRet ~= 0 ) then 
            wms_base.Notice_Audio( strLuaDEID, sku_list, "error.mp3" ) 
            return
        end        
        if scan_type == "SN" then
            prompt_info = "请扫商品SN"
        else
            prompt_info = "请扫商品条码"
        end
        action = 
        {
            {
                action_type = "set_panel_dlg_attr",
                value = {
                        panel_name = "组盘输入",
                        attrs = {
                                    { attr = "S_ITEM_CODE", value = "" },
                                    { attr = "S_ITEM_NAME", value = "" },
                                    { attr = "BarCode", prompt = prompt_info },
                                    { attr = "Prompt", value = prompt_info },
                                    { attr = "S_BATCH_NO", value = "" },
                                    { attr = "F_QTY", value = 0 },
                                    { attr = "ExpDate", value = "" },
                                    { attr = "TrayCode", value = tray_code },
                                    { attr = "F_ACT_QTY", value = 0, enable = false }
                                }
                        }
                 
            },
            {
                action_type = "set_dlg_cursor",
                value = {
                    ctrl_id = "BarCode"
                }
            },
            {
                action_type = "play_audio",
                value = wms_url.."/static/audio/".."success.mp3"        
            }        
        }    
        mobox.removeCacheValue( "CurInboundTask", groupID )       
    else
        if lua.StrIsEmpty( cur_tray_code ) then
            wms_base.Notice_Audio( strLuaDEID, "请先扫周转箱!", "error.mp3" ) 
            return            
        end
        -- 获取当前亮灯的周转箱号+入库任务信息, 局部变量 CurInboundTask
        -- { tray_code="xx", pac_detail_id = "xxx", loc_code = "" } 
        nRet, strRetInfo = mobox.getCacheValue( "CurInboundTask", groupID )
        local cur_in_task = {}
        if ( nRet == 0 ) then 
            local success 
            success, cur_in_task = pcall( json.decode, strRetInfo )
            if ( success == false ) then
                wms_base.Notice_Audio( strLuaDEID, "获取当前入库任务缓存信息失败!", "error.mp3" ) 
                return   
            end
        end
 
        local sku_list = {}
        local reload_traybox_detail = false
 
        -- 获取周转箱缓存中的货品编码去匹配输入的条码
        nRet, strRetInfo = mobox.getCacheValue( cur_tray_code.."->Detail", groupID )
        if nRet == 0 then
            success, sku_list = pcall( json.decode, strRetInfo)
            if ( success == false ) then
                reload_traybox_detail = true
            end
        end 
    
        if reload_traybox_detail then
            nRet, sku_list = gk_base.Set_CurTrayBoxDetail( strLuaDEID, station, bar_code )
            if ( nRet ~= 0 ) then 
                wms_base.Notice_Audio( strLuaDEID, sku_list, "error.mp3" ) 
                return
            end   
        end        
        -- 扫单一的商品条码,ScanType 不能是 SN
        local find = false
        local find_sku = {}
        local next_prompt_info = ''
        local scaned_sn_set = {}        -- 已经扫描过的SN号
 
        if prompt == "请扫商品条码" then
            for _, sku in ipairs( sku_list ) do
                if string.find( bar_code, sku.item_code ) then
                    if #sku.item_code == #bar_code then
                        -- 说明当前扫的是完整的商品编码,还要继续扫批次号,分的两段编码
                        action = 
                        {
                            {
                                action_type = "set_panel_dlg_attr",
                                value = {
                                        panel_name = "组盘输入",
                                        attrs = {
                                                    { attr = "BarCode", prompt = "请扫商品批号" },
                                                    { attr = "Prompt", value = "请扫商品批号" },                                        
                                                    { attr = "S_ITEM_CODE", value = bar_code },  
                                                    { attr = "S_BATCH_NO", value = "" }
                                                }
                                        }
                                
                            },
                            {
                                action_type = "set_dlg_cursor",
                                value = {
                                    ctrl_id = "BarCode"
                                }
                            },                           
                            {
                                action_type = "play_audio",
                                value = wms_url.."/static/audio/".."success.mp3"        
                            }        
                        }  
                        nRet, strRetInfo = mobox.setAction( strLuaDEID, lua.table2str(action)  )
                        if ( nRet ~= 0 ) then 
                            lua.Stop( strLuaDEID, "setAction失败! "..strRetInfo ) 
                            return
                        end 
                        return 
                    else
                        if string.find( bar_code, sku.batch_no ) then
                            if string.find( bar_code, sku.exp_date ) then
                                find = true
                                find_sku = sku
                                break
                            end
                        end
                    end
                end
            end
            next_prompt_info = "请扫商品条码"
        elseif prompt == "请扫商品批号" then 
            -- 分两次扫码的模式,bar_code 里面包含 批次号和有效期
            if item_code == '' then
                wms_base.Notice_Audio( strLuaDEID, "请先扫商品编码!", "error.mp3" ) 
                return                      
            end
            for _, sku in ipairs( sku_list ) do
                if string.find( item_code, sku.item_code ) then
                    if string.find( bar_code, sku.batch_no ) then
                        if string.find( bar_code, sku.exp_date ) then
                            find = true
                            find_sku = sku
                            break
                        end
                    end
 
                end  
            end
            next_prompt_info = "请扫商品条码" 
        elseif prompt == "请扫商品SN" then
            -- 从 S_SN 里进行比对
            -- 从缓存获取周转箱已经扫描过的SN
            nRet, strRetInfo = mobox.getCacheValue( cur_tray_code.."->SN", groupID )
 
            if nRet == 0 then
                success, scaned_sn_set = pcall( json.decode, strRetInfo)
                if ( success == false ) then
                    scaned_sn_set = {}
                end
            end   
            -- 如果已经扫描过,报错
            if not lua.isTableEmpty( scaned_sn_set ) then
                for _, sn in ipairs( scaned_sn_set ) do
                    if bar_code == sn then
                        wms_base.Notice_Audio( strLuaDEID, "扫描的SN'"..bar_code.."'已经扫描过一次!", "error.mp3" ) 
                        return                           
                    end
                end
            end
            -- 判断 SN 是否 在S_SN中
            for _, sku in ipairs( sku_list ) do
                if string.find( sku.sn, bar_code ) then
                    find = true
                    find_sku = sku
                    break                    
                end
            end
            next_prompt_info = "请扫商品SN"
        else
            wms_base.Notice_Audio( strLuaDEID, "扫码提示'"..prompt.."'系统没定义!", "error.mp3" ) 
            return        
        end
 
        if find then
            -- 通过 商品编码+批次+有效期+周转箱 匹配到
            local strCondition
            local pac_detail_objs
    
            strCondition = "S_ITEM_CODE = '"..find_sku.item_code.."' AND S_STORER = '"..find_sku.storer.."' AND S_ITEM_STATE = '"..find_sku.item_state.."' AND "..
                           "S_BATCH_NO = '"..find_sku.batch_no.."' AND N_B_STATE = "..PAC_DETAIL_STATE.Palletizing.." AND "..
                           "D_EXP_DATE = '"..find_sku.exp_date_long.."' AND S_UDF20 = '"..cur_tray_code.."'"
    
            nRet, pac_detail_objs = m3.QueryDataObject( strLuaDEID, "Pre_Alloc_CNTR_Detail", strCondition, "" )
            if nRet ~= 0 then 
                wms_base.Notice_Audio( strLuaDEID, "查询入库任务明细(预分配)错误!", "error.mp3" ) 
                return            
            end
            if pac_detail_objs == '' then
                wms_base.Notice_Audio( strLuaDEID, "系统无法定位到有效的任务明细! (没满足条件)请看系统日志" )
                lua.DebugEx( strLuaDEID, "站台["..station.."]扫码无法定位入库任务 -->", strCondition )
                return              
            end
            if #pac_detail_objs > 1 then
                wms_base.Notice_Audio( strLuaDEID, "系统无法定位到有效的任务明细! (超过1条)请看系统日志" )
                lua.DebugEx( strLuaDEID, "站台["..station.."]扫码无法定位入库任务 -->", strCondition )
                return              
            end                   
    
            local obj_attrs = m3.KeyValueAttrsToObjAttr( pac_detail_objs[1].attrs )   
            local cntr_code = obj_attrs.S_CNTR_CODE or ''
            local cell_no = obj_attrs.S_CELL_NO or ''
            local loc_code
            nRet, loc_code = wms_cntr.Get_Container_Loc( strLuaDEID, cntr_code )
            if nRet ~= 0 then
                wms_base.Notice_Audio( strLuaDEID, " Get_Container_Loc 失败!"..loc_code, "error.mp3" ) 
                return
            end   
            if loc_code == '' then
                wms_base.Notice_Audio( strLuaDEID, "料箱'"..cntr_code.."'没有和货位进行绑定!", "error.mp3")
                return
            end    
            local login_info
            nRet, login_info = wms_base.Get_CurLoginUserInfo( strLuaDEID )
            if ( nRet ~= 0 ) then 
                wms_base.Notice_Audio( strLuaDEID, login_info, "error.mp3" )
                return
            end
 
            -- 亮灯,设置站台缓存, 这个缓存要拍灯后才起取消
            local light_action = {}
            if lua.isTableEmpty( cur_in_task ) then 
                local in_task =  { 
                    tray_code = cur_tray_code, 
                    pac_detail_id = pac_detail_objs[1].id,
                    loc_code = loc_code
                } 
                nRet, strRetInfo = gk_base.Lighting( loc_code, obj_attrs.S_CELL_NO, lua.Get_NumAttrValue(obj_attrs.F_QTY), 
                                                    PTL_COLOR.Green, 1, login_info.login, "站台入库" )
                if nRet ~= 0 then
                    wms_base.Notice_Audio( strLuaDEID, "货位'"..loc_code.."' 亮灯失败! "..strRetInfo, "error.mp3" )
                    return            
                end
                nRet = mobox.setCacheValue( "CurInboundTask", lua.table2str( in_task ), groupID )
                if nRet ~= 0 then
                    wms_base.Notice_Audio( strLuaDEID, "设置站台缓存[CurInboundTask]失败!", "error.mp3")
                    return            
                end
 
                light_action = 
                {
                    action_type = "refresh_related_panel",
                    value = {  
                                {
                                    panel_name = "料格显示",
                                    input_parameter = {
                                        cell_no = obj_attrs.S_CELL_NO,
                                        loc_code = loc_code,
                                        light_color = "#00FF00"         -- 绿色
                                    }
                                }  
                            }       
                }  
            else
                -- 已经有亮灯,判断一下是否是同一个入库任务
                if cur_in_task.pac_detail_id ~= pac_detail_objs[1].id then
                    wms_base.Notice_Audio( strLuaDEID, "扫码的商品不在当前入库任务里!")
                    return                    
                end
            end
            act_qty = act_qty + 1
            action = 
            {
                {
                    action_type = "set_panel_dlg_attr",
                    value = {
                            panel_name = "组盘输入",
                            attrs = {
                                        { attr = "BarCode", prompt = next_prompt_info },
                                        { attr = "Prompt", value = next_prompt_info },                                
                                        { attr = "S_ITEM_CODE", value = find_sku.item_code },
                                        { attr = "S_ITEM_NAME", value = find_sku.item_name },
                                        { attr = "S_BATCH_NO", value = find_sku.batch_no },
                                        { attr = "F_QTY", value = obj_attrs.F_QTY },
                                        { attr = "ExpDate", value = find_sku.exp_date_long },
                                        { attr = "F_ACT_QTY", value = act_qty, enable = true }
                                    }
                            }
                    
                },
                {
                    action_type = "set_dlg_cursor",
                    value = {
                        ctrl_id = "BarCode"
                    }
                },                    
                {
                    action_type = "play_audio",
                    value = wms_url.."/static/audio/".."success.mp3"        
                }        
            }   
            if not lua.isTableEmpty( light_action ) then
                table.insert( action, light_action )
            end
 
            -- 如果是扫描的SN 需要把SN保存的缓存
            if prompt == "请扫商品SN" then
                table.insert( scaned_sn_set, bar_code )
                nRet = mobox.setCacheValue( cur_tray_code.."->SN", lua.table2str( scaned_sn_set ), groupID )
                if nRet ~= 0 then
                    wms_base.Notice_Audio( strLuaDEID, "设置站台缓存[SN]失败!", "error.mp3")
                    return            
                end                
            end
        else
            -- 没找到报错
            action = 
            {
                -- 设置当前光标还是在 商品编码输入框
                {
                    action_type = "set_dlg_cursor",
                    value = {
                        ctrl_id = "BarCode"
                    }
                },           
                {
                    action_type = "play_audio",
                    value = wms_url.."/static/audio/".."notexist.mp3"        
                }        
            }          
        end
    end
 
    lua.DebugEx( strLuaDEID, "item code change action", action )
 
    nRet, strRetInfo = mobox.setAction( strLuaDEID, lua.table2str(action)  )
    if ( nRet ~= 0 ) then 
        lua.Stop( strLuaDEID, "setAction失败! "..strRetInfo ) 
        return
    end 
 
end