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
--[[
   编码: AMS-01-33
   名称: 
   作者:
   日期:2025-06-03
 
   函数: Test
   功能:
 
   更改记录:
 
--]] json = require("json")
mobox = require("OILua_JavelinExt")
m3 = require("oi_base_mobox")
wms_base = require("wms_base")
wms_wh = require("wms_wh")
function Test(strLuaDEID)
    local objs, nRet, strRetInfo
    -- 获取选中的数据对象
    nRet, objs = m3.GetSysDataJson(strLuaDEID)
    if (nRet ~= 0) then
        lua.Error(strLuaDEID, debug.getinfo(1), objs)
    end
    lua.Debug(strLuaDEID, debug.getinfo(1), "objs!", objs)
    if (#objs > 5) then
        lua.Stop(strLuaDEID, "最多只能选择5条数据对象!")
        return
    elseif (#objs == 0) then
        lua.Stop(strLuaDEID, "请选择数据对象!")
        return
    end
 
    nRet, attrs = m3.GetRuntimeParam(strLuaDEID)
    if (nRet ~= 0) then
        lua.Error(strLuaDEID, debug.getinfo(1), "m3.GetSysInputParameter 失败! " .. attrs)
    end
    
    -- 获取拣货工位
    local end_loc_code = attrs.formQueryDataObj.end_loc_code
    if (end_loc_code == nil or end_loc_code == '') then
        lua.Stop(strLuaDEID, "请选择拣货工位!")
        return
    end
    -- 获取机台信息
    nRet, end_loc_code = wms_base.Get_sConst2(strLuaDEID, "机台-" .. end_loc_code)
    local success, ext_data = pcall(json.decode, end_loc_code)
    if (success == false) then
        lua.Error(strLuaDEID, debug.getinfo(1), "机台格式不合法!")
    end
    lua.Debug(strLuaDEID, debug.getinfo(1), "ext_data", ext_data)
    -- 获取终点信息
    local to_loc
    nRet, to_loc = wms_wh.GetLocInfo(ext_data.jh_loc_code)
    if (nRet ~= 0) then
        lua.Error(strLuaDEID, debug.getinfo(1), "获取终点信息失败!" .. to_loc)
    end
 
    -- 创建SN出库作业
    local cntr_condition = ''
    for n = 1, #objs do
        -- 生成SN料箱出库的来源单号用来做唯一标识
        local strCode = ''
        local strHeader = 'SNCK' .. os.date("%y%m%d") .. '-'
        nRet, strCode = mobox.getSerialNumber("SN料箱出库", strHeader, 5)
        if (nRet ~= 0) then
            lua.Error(strLuaDEID, debug.getinfo(1), '申请SN料箱出库编码失败!' .. strCode)
        end
 
        local cntr_code = m3.KeyValueAttrsToObjAttr(objs[n].attrs)["a.S_CODE"]
        local loc_code = m3.KeyValueAttrsToObjAttr(objs[n].attrs)["b.S_LOC_CODE"]
        cntr_condition = cntr_condition .. "'" .. cntr_code .. "',"
 
        -- 获取起点信息
        local from_loc
        nRet, from_loc = wms_wh.GetLocInfo(loc_code)
        if (nRet ~= 0) then
            lua.Error(strLuaDEID, debug.getinfo(1), "获取起点信息失败!" .. from_loc)
        end
 
        local operation = m3.AllocObject(strLuaDEID, "Operation")
        operation.start_wh_code = from_loc.wh_code
        operation.start_area_code = from_loc.area_code
        operation.start_loc_code = from_loc.code
        operation.end_wh_code = to_loc.wh_code
        operation.end_area_code = to_loc.area_code
        operation.end_loc_code = to_loc.code
        operation.op_type = wms_base.Get_nConst(strLuaDEID, "作业类型-出库")
        operation.op_def_name = "SN料箱出库" -- 注意这些可能会根据项目的不同而不同,就是出库的作业类型
        operation.cntr_code = cntr_code
        operation.bs_no = strCode
        nRet, operation = m3.CreateDataObj(strLuaDEID, operation)
        if (nRet ~= 0) then
            lua.Error(strLuaDEID, debug.getinfo(1), '创建【作业】失败!' .. operation)
        end
 
        -- 修改容器状态为出库中
        local strCondition = "S_CODE = '" .. cntr_code .. "'"
        local strUpdateSql = "S_UDF02 = '出库中'"
        nRet, strRetInfo = mobox.updateDataAttrByCondition(strLuaDEID, "Container", strCondition, strUpdateSql)
        if (nRet ~= 0) then
            lua.Stop(strLuaDEID, "更新【容器】信息失败!" .. strRetInfo)
            return
        end
    end
 
    cntr_condition = lua.trim_laster_char(cntr_condition)
 
    -- 创建入库单和入库单明细
    -- 创建入库单
    local header = 'RK' .. os.date("%y%m%d") .. '-'
    local ret, order_no = mobox.getSerialNumber("入库单", header, 4)
    local order = m3.AllocObject(strLuaDEID, "Inbound_Order")
    order.state = "发布"
    order.no = order_no
    order.bs_no = order_no
    order.op_type = "SN取消"
    order.area_code = "0001"
    order.wh_code = "0001"
    order.sour_no = order_no
 
    local ret4, msg4 = m3.CreateDataObj(strLuaDEID, order)
    if ret4 ~= 0 then
        lua.Stop(strLuaDEID, "创建入库单失败:" .. msg4)
        return
    end
 
    -- 合并物料并创建入库单明细
    local items = {}
    -- 获取所有该来源单号的容器货品明细信息
    local cg_detail
    local strCondition = "S_CNTR_CODE IN(" .. cntr_condition .. ")"
    nRet, cg_detail = m3.QueryDataObject(strLuaDEID, "CG_Detail", strCondition)
    if (nRet ~= 0) then
        lua.Stop(strLuaDEID, "获取【容器货品明细】信息失败!" .. cg_detail)
        return
    end
 
    if (#cg_detail == 0) then
        lua.Stop(strLuaDEID, "没有SN料箱的货品信息!")
        return
    end
 
    for i = 1, #cg_detail do
        local attr = m3.KeyValueAttrsToObjAttr(cg_detail[i].attrs)
        items[i] = attr
    end
 
    -- 合并相同物料编码的总数量
    local merged_items = {}
    for _, item in ipairs(items) do
        local key = item.S_ITEM_CODE
        if merged_items[key] == nil then
            merged_items[key] = {
                S_ITEM_CODE = item.S_ITEM_CODE, -- 物料编码
                S_ITEM_NAME = item.S_ITEM_NAME, -- 物料名称
                S_BS_NO = item.S_BS_NO, -- 来源单号
                F_QTY = item.F_QTY, -- 数量
                D_PRD_DATE = item.D_PRD_DATE, -- 生产日期
                N_ITEM_STATE = item.N_ITEM_STATE, -- 物料状态
                S_SUPPLIER_NO = item.S_SUPPLIER_NO, -- 供应商编码
                S_SUPPLIER_NAME = item.S_SUPPLIER_NAME, -- 供应商名称
                N_BS_ROW_NO = item.N_BS_ROW_NO, -- 来源单行号
                F_WEIGHT = item.F_WEIGHT, -- 重量
 
                S_EXT_ATTR1 = item.S_EXT_ATTR1, -- 扩展属性1
                S_EXT_ATTR2 = item.S_EXT_ATTR2, -- 扩展属性2
                S_EXT_ATTR3 = item.S_EXT_ATTR3, -- 扩展属性3
                S_EXT_ATTR4 = item.S_EXT_ATTR4, -- 扩展属性4
                S_EXT_ATTR5 = item.S_EXT_ATTR5 -- 扩展属性5
 
            }
        else
            merged_items[key].F_QTY = merged_items[key].F_QTY + item.F_QTY
        end
    end
 
    -- 创建入库单明细
    for _, merge_item in pairs(merged_items) do
        -- 获取SKU信息
        local cond_item = "S_ITEM_CODE = '" .. merge_item.S_ITEM_CODE .. "'"
        local nRet, itemAttrs = m3.GetDataObjByCondition(strLuaDEID, "SKU", cond_item)
        if (nRet ~= 0 or not itemAttrs) then
            lua.Stop(strLuaDEID, "查询物料表失败: " .. merge_item.S_ITEM_CODE)
            return
        end
 
        local erp_detail = m3.AllocObject(strLuaDEID, "Inbound_Detail")
        erp_detail.io_no = order_no
        erp_detail.item_code = merge_item.S_ITEM_CODE
        erp_detail.item_name = merge_item.S_ITEM_NAME
        erp_detail.qty = merge_item.F_QTY
        erp_detail.item_state = merge_item.N_ITEM_STATE
        erp_detail.prd_date = merge_item.D_PRD_DATE
        erp_detail.supplier = merge_item.S_SUPPLIER_NO
        erp_detail.supplier_name = merge_item.S_SUPPLIER_NAME
        erp_detail.item_name = merge_item.S_ITEM_NAME
 
        erp_detail.s_material = itemAttrs.udf02 -- 物料材质
        erp_detail.ext_attr1 = merge_item.S_EXT_ATTR1
        erp_detail.ext_attr2 = merge_item.S_EXT_ATTR2
        erp_detail.ext_attr3 = merge_item.S_EXT_ATTR3
        erp_detail.ext_attr4 = merge_item.S_EXT_ATTR4
        erp_detail.ext_attr5 = merge_item.S_EXT_ATTR5
        local ret_add, msg_add = m3.CreateDataObj(strLuaDEID, erp_detail)
        if ret_add ~= 0 then
            lua.Stop(strLuaDEID, "创建 Inbound_Detail 失败:" .. msg_add)
            return
        end
    end
 
    mobox.setInfo(strLuaDEID, "出库成功!")
    -- 刷新当前页面
    local strAction = '[{"action_type":"refresh","value":""}]'
    nRet, strRetInfo = mobox.setAction(strLuaDEID, strAction)
    if ( nRet ~= 0 ) then Error( strLuaDEID, debug.getinfo(1), "setAction错误: "..strRetInfo) end  
end