--[[
|
编码: GK-API-04
|
名称: GK-WMS-Receipt_Sync
|
作者: 袁峰
|
入口函数:Receipt_Sync
|
功能说明: 接收上游系统的XML数据,创建收货单主表(Receipt_Order)和明细记录(Receipt_Detail)。
|
输入参数: xmlData(XML格式的字符串)
|
返回参数: xml 格式的响应报文:
|
形如:
|
<response>
|
<message>收货单已存在</message>
|
<code>204</code>
|
<flag>failure</flag>
|
<error>收货单[SO2025050701]已存在</error>
|
</response>
|
|
调用示例: Receipt_Sync(xmlData)
|
注意事项: 1. 输入参数xmlData为XML格式的字符串,包含了收货单主表和明细记录的数据。
|
2. 函数会解析XML数据,并将其插入到数据库中。
|
3. 允许xml 传入1条或者多条收货单主表数据,每条收货单主表数据可以包含多条收货单明细数据。
|
|
变更历史: 2025-05-14 优化返回结果处理
|
|
XML 报文示例如下;
|
<soapenv:Envelope
|
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
|
<soap:Header
|
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
|
</soap:Header>
|
<soapenv:Body>
|
<v1:InSmallPieceReq
|
xmlns:v1="http://www.gkht.com/InReceive/INV/Ebs/Schemas/InSmallPiece/V1.0">
|
<v1:SmallPiece_Input>
|
<v1:RESTHeader>
|
<v1:Responsibility/>
|
<v1:RespApplication/>
|
<v1:SecurityGroup/>
|
<v1:NLSLanguage>SIMPLIFIED CHINESE</v1:NLSLanguage>
|
<v1:Org_Id>0</v1:Org_Id>
|
</v1:RESTHeader>
|
<v1:InputParameters>
|
<!--1 or more repetitions:-->
|
<v1:SmallPiece_TB>
|
<v1:orderNo>SO2025050601</v1:orderNo>
|
<v1:asnNo>KPD00001</v1:asnNo>
|
<v1:storerId>CGKHTY</v1:storerId>
|
<v1:ownerId>CGKHTY</v1:ownerId>
|
<v1:orderDate>2025-05-06</v1:orderDate>
|
<v1:priority>0</v1:priority>
|
<v1:memo></v1:memo>
|
<v1:SmallPiece_TB_ITEM>
|
<v1:orderItemId>1</v1:orderItemId>
|
<v1:skuId>XR33201-2L080B</v1:skuId>
|
<v1:skuStatus>AVL</v1:skuStatus>
|
<v1:qty>2</v1:qty>
|
<v1:batchNo>PHI00000000000001309</v1:batchNo>
|
<v1:produceCode>YL201125</v1:produceCode>
|
<v1:productDate>2016-04-23</v1:productDate>
|
<v1:expiryDate>2099-12-31</v1:expiryDate>
|
<v1:registerNo>国食药监械(准)字2012</v1:registerNo>
|
</v1:SmallPiece_TB_ITEM>
|
<v1:SmallPiece_TB_ITEM>
|
<v1:orderItemId>2</v1:orderItemId>
|
<v1:skuId>KH32803017</v1:skuId>
|
<v1:skuStatus>AVL</v1:skuStatus>
|
<v1:qty>5</v1:qty>
|
<v1:batchNo>PHI00000000000001308</v1:batchNo>
|
<v1:produceCode>YL2011256</v1:produceCode>
|
<v1:productDate>2023-04-23</v1:productDate>
|
<v1:expiryDate>2099-12-31</v1:expiryDate>
|
<v1:registerNo></v1:registerNo>
|
</v1:SmallPiece_TB_ITEM>
|
</v1:SmallPiece_TB>
|
</v1:InputParameters>
|
</v1:SmallPiece_Input>
|
</v1:InSmallPieceReq>
|
</soapenv:Body>
|
</soapenv:Envelope>
|
--]] wms_base = require("wms_base")
|
xml = require("oi_base_xml")
|
mobox = require("OILua_JavelinExt")
|
m3 = require("oi_base_mobox")
|
function Create_result(flag, code, msg)
|
return {
|
flag = flag or "success",
|
code = code or "0",
|
message = msg or ""
|
}
|
end
|
function Receipt_Sync(strLuaDEID)
|
-- 初始化最终结果
|
local FinalRes = Create_result()
|
-- 1. 获取 xml 数据包
|
local nRet, soap_xml = mobox.getCurEditDataPacket(strLuaDEID)
|
if nRet ~= 0 then
|
FinalRes = Create_result("failure", "201", "无法获取数据包: " .. soap_xml)
|
local xml_result = xml.json_to_xml(FinalRes, "response")
|
mobox.returnValue(strLuaDEID, 0, xml_result, 0)
|
lua.Stop(strLuaDEID, "获取数据包失败", soap_xml)
|
return
|
end
|
|
lua.DebugEx(strLuaDEID, "获取到的数据包", soap_xml)
|
|
-- 2. 解析 xml
|
local nRet, parsed_data = xml.parse(soap_xml)
|
if nRet ~= 0 then
|
FinalRes = Create_result("failure", "202", "xml 格式非法" .. parsed_data)
|
local xml_result = xml.json_to_xml(FinalRes, "response")
|
mobox.returnValue(strLuaDEID, 0, xml_result, 0)
|
lua.Stop(strLuaDEID, "xml格式非法", parsed_data)
|
return
|
end
|
|
-- 3. 提取主表数据
|
local receipt_data = parsed_data.Envelope.Body.InSmallPieceReq.SmallPiece_Input
|
local input_params = receipt_data.InputParameters
|
|
-- 检查是否存在 SmallPiece_TB
|
if not input_params or not input_params.SmallPiece_TB then
|
FinalRes = Create_result("failure", "203", "xml 数据格式错误,缺少 SmallPiece_TB")
|
local xml_result = xml.json_to_xml(FinalRes, "response")
|
mobox.returnValue(strLuaDEID, 0, xml_result, 0)
|
lua.Stop(strLuaDEID, "xml 数据格式错误", xml_result)
|
return
|
end
|
|
-- 4. 统一处理:确保 receipt_headers 是数组(即使只有一个主表)
|
local receipt_headers = input_params.SmallPiece_TB
|
if receipt_headers[1] == nil then
|
receipt_headers = {receipt_headers}
|
end
|
|
-- 5. 获取系统常量
|
local RetWH_COE, CONST_WH = wms_base.Get_sConst2("GK_Default_Warehouse")
|
local RetFAC_COE, CONST_FACTORY = wms_base.Get_sConst2("GK_Default_Factory")
|
|
-- 仓库常量异常捕获
|
if (RetWH_COE ~= 0) then
|
FinalRes = Create_result("failure", "204", "获取仓库常量失败" .. CONST_WH)
|
local xml_result = xml.json_to_xml(FinalRes, "response")
|
mobox.returnValue(strLuaDEID, 0, xml_result, 0)
|
lua.Stop(strLuaDEID, "获取系统常量:GK_Default_Warehouse 失败", xml_result)
|
return
|
end
|
|
-- 工厂常量异常捕获
|
if (RetFAC_COE ~= 0) then
|
FinalRes = Create_result("failure", "204", "获取工厂常量失败" .. CONST_FACTORY)
|
local xml_result = xml.json_to_xml(FinalRes, "response")
|
mobox.returnValue(strLuaDEID, 0, xml_result, 0)
|
lua.Stop(strLuaDEID, "获取系统常量:GK_Default_Factory失败", xml_result)
|
return
|
end
|
-- 6. 遍历所有收货单
|
for i = 1, #receipt_headers do
|
local header = receipt_headers[i]
|
-- 检查收货单是否已存在
|
local strCondition = string.format("S_NO = '%s'", header.orderNo)
|
lua.DebugEx(strLuaDEID, "查询收货单条件", strCondition)
|
local nRet, retReceipt = m3.GetDataObjByCondition(strLuaDEID, "Receipt_Order", strCondition)
|
|
if nRet == 0 then
|
-- 查询成功且找到记录,说明收货单已存在
|
lua.DebugEx(strLuaDEID, "查询成功,收货单已存在", retReceipt)
|
FinalRes = Create_result("failure", "1", "收货单[" .. header.orderNo .. "]已存在")
|
local xml_result = xml.json_to_xml(FinalRes, "response")
|
mobox.returnValue(strLuaDEID, 0, xml_result, 0)
|
lua.Stop(strLuaDEID, "收货单已存在", retReceipt)
|
return
|
elseif nRet ~= 1 then
|
-- 查询出错
|
lua.DebugEx(strLuaDEID, "查询出错", retReceipt)
|
FinalRes = Create_result("failure", "206",
|
"检查收货单[" .. header.orderNo .. "]时出错: " .. retReceipt)
|
local xml_result = xml.json_to_xml(FinalRes, "response")
|
mobox.returnValue(strLuaDEID, 0, xml_result, 0)
|
lua.Stop(strLuaDEID, "检查收货单是否存在时出错", xml_result)
|
return
|
end
|
|
-- 创建主表数据
|
local receipt = m3.AllocObject(strLuaDEID, "Receipt_Order")
|
receipt.no = header.orderNo
|
receipt.asn_no = header.asnNo
|
receipt.op_date = header.orderDate
|
receipt.priority = header.priority
|
receipt.note = header.memo or ""
|
receipt.bs_type = "SMALL_PIECE"
|
receipt.factory = CONST_FACTORY
|
receipt.wh_code = CONST_WH
|
|
-- 检查是否已存在相同关键字的记录
|
local nRet, ret_info = m3.CreateDataObj(strLuaDEID, receipt)
|
if nRet ~= 0 then
|
-- 再次检查是否真的存在
|
local nRetCheck, retReceiptCheck = m3.GetDataObjByCondition(strLuaDEID, "Receipt_Order", strCondition)
|
if nRetCheck == 0 then
|
FinalRes = Create_result("failure", "205", "收货单[" .. header.orderNo .. "]已存在")
|
else
|
FinalRes = Create_result("failure", "207",
|
"收货单[" .. header.orderNo .. "]创建失败: " .. ret_info)
|
end
|
local xml_result = xml.json_to_xml(FinalRes, "response")
|
mobox.returnValue(strLuaDEID, 0, xml_result, 0)
|
lua.Stop(strLuaDEID, "创建收货单主表失败", ret_info)
|
return
|
end
|
|
-- 检查明细数据是否存在
|
if header.SmallPiece_TB_ITEM == nil then
|
lua.DebugEx(strLuaDEID, "警告:收货单 " .. header.orderNo .. " 无明细数据")
|
FinalRes = Create_result("failure", "208", "收货单[" .. header.orderNo .. "]无明细数据")
|
local xml_result = xml.json_to_xml(FinalRes, "response")
|
mobox.returnValue(strLuaDEID, 0, xml_result, 0)
|
lua.Stop(strLuaDEID, "收货单无明细数据", xml_result)
|
return
|
end
|
|
-- 7. 遍历当前收货单的所有明细数据
|
local details = header.SmallPiece_TB_ITEM
|
-- 确保 details 是数组(即使只有一个明细)
|
if details[1] == nil then
|
details = {details}
|
end
|
|
for j = 1, #details do
|
local item = details[j]
|
-- 先检验skuId是否存在
|
local strCondition = string.format("S_ITEM_CODE= '%s' AND S_STORER = '%s'", item.skuId, header.storerId)
|
-- 执行查询
|
local nRet, receiptInfo = m3.GetDataObjByCondition(strLuaDEID, "SKU", strCondition)
|
if (nRet ~= 0) then
|
lua.DebugEx(strLuaDEID, "SKU查询出错",
|
"ITEM_CODE: " .. item.skuId .. ", 错误信息: " .. receiptInfo)
|
FinalRes = Create_result("failure", "207",
|
"检查 SKU 是否存在时出错:SKU ID为 " .. item.skuId .. ", 错误: " .. receiptInfo)
|
local xml_result = xml.json_to_xml(FinalRes, "response")
|
mobox.returnValue(strLuaDEID, 0, xml_result, 0)
|
lua.Stop(strLuaDEID, "查询失败", xml_result)
|
return
|
elseif (receiptInfo == "") then
|
lua.DebugEx(strLuaDEID, "查询为空", "ITEM_CODE:" .. item.skuId)
|
FinalRes = Create_result("failure", "208", "查询数据为空:ITEM_CODE : " .. item.skuId)
|
local xml_result = xml.json_to_xml(FinalRes, "response")
|
mobox.returnValue(strLuaDEID, 0, xml_result, 0)
|
lua.Stop(strLuaDEID, "物料数据不存在", xml_result)
|
return
|
end
|
|
-- 创建明细数据
|
local receipt_detail = m3.AllocObject(strLuaDEID, "Receipt_Detail")
|
receipt_detail.row_no = item.orderItemId
|
receipt_detail.receipt_no = header.orderNo
|
receipt_detail.qty = lua.Get_NumAttrValue(item.qty) or 0
|
receipt_detail.acc_put_qty = lua.Get_NumAttrValue("0")
|
receipt_detail.acc_unq_qty = lua.Get_NumAttrValue("0")
|
receipt_detail.acc_c_qty = lua.Get_NumAttrValue("0")
|
receipt_detail.item_code = item.skuId
|
receipt_detail.item_state = item.skuStatus
|
receipt_detail.item_name = receiptInfo.item_name or ""
|
receipt_detail.wu = "kg"
|
receipt_detail.batch_no = item.produceCode
|
receipt_detail.prd_date = item.productDate
|
receipt_detail.exp_date = item.expiryDate
|
receipt_detail.udf01 = item.registerNo
|
receipt_detail.storer = header.storerId
|
receipt_detail.owner = header.ownerId
|
receipt_detail.item_name = receiptInfo.item_name or ""
|
receipt_detail.net_weight = receiptInfo.weight or 0
|
receipt_detail.gross_weight = receiptInfo.weight or 0
|
receipt_detail.uom = receiptInfo.uom or ""
|
|
lua.DebugEx(strLuaDEID, "创建明细数据:", receipt_detail)
|
local nRet, ret_info = m3.CreateDataObj(strLuaDEID, receipt_detail)
|
if nRet ~= 0 then
|
FinalRes = Create_result("failure", "208", "创建收货明细失败: " .. ret_info)
|
local xml_result = xml.json_to_xml(FinalRes, "response")
|
mobox.returnValue(strLuaDEID, 0, xml_result, 0)
|
lua.DebugEx(strLuaDEID, "创建收货明细失败", xml_result)
|
lua.Stop(strLuaDEID, "创建收货明细失败", ret_info)
|
return
|
end
|
end
|
end
|
|
-- 8. 返回成功
|
FinalRes = Create_result("success", "0", "收货单创建成功")
|
local xml_result = xml.json_to_xml(FinalRes, "response")
|
mobox.returnValue(strLuaDEID, 0, xml_result, 0)
|
end
|