--[[
|
编码: GK-API-05
|
名称: 小件任务创建接口
|
作者: Peter fang
|
日期: 2025-05-13
|
|
入口函数: Inbound_Sync
|
来源项目: 国科项目
|
|
功能说明:
|
1. 接收来自上游系统的XML格式数据,并解析该数据,创建Inbound Order及Inbound Detail
|
2. 创建Inbound Palletization及Inbound Pallet Detail
|
]]
|
|
wms_base = require( "wms_base" )
|
xml=require( "oi_base_xml" )
|
wms_pallet = require( "wms_palletizing" )
|
|
-- 创建统一返回结果
|
local function create_result(flag, code, msg)
|
return {
|
flag = flag or "success",
|
code = code or "0",
|
message = msg or ""
|
}
|
end
|
|
local function return_value(strLuaDEID, success,err_msg)
|
if success==false then
|
lua.Stop( strLuaDEID, err_msg )
|
end
|
local flag = "success"
|
local code="0"
|
if success==true then
|
flag = "success"
|
code="0"
|
else
|
flag = "failure"
|
code="1"
|
end
|
local result= create_result( flag, code, err_msg )
|
local xml_result = xml.json_to_xml(result, "response")
|
local value=0
|
if flag=="success" then
|
value=0
|
else
|
value=3
|
end
|
mobox.returnValue(strLuaDEID,1,xml_result,value)
|
end
|
|
local function count_cell_list(t)
|
local count = 0
|
for cell,group in pairs(t) do
|
if(cell~=nil and cell~="") then
|
count = count + 1
|
end
|
end
|
return count
|
end
|
|
local function group_by(t, key)
|
local result = {}
|
local getKey = type(key) == "function" and key or function(item)
|
return item[key]
|
end
|
for _, item in ipairs(t) do
|
local groupKey = getKey(item)
|
if groupKey ~= nil then
|
if not result[groupKey] then
|
result[groupKey] = {}
|
end
|
table.insert(result[groupKey], item)
|
end
|
end
|
return result
|
end
|
|
local function goods_count(items)
|
local group_table = group_by(items, "skuId")
|
local count = 0
|
for _ in pairs(group_table) do
|
count = count + 1
|
end
|
return count
|
end
|
|
local function sku_count(items)
|
local qty=0
|
for _,v in ipairs(items) do
|
if v.qty~=nil then
|
qty=qty+lua.Get_NumAttrValue(v.qty)
|
end
|
end
|
return qty
|
end
|
|
local function get_celltype(group,cid)
|
local spec="A"
|
local cellItems={}
|
for _,cell in ipairs(group) do
|
table.insert(cellItems,cell)
|
end
|
local by_cell=group_by(cellItems,"boxCode")
|
local cellCount=count_cell_list(by_cell)
|
if (cellCount==1) then
|
spec="A"
|
elseif (cellCount==2) then
|
spec="B"
|
elseif (cellCount==3) then
|
spec="C"
|
elseif (cellCount==4) then
|
spec="D"
|
elseif (cellCount==6) then
|
spec="E"
|
elseif (cellCount==8) then
|
spec="F"
|
else
|
return 1, "料格数量"..cellCount.. "料箱规格超出系统定义!cntr_code= "..cid
|
end
|
return 0, spec
|
end
|
|
local function create_inbound_pallet(strLuaDEID,inboundData,items)
|
local nRet,strRetInfo
|
local by_pallet=group_by(items,"cid")
|
for cid, group in pairs(by_pallet) do
|
if cid~=nil and cid~="" then
|
if(group[1].boxCode~=nil and group[1].boxCode~="") then
|
local spec
|
nRet, spec = get_celltype(group,cid)
|
if nRet ~= 0 then
|
return 1, spec
|
end
|
local detail_item_data = {}
|
for _,cell in ipairs(group) do
|
local strTableName = "TN_SKU"
|
local strFieldList = '["S_UDF01"]'
|
local strCondition =string.format("S_STORER='%s' AND S_ITEM_CODE='%s'",inboundData.storerId,cell.skuId)
|
nRet, strRetInfo=mobox.queryTable(strLuaDEID,strTableName, strFieldList,1, strCondition, "")
|
if (nRet ~= 0) then
|
return 1,"查询SKU信息失败!"..strRetInfo
|
end
|
local sku_info = json.decode(strRetInfo)
|
if not sku_info or #sku_info == 0 then
|
return 1,"SKU信息不存在"..cell.skuId
|
end
|
local sudf01 = sku_info[1].S_UDF01
|
|
local item_detail
|
item_detail = m3.AllocObject2( strLuaDEID, "INB_Pallet_Detail")
|
item_detail.S_CNTR_CODE = cid
|
local seg = lua.split(cell.boxCode, '-' )
|
if ( #seg ~= 2 ) then
|
return 1,"料格号格式错误"..cell.boxCode
|
end
|
item_detail.S_CELL_NO =spec.."-".. lua.StrToNumber(seg[2])
|
item_detail.S_CELL_CODE=cell.boxCode
|
item_detail.S_ITEM_CODE = cell.skuId
|
item_detail.S_ITEM_NAME = cell.skuId
|
item_detail.S_ITEM_STATE=cell.skuStatus
|
item_detail.S_STORER=inboundData.storerId
|
item_detail.S_OWNER=inboundData.ownerId
|
item_detail.F_QTY = cell.qty
|
item_detail.S_UDF01=sudf01
|
item_detail.S_UDF02=cell.registerNo
|
item_detail.S_WMS_BN = cell.batchNo
|
item_detail.S_BATCH_NO = cell.produceCode
|
item_detail.D_PRD_DATE = cell.productDate
|
item_detail.D_EXP_DATE = cell.expiryDate
|
item_detail.S_BS_TYPE="GK_IN_TASK"
|
item_detail.S_BS_NO=inboundData.taskId
|
item_detail.N_BS_ROW_NO=cell.row_no
|
|
table.insert(detail_item_data, item_detail)
|
end
|
nRet, strRetInfo = wms_pallet.Add_INB_Pallet_Detail( strLuaDEID, cid, detail_item_data,"GK_IN_TASK",inboundData.taskId)
|
if nRet ~= 0 then
|
return 1,"生成码盘信息失败".. strRetInfo
|
end
|
end
|
end
|
end
|
return 0, ""
|
end
|
local function create_container(strLuaDEID,inboundData,items)
|
local nRet,strRetInfo
|
local by_pallet=group_by(items,"cid")
|
for cid, group in pairs(by_pallet) do
|
if cid~=nil and cid~="" then
|
if(group[1].boxCode~=nil and group[1].boxCode~="") then
|
local spec
|
nRet, spec = get_celltype(group,cid)
|
if nRet ~= 0 then
|
lua.DebugEx(strLuaDEID, "create_container", group[1].boxCode.. "cid="..cid)
|
return 1, spec
|
end
|
nRet, strRetInfo = wms.wms_GetConst("WMS_Default_CNTR_Type")
|
if (nRet ~= 0) then
|
return 1, "获取默认容器类型失败!"..strRetInfo
|
end
|
local container_type_info = strRetInfo
|
local strObjID
|
local strCondition =string.format("S_CODE='%s'",cid)
|
nRet,strObjID,strRetInfo=mobox.getDataObjAttrByKeyAttr(strLuaDEID,"Container",strCondition)
|
if nRet ~= 0 then
|
return 1,cid.."容器不存在!"..strRetInfo
|
end
|
local update_container_obj={
|
{
|
id = strObjID,
|
attrs = {
|
{ attr = "S_CTD_CODE", value =container_type_info }, -- 料箱类型
|
{ attr = "S_SPEC", value = spec }, -- 料箱规格
|
}
|
}}
|
nRet, strRetInfo = mobox.updateDataObj( strLuaDEID, "Container", lua.table2str(update_container_obj) )
|
if (nRet ~= 0 ) then
|
return 1,"更新【Container】属性失败!"..strRetInfo
|
end
|
end
|
end
|
end
|
return 0, ""
|
end
|
|
local function create_inbound_detail(strLuaDEID,detail)
|
local nRet, strRetInfo
|
|
local inbound_detail = m3.AllocObject( strLuaDEID,"Inbound_Detail")
|
inbound_detail.row_no=detail.row_no
|
inbound_detail.io_no=detail.io_no
|
inbound_detail.storer=detail.storer
|
inbound_detail.owner=detail.owner
|
inbound_detail.bs_row_no=detail.bs_row_no
|
inbound_detail.item_code=detail.item_code
|
inbound_detail.item_name=detail.item_name
|
inbound_detail.item_state=detail.item_state
|
inbound_detail.qty=detail.qty
|
inbound_detail.wms_bn=detail.wms_bn
|
inbound_detail.batch_no=detail.batch_no
|
inbound_detail.prd_date=detail.prd_date
|
inbound_detail.exp_date=detail.exp_date
|
inbound_detail.ext_attr1=detail.registerNo
|
inbound_detail.note=detail.note
|
|
local strTableName = "TN_SKU"
|
local strFieldList = '["S_UDF01"]'
|
local strCondition =string.format("S_STORER='%s' AND S_ITEM_CODE='%s'",detail.storer,detail.item_code)
|
nRet, strRetInfo=mobox.queryTable(strLuaDEID,strTableName, strFieldList,1, strCondition, "")
|
if (nRet ~= 0) then
|
return 1,"查询SKU信息失败!"..strRetInfo
|
end
|
local sku_info = json.decode(strRetInfo)
|
if not sku_info or #sku_info == 0 then
|
return 1,"SKU信息不存在"..detail.item_code
|
end
|
inbound_detail.udf01 = sku_info[1].S_UDF01
|
inbound_detail.udf19=detail.sn
|
inbound_detail.udf20=detail.cntr_code
|
|
nRet, strRetInfo = m3.CreateDataObj(strLuaDEID, inbound_detail)
|
if (nRet==nil or nRet ~= 0 ) then
|
return 1, "创建系统入库单明细失败!"..strRetInfo.."no= "..detail.io_no
|
end
|
return 0, ""
|
end
|
|
local function gk_create_inbound_detail(strLuaDEID,inboundData, inboundItem,index)
|
local nRet, strRetInfo
|
|
local inbound_detail = m3.AllocObject( strLuaDEID,"GK_IN_TASK_Detail")
|
inbound_detail.row_no=index
|
inbound_detail.io_no=inboundData.taskId
|
inbound_detail.storer=inboundData.storerId
|
inbound_detail.owner=inboundData.ownerId
|
inbound_detail.bs_row_no=inboundItem.orderItemId
|
inbound_detail.item_code=inboundItem.skuId
|
inbound_detail.item_name=inboundItem.skuId
|
inbound_detail.item_state=inboundItem.skuStatus
|
inbound_detail.qty=lua.Get_NumAttrValue(inboundItem.qty)
|
inbound_detail.wms_bn=inboundItem.batchNo
|
inbound_detail.batch_no=inboundItem.produceCode
|
inbound_detail.prd_date=inboundItem.productDate
|
inbound_detail.exp_date=inboundItem.expiryDate
|
inbound_detail.register_no=inboundItem.registerNo
|
inbound_detail.cntr_code=inboundItem.cid
|
inbound_detail.cell_no=inboundItem.boxCode
|
inbound_detail.sn=inboundItem.sn
|
|
nRet, strRetInfo = m3.CreateDataObj(strLuaDEID, inbound_detail)
|
if (nRet==nil or nRet ~= 0 ) then
|
return 1, "创建入库单明细失败!"..strRetInfo.."no= "..inboundData.taskId
|
end
|
|
if inboundItem.boxCode==nil or inboundItem.boxCode=="" then
|
nRet, strRetInfo=create_inbound_detail(strLuaDEID,inbound_detail)
|
if (nRet==nil or nRet ~= 0 ) then
|
return 1, "创建系统入库单明细失败!"..strRetInfo.."no= "..inboundData.taskId
|
end
|
end
|
|
return 0, ""
|
end
|
|
local function create_inbound(strLuaDEID, gk_inbound)
|
local nRet, strRetInfo
|
|
local inbound = m3.AllocObject( strLuaDEID,"Inbound_Order")
|
inbound.no=gk_inbound.no
|
inbound.b_state=0
|
inbound.factory=gk_inbound.factory
|
inbound.wh_code=gk_inbound.wh_code
|
inbound.bs_type=gk_inbound.bs_type
|
inbound.bs_no=gk_inbound.bs_no
|
inbound.asn_no=gk_inbound.asn_no
|
inbound.op_date=gk_inbound.op_date
|
inbound.priority=gk_inbound.priority
|
inbound.note=gk_inbound.note
|
|
nRet, strRetInfo = m3.CreateDataObj(strLuaDEID, inbound)
|
if (nRet==nil or nRet ~= 0 ) then
|
return 1, "创建系统入库单失败!"..strRetInfo.."no= "..gk_inbound.no
|
end
|
|
return 0,""
|
end
|
|
local function gk_create_inbound(strLuaDEID, inboundData,emptyCellCount)
|
local nRet, strRetInfo
|
|
local inbound = m3.AllocObject( strLuaDEID,"GK_IN_TASK")
|
inbound.no=inboundData.taskId
|
inbound.b_state=0
|
nRet, strRetInfo = wms.wms_GetConst("WMS_Default_Factory")
|
if (nRet ~= 0) then
|
return 1, "获取默认工厂失败!"..strRetInfo
|
end
|
inbound.factory=strRetInfo
|
nRet, strRetInfo = wms.wms_GetConst("WMS_Default_Warehouse")
|
if (nRet ~= 0) then
|
return 1, "获取默认仓库失败!"..strRetInfo
|
end
|
inbound.wh_code=strRetInfo
|
inbound.bs_type=inboundData.taskType
|
inbound.bs_no=inboundData.orderNo
|
inbound.asn_no=inboundData.asnNo
|
inbound.op_date=inboundData.orderDate
|
inbound.priority=inboundData.priority
|
inbound.source_no=inboundData.sourceBillNo
|
inbound.note=inboundData.memo
|
|
local detailItems = inboundData.TaskCreation_TB_ITEM
|
local items={}
|
if detailItems==nil then
|
return 1, "入库单明细为空"
|
end
|
if detailItems[1]==nil then
|
items={detailItems}
|
else
|
items=detailItems
|
end
|
if items==nil or items[1]==nil then
|
return 1, "入库单明细为空"
|
else
|
local index=1
|
for index, item in ipairs(items) do
|
--创建入库单明细
|
item.row_no=index
|
nRet, strRetInfo = gk_create_inbound_detail(strLuaDEID,inboundData,item,index)
|
if (nRet==nil or nRet ~= 0 ) then
|
return 1, "创建入库单明细失败!"..strRetInfo.."no= "..inboundData.taskId
|
end
|
end
|
end
|
|
inbound.good_type_num=goods_count(items)
|
inbound.total_qty=sku_count(items)
|
nRet, inbound = m3.CreateDataObj(strLuaDEID, inbound)
|
if (nRet==nil or nRet ~= 0 ) then
|
return 1, "创建入库单失败!"..inbound.."no= "..inboundData.taskId
|
end
|
|
--创建系统入库单
|
if emptyCellCount>0 then
|
nRet, strRetInfo =create_inbound(strLuaDEID,inbound)
|
if (nRet==nil or nRet ~= 0 ) then
|
return 1, "创建系统入库单失败!"..strRetInfo.."no= "..inboundData.taskId
|
end
|
end
|
|
--创建入库单码盘
|
nRet, strRetInfo =create_inbound_pallet(strLuaDEID,inboundData,items)
|
if (nRet==nil or nRet ~= 0 ) then
|
return 1, "创建码盘明细失败!"..strRetInfo.."no= "..inboundData.taskId
|
end
|
|
--创建容器料格信息
|
nRet, strRetInfo = create_container(strLuaDEID,inboundData,items)
|
if (nRet==nil or nRet ~= 0 ) then
|
return 1, "创建容器失败!"..strRetInfo.."no= "..inboundData.taskId
|
end
|
|
return 0,""
|
end
|
|
function Inbound_Sync(strLuaDEID)
|
local nRet, strRetInfo
|
local err_msg
|
|
--m3.PrintLuaDEInfo(strLuaDEID)
|
|
local soap_xml
|
nRet,soap_xml=mobox.getCurEditDataPacket(strLuaDEID)
|
if ( nRet ~=0 ) then
|
return_value(strLuaDEID, false, "无法获取数据包 !"..soap_xml)
|
return
|
end
|
local parsed_data
|
nRet, parsed_data = xml.parse(soap_xml)
|
if ( nRet ~= 0 ) then
|
return_value(strLuaDEID, false, "接口输入的XML格式非法!")
|
return
|
end
|
|
local inboundItems=parsed_data.Envelope.Body.InTaskCreationReq.TaskCreation_Input.InputParameters.TaskCreation_TB
|
local inboundSet={}
|
if inboundItems==nil then
|
return_value(strLuaDEID, false, "接口输入的XML格式非法!")
|
return
|
end
|
if inboundItems[1]==nil then
|
inboundSet={inboundItems}
|
else
|
inboundSet=inboundItems
|
end
|
|
for i, inboundData in ipairs(inboundSet) do
|
if inboundData.TaskCreation_TB_ITEM==nil then
|
err_msg = "入库明细为空"
|
return_value(strLuaDEID, false, err_msg)
|
return
|
end
|
|
--判断InBound是否存在,取消状态后面考虑
|
local id
|
local strCondition =string.format("S_NO='%s' AND N_B_STATE<>91",inboundData.taskId)
|
nRet,id,strRetInfo=mobox.getDataObjAttrByKeyAttr(strLuaDEID,"GK_IN_TASK",strCondition)
|
if nRet==0 then
|
err_msg = "入库单号已存在!"..inboundData.taskId
|
return_value(strLuaDEID, true, err_msg)
|
return
|
end
|
|
--判断料箱是否存在,不存在拒绝
|
local detailItems = inboundData.TaskCreation_TB_ITEM
|
local items={}
|
if detailItems==nil then
|
return 1, "入库单明细为空"
|
end
|
if detailItems[1]==nil then
|
items={detailItems}
|
else
|
items=detailItems
|
end
|
local by_pallet=group_by(items,"cid")
|
local emptyCellCount=0
|
for cid, group in pairs(by_pallet) do
|
for _,cell in ipairs(group) do
|
if cell.boxCode==nil or cell.boxCode=="" then
|
emptyCellCount=emptyCellCount+1
|
end
|
end
|
if cid~=nil and cid~="" then
|
--判断料箱是否存在
|
local strCondition =string.format("S_CODE='%s'",cid)
|
nRet,strRetInfo=mobox.existThisData(strLuaDEID,"Container",strCondition)
|
if nRet~=0 then
|
err_msg = "查询数据出错!"..cid
|
return_value(strLuaDEID, false, err_msg)
|
return
|
end
|
if strRetInfo=="no" then
|
err_msg = "料箱不存在!"..cid
|
return_value(strLuaDEID, false, err_msg)
|
return
|
end
|
--判断料箱是否完成入库
|
local strCondition =string.format("S_CNTR_CODE='%s' AND N_B_STATE NOT IN(30,40)",cid)
|
nRet,strRetInfo=mobox.existThisData(strLuaDEID,"Inbound_Palletization",strCondition)
|
if nRet~=0 then
|
err_msg = "查询数据出错!"..cid
|
return_value(strLuaDEID, false, err_msg)
|
return
|
end
|
if strRetInfo=="yes" then
|
err_msg = "该料箱码盘数据未完成!"..cid
|
return_value(strLuaDEID, false, err_msg)
|
return
|
end
|
--判断料箱是否存在库存
|
strCondition=string.format("S_CNTR_CODE='%s' AND F_QTY>0",cid)
|
nRet,strRetInfo=mobox.existThisData(strLuaDEID,"INV_Detail",strCondition)
|
if nRet~=0 then
|
err_msg = "查询数据出错!"..cid
|
return_value(strLuaDEID, false, err_msg..strRetInfo)
|
return
|
end
|
if strRetInfo=="yes" then
|
lua.DebugEx(strLuaDEID, "料箱存在库存")
|
err_msg = "料箱存在库存"..cid
|
return_value(strLuaDEID, false, err_msg..strRetInfo)
|
return
|
end
|
end
|
end
|
|
nRet,err_msg= gk_create_inbound(strLuaDEID, inboundData,emptyCellCount)
|
if (nRet==nil or nRet~=0) then
|
return_value(strLuaDEID, false, err_msg)
|
else
|
return_value(strLuaDEID, true, "")
|
end
|
end
|
end
|