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
--[[
   编码: JX-19-12
   名称: 
   作者:
   日期:2025-02-15
 
   函数: AftercodeChange
   功能:
 
   更改记录:
 
--]]
 
m3 = require("oi_base_mobox")
wms_base = require( "wms_base" )
wms_wh = require( "wms_wh" )
 
function AftercodeChange(strLuaDEID)
    local nRet, strRetInfo
    local attrs
 
    -- 获取表头界面中的输入条码
    nRet, attrs = m3.GetSysInputParameter(strLuaDEID)
    if (nRet ~= 0) then
        mobox.setInfo(strLuaDEID,  "获取当前输入面板里的属性失败! " .. attrs)
        return
    end
    
    local input_attr = m3.KeyValueAttrsToObjAttr(attrs)
    local item_code = input_attr.S_ITEM_CODE        --商品编码
    local cell_no = input_attr.S_CELL_NO            --料格号
    local cntr_code = input_attr.S_CNTR_CODE        -- 容器号
    
    local container_data, nRet
    local strCondition = "S_TO_NO IN (SELECT S_NO FROM TN_JX_Transfer_Order WHERE S_CNTR_CODE = '" .. cntr_code .. "' AND N_B_STATE = 1)"
    nRet, container_data = m3.QueryDataObject(strLuaDEID, "JX_TO_Detail", strCondition)
 
    -- 如果没有查询到数据,直接跳过,不做报错
    if nRet ~= 0 or not container_data or #container_data == 0 then
        --跳过
    else
        -- 如果查询到了数据,则进行后续处理和比对
        for i = 1, #container_data do
            local detail_attrs = m3.KeyValueAttrsToObjAttr(container_data[i].attrs)
            local current_cell_no = detail_attrs.S_CELL_NO       -- 料格号
            local current_item_code = detail_attrs.S_ITEM_CODE   -- 商品编码
 
            if current_cell_no == cell_no then
                if current_item_code ~= item_code then
                    mobox.setInfo(strLuaDEID, "已审核的料箱的料格号 " .. cell_no .. " 已存在物料编码 " .. current_item_code .. ",不能更新为物料编码 " .. item_code)
                    return
                end
            end
        end
    end
 
    
    -- 查询巨沃移库量表
    local f_qty,f_acc_m_qty,f_acc_p_qty
    local strCondition = "S_ITEM_CODE = '" .. item_code .. "'"
    nRet, strRetInfo = m3.GetDataObjByCondition(strLuaDEID, "JX_Inventory_Transfer", strCondition)
    if (nRet ~= 0) then 
        mobox.setInfo(strLuaDEID, "查询 JX_Inventory_Transfer 表失败: " .. strRetInfo) 
        return
    end
    
    f_qty = lua.Get_NumAttrValue(strRetInfo.qty) 
    f_acc_m_qty = lua.Get_NumAttrValue(strRetInfo.acc_m_qty)
    if(f_qty == f_acc_m_qty) then
        mobox.setInfo(strLuaDEID, "编码 = '" .. item_code .. "'的物料已分配完!")
        return
    end
    
    -- 取实际数量和分配数量,做差异值
    f_acc_p_qty = f_qty - f_acc_m_qty
    
    local page_sorting = nil
    local page_completed = nil
 
    -- 正在码盘和已码盘页面
    local obj
    nRet, obj = m3.GetSysDataJson(strLuaDEID) 
    if (nRet ~= 0) then
        mobox.setInfo(strLuaDEID, "无法获取数据包! " .. obj)
        return
    end
    
    for i = 1, #obj do
        local page = obj[i]
        if (page.page_name == "码盘中") then
            page_sorting = page
        elseif (page.page_name == "已码盘") then
            page_completed = page
        end
    end
    
    local item_list_sorting = page_sorting.item_list
    local item_list_completed = page_completed.item_list
    
    
    -- 检查“已码盘”中是否已有料格号与物料编码匹配
    local current_cell_no , current_item_code
    for n = 1, #item_list_completed do
        local attrs_obj = m3.KeyValueAttrsToObjAttr(item_list_completed[n].attrs)
    
        current_cell_no = attrs_obj.S_CELL_NO       -- 料格号
        current_item_code = attrs_obj.S_ITEM_CODE   -- 商品编码
    
        if (current_cell_no == cell_no) then
            if (current_item_code == item_code) then
                mobox.setInfo(strLuaDEID, "料格号 " .. cell_no .. " 已存在物料编码 " .. current_item_code .. ",请到已码盘相关料格修改数量")
                return
            else
                mobox.setInfo(strLuaDEID, "料格号 " .. cell_no .. " 已存在物料编码 " .. current_item_code .. ",不能更新为物料编码 " .. item_code)
                return
            end
        end
    end
 
 
 
 
   -- 检查正在码盘中是否已有料格号与物料编码匹配
    local item_found = false
    local action = {} 
 
    if (item_list_sorting ~= nil and item_list_sorting ~= '') then
        local current_cell_no
        local current_item_code 
        local current_qty
 
        for n = 1, #item_list_sorting do
            local attrs_obj = m3.KeyValueAttrsToObjAttr(item_list_sorting[n].attrs)
    
            current_cell_no = attrs_obj.S_CELL_NO       -- 料格号
            current_item_code = attrs_obj.S_ITEM_CODE   -- 商品编码
            current_qty = tonumber(attrs_obj.F_QTY)     -- 获取当前数量
    
            if (current_cell_no == cell_no) then
                if (current_item_code ~= item_code) then
                    mobox.setInfo(strLuaDEID, "料格号 " .. cell_no .. " 已存在物料编码 " .. current_item_code .. ",不能更新为物料编码 " .. item_code)
                    return
                else
                    item_found = true
    
                    -- 更新正在码盘页面的数量
                    local update_action = {
                        action_type = "set_subtable_page_row",
                        value = {
                            page_name = "码盘中", 
                            row = {
                                {
                                    condition = { 
                                        {
                                            attr = "S_ITEM_CODE",  
                                            value = current_item_code    
                                        },
                                        {
                                            attr = "S_CELL_NO", 
                                            value = current_cell_no 
                                        }
                                    },
                                    attrs = {
                                        {
                                            attr = "F_QTY", 
                                            operation = "+", 
                                            value = "1" 
                                        }
                                    }
                                }
                            }
                        }
                    }
    
                    table.insert(action, update_action)
                end
            end
        end
    end
    
    -- 如果没有找到料格号匹配的记录,则插入新行
    if not item_found then
        -- 查询条码表,获取物料编码,因为输入的是条码和编码
        -- local strCondition = "S_BARCODE = '" .. item_code .. "'"
        -- nRet, strRetInfo = m3.GetDataObjByCondition(strLuaDEID, "JX_Material_Barcode", strCondition)
        -- if nRet ~= 0 then
        --     mobox.setInfo(strLuaDEID,  "查询 Material 表失败: " .. strRetInfo)
        --     return
        -- end
        -- local material_code = strRetInfo.item_code      -- 物料编码
        --,再用物料编码查询物料表
        local strCondition = "S_ITEM_CODE = '" .. item_code .. "'"
        nRet, strRetInfo = m3.GetDataObjByCondition(strLuaDEID, "Material", strCondition)
        if nRet ~= 0 then
            mobox.setInfo(strLuaDEID,  "查询 Material 表失败: " .. strRetInfo)
            return
        end
        local material_name = strRetInfo.item_name              -- 物料名称
    
        -- 准备插入数据
        local attr_array = {}
        local insert_row = {}
    
        attr_array[1] = lua.KeyValueObj("S_ITEM_NAME", material_name)       -- 物料名称 
        attr_array[2] = lua.KeyValueObj("S_ITEM_CODE", item_code)       -- 物料编码 
        attr_array[3] = lua.KeyValueObj("S_CELL_NO", cell_no)               -- 料格号
        attr_array[4] = lua.KeyValueObj("F_QTY", "1")                       -- 数量
        attr_array[5] = lua.KeyValueObj("F_ALLOC_QTY", f_acc_p_qty)         -- 可码盘数量
    
        insert_row.attrs = attr_array
        
        local test_row = {}
        test_row[1] = insert_row
 
    
        -- 插入新行
        local insert_action = {
            action_type = "insert_subtable_page_row",
            value = {
                page_name = "码盘中",  
                row = test_row
            }
        }
    
        table.insert(action, insert_action) 
    
 
    end 
        
    -- 设置主页面物料编码输入框为空
        local set_dlg_action = {
            action_type = "set_dlg_attr",
            value = {
                lua.KeyValueObj("S_ITEM_CODE", "")
            }
        }
    
        table.insert(action, set_dlg_action) 
    
    -- 执行动作
    nRet, strRetInfo = mobox.setAction(strLuaDEID, lua.table2str(action))
    if (nRet ~= 0) then
        mobox.setInfo(strLuaDEID,  "setAction失败! " .. strRetInfo .. ' action = ' .. lua.table2str(action))
        return
    end
 
 
 
end