--[[
|
编码: 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
|