--[[
|
编码: AMS-105-1
|
名称:
|
作者:
|
日期:2025-05-23
|
|
函数: Import
|
功能:
|
|
更改记录:
|
|
--]]
|
|
json = require("json")
|
mobox = require("OILua_JavelinExt")
|
m3 = require("oi_base_mobox")
|
wms_base = require("wms_base")
|
|
function Import(strLuaDEID)
|
|
local nRet, row_data = m3.GetSysDataJson(strLuaDEID)
|
if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "无法获取入库单导入数据!") end
|
lua.Debug(strLuaDEID, debug.getinfo(1), 'row_data', row_data)
|
|
local row_attrs, nCount, nRows
|
nRows = #row_data
|
if (nRows == 0) then
|
mobox.setInfo(strLuaDEID, "导入数据为空!")
|
return
|
end
|
|
-- 按库区分组的入库单数据
|
local area_groups = {}
|
local err = {}
|
-- 循环处理每一行数据
|
for row = 1, nRows do
|
row_attrs = row_data[row]
|
nCount = #row_attrs
|
lua.Debug(strLuaDEID, debug.getinfo(1), 'row_attrs', row_attrs)
|
-- 初始化变量
|
local wh_code = ''
|
local item_code = ''
|
local qty = ''
|
local bs_no = ''
|
local s_bs_no = ''
|
local bs_row_no = ''
|
local batch_no = ''
|
local bs_type = ''
|
local note = ''
|
|
-- 子表扩展字段
|
local stock_status = ''
|
local production_date = ''
|
local supplier_code = ''
|
local supplier_name = ''
|
local net_weight = ''
|
local origin = ''
|
local remark1 = ''
|
local remark2 = ''
|
local remark3 = ''
|
local remark4 = ''
|
local remark5 = ''
|
|
-- 提取基础信息和扩展字段
|
for n = 1, nCount do
|
local strAttr = row_attrs[n].attr
|
local strValue = row_attrs[n].value
|
lua.Debug(strLuaDEID, debug.getinfo(1), 'strAttr', strAttr)
|
-- 主表字段
|
if strAttr == "货品编码" then
|
item_code = strValue
|
elseif strAttr == "数量" then
|
qty = strValue
|
elseif strAttr == "来源单号" then
|
bs_no = strValue
|
elseif strAttr == "物料来源单号" then
|
s_bs_no = strValue
|
elseif strAttr == "来源单行号" then
|
bs_row_no = strValue
|
elseif strAttr == "业务类型" then
|
bs_type = strValue
|
elseif strAttr == "备注" then
|
note = strValue
|
elseif strAttr == "库存状态" or strAttr == "质量状态" then
|
stock_status = strValue
|
elseif strAttr == "生产日期" then
|
production_date = strValue
|
elseif strAttr == "供应商代码" then
|
supplier_code = strValue
|
elseif strAttr == "供应商名称" then
|
supplier_name = strValue
|
elseif strAttr == "净重" then
|
net_weight = strValue
|
elseif strAttr == "原产地" then
|
origin = strValue
|
elseif strAttr == "备注1" then
|
remark1 = strValue
|
elseif strAttr == "备注2" then
|
remark2 = strValue
|
elseif strAttr == "备注3" then
|
remark3 = strValue
|
elseif strAttr == "备注4" then
|
remark4 = strValue
|
elseif strAttr == "备注5" then
|
remark5 = strValue
|
end
|
end
|
|
-- 基础校验
|
if item_code == '' then
|
table.insert(err, "第"..row.."行:货品编码不能为空")
|
elseif not tonumber(qty) or tonumber(qty) <= 0 then
|
table.insert(err, "第"..row.."行:数量必须大于0")
|
elseif bs_no == '' then
|
table.insert(err, "第"..row.."行:来源单号不能为空")
|
elseif s_bs_no == '' then
|
table.insert(err, "第"..row.."行:物料来源单号不能为空")
|
elseif bs_row_no == '' then
|
table.insert(err, "第"..row.."行:来源单行号不能为空")
|
else
|
-- 查询物料获取库区信息
|
local cond = "S_ITEM_CODE = '" .. item_code .. "'"
|
local ret1, id, materialAttrs = mobox.getDataObjAttrByKeyAttr(strLuaDEID, "SKU", cond)
|
lua.Debug(strLuaDEID, debug.getinfo(1), 'materialAttrs', materialAttrs)
|
if ret1 ~= 0 or not materialAttrs then
|
table.insert(err, "第"..row.."行:获取物料失败 - "..item_code)
|
else
|
local ret2, materialJson = mobox.objAttrsToLuaJson("SKU", materialAttrs)
|
if ret2 ~= 0 then
|
table.insert(err, "第"..row.."行:物料JSON转换失败 - "..item_code)
|
else
|
local ok, material = pcall(json.decode, materialJson)
|
if not ok or not material or not material.udf01 then
|
table.insert(err, "第"..row.."行:物料无有效area_code - "..item_code)
|
else
|
-- 添加到对应库区的分组
|
local area_code = material.udf01
|
if not area_groups[area_code] then
|
area_groups[area_code] = {
|
wh_code = wh_code,
|
bs_type = bs_type,
|
note = note,
|
items = {}
|
}
|
end
|
|
-- 保存物料明细及扩展字段
|
table.insert(area_groups[area_code].items, {
|
wh_code = wh_code,
|
area_code = area_code,
|
item_code = item_code,
|
item_name = material.item_name,
|
qty = qty,
|
bs_no = bs_no,
|
s_bs_no = s_bs_no,
|
bs_row_no = bs_row_no,
|
stock_status = stock_status,
|
production_date = production_date,
|
supplier_code = supplier_code,
|
supplier_name = supplier_name,
|
net_weight = net_weight,
|
origin = origin,
|
remark1 = remark1,
|
remark2 = remark2,
|
remark3 = remark3,
|
remark4 = remark4,
|
remark5 = remark5,
|
material = material -- 保存物料完整信息
|
})
|
end
|
end
|
end
|
end
|
end
|
-- 如果有错误,直接返回
|
if #err > 0 then
|
mobox.setInfo(strLuaDEID, "导入失败:\n"..table.concat(err, "\n"))
|
return
|
end
|
local nRet,wh_code = wms_base.Get_sConst2( strLuaDEID, "默认工厂标识" )
|
if ( nRet ~= 0 ) then
|
lua.Stop(strLuaDEID, "系统常量:默认工厂标识不存在:")
|
return
|
end
|
if ( wh_code == '' ) then
|
lua.Stop(strLuaDEID, "系统常量'默认工厂标识'必须有值!:")
|
return
|
end
|
-- 按库区创建入库单
|
local result_codes = {}
|
for area_code, group in pairs(area_groups) do
|
-- 生成入库单号
|
local header = 'RK' .. os.date("%y%m%d") .. '-'
|
local ret, order_no = mobox.getSerialNumber("ERP入库单", header, 4)
|
if ret ~= 0 then
|
mobox.setInfo(strLuaDEID, "申请入库单编码失败:" .. order_no)
|
return
|
end
|
|
-- 创建入库单主表(包含扩展字段)
|
local inbound_Order = m3.AllocObject(strLuaDEID, "ERP_Inbound_Order")
|
inbound_Order.no = order_no
|
inbound_Order.wh_code = wh_code
|
inbound_Order.area_code = area_code
|
inbound_Order.op_type = group.bs_type
|
inbound_Order.bs_no = group.items[1].bs_no
|
inbound_Order.note = group.note
|
|
-- 创建入库单明细(包含扩展字段)
|
for _, item in ipairs(group.items) do
|
local inbound_Detail = m3.AllocObject(strLuaDEID, "ERP_Inbound_Detail")
|
inbound_Detail.io_no = order_no
|
inbound_Detail.item_code = item.item_code
|
inbound_Detail.item_name = item.item_name
|
inbound_Detail.qty = item.qty
|
inbound_Detail.bs_no = item.s_bs_no
|
inbound_Detail.bs_row_no = item.bs_row_no
|
|
-- 扩展字段
|
inbound_Detail.item_state = item.stock_status or ""
|
inbound_Detail.prd_date = item.production_date or ""
|
inbound_Detail.supplier = item.supplier_code or ""
|
inbound_Detail.supplier_name = item.supplier_name or ""
|
inbound_Detail.weight = item.net_weight or 0
|
--inbound_Detail.owner = item.origin
|
inbound_Detail.ext_attr1 = item.origin
|
inbound_Detail.ext_attr2 = item.remark2
|
inbound_Detail.ext_attr3 = item.remark3
|
inbound_Detail.ext_attr4 = item.remark4
|
inbound_Detail.ext_attr5 = item.remark5
|
|
|
local nRet, inbound_Detail = m3.CreateDataObj(strLuaDEID, inbound_Detail)
|
if (nRet ~= 0) then
|
lua.Stop(strLuaDEID, "创建入库单明细失败:"..inbound_Detail)
|
return
|
end
|
end
|
|
-- 保存入库单主表
|
local nRet, inbound_Order = m3.CreateDataObj(strLuaDEID, inbound_Order)
|
if (nRet ~= 0) then
|
lua.Stop(strLuaDEID, "创建入库单失败:"..inbound_Order)
|
return
|
end
|
|
table.insert(result_codes, order_no)
|
end
|
|
-- 返回成功结果
|
mobox.setInfo(strLuaDEID, "入库单创建成功!\n单号:"..table.concat(result_codes, ","))
|
end
|