1
Jianw
2025-07-09 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
--[[
    编码: AMS-API-01
    名称: W01-物料主数据
    作者: qianjin
    日期: 2025-07-09
 
    入口函数: GetMaterialData
 
    功能说明:
        WMS系统提供物料主数据接口,供ERP系统调用,推送物料基础数据,WMS同时支持物料信息导入功能。
        输入 datajson
        {
        "Name": "GetMaterialData",
        "Source": "ERP",
        "Data": [
                {
                "skuId": "1",
                "storerId": "1",
                "skuDec": "1",
                "spec": "1",
                "unit": "1",
                "isBatchMgr": 0,
                "isSnMgr": 0,
                "category": "1",
                "isQM":"Y"
                }
            ]
        }
        创建【物料/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 = {
            err_code = 1,
            err_msg = "无法获取数据包!" .. item_info,
            result = nil
        }
        mobox.returnValue(strLuaDEID, 1, lua.table2str(result))
        return
    end
    lua.Debug(strLuaDEID, debug.getinfo(1), 'item_info:', item_info)
 
    if (item_info == '' or item_info == nil) then
        local result = {
            err_code = 1,
            err_msg = "数据对象内容有误!",
            result = nil
        }
        mobox.returnValue(strLuaDEID, 1, lua.table2str(result))
        return
    end
 
    lua.Debug(strLuaDEID, debug.getinfo(1), ' #item_info:',  #item_info.Data)
 
    if (item_info.Source ~= 'ERP') then
         local result = {
            err_code = 1,
            err_msg = "物料来源有误,不是ERP!",
            result = nil
        }
        mobox.returnValue(strLuaDEID, 1, lua.table2str(result))
        return
    end
    
    for i = 1, #item_info.Data do
        -- 校验参数不能为空
        local skuId = item_info.Data[i].skuId
        if (skuId == nil or skuId == '') then
            local result = {
                err_code = 1,
                err_msg = "物料号不能为空!",
                result = nil
            }
            mobox.returnValue(strLuaDEID, 1, lua.table2str(result))
            return
        end
 
        local storerId = item_info.Data[i].storerId
        if (storerId == nil or storerId == '') then
            local result = {
                err_code = 1,
                err_msg = "货主不能为空!",
                result = nil
            }
            mobox.returnValue(strLuaDEID, 1, lua.table2str(result))
            return
        end
 
        local skuDec = item_info.Data[i].skuDec
        local spec = item_info.Data[i].spec
        local unit = item_info.Data[i].unit
        local isBatchMgr = tonumber(item_info.Data[i].isBatchMgr)
        local isSnMgr = tonumber(item_info.Data[i].isSnMgr)
        local category = item_info.Data[i].category
        local isQM = item_info.Data[i].isQM
        
         lua.Debug(strLuaDEID, debug.getinfo(1), ' skuId:',  skuId)
 
        -- 根据物料编码判断是否存在,存在则更新
        local strCondition = "S_ITEM_CODE = '" .. skuId .. "'"
        nRet, id, strRetInfo = mobox.getDataObjAttrByKeyAttr(strLuaDEID, "SKU", strCondition)
        if (nRet > 1) then
            local result = {
                err_code = 1,
                err_msg = "判断物料号 S_ITEM_CODE ='" .. skuId .. "' 是否存在时失败!" .. 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 = skuId
            materail_info.storer = storerId
            materail_info.item_name = skuDec
            materail_info.spec = spec
            materail_info.unit = unit
            materail_info.is_batch_mgr = isBatchMgr
            materail_info.is_sn_mgt = isSnMgr
            materail_info.category = category
            materail_info.is_qm = isQM
            materail_info.count_method = "1"
     
            nRet, strRetInfo = m3.CreateDataObj(strLuaDEID, materail_info)
            if (nRet ~= 0) then
                local result = {
                    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 = skuId },
                        { attr = "S_ITEM_NAME",     value = skuDec },
                        { attr = "S_STORER",         value = storerId },
                        { attr = "S_SPEC",          value = spec },
                        { attr = "S_UNIT",         value = unit },
                        { attr = "N_ISBATCHMGR",       value = isBatchMgr },
                        { attr = "C_ISSNMGT", value = isSnMgr },
                        { attr = "S_CATEGORY", value = category },
                        { attr = "C_ISQM",     value = isQM },
                    }
                }
            }
            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