--[[ 编码: GK-API-04 名称: GK-WMS-Receipt_Sync 作者: 袁峰 入口函数:Receipt_Sync 功能说明: 接收上游系统的XML数据,创建收货单主表(Receipt_Order)和明细记录(Receipt_Detail)。 输入参数: xmlData(XML格式的字符串) 返回参数: xml 格式的响应报文: 形如: 收货单已存在 204 failure 收货单[SO2025050701]已存在 调用示例: Receipt_Sync(xmlData) 注意事项: 1. 输入参数xmlData为XML格式的字符串,包含了收货单主表和明细记录的数据。 2. 函数会解析XML数据,并将其插入到数据库中。 3. 允许xml 传入1条或者多条收货单主表数据,每条收货单主表数据可以包含多条收货单明细数据。 变更历史: 2025-05-14 优化返回结果处理 XML 报文示例如下; SIMPLIFIED CHINESE 0 SO2025050701 KPD00002 CGKHTY CGKHTY 2025-05-07 0 收货单1备注 1 100118100 AVL 3 PHI00000000000001310 YL201126 2016-05-23 2099-12-31 国食药监械(准)字2013 2 100117231 AVL 4 PHI00000000000001311 YL201127 2023-05-23 2099-12-31 SO2025050702 KPD00003 CGKHTY CGKHTY 2025-05-07 1 收货单2备注 1 200000002 AVL 2 PHI00000000000001312 YL201128 2016-06-23 2099-12-31 国食药监械(准)字2014 2 100117238 AVL 5 PHI00000000000001313 YL201129 2023-06-23 2099-12-31 --]] 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, error) return { flag = flag or "success", code = code or "0", message = msg or "", error = error or "" } end function Receipt_Sync(strLuaDEID) -- 1. 获取 xml 数据包 local nRet, soap_xml = mobox.getCurEditDataPacket(strLuaDEID) if nRet ~= 0 then FinalRes = Create_result("failure", "201", "无法获取数据包: " .. soap_xml) lua.Stop(strLuaDEID, "获取数据包失败", FinalRes) end -- 2. 解析 xml local nRet, parsed_data = xml.parse(soap_xml) if nRet ~= 0 then FinalRes = Create_result("failure", "202", "xml 格式非法") lua.Stop(strLuaDEID, "xml格式非法", FinalRes) 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") lua.Stop(strLuaDEID, "xml 数据格式错误", FinalRes) 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(strLuaDEID, "GK_Default_Warehouse") local RetFAC_COE, CONST_FACTORY = wms_base.Get_sConst2(strLuaDEID, "GK_Default_Factory") if (RetWH_COE ~= 0 or RetFAC_COE ~= 0) then FinalRes = Create_result("failure", "204", "获取仓库/工厂常量失败") lua.Stop(strLuaDEID, "获取系统常量失败", FinalRes) return end -- 6. 遍历所有收货单 local result = Create_result() for i = 1, #receipt_headers do local header = receipt_headers[i] -- 检查收货单是否已存在 local strCondition = string.format("S_NO = '%s'", header.orderNo) lua.DebugEx(strLuaDEID, "SQL 条件", strCondition) local nRet, strRetInfo = mobox.getDataObjAttrByKeyAttr(strLuaDEID, "Receipt_Order", strCondition) if nRet == 0 then FinalRes = Create_result("failure", "205", "收货单已存在", "收货单[" .. header.orderNo .. "]已存在") lua.Stop(strLuaDEID, "收货单已存在", FinalRes) elseif nRet > 1 then FinalRes = Create_result("failure", "206", "系统错误", "检查收货单是否存在时出错: " .. strRetInfo) lua.Stop(strLuaDEID, "检查收货单是否存在时出错", FinalRes) 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 lua.DebugEx(strLuaDEID, "创建的表单:", receipt); local nRet, ret_info = m3.CreateDataObj(strLuaDEID, receipt) if nRet ~= 0 then FinalRes = Create_result("failure", "207", "创建收货单主表失败", "收货单[" .. header.orderNo .. "]创建失败: " .. ret_info) lua.Stop(strLuaDEID, "创建收货单主表失败", FinalRes) end -- 检查明细数据是否存在 if not header.SmallPiece_TB_ITEM then lua.DebugEx(strLuaDEID, "警告:收货单 " .. header.orderNo .. " 无明细数据") else -- 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] -- 创建明细数据 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.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 -- 获取物料信息 if item.skuId ~= nil then local nRet, mat_info = m3.GetDataObjByCondition(strLuaDEID, "Material", "S_ITEM_CODE='" .. item.skuId .. "'") if nRet == 0 then receipt_detail.item_name = mat_info.item_name or "" receipt_detail.net_weight = mat_info.weight or 0 receipt_detail.gross_weight = mat_info.weight or 0 receipt_detail.uom = mat_info.uom or "" end end local nRet, ret_info = m3.CreateDataObj(strLuaDEID, receipt_detail) if nRet ~= 0 then FinalRes = Create_result("failure", "208", "创建收货明细失败", "行号[" .. item.orderItemId .. "]创建失败: " .. ret_info) lua.Stop(strLuaDEID, "创建收货明细失败", FinalRes) end 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