--[[
|
编码: AMS-90-10
|
名称: 物料-导入
|
作者:LZH
|
日期:2025-4-17
|
入口函数: Import
|
|
功能说明:
|
|
变更历史:
|
|
--]]
|
wms_base = require("wms_base")
|
|
local function value_sort(a, b) return a < b end
|
|
function Import(strLuaDEID)
|
local nRet, strRetInfo, strNo
|
-- 获取导入的数据, 返回 [{"attr":"xx","value":""},...]
|
local row_data = {}
|
nRet, row_data = m3.GetSysDataJson(strLuaDEID)
|
if (nRet ~= 0 or strRetInfo == '') then lua.Error(strLuaDEID, debug.getinfo(1), "无法获取导入数据!") end
|
-- lua.Debug(strLuaDEID, debug.getinfo(1), '修改条数:', row_data)
|
|
local row_attrs
|
local n, nCount, nRows, nIndex
|
|
-- 导入是可分多页分批导入, 因此每次的起始行不一样
|
nRet, strRetInfo = mobox.getGlobalAttrValue(strLuaDEID, "start_row")
|
if (nRet ~= 0) then
|
mobox.error(strLuaDEID, "获取批量导入全局参数 start_row 失败!" .. strRetInfo)
|
return
|
end
|
local nStartRow = tonumber(strRetInfo)
|
local err_msg_list = {} -- 用于写导入错误信息
|
local str_err_msg
|
|
-- 步骤1 获取从excel导入的一行数据
|
nRow = nStartRow
|
for row = 1, #row_data do
|
row_attrs = row_data[row]
|
nCount = #row_attrs
|
-- 初始化 货位 对象
|
local materail = m3.AllocObject(strLuaDEID, "SKU")
|
nIndex = 1
|
local value_array = {}
|
strNo = ''
|
str_err_msg = ''
|
for n = 1, nCount do
|
strAttr = row_attrs[n].attr
|
strValue = row_attrs[n].value
|
if (strAttr ~= '') then
|
if (strAttr == "物料号") then
|
if (strValue == '') then
|
str_err_msg = strAttr .. "不能为空!"
|
goto err_msg_process
|
end
|
materail.item_code = strValue
|
--V1.2
|
elseif (strAttr == "物料描述") then
|
if (strValue == '') then
|
str_err_msg = strAttr .. "不能为空!"
|
goto err_msg_process
|
end
|
materail.item_name = strValue
|
elseif (strAttr == "存储库区") then
|
if (strValue == '') then
|
str_err_msg = strAttr .. "不能为空!"
|
goto err_msg_process
|
end
|
materail.udf01 = strValue
|
elseif (strAttr == "材质") then
|
materail.udf02 = strValue
|
elseif (strAttr == "ABC类型") then
|
if (strValue == '') then
|
str_err_msg = strAttr .. "不能为空!"
|
goto err_msg_process
|
end
|
materail.abc_type = strValue
|
elseif (strAttr == "数量上限") then
|
materail.loading_limit = strValue
|
elseif (strAttr == "单件重量") then
|
materail.weight = tonumber(strValue)
|
if (tonumber(strValue) == 50) then
|
materail.ctd_code = "CTD-001"
|
else
|
materail.ctd_code = "CTD-002"
|
end
|
elseif (strAttr == "料箱重量") then
|
if (strValue == '') then
|
str_err_msg = strAttr .. "不能为空!"
|
goto err_msg_process
|
end
|
materail.load_capacity = strValue
|
elseif (strAttr == "料箱规格") then
|
materail.cell_type = strValue
|
end
|
end
|
end
|
|
::err_msg_process::
|
if (str_err_msg ~= '') then
|
local err_msg_row = {
|
row_no = nRow,
|
err_msg = str_err_msg
|
}
|
table.insert(err_msg_list, err_msg_row)
|
else
|
if (tonumber(materail.loading_limit) == nil) then
|
materail.count_method = 'Limit'
|
if (tonumber(materail.weight) == nil) then
|
local err_msg_row = {
|
row_no = nRow,
|
err_msg = "数量上限和单件重量必须有一个有值!"
|
}
|
table.insert(err_msg_list, err_msg_row)
|
goto back
|
end
|
else
|
materail.count_method = 'Weight'
|
if (tonumber(materail.weight) ~= nil) then
|
local err_msg_row = {
|
row_no = nRow,
|
err_msg = "数量上限和单件重量有且只有一个有值!"
|
}
|
table.insert(err_msg_list, err_msg_row)
|
goto back
|
end
|
end
|
local strCondition = "S_CODE = '" .. materail.udf01 .. "'"
|
nRet, id, strRetInfo = mobox.getDataObjAttrByKeyAttr(strLuaDEID, "Area", strCondition)
|
if (nRet ~= 0) then
|
local err_msg_row = {
|
row_no = nRow,
|
err_msg = "判断库区'" .. area_code .. "' 是否存在时失败!" .. strRetInfo
|
}
|
table.insert(err_msg_list, err_msg_row)
|
goto back
|
end
|
-- lua.Debug(strLuaDEID, debug.getinfo(1), 'materail:', materail)
|
-- 判断物料是否已经存在,如果已经存在刷新新的属性
|
local strCondition = "S_ITEM_CODE = '" .. materail.item_code .. "'"
|
nRet, id, strRetInfo = mobox.getDataObjAttrByKeyAttr(strLuaDEID, "SKU", strCondition)
|
if (nRet > 1) then
|
local err_msg_row = {
|
row_no = nRow,
|
err_msg = "导入物料信息失败: 在检查 inco = '" .. materail.item_code .. "'是否存在时失败!" .. strRetInfo
|
}
|
table.insert(err_msg_list, err_msg_row)
|
end
|
if (nRet == 1) then
|
-- 不存在
|
nRet, materail = m3.CreateDataObj(strLuaDEID, materail)
|
if (nRet ~= 0) then
|
local err_msg_row = {
|
row_no = nRow,
|
err_msg = "序号='" .. strNo .. "'导入行在创建【物料】时失败!" .. materail
|
}
|
table.insert(err_msg_list, err_msg_row)
|
end
|
else
|
-- 已经存在更新数据对象
|
local update_data_obj = {
|
{
|
id = id,
|
attrs = {
|
{ attr = "S_ITEM_CODE", value = materail.item_code },
|
{ attr = "S_ITEM_NAME", value = materail.item_name },
|
{ attr = "S_UDF01", value = materail.udf01 },
|
{ attr = "S_UDF02", value = materail.udf02 },
|
{ attr = "S_ABCTYPE", value = materail.abc_type },
|
{ attr = "N_LOADING_LIMIT", value = materail.loading_limit },
|
{ attr = "F_LOAD_CAPACITY", value = materail.load_capacity },
|
{ attr = "S_CELL_TYPE", value = materail.cell_type },
|
{ attr = "S_CTD_CODE", value = materail.ctd_code },
|
{ attr = "S_COUNT_METHOD", value = materail.count_method },
|
{ attr = "F_WEIGHT", value = materail.weight }
|
}
|
}
|
}
|
nRet, strRetInfo = mobox.updateDataObj(strLuaDEID, "SKU", lua.table2str(update_data_obj))
|
if (nRet ~= 0) then
|
local err_msg_row = {
|
row_no = nRow,
|
err_msg = "序号='" .. strNo .. "'导入行在更新【物料】属性失败!" .. strRetInfo
|
}
|
table.insert(err_msg_list, err_msg_row)
|
end
|
end
|
end
|
::back::
|
nRow = nRow + 1
|
end
|
lua.Debug(strLuaDEID, debug.getinfo(1), 'err_msg_list:', #err_msg_list)
|
-- 把导入过程中的错误信息返回到前端
|
local import_fail = {
|
result = {
|
fail = err_msg_list
|
}
|
}
|
|
if (#err_msg_list > 0) then
|
lua.Error(strLuaDEID, debug.getinfo(1), lua.table2str(import_fail))
|
end
|
end
|