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
--[[  
    编码: JX-19-14
    名称: 审核确定
    作者:
    日期:
 
    级别:
 
    函数: AfterSubmit
 
--]]
 
json  = require ("json")
mobox = require ("OILua_JavelinExt")
m3 = require("oi_base_mobox")
jx_base= require( "jx_base" )
wms_base = require( "wms_base" )
wms_wh = require( "wms_wh" )
 
-- 主函数
function AfterSubmit(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 cntr_code = input_attr.S_CNTR_CODE
    local strCode = 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
 
    local item_list = obj[1].item_list
    
    local items_for_api = {} 
    local strUpdateSql 
    local cell_no, item_code, qty, f_alloc_qty, 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)
        f_alloc_qty = lua.Get_NumAttrValue(attrs_obj.F_ALLOC_QTY)
        -- 将物料数据加入批量调用接口的列表
        table.insert(items_for_api, { inco = item_code, qty = qty, fwid = "JX-C-P001", owid = "JX-C-P002" })
    end
 
 
    -----------------------------------------------------------------------------------------------------------------------------------------
    -- 获取巨沃WMS服务地址
    local jw_wms = wms_base.Get_sConst(strLuaDEID, "巨沃WMS服务地址")
    local strurl = jw_wms .. '?service=newOffShelfs&appkey=gwall&secret=null&format=JSON'
 
    -- 设置请求体
    local body = {
        whco = "JX", 
        items = items_for_api,
        orderID = strCode 
    }
 
    -- 输出调试信息
    lua.Debug(strLuaDEID, debug.getinfo(1), "调巨沃接口-->", strurl)
    lua.Debug(strLuaDEID, debug.getinfo(1), "body", body)
 
    local nRet, strRetInfo = mobox.sendHttpRequest(strurl, "", "content=" .. lua.table2str(body))
    if (nRet ~= 0 or strRetInfo == '') then
        mobox.setInfo(strLuaDEID, "调用巨沃WMS接口失败! " .. strRetInfo)
        return
    end
    -- 输出接口返回结果
    lua.Debug(strLuaDEID, debug.getinfo(1), "调巨沃接口结果-->", strRetInfo)
 
    -- 解析接口返回结果,判断是否成功,一切基于调接口成功与否
    local strSetAttr
    local curTime = os.date("%Y-%m-%d %H:%M:%S")
    strCondition = "S_NO = '"..strCode.."'"
 
    local result = json.decode(strRetInfo)
    lua.Debug(strLuaDEID, debug.getinfo(1), "result-->", result)
    
    if (result.isSuccess == false) then
        
        lua.Debug( strLuaDEID, debug.getinfo(1), "JW-WMS newOffShelfs 接口错误", strRetInfo )
        strSetAttr = "N_CR_STATE = 2, S_CR_ERR = 'JW-WMS newOffShelfs 接口错误, 详细信息看日志!', T_CR = '"..curTime.."'"
        
        nRet, strRetInfo = mobox.updateDataAttrByCondition( strLuaDEID, "JX_Transfer_Order", strCondition, strSetAttr )
        if ( nRet ~= 0 ) then  
            lua.Error( strLuaDEID, debug.getinfo(1), "完工回报失败!"..strRetInfo ) 
        end  
        mobox.setInfo(strLuaDEID, "编码 = '" .. strCode .. "'完工回报失败!原因 '" ..result.body.. "' ")    --调接口失败,回退,不继续下面的操作
        return
    else
        strSetAttr = "N_CR_STATE = 1, S_CR_ERR = '', T_CR = '"..curTime.."'"
        nRet, strRetInfo = mobox.updateDataAttrByCondition( strLuaDEID, "JX_Transfer_Order", strCondition, strSetAttr )
        if ( nRet ~= 0 ) then  
            mobox.setInfo( strLuaDEID,  "更新JX_Transfer_Order完工回报信息失败!"..strRetInfo ) 
            return
        end  
    end
        
 
    -----------------------------------------------------------------------------------------------------------------------------------------
    -- 删除 对应的码盘单明细表,以码盘号作为条件
    -- 注意,先删除再创建
    local strCondition = "S_TO_NO = '" .. strCode .. "' "
    nRet, strRetInfo = mobox.deleteDataObject(strLuaDEID, "JX_TO_Detail", strCondition)
    if (nRet ~= 0) then
        lua.Error(strLuaDEID, debug.getinfo(1), "删除 JX_TO_Detail 失败! " .. strRetInfo)
    end
    
    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)
        f_alloc_qty = lua.Get_NumAttrValue(attrs_obj.F_ALLOC_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
 
        lua.Debug(strLuaDEID, debug.getinfo(1), "strRetInfo-->", strRetInfo)
 
        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
 
        lua.Debug(strLuaDEID, debug.getinfo(1), "f_acc_p_qty-->", f_acc_p_qty)
        
        if (qty > f_acc_p_qty) then
            mobox.setInfo(strLuaDEID, "物料编码 " .. item_code .. " 的数量超出库存量!")
            return
        end
 
        -- 更新巨沃移库量表
        local strUpdateSql = "F_ACC_M_QTY = F_ACC_M_QTY + " .. qty
        strCondition = "S_ITEM_CODE = '"..item_code.."'"
        nRet, strRetInfo = mobox.updateDataAttrByCondition( strLuaDEID, "JX_Inventory_Transfer", strCondition, strUpdateSql )
        if ( nRet ~= 0 ) then  
            mobox.setInfo( strLuaDEID, debug.getinfo(1), "更新【巨星任务】信息失败!"..strRetInfo ) 
            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
        -- 创建码盘单明细表对象
        local jx_to_detail = m3.AllocObject(strLuaDEID, "JX_TO_Detail")
        jx_to_detail.to_no = strCode
        jx_to_detail.cntr_code = cntr_code
        jx_to_detail.item_code = item_code
        jx_to_detail.qty = qty
        jx_to_detail.cell_no = cell_no
        jx_to_detail.f_alloc_qty = f_alloc_qty
        jx_to_detail.volume = strRetInfo.volume
        jx_to_detail.weight = strRetInfo.weight
        jx_to_detail.item_name = strRetInfo.item_name  
 
        nRet, jx_to_detail = m3.CreateDataObj(strLuaDEID, jx_to_detail)
        if (nRet ~= 0) then
            mobox.setInfo(strLuaDEID, '创建码盘单明细对象失败!' .. jx_to_detail)
            return
        end
    end
    -- 更新状态为审核即1
    local strCondition
    strUpdateSql = "N_B_STATE = 1"
    strCondition = "S_NO = '"..strCode.."'"
    nRet, strRetInfo = mobox.updateDataAttrByCondition( strLuaDEID, "JX_Transfer_Order", strCondition, strUpdateSql )
    if ( nRet ~= 0 ) then  
        mobox.setInfo( strLuaDEID,"更新JX_Transfer_Order信息失败!"..strRetInfo ) 
        return
    end   
    
    -- 创建 JX_Task 任务池
    -- 查询该料箱是否有任务池,有则不创建
    local strCondition = "S_CNTR_CODE = '" .. cntr_code .. "' AND ( N_B_STATE = 0 or N_B_STATE = 1 )"
    local nRet, data_objs = m3.QueryDataObject(strLuaDEID, "JX_Task", strCondition)
    if (nRet ~= 0) then
        mobox.setInfo(strLuaDEID, debug.getinfo(1), "查询任务池失败! " .. data_objs)
        return
    end
    if (data_objs == '') then
        local jx_task = m3.AllocObject(strLuaDEID, "JX_Task")
        jx_task.cntr_code = cntr_code  -- 托盘号
        jx_task.source_sys = "巨沃"   -- 来源系统
        jx_task.sour_no = strCode    -- 上游任务号
        jx_task.end_area_code = wms_base.Get_sConst(strLuaDEID, "料箱库存储区")
        jx_task.task_type = "入库"   -- 作业类型
        jx_task.create_method = "JW-WMS"
        jx_task.start_wh_code = wms_base.Get_sConst(strLuaDEID, "默认仓库标识")
 
        nRet, jx_task = m3.CreateDataObj(strLuaDEID, jx_task)
        if (nRet ~= 0) then
            mobox.setInfo(strLuaDEID,  "创建 JX_Task 任务池记录失败! " .. jx_task)
            return
        end
    end
    
 
    --  清空"已码盘"页面
    local action = {}
    action[1] = {
        action_type = "clear_subpage_rows",
        value = {
            page_name = "已码盘"
        }
    }
        action[2] = {
        action_type = "set_dlg_attr",
        value = {
            {
                attr = "S_EXT_ATTR1",
                value = ""
            }
        }
    }
        action[3] = {
        action_type = "set_dlg_attr",
        value = {
            {
                attr = "S_CNTR_CODE",
                value = ""
            }
        }
    }
    
        action[4] = {
        action_type = "set_dlg_attr",
        value = {
            {
                attr = "S_CELL_NO",
                value = ""
            }
        }
    }
 
    nRet, strRetInfo = mobox.setAction(strLuaDEID, lua.table2str(action))
    if (nRet ~= 0) then
        mobox.setInfo(strLuaDEID, "清空已码盘页面失败! " .. strRetInfo)
        return
    end
    mobox.setInfo(strLuaDEID, "审核成功!")
end