fy36
2025-05-30 d63516d67a13061977c1c7851f069f8c5a438466
修复已知问题,保证有返回值
2个文件已修改
319 ■■■■ 已修改文件
lua_code/Lua/Count_Pan_Sync.lua 22 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
lua_code/Lua/Receipt_Sync.lua 297 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
lua_code/Lua/Count_Pan_Sync.lua
@@ -83,11 +83,12 @@
 入口函数:Count_Pan_Sync
 说明:解析 xml,支持多个 InventoryTasks_TB(主表),每个主表下可包含多个明细(InventoryTasks_TB_ITEM)
]] --
-- 引入依赖库
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 {
@@ -102,7 +103,7 @@
    -- 1. 获取 xml 数据包
    local nRet, soap_xml = mobox.getCurEditDataPacket(strLuaDEID)
    if nRet ~= 0 then
        FinalRes = Create_result("failure", "201", "无法获取数据包: " .. soap_xml)
        FinalRes = Create_result("failure", "1", "无法获取数据包: " .. soap_xml)
        lua.Stop(strLuaDEID, "获取数据包失败", FinalRes)
        return
    end
@@ -115,7 +116,7 @@
        return
    end
    -- 3. 提取主表数据(可能多个 InventoryTasks_TB)
    --  提取主表数据(可能多个 InventoryTasks_TB)
    local receipt_data = parsed_data.Envelope.Body.InventoryTasksReq.InventoryTasks_Input
    local input_params = receipt_data.InputParameters
@@ -126,7 +127,7 @@
        return
    end
    -- 4. 判断 InventoryTasks_TB 是单个对象还是数组
    --  判断 InventoryTasks_TB 是单个对象还是数组
    -- xml 解析后,如果只有一个 InventoryTasks_TB,可能返回 table;多个则可能是数组
    local mainTables = input_params.InventoryTasks_TB
    if not mainTables then
@@ -139,7 +140,7 @@
        -- 如果 mainTables 不是数组(单个主表),则包装成数组
        mainTables = {mainTables}
    end
    -- 6. 遍历所有主表数据
    -- 遍历所有主表数据
    local result = Create_result()
    for i = 1, #mainTables do
        local mainData = mainTables[i]
@@ -168,7 +169,7 @@
        count_plan.b_state = 0;
        local RetWH_COE, CONST_WH = wms_base.Get_sConst2(strLuaDEID, "GK_Default_Warehouse")
        if (RetWH_COE ~= 0) then
            FinalRes = Create_result("failure", "201", "获取仓库常量失败", CONST_WH)
            FinalRes = Create_result("failure", "1", "获取仓库常量失败", CONST_WH)
            lua.Stop(strLuaDEID, "获取仓库常量失败", CONST_WH)
        else
            count_plan.wh_code = CONST_WH; -- 仓库
@@ -195,7 +196,6 @@
            if details[1] == nil then
                details = {details}
            end
            -- 遍历主表数据及其明细数据
            for j = 1, #details do
                local item = details[j]
@@ -215,17 +215,15 @@
                    result.code = "102"
                    result.message = "创建明细数据失败"
                    result.error = "行号[" .. count_plan_item.row_no .. "]创建失败: " .. ret_info
                    FinalRes = Create_result("failure", "206", "明细表数据创建失败: ", ret_info)
                    lua.DebugEx(strLuaDEID, "明细表数据创建失败: ", ret_info)
                    FinalRes = Create_result("failure", "1", "明细表数据创建失败 ", ret_info)
                    lua.Stop(strLuaDEID, "创建明细表数据失败" .. count_plan.cp_no, ret_info)
                else
                else
                    lua.DebugEx(strLuaDEID, "明细数据创建成功!", count_plan_item)
                    FinalRes = Create_result("success", "0", "成功", "123")
                end
            end
        end
    end
    -- 8. 返回成功
    local xml_result = xml.json_to_xml(FinalRes, "response")
    mobox.returnValue(strLuaDEID, 0, xml_result, 0)
end
lua_code/Lua/Receipt_Sync.lua
@@ -22,11 +22,14 @@
 变更历史: 2025-05-14 优化返回结果处理
 
 XML 报文示例如下;
 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Header xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<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:InSmallPieceReq
            xmlns:v1="http://www.gkht.com/InReceive/INV/Ebs/Schemas/InSmallPiece/V1.0">
            <v1:SmallPiece_Input>
                <v1:RESTHeader>
                    <v1:Responsibility/>
@@ -36,68 +39,36 @@
                    <v1:Org_Id>0</v1:Org_Id>
                </v1:RESTHeader>
                <v1:InputParameters>
                    <!-- 第一个收货单 -->
                    <v1:SmallPiece_TB>
                        <v1:orderNo>SO2025050701</v1:orderNo>
                        <v1:asnNo>KPD00002</v1:asnNo>
                        <v1:storerId>CGKHTY</v1:storerId>
                        <v1:ownerId>CGKHTY</v1:ownerId>
                        <v1:orderDate>2025-05-07</v1:orderDate>
                        <v1:priority>0</v1:priority>
                        <v1:memo>收货单1备注</v1:memo>
                        <v1:SmallPiece_TB_ITEM>
                        <!--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>100118100</v1:skuId>
                            <v1:skuStatus>AVL</v1:skuStatus>
                            <v1:qty>3</v1:qty>
                            <v1:batchNo>PHI00000000000001310</v1:batchNo>
                            <v1:produceCode>YL201126</v1:produceCode>
                            <v1:productDate>2016-05-23</v1:productDate>
                            <v1:expiryDate>2099-12-31</v1:expiryDate>
                            <v1:registerNo>国食药监械(准)字2013</v1:registerNo>
                        </v1:SmallPiece_TB_ITEM>
                        <v1:SmallPiece_TB_ITEM>
                            <v1:orderItemId>2</v1:orderItemId>
                            <v1:skuId>100117231</v1:skuId>
                            <v1:skuStatus>AVL</v1:skuStatus>
                            <v1:qty>4</v1:qty>
                            <v1:batchNo>PHI00000000000001311</v1:batchNo>
                            <v1:produceCode>YL201127</v1:produceCode>
                            <v1:productDate>2023-05-23</v1:productDate>
                            <v1:expiryDate>2099-12-31</v1:expiryDate>
                            <v1:registerNo></v1:registerNo>
                        </v1:SmallPiece_TB_ITEM>
                    </v1:SmallPiece_TB>
                    <!-- 第二个收货单 -->
                    <v1:SmallPiece_TB>
                        <v1:orderNo>SO2025050702</v1:orderNo>
                        <v1:asnNo>KPD00003</v1:asnNo>
                        <v1:storerId>CGKHTY</v1:storerId>
                        <v1:ownerId>CGKHTY</v1:ownerId>
                        <v1:orderDate>2025-05-07</v1:orderDate>
                        <v1:priority>1</v1:priority>
                        <v1:memo>收货单2备注</v1:memo>
                        <v1:SmallPiece_TB_ITEM>
                            <v1:orderItemId>1</v1:orderItemId>
                            <v1:skuId>200000002</v1:skuId>
                            <v1:skuId>XR33201-2L080B</v1:skuId>
                            <v1:skuStatus>AVL</v1:skuStatus>
                            <v1:qty>2</v1:qty>
                            <v1:batchNo>PHI00000000000001312</v1:batchNo>
                            <v1:produceCode>YL201128</v1:produceCode>
                            <v1:productDate>2016-06-23</v1:productDate>
                            <v1:expiryDate>2099-12-31</v1:expiryDate>
                            <v1:registerNo>国食药监械(准)字2014</v1:registerNo>
                                    <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:SmallPiece_TB_ITEM>
                            <v1:orderItemId>2</v1:orderItemId>
                            <v1:skuId>100117238</v1:skuId>
                            <v1:skuId>KH32803017</v1:skuId>
                            <v1:skuStatus>AVL</v1:skuStatus>
                            <v1:qty>5</v1:qty>
                            <v1:batchNo>PHI00000000000001313</v1:batchNo>
                            <v1:produceCode>YL201129</v1:produceCode>
                            <v1:productDate>2023-06-23</v1:productDate>
                            <v1:expiryDate>2099-12-31</v1:expiryDate>
                            <v1:registerNo></v1:registerNo>
                                  <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>
@@ -105,39 +76,34 @@
        </v1:InSmallPieceReq>
    </soapenv:Body>
</soapenv:Envelope>
--]]
wms_base = require("wms_base")
--]] 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)
    -- 初始化最终结果
    local FinalRes = Create_result()
    -- 1. 获取 xml 数据包
    local nRet, soap_xml = mobox.getCurEditDataPacket(strLuaDEID)
    if nRet ~= 0 then
        FinalRes = Create_result("failure", "201", "无法获取数据包: " .. soap_xml)
        lua.Stop(strLuaDEID, "获取数据包失败", FinalRes)
        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 格式非法")
        lua.Stop(strLuaDEID, "xml格式非法", FinalRes)
        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. 提取主表数据
@@ -147,7 +113,9 @@
    -- 检查是否存在 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)
        local xml_result = xml.json_to_xml(FinalRes, "response")
        mobox.returnValue(strLuaDEID, 0, xml_result, 0)
        lua.Stop(strLuaDEID, "xml 数据格式错误", xml_result)
        return
    end
@@ -162,29 +130,38 @@
    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
        local xml_result = xml.json_to_xml(FinalRes, "response")
        mobox.returnValue(strLuaDEID, 0, xml_result, 0)
        lua.Stop(strLuaDEID, "获取系统常量失败", xml_result)
        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)
        lua.DebugEx(strLuaDEID, "查询收货单条件", strCondition)
        local nRet, retReceipt = m3.GetDataObjByCondition(strLuaDEID, "Receipt_Order", strCondition)
        if nRet == 0 then
            FinalRes = Create_result("failure", "205", "收货单已存在",
            -- 查询成功且找到记录,说明收货单已存在
            lua.DebugEx(strLuaDEID, "查询成功,收货单已存在", retReceipt)
            FinalRes = Create_result("failure", "1", "收货单已存在",
                "收货单[" .. header.orderNo .. "]已存在")
            lua.Stop(strLuaDEID, "收货单已存在", FinalRes)
        elseif nRet > 1 then
            FinalRes = Create_result("failure", "206", "系统错误",
                "检查收货单是否存在时出错: " .. strRetInfo)
            lua.Stop(strLuaDEID, "检查收货单是否存在时出错", FinalRes)
            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
        -- 创建主表数据
@@ -197,71 +174,105 @@
        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)
            -- 再次检查是否真的存在
            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 not header.SmallPiece_TB_ITEM then
        if header.SmallPiece_TB_ITEM == nil then
            lua.DebugEx(strLuaDEID, "警告:收货单 " .. header.orderNo .. " 无明细数据")
        else
            -- 7. 遍历当前收货单的所有明细数据
            local details = header.SmallPiece_TB_ITEM
            -- 确保 details 是数组(即使只有一个明细)
            if details[1] == nil then
                details = {details}
            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
            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
            -- 创建明细数据
            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 ""
                -- 获取物料信息
                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
            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", "收货单创建成功")
    FinalRes = Create_result("success", "0", "收货单创建成功")
    local xml_result = xml.json_to_xml(FinalRes, "response")
    mobox.returnValue(strLuaDEID, 0, xml_result, 0)
end