From bd28aa0a34f2c9e5fee6aad0b5343cf869b787ca Mon Sep 17 00:00:00 2001
From: fy36 <1046342473@qq.com>
Date: 星期四, 15 五月 2025 19:16:15 +0800
Subject: [PATCH] 1. 修复请求接口报错 xml解析异常的问题 2. 完善接口注释说明和代码逻辑

---
 lua_code/Lua/GK-API-04 GK-WMS-Receipt_Sync.lua |  230 ++++++++++++++++++++++++++++++++++++++++----------------
 1 files changed, 163 insertions(+), 67 deletions(-)

diff --git a/lua_code/Lua/GK-API-04 GK-WMS-Receipt_Sync.lua b/lua_code/Lua/GK-API-04 GK-WMS-Receipt_Sync.lua
index 702ed7a..2060e3f 100644
--- a/lua_code/Lua/GK-API-04 GK-WMS-Receipt_Sync.lua
+++ b/lua_code/Lua/GK-API-04 GK-WMS-Receipt_Sync.lua
@@ -1,15 +1,27 @@
 --[[
- 缂栫爜: 
- 鍚嶇О: Receipt_Sync 
+ 缂栫爜: GK-API-04
+ 鍚嶇О: GK-WMS-Receipt_Sync
  浣滆�: 琚佸嘲
- 鍏ュ彛鍑芥暟锛歊eceipt_Sync 
- 鍔熻兘璇存槑:  鎺ユ敹鏉ヨ嚜涓婃父绯荤粺鐨�XML 鏍煎紡鏁版嵁锛屽苟瑙f瀽璇ユ暟鎹� 鍒涘缓鏀惰揣鍗曚富琛�Record_Order 鍜�鏀惰揣鍗曟槑缁嗘暟鎹�Receipt_Detail 
+ 鍏ュ彛鍑芥暟锛歊eceipt_Sync
+ 鍔熻兘璇存槑: 鎺ユ敹涓婃父绯荤粺鐨刋ML鏁版嵁锛屽垱寤烘敹璐у崟涓昏〃锛圧eceipt_Order锛夊拰鏄庣粏璁板綍锛圧eceipt_Detail锛夈�
+ 杈撳叆鍙傛暟: xmlData锛圶ML鏍煎紡鐨勫瓧绗︿覆锛�+ 杩斿洖鍙傛暟: xml 鏍煎紡鐨勫搷搴旀姤鏂囷細
+ 褰㈠锛�+ <response>
+    <message>鏀惰揣鍗曞凡瀛樺湪</message>
+    <code>204</code>
+    <flag>failure</flag>
+    <error>鏀惰揣鍗昜SO2025050701]宸插瓨鍦�/error>
+ </response>
+ 璋冪敤绀轰緥: Receipt_Sync(xmlData)
+ 娉ㄦ剰浜嬮」: 1. 杈撳叆鍙傛暟xmlData涓篨ML鏍煎紡鐨勫瓧绗︿覆锛屽寘鍚簡鏀惰揣鍗曚富琛ㄥ拰鏄庣粏璁板綍鐨勬暟鎹�
+          2. 鍑芥暟浼氳В鏋怷ML鏁版嵁锛屽苟灏嗗叾鎻掑叆鍒版暟鎹簱涓�
+          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>
@@ -92,16 +104,26 @@
         </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")
 
     -- 鍩烘湰鍏宠仈淇℃伅
@@ -109,12 +131,17 @@
     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
@@ -143,71 +170,140 @@
 
     -- 鍏朵粬涓氬姟瀛楁
     receipt_detail.udf01 = detail_item.registerNo
-    receipt_detail.storer = header.storerId  -- 淇瀛楁鍚嶏細storeId -> storerId锛堟牴鎹甔ML鏍蜂緥锛�+    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)
-    -- 鑾峰彇骞惰В鏋怷ML
+    local final_result = create_result()
+    local is_stop = 0 -- 0:姝e父 3:闇�鍥炴粴
+
+    -- 1. 鑾峰彇骞惰В鏋怷ML
     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. 瑙f瀽XML鏁版嵁缁撴瀯
+            local receipt_data = parsed_data.Envelope.Body.InSmallPieceReq.SmallPiece_Input
+            local input_params = receipt_data.InputParameters
 
-    -- 瑙f瀽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
+    -- 杞崲涓篨ML鏍煎紡杩斿洖
+    local xml_result = xml.json_to_xml(final_result, "response")
+
+    lua.DebugEx(strLuaDEID, "final_result锛�, final_result);
+    lua.DebugEx(strLuaDEID, "xml_result锛�, xml_result);
+    -- 涓�緥鎸夌収榛樿涓�鐨�is_stop 鏉ュ鐞�+    mobox.returnValue(strLuaDEID, 0, xml_result, 0)
 end
\ No newline at end of file

--
Gitblit v1.9.1