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
--[[  
    编码: JX-19-04 
    名称: 码盘中-确定  
    作者:
    日期:
 
    级别:
 
    函数: ClickOK
 
    功能:
--]]
 
wms_base = require( "wms_base" )
wms_wh = require( "wms_wh" )
 
 
-- 主函数
function ClickOK(strLuaDEID)
    local nRet, strRetInfo
    local attrs
 
    local now = os.date("%Y%m%d%H%M%S")
    local randomNum = math.random(1, 99999)
    local strHeader = 'HHWMS'..os.date("%y%m")..'-'
    local strCode
 
    -- 获取码盘单号
    nRet, strCode = mobox.getSerialNumber( "巨沃移库单", strHeader, 5 )  
    if (nRet ~= 0) then 
        mobox.setInfo(strLuaDEID, '申请码盘单编码失败!'..strCode) 
        return
    end
 
    -- 获取输入参数
    nRet, attrs = m3.GetSysInputParameter(strLuaDEID)
    if (nRet ~= 0) then
        mobox.setInfo(strLuaDEID, "获取当前输入面板里的属性失败! " .. attrs)
        return
    end
    lua.Debug(strLuaDEID, debug.getinfo(1), "attrs", attrs)
    
    local input_attr = m3.KeyValueAttrsToObjAttr(attrs)
 
    local cntr_code = input_attr.S_CNTR_CODE
    local no = input_attr.S_EXT_ATTR1
 
    if (cntr_code == nil or cntr_code == '') then
        mobox.setInfo(strLuaDEID, "容器编码不能为空!")
        return
    end
 
    -- 获取"正在码盘"数据
    local obj
    nRet, obj = m3.GetSysDataJson(strLuaDEID)
    if (nRet ~= 0) then
        mobox.setInfo(strLuaDEID, "无法获取数据包!" .. obj)
        return
    end
    
    lua.Debug(strLuaDEID, debug.getinfo(1), "obj", obj)
    
    local item_list = obj[1].item_list
    local action = {}
    
    -- 如果码盘单号为空的话,那么就是没有新建状态的码盘单,则需要新建一个码盘单数据
    if (no == '') then
        
        local cell_no, item_code, qty
        local f_qty, f_acc_m_qty, f_acc_p_qty
        -- 创建码盘单对象
        local transfer_order = m3.AllocObject(strLuaDEID, "JX_Transfer_Order")
        transfer_order.no = strCode
        transfer_order.cntr_code = cntr_code
        
            for i = 1, #item_list do
                local item = item_list[i]
                local attrs_obj = m3.KeyValueAttrsToObjAttr(item.attrs)
                cell_no = attrs_obj.S_CELL_NO
                item_code = attrs_obj.S_ITEM_CODE
                qty = lua.Get_NumAttrValue(attrs_obj.F_QTY)
    
                -- 查询巨沃移库量表,校验库存数量
                local strCondition = "S_ITEM_CODE = '" .. item_code .. "'"
                nRet, strRetInfo = m3.GetDataObjByCondition(strLuaDEID, "JX_Inventory_Transfer", strCondition)
                if (nRet ~= 0) then 
                    mobox.setInfo(strLuaDEID, "查询 Material 表失败: " .. strRetInfo) 
                    return
                end
    
                f_qty = lua.Get_NumAttrValue(strRetInfo.qty) 
                f_acc_m_qty = lua.Get_NumAttrValue(strRetInfo.acc_m_qty)
                f_acc_p_qty = f_qty - f_acc_m_qty
                if (qty > f_acc_p_qty) then
                    mobox.setInfo(strLuaDEID, "物料编码 " .. item_code .. " 的数量超出库存量!")
                    return
                end
    
                -- 查询物料信息
                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
    
                -- 创建JX_TO_Detail对象并插入数据
                local to_detail = m3.AllocObject(strLuaDEID, "JX_TO_Detail")
                to_detail.to_no = strCode
                to_detail.cntr_code = cntr_code
                to_detail.item_code = item_code
                to_detail.item_name = strRetInfo.item_name
                to_detail.qty = qty
                to_detail.cell_no = cell_no
                to_detail.f_alloc_qty = f_acc_p_qty
                to_detail.volume = strRetInfo.volume
                to_detail.weight = strRetInfo.weight
    
                nRet, to_detail = m3.CreateDataObj(strLuaDEID, to_detail)
                if (nRet ~= 0) then
                    mobox.setInfo(strLuaDEID,  '创建码盘明细对象失败!' .. to_detail)
                    return
                end
            end
        
        nRet, transfer_order = m3.CreateDataObj(strLuaDEID, transfer_order)
        if (nRet ~= 0) then
            mobox.setInfo(strLuaDEID, '创建码盘对象失败!' .. transfer_order)
            return
        end
            
            -- 查询明细数据
        local detail_strCondition = "S_TO_NO = '" .. strCode .. "'"
        local detail_data_objs
        nRet, detail_data_objs = m3.QueryDataObject(strLuaDEID, "JX_TO_Detail", detail_strCondition)
        if (nRet ~= 0) then
            mobox.setInfo(strLuaDEID,"查询明细数据失败! " .. detail_data_objs)
            return
        end
        lua.Debug(strLuaDEID, debug.getinfo(1), "detail_data_objs", detail_data_objs)
        
        action[1] = 
            {
                action_type = "set_dlg_attr",
                value = {
                    { attr = "S_EXT_ATTR1", value = strCode }
                }
            }
        -- 将所有行数据批量插入到页面
        action[2] = {
            action_type = "set_subtable_page_content",
            value = {
                page_name = "已码盘",
                content = detail_data_objs,
                clear = true,
                clear_confirm = false,
                checkbox = false
            }
        }
        
        action[3] = {
            action_type = "clear_subpage_rows",
            value = {
                page_name = "码盘中"
            }
        }
        
        -- 执行所有操作
        nRet, strRetInfo = mobox.setAction(strLuaDEID, lua.table2str(action))
        if (nRet ~= 0) then
            mobox.setInfo(strLuaDEID,"执行操作失败! " .. strRetInfo)
            return
        end
        
    else
        local page_info = {}
        local cell_no, item_code, qty
        local f_qty, f_acc_m_qty, f_acc_p_qty
        
        for i = 1, #item_list do
            local item = item_list[i]
            local attrs_obj = m3.KeyValueAttrsToObjAttr(item.attrs)
            cell_no = attrs_obj.S_CELL_NO
            item_code = attrs_obj.S_ITEM_CODE
            qty = lua.Get_NumAttrValue(attrs_obj.F_QTY)
            
             -- 查询巨沃移库量表,校验库存数量
            local strCondition = "S_ITEM_CODE = '" .. item_code .. "'"
            nRet, strRetInfo = m3.GetDataObjByCondition(strLuaDEID, "JX_Inventory_Transfer", strCondition)
            if (nRet ~= 0) then 
                lua.Error(strLuaDEID, debug.getinfo(1), "查询 Material 表失败: " .. strRetInfo) 
            end
 
            f_qty = lua.Get_NumAttrValue(strRetInfo.qty) 
            f_acc_m_qty = lua.Get_NumAttrValue(strRetInfo.acc_m_qty)
            f_acc_p_qty = f_qty - f_acc_m_qty
            if (qty > f_acc_p_qty) then
                lua.Error(strLuaDEID, debug.getinfo(1), "物料编码 " .. item_code .. " 的数量超出库存量!")
            end
 
            -- 查询物料信息
            local strCondition = "S_ITEM_CODE = '" .. item_code .. "'"
            nRet, strRetInfo = m3.GetDataObjByCondition(strLuaDEID, "Material", strCondition)
            if (nRet ~= 0) then 
                lua.Error(strLuaDEID, debug.getinfo(1), "查询 Material 表失败: " .. strRetInfo) 
            end
 
            -- 创建JX_TO_Detail对象并插入数据
            local to_detail = m3.AllocObject(strLuaDEID, "JX_TO_Detail")
            to_detail.to_no = no
            to_detail.cntr_code = cntr_code
            to_detail.item_code = item_code
            to_detail.item_name = strRetInfo.item_name
            to_detail.qty = qty
            to_detail.cell_no = cell_no
            to_detail.f_alloc_qty = f_acc_p_qty
            to_detail.volume = strRetInfo.volume
            to_detail.weight = strRetInfo.weight
 
            nRet, to_detail = m3.CreateDataObj(strLuaDEID, to_detail)
            if (nRet ~= 0) then
                mobox.setInf(strLuaDEID,  '创建码盘明细对象失败!' .. to_detail)
                return
            end
            
        end
        
            -- 查询明细数据
        local detail_strCondition = "S_TO_NO = '" .. no .. "'"
        local detail_data_objs
        nRet, detail_data_objs = m3.QueryDataObject(strLuaDEID, "JX_TO_Detail", detail_strCondition)
        if (nRet ~= 0) then
            mobox.setInfo(strLuaDEID, "查询明细数据失败! " .. detail_data_objs)
            return
        end
        lua.Debug(strLuaDEID, debug.getinfo(1), "detail_data_objs", detail_data_objs)
        -- 将所有行数据批量插入到页面
        action[1] = {
            action_type = "set_subtable_page_content",
            value = {
                page_name = "已码盘",
                content = detail_data_objs,
                clear = true,
                clear_confirm = false,
                checkbox = false
            }
        }
        
        action[2] = {
            action_type = "clear_subpage_rows",
            value = {
                page_name = "码盘中"
            }
        }
        
    
        -- 执行所有操作
        nRet, strRetInfo = mobox.setAction(strLuaDEID, lua.table2str(action))
        if (nRet ~= 0) then
            mobox.setInfo(strLuaDEID,"执行操作失败! " .. strRetInfo)
            return
        end
    end
 
    
 
end