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
--[[
    编码: AMS-API-01
    名称: W01-物料主数据
    作者:LZH
    日期: 2025-4-15
 
    入口函数: GetMaterialData
 
    功能说明:
        WMS系统提供物料主数据接口,供ERP系统调用,推送物料基础数据,WMS同时支持物料信息导入功能。
        输入 datajson
        {
        "Name": "GetMaterialData",
        "Source": "ERP",
        "Data": [
        {
                "S_ITEM_CODE": "test001",
                "S_ITEM_NAME": "测试物料1",
                "S_AREA_CODE": "料箱库",
                "S_ITEM_SPEC": "20*22.1L",
                "S_MATERIAL": "塑料",
                "S_EXT_ATTR1": "A",
                "S_EXT_ATTR2": "0",
                "F_WEIGHT": "20",
                "S_EXT_ATTR3": "100",
                "S_EXT_ATTR4": "A",
                "SourceKey": "1212123"
            }
            ]
        }
        创建【物料/Material】
    变更记录:
--]]
 
wms_base = require("wms_base")
function GetMaterialData(strLuaDEID)
    -- step1 获取接口中的Data
    local nRet, item_info, id, strRetInfo
    nRet, item_info = m3.GetSysDataJson(strLuaDEID)
    if (nRet ~= 0) then
        local result = {
            SourceKey = {},
            err_code = 1,
            err_msg = "无法获取数据包!" .. item_info,
            result = nil
        }
        mobox.returnValue(strLuaDEID, 1, lua.table2str(result))
        return
    end
 
    if (#item_info == 0) then
        local result = {
            SourceKey = {},
            err_code = 1,
            err_msg = "数据对象内容有误!",
            result = nil
        }
        mobox.returnValue(strLuaDEID, 1, lua.table2str(result))
        return
    end
 
    local SourceKey_list = {}
    for i = 1, #item_info do
        local SourceKey = item_info[i].SourceKey
        SourceKey_list[i] = SourceKey
 
        -- 校验参数不能为空
        local item_code = item_info[i].S_ITEM_CODE
        if (item_code == nil or item_code == '') then
            local result = {
                SourceKey = {SourceKey},
                err_code = 1,
                err_msg = "物料号不能为空!",
                result = nil
            }
            mobox.returnValue(strLuaDEID, 1, lua.table2str(result))
            return
        end
        local item_name = item_info[i].S_ITEM_NAME
        if (item_name == nil or item_name == '') then
            local result = {
                SourceKey = {SourceKey},
                err_code = 1,
                err_msg = "物料名称不能为空!",
                result = nil
            }
            mobox.returnValue(strLuaDEID, 1, lua.table2str(result))
            return
        end
        local area_code = item_info[i].S_AREA_CODE
        if (area_code == nil or area_code == '') then
            local result = {
                SourceKey = {SourceKey},
                err_code = 1,
                err_msg = "库区不能为空!",
                result = nil
            }
            mobox.returnValue(strLuaDEID, 1, lua.table2str(result))
            return
        end
        
        -- 根据库区编码判断库区是否存在
        local strCondition = "S_CODE = '" .. area_code .. "'"
        nRet, id, strRetInfo = mobox.getDataObjAttrByKeyAttr(strLuaDEID, "Area", strCondition)
        if (nRet ~= 0) then
            local result = {
                SourceKey = {SourceKey},
                err_code = 1,
                err_msg = "判断库区'" .. area_code .. "' 是否存在时失败!" .. strRetInfo,
                result = nil
            }
            mobox.returnValue(strLuaDEID, 1, lua.table2str(result))
            return
        end
        local item_spec = item_info[i].S_ITEM_SPEC
        local material = item_info[i].S_MATERIAL
        local ext_attr1 = item_info[i].S_EXT_ATTR1
        if (ext_attr1 == nil or ext_attr1 == '') then
            local result = {
                SourceKey = {SourceKey},
                err_code = 1,
                err_msg = "ABC类型不能为空!",
                result = nil
            }
            mobox.returnValue(strLuaDEID, 1, lua.table2str(result))
            return
        end
        local weight = tonumber(item_info[i].F_WEIGHT)
        local ext_attr2 = tonumber(item_info[i].S_EXT_ATTR2)
        -- lua.Debug(strLuaDEID, debug.getinfo(1), '修改条数:', ext_attr2)
        local count_method = ''
        if (tonumber(ext_attr2) == nil or tonumber(ext_attr2) == 0) then
            count_method = 'Limit'
            if (weight == nil or weight == 0) then
                local result = {
                    SourceKey = {SourceKey},
                    err_code = 1,
                    err_msg = "数量上限和单件重量必须有一个有值!",
                    result = nil
                }
                mobox.returnValue(strLuaDEID, 1, lua.table2str(result))
                return
            end
        else
            count_method = 'Weight'
            if (weight ~= nil and weight ~= 0) then
                local result = {
                    SourceKey = {SourceKey},
                    err_code = 1,
                    err_msg = "数量上限和单件重量有且只有一个有值!",
                    result = nil
                }
                mobox.returnValue(strLuaDEID, 1, lua.table2str(result))
                return
            end
        end
 
        local ctd_code = ''
        local ext_attr3 = item_info[i].S_EXT_ATTR3
        if (ext_attr3 == nil or ext_attr3 == '') then
            local result = {
                SourceKey = {SourceKey},
                err_code = 1,
                err_msg = "料箱重量不能为空!",
                result = nil
            }
            mobox.returnValue(strLuaDEID, 1, lua.table2str(result))
            return
        end
        if (tonumber(ext_attr3) == 50) then
            ctd_code = "CTD-001"
        elseif (tonumber(ext_attr3) == 100) then
            ctd_code = "CTD-002"
        end
        local ext_attr4 = item_info[i].S_EXT_ATTR4
        if (ext_attr4 == nil or ext_attr4 == '') then
            local result = {
                SourceKey = {SourceKey},
                err_code = 1,
                err_msg = "料箱规格不能为空!",
                result = nil
            }
            mobox.returnValue(strLuaDEID, 1, lua.table2str(result))
            return
        end
 
        -- 根据物料编码判断是否存在,存在则更新
        local strCondition = "S_ITEM_CODE = '" .. item_code .. "'"
        nRet, id, strRetInfo = mobox.getDataObjAttrByKeyAttr(strLuaDEID, "SKU", strCondition)
        if (nRet > 1) then
            local result = {
                SourceKey = {SourceKey},
                err_code = 1,
                err_msg = "判断物料号 S_ITEM_CODE ='" .. item_code .. "' 是否存在时失败!" .. strRetInfo,
                result = nil
            }
            mobox.returnValue(strLuaDEID, 1, lua.table2str(result))
            return
        elseif (nRet == 1) then
            -- 创建物料信息
            local materail_info = m3.AllocObject(strLuaDEID, "SKU")
            materail_info.item_code = item_code
            materail_info.item_name = item_name
            materail_info.udf01 = area_code
            materail_info.spec = item_spec
            materail_info.udf02 = material
            materail_info.abc_type = ext_attr1
            materail_info.loading_limit = ext_attr2
            materail_info.load_capacity = ext_attr3
            materail_info.cell_type = ext_attr4
            materail_info.weight = weight
            materail_info.count_method = count_method
            materail_info.ctd_code = ctd_code
            nRet, strRetInfo = m3.CreateDataObj(strLuaDEID, materail_info)
            if (nRet ~= 0) then
                local result = {
                    SourceKey = {SourceKey},
                    err_code = 1,
                    err_msg = "调用方法CreateDataObj出错: " .. strRetInfo,
                    result = nil
                }
                mobox.returnValue(strLuaDEID, 1, lua.table2str(result))
                return
            end
        else
            -- 已经存在更新数据对象
            local update_data_obj = {
                {
                    id = id,
                    attrs = {
                        { attr = "S_ITEM_CODE",     value = item_code },
                        { attr = "S_ITEM_NAME",     value = item_name },
                        { attr = "S_UDF01",         value = area_code },
                        { attr = "S_SPEC",          value = item_spec },
                        { attr = "S_UDF02",         value = material },
                        { attr = "S_ABCTYPE",       value = ext_attr1 },
                        { attr = "N_LOADING_LIMIT", value = ext_attr2 },
                        { attr = "F_LOAD_CAPACITY", value = ext_attr3 },
                        { attr = "S_CELL_TYPE",     value = ext_attr4 },
                        { attr = "S_COUNT_METHOD",  value = count_method },
                        { attr = "S_CTD_CODE",      value = ctd_code },
                        { attr = "F_WEIGHT",        value = weight }
                    }
                }
            }
            nRet, strRetInfo = mobox.updateDataObj(strLuaDEID, "SKU", lua.table2str(update_data_obj))
            if (nRet ~= 0) then
                local result = {
                    SourceKey = {SourceKey},
                    err_code = 1,
                    err_msg = "更新【物料】属性失败!" .. strRetInfo,
                    result = nil
                }
                mobox.returnValue(strLuaDEID, 1, lua.table2str(result))
                return
            end
        end
    end
 
    local data = {
        SourceKey = SourceKey_list,
        err_code = 0,
        err_msg = "同步【物料】成功!",
        result = nil
    }
    mobox.returnValue(strLuaDEID, 1, lua.table2str(data))
end