| | |
| | | --[[ |
| | | 编码: |
| | | 名称: Receipt_Sync |
| | | 编码: GK-API-04 |
| | | 名称: GK-WMS-Receipt_Sync |
| | | 作者: 袁峰 |
| | | 入口函数:Receipt_Sync |
| | | 功能说明: 接收来自上游系统的 XML 格式数据,并解析该数据, 创建收货单主表 Record_Order 和 收货单明细数据 Receipt_Detail |
| | | 入口函数: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条或者多条收货单主表数据,每条收货单主表数据可以包含多条收货 |
| | | 单明细数据。 |
| | | |
| | | 变更历史: |
| | | |
| | | XML 文件交互案例: |
| | | |
| | | <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> |
| | | 变更历史: 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> |
| | | </soapenv:Body> |
| | | </soapenv:Envelope> |
| | | |
| | | --]] |
| | | |
| | | |
| | | wms_base = require("wms_base") |
| | | xml = require("oi_base_xml") |
| | | mobox = require("OILua_JavelinExt") |
| | | |
| | | -- 创建收货单明细对象(完全保持原有逻辑不变) |
| | | |
| | | -- 创建统一返回结果 |
| | | local function create_result(flag, code, msg, error) |
| | | return { |
| | | flag = flag or "success", |
| | | code = code or "0", |
| | | message = msg or "", |
| | | error = error or "" |
| | | } |
| | | end |
| | | |
| | | -- 创建收货单明细对象 |
| | | local function create_receipt_detail(strLuaDEID, detail_item, no, header) |
| | | local result = create_result() |
| | | local receipt_detail = m3.AllocObject(strLuaDEID, "Receipt_Detail") |
| | | |
| | | -- 基本关联信息 |
| | |
| | | receipt_detail.receipt_no = no |
| | | |
| | | -- 物料信息查询 |
| | | local material_info; |
| | | local material_info |
| | | if detail_item.skuId ~= nil then |
| | | local nRet, mat_info = m3.GetDataObjByCondition(strLuaDEID, "Material", |
| | | "S_ITEM_CODE='" .. detail_item.skuId .. "'") |
| | | |
| | | if nRet ~= 0 then |
| | | lua.Stop(strLuaDEID, "物料数据获取失败:", mat_info) |
| | | result.flag = "failure" |
| | | result.code = "101" |
| | | result.message = "物料数据获取失败" |
| | | result.error = "物料[" .. detail_item.skuId .. "]查询失败: " .. mat_info |
| | | return result |
| | | else |
| | | material_info = mat_info |
| | | end |
| | |
| | | |
| | | -- 其他业务字段 |
| | | receipt_detail.udf01 = detail_item.registerNo |
| | | receipt_detail.storer = header.storerId -- 修正字段名:storeId -> storerId(根据XML样例) |
| | | receipt_detail.storer = header.storerId |
| | | receipt_detail.owner = header.ownerId |
| | | |
| | | local nRet, result = m3.CreateDataObj(strLuaDEID, receipt_detail) |
| | | local nRet, ret_info = m3.CreateDataObj(strLuaDEID, receipt_detail) |
| | | if nRet ~= 0 then |
| | | lua.Stop(strLuaDEID, "创建收货明细记录失败", "返回值" .. nRet .. " 返回数据:" .. result) |
| | | result.flag = "failure" |
| | | result.code = "102" |
| | | result.message = "创建收货明细失败" |
| | | result.error = "行号[" .. detail_item.orderItemId .. "]创建失败: " .. ret_info |
| | | end |
| | | |
| | | return nRet, result |
| | | return result |
| | | end |
| | | |
| | | -- 处理单个收货单 |
| | | local function process_receipt(strLuaDEID, header, CONST_FACTORY, CONST_WH, final_result) |
| | | -- 检查收货单是否已存在 |
| | | local strCondition = string.format("S_NO = '%s'", header.orderNo) |
| | | local nRet, id, strRetInfo = mobox.getDataObjAttrByKeyAttr(strLuaDEID, "Receipt_Order", strCondition) |
| | | |
| | | if nRet == 0 then |
| | | final_result.flag = "failure" |
| | | final_result.code = "204" |
| | | final_result.message = "收货单已存在" |
| | | final_result.error = "收货单[" .. header.orderNo .. "]已存在" |
| | | return false |
| | | elseif nRet > 1 then |
| | | final_result.flag = "failure" |
| | | final_result.code = "205" |
| | | final_result.message = "系统错误" |
| | | final_result.error = "检查收货单是否存在时出错: " .. strRetInfo |
| | | return false |
| | | 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, result = m3.CreateDataObj(strLuaDEID, receipt) |
| | | if nRet ~= 0 then |
| | | final_result.flag = "failure" |
| | | final_result.code = "206" |
| | | final_result.message = "创建收货单失败" |
| | | final_result.error = "创建收货单[" .. header.orderNo .. "]失败: " .. result |
| | | return false |
| | | end |
| | | |
| | | -- 处理明细数据 |
| | | local detail_items = header.SmallPiece_TB_ITEM |
| | | if not detail_items then |
| | | final_result.flag = "failure" |
| | | final_result.code = "207" |
| | | final_result.message = "缺少明细数据" |
| | | final_result.error = "收货单[" .. header.orderNo .. "]缺少明细数据" |
| | | return false |
| | | end |
| | | |
| | | -- 判断是单个明细还是多个明细 |
| | | if detail_items.orderItemId then -- 如果是单个明细 |
| | | detail_items = {detail_items} -- 转为单元素数组 |
| | | end |
| | | |
| | | for _, detail_item in ipairs(detail_items) do |
| | | local detail_result = create_receipt_detail(strLuaDEID, detail_item, receipt.no, header) |
| | | if detail_result.flag == "failure" then |
| | | final_result.flag = "failure" |
| | | final_result.code = detail_result.code or "208" |
| | | final_result.message = detail_result.message or "创建明细失败" |
| | | final_result.error = detail_result.error or "创建明细失败" |
| | | return false |
| | | end |
| | | end |
| | | |
| | | return true |
| | | end |
| | | |
| | | function Receipt_Sync(strLuaDEID) |
| | | -- 获取并解析XML |
| | | local final_result = create_result() |
| | | local is_stop = 0 -- 0:正常 3:需要回滚 |
| | | |
| | | -- 1. 获取并解析XML |
| | | local nRet, soap_xml = mobox.getCurEditDataPacket(strLuaDEID) |
| | | if nRet ~= 0 then lua.Stop(strLuaDEID, "无法获取数据包!" .. soap_xml) return end |
| | | |
| | | local nRet, parsed_data = xml.parse(soap_xml) |
| | | if nRet ~= 0 then lua.Stop(strLuaDEID, "XML格式非法!") return end |
| | | if nRet ~= 0 then |
| | | final_result = create_result("failure", "201", "无法获取数据包: " .. soap_xml) |
| | | is_stop = 3 |
| | | else |
| | | local nRet, parsed_data = xml.parse(soap_xml) |
| | | if nRet ~= 0 then |
| | | final_result = create_result("failure", "202", "XML格式非法") |
| | | is_stop = 3 |
| | | else |
| | | -- 2. 解析XML数据结构 |
| | | local receipt_data = parsed_data.Envelope.Body.InSmallPieceReq.SmallPiece_Input |
| | | local input_params = receipt_data.InputParameters |
| | | |
| | | -- 解析XML数据结构(修改为支持多个收货单) |
| | | local receipt_data = parsed_data.Envelope.Body.InSmallPieceReq.SmallPiece_Input |
| | | local input_params = receipt_data.InputParameters |
| | | |
| | | -- 判断是单个收货单还是多个收货单 |
| | | local receipt_headers = input_params.SmallPiece_TB |
| | | if receipt_headers.orderNo then -- 如果是单个收货单 |
| | | receipt_headers = {receipt_headers} -- 转为单元素数组 |
| | | end |
| | | -- 判断是单个收货单还是多个收货单 |
| | | local receipt_headers = input_params.SmallPiece_TB |
| | | if receipt_headers.orderNo then -- 如果是单个收货单 |
| | | receipt_headers = {receipt_headers} -- 转为单元素数组 |
| | | end |
| | | |
| | | -- 获取常量 |
| | | local nRet1, CONST_FACTORY = wms_base.Get_sConst2(strLuaDEID, "GK_Default_Factory") |
| | | local nRet2, CONST_WH = wms_base.Get_sConst2(strLuaDEID, "GK_Default_Warehouse") |
| | | if nRet1 ~= 0 then lua.Stop(strLuaDEID, "获取默认工厂标识失败" .. CONST_FACTORY) end |
| | | if nRet2 ~= 0 then lua.Stop(strLuaDEID, "获取默认仓库编号失败" .. CONST_WH) end |
| | | |
| | | -- 处理每个收货单(新增循环) |
| | | for _, header in ipairs(receipt_headers) do |
| | | -- 创建主表记录 |
| | | 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 |
| | | receipt.bs_type = "SMALL_PIECE" |
| | | receipt.factory = CONST_FACTORY |
| | | receipt.wh_code = CONST_WH |
| | | |
| | | local nRet, result = m3.CreateDataObj(strLuaDEID, receipt) |
| | | if nRet ~= 0 then lua.Stop(strLuaDEID, "创建收货单主表失败!" .. result) end |
| | | |
| | | -- 处理明细 |
| | | local detail_items = header.SmallPiece_TB_ITEM |
| | | if not detail_items then lua.Stop(strLuaDEID, "缺少收货单明细数据!") end |
| | | |
| | | -- 判断是单个明细还是多个明细 |
| | | if detail_items.orderItemId then -- 如果是单个明细 |
| | | detail_items = {detail_items} -- 转为单元素数组 |
| | | end |
| | | |
| | | for _, detail_item in ipairs(detail_items) do |
| | | nRet = create_receipt_detail(strLuaDEID, detail_item, receipt.no, header) |
| | | if nRet ~= 0 then |
| | | lua.Stop(strLuaDEID, "创建收货明细失败: skuId=" .. detail_item.skuId .. ", batchNo=" .. detail_item.batchNo) |
| | | -- 3. 获取常量 |
| | | local nRet1, CONST_FACTORY = wms_base.Get_sConst2(strLuaDEID, "GK_Default_Factory") |
| | | local nRet2, CONST_WH = wms_base.Get_sConst2(strLuaDEID, "GK_Default_Warehouse") |
| | | if nRet1 ~= 0 or nRet2 ~= 0 then |
| | | final_result = create_result("failure", "203", "获取系统常量失败") |
| | | is_stop = 3 |
| | | else |
| | | -- 4. 处理每个收货单 |
| | | for _, header in ipairs(receipt_headers) do |
| | | if not process_receipt(strLuaDEID, header, CONST_FACTORY, CONST_WH, final_result) then |
| | | is_stop = 3 |
| | | break |
| | | end |
| | | end |
| | | end |
| | | end |
| | | end |
| | | -- 返回处理结果 |
| | | if final_result.flag == "success" then |
| | | final_result.message = "收货单创建成功" |
| | | end |
| | | -- 转换为XML格式返回 |
| | | local xml_result = xml.json_to_xml(final_result, "response") |
| | | |
| | | lua.DebugEx(strLuaDEID, "final_result:", final_result); |
| | | lua.DebugEx(strLuaDEID, "xml_result:", xml_result); |
| | | -- 一律按照默认为0的 is_stop 来处理 |
| | | mobox.returnValue(strLuaDEID, 0, xml_result, 0) |
| | | end |