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