| | |
| | | 作者: 袁峰 |
| | | 入口函数:Count_Pan_Sync |
| | | 功能说明: 该接口是同步接口, |
| | | 上游系统 调用该接口后,WES 的响应报文 success 说明 WES 已经将该报文接收成功; |
| | | 上游系统调用该接口后,WES的响应报文success说明WES已经将该报文接收成功; |
| | | 需要同步盘点任务表和盘点任务明细表xml结构。 |
| | | 变更历史: |
| | | |
| | |
| | | </soapenv:Envelope> |
| | | |
| | | 响应示例: |
| | | <flag>sucess</flag> |
| | | <code>0</code> |
| | | <message>成功</message> |
| | | ]] --[[ |
| | | 功能:盘点计划同步接口(支持多主表+多明细) |
| | | 作者:袁峰 |
| | | 入口函数:Count_Pan_Sync |
| | | 说明:解析 xml,支持多个 InventoryTasks_TB(主表),每个主表下可包含多个明细(InventoryTasks_TB_ITEM) |
| | | <response> |
| | | <flag>success</flag> |
| | | <code>0</code> |
| | | <message>成功</message> |
| | | </response> |
| | | ]] -- |
| | | -- 引入依赖库 |
| | | wms_base = require("wms_base") |
| | | xml = require("oi_base_xml") |
| | | mobox = require("OILua_JavelinExt") |
| | | m3 = require("oi_base_mobox") |
| | | wms = require("OILua_WMS") |
| | | |
| | | -- 创建统一返回结果 |
| | | function Create_result(flag, code, msg, error) |
| | | return { |
| | | flag = flag or "success", |
| | | code = code or "0", |
| | | message = msg or "", |
| | | error = error or "" |
| | | message = msg or "" |
| | | } |
| | | end |
| | | |
| | | function Count_Pan_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. 提取主表数据(可能多个 InventoryTasks_TB) |
| | | -- 3. 提取主表数据 |
| | | local receipt_data = parsed_data.Envelope.Body.InventoryTasksReq.InventoryTasks_Input |
| | | local input_params = receipt_data.InputParameters |
| | | |
| | | -- 检查是否存在 InventoryTasks_TB |
| | | if not input_params or not input_params.InventoryTasks_TB then |
| | | FinalRes = Create_result("failure", "203", "xml 数据格式错误,缺少 InventoryTasks_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 |
| | | |
| | | -- 4. 判断 InventoryTasks_TB 是单个对象还是数组 |
| | | -- xml 解析后,如果只有一个 InventoryTasks_TB,可能返回 table;多个则可能是数组 |
| | | -- 4. 统一处理:确保 mainTables 是数组(即使只有一个主表) |
| | | local mainTables = input_params.InventoryTasks_TB |
| | | if not mainTables then |
| | | FinalRes = Create_result("failure", "204", "xml 数据格式错误,InventoryTasks_TB 为空") |
| | | lua.Stop(strLuaDEID, "xml 数据格式错误", FinalRes) |
| | | return |
| | | end |
| | | -- 5. 统一处理:确保 mainTables 是数组(即使只有一个主表) |
| | | if mainTables[1] == nil then |
| | | -- 如果 mainTables 不是数组(单个主表),则包装成数组 |
| | | mainTables = {mainTables} |
| | | end |
| | | -- 6. 遍历所有主表数据 |
| | | local result = Create_result() |
| | | -- 5. 获取系统常量 |
| | | local RetWH_COE, CONST_WH = wms_base.Get_sConst2("GK_Default_Warehouse") |
| | | if RetWH_COE ~= 0 then |
| | | lua.DebugEx(strLuaDEID, "获取仓库常量失败!", CONST_WH) |
| | | 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, "获取系统常量失败", xml_result) |
| | | return |
| | | end |
| | | |
| | | -- 6. 遍历所有盘点任务 |
| | | for i = 1, #mainTables do |
| | | local mainData = mainTables[i] |
| | | -- lua.DebugEx(strLuaDEID, "主表任务ID:", mainData.taskId) |
| | | |
| | | -- 检查盘点任务是否已存在 |
| | | local strCondition = string.format("S_CP_NO = '%s'", mainData.taskId) |
| | | lua.DebugEx(strLuaDEID, "查询盘点任务条件", strCondition) |
| | | local nRet, retCountPlan = m3.GetDataObjByCondition(strLuaDEID, "Count_Plan", strCondition) |
| | | lua.DebugEx(strLuaDEID, "查询结果", retCountPlan) |
| | | if nRet == 0 then |
| | | -- 查询成功且找到记录,说明盘点任务已存在 |
| | | lua.DebugEx(strLuaDEID, "查询成功,盘点任务已存在", retCountPlan) |
| | | FinalRes = Create_result("failure", "1", "盘点任务已存在", |
| | | "盘点任务[" .. mainData.taskId .. "]已存在") |
| | | local xml_result = xml.json_to_xml(FinalRes, "response") |
| | | mobox.returnValue(strLuaDEID, 0, xml_result, 0) |
| | | lua.Stop(strLuaDEID, "盘点任务已存在", retCountPlan) |
| | | return |
| | | -- 如果是nRet=1,那么表示这个数据在表中不存在,可以放心插入新数据 |
| | | elseif nRet ~= 1 then |
| | | -- 查询出错 |
| | | lua.DebugEx(strLuaDEID, "查询出错", retCountPlan) |
| | | FinalRes = Create_result("failure", "206", "检查盘点任务是否存在时出错", |
| | | "检查盘点任务[" .. mainData.taskId .. "]时出错: " .. retCountPlan) |
| | | local xml_result = xml.json_to_xml(FinalRes, "response") |
| | | mobox.returnValue(strLuaDEID, 0, xml_result, 0) |
| | | lua.Stop(strLuaDEID, "检查盘点任务是否存在时出错", xml_result) |
| | | return |
| | | end |
| | | |
| | | -- 创建主表数据 |
| | | local count_plan = m3.AllocObject(strLuaDEID, "Count_Plan") |
| | | count_plan.cp_no = mainData.taskId; -- 盘点计划号 |
| | | count_plan.inventory_mode = mainTables[i].inventoryMode; -- 盘点方式 |
| | | count_plan.work_mode = mainData.workMode; -- 作业方式 |
| | | count_plan.count_limit = mainData.MaintenanceNumber; -- 盘点数量限制 |
| | | count_plan.type = mainTables[i].inventoryType; -- 盘点类型 |
| | | count_plan.bs_no = mainData.orderNo; -- 来源单号 |
| | | count_plan.storeId = mainData.storer; -- 货主编码 |
| | | count_plan.ownerId = mainData.owner; -- 物权 |
| | | count_plan.begin_time = mainData.mtBeginDate; -- 动碰开始时间 |
| | | count_plan.end_time = mainData.mtEndDate; -- 动碰结束时间 |
| | | count_plan.prod_line = mainData.productLine; -- 产品线 |
| | | count_plan.begin_location = mainData.beginLocation; -- 开始库位 |
| | | count_plan.end_location = mainData.endLocation; -- 结束库位 |
| | | count_plan.area_code = mainData.areaCode; -- 库区 |
| | | count_plan.op_date = mainData.orderDate; -- 订单日期 |
| | | count_plan.priority = mainData.priority; -- 优先级 |
| | | count_plan.note = mainData.memo; -- 备注 |
| | | -- count_plan.type = wms_base.Get_nConst2(strLuaDEID, "WMS_CountType") |
| | | count_plan.plan_total = 0; -- 计划盘点数 |
| | | 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) |
| | | lua.Stop(strLuaDEID, "获取仓库常量失败", CONST_WH) |
| | | else |
| | | count_plan.wh_code = CONST_WH; -- 仓库 |
| | | count_plan.acc_finish = 0; |
| | | end |
| | | lua.DebugEx(strLuaDEID, "创建的表单:", count_plan); |
| | | count_plan.cp_no = mainData.taskId |
| | | count_plan.inventory_mode = mainData.inventoryMode |
| | | count_plan.work_mode = mainData.workMode |
| | | count_plan.count_limit = mainData.MaintenanceNumber |
| | | count_plan.pan_type = mainData.inventoryType or "" |
| | | lua.DebugEx(strLuaDEID, "容器类型:", count_plan.pan_type); |
| | | count_plan.bs_no = mainData.orderNo |
| | | count_plan.storer = mainData.storerId |
| | | count_plan.owner = mainData.ownerId |
| | | count_plan.begin_time = mainData.mtBeginDate |
| | | count_plan.end_time = mainData.mtEndDate |
| | | count_plan.wh_code = CONST_WH |
| | | count_plan.plan_total = 0 |
| | | count_plan.b_state = 0 |
| | | count_plan.acc_finish = 0 |
| | | lua.DebugEx(strLuaDEID, "获取创建数据:", count_plan); |
| | | -- 检查是否已存在相同关键字的记录 |
| | | local nRet, ret_info = m3.CreateDataObj(strLuaDEID, count_plan) |
| | | lua.DebugEx(strLuaDEID, "创建结果", ret_info) |
| | | if nRet ~= 0 then |
| | | result.flag = "failure" |
| | | result.code = "102" |
| | | result.message = "创建主表数据失败" |
| | | result.error = "行号[" .. count_plan.cp_no .. "]创建失败: " .. ret_info |
| | | lua.DebugEx(strLuaDEID, "主表数据创建失败: ", ret_info) |
| | | FinalRes = Create_result("failure", "205", "主表数据创建失败: ", ret_info) |
| | | lua.Stop(strLuaDEID, "创建主表数据失败" .. count_plan.cp_no, ret_info) |
| | | -- 再次检查是否真的存在 |
| | | local nRetCheck, retCountPlanCheck = m3.GetDataObjByCondition(strLuaDEID, "Count_Plan", strCondition) |
| | | lua.DebugEx(strLuaDEID, "再次查询结果", retCountPlanCheck) |
| | | if nRetCheck == 0 then |
| | | FinalRes = Create_result("failure", "205", "盘点任务已存在", |
| | | "盘点任务[" .. mainData.taskId .. "]已存在") |
| | | else |
| | | FinalRes = Create_result("failure", "207", "创建盘点任务主表失败", |
| | | "盘点任务[" .. mainData.taskId .. "]创建失败: " .. 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 mainData.InventoryTasks_TB_ITEM then |
| | | lua.DebugEx(strLuaDEID, "警告:任务 " .. mainData.taskId .. " 无明细数据") |
| | | if mainData.InventoryTasks_TB_ITEM == nil then |
| | | lua.DebugEx(strLuaDEID, "警告:盘点任务 " .. mainData.taskId .. " 无明细数据") |
| | | -- 无明细数据不视为错误,继续处理下一个任务 |
| | | else |
| | | -- 7. 遍历当前主表的所有明细数据 |
| | | -- 7. 遍历当前盘点任务的所有明细数据 |
| | | local details = mainData.InventoryTasks_TB_ITEM |
| | | -- 确保 details 是数组(即使只有一个明细) |
| | | if details[1] == nil then |
| | | details = {details} |
| | | end |
| | | |
| | | -- 遍历主表数据及其明细数据 |
| | | for j = 1, #details do |
| | | local item = details[j] |
| | | |
| | | -- 创建明细数据 |
| | | local count_plan_item = m3.AllocObject(strLuaDEID, "Count_Plan_Detail") |
| | | count_plan_item.cp_no = mainData.taskId; -- 盘点计划号(关联主表) |
| | | count_plan_item.row_no = item.orderItemId; -- 订单编号 |
| | | count_plan_item.qty = item.qty; -- 计划数量 |
| | | count_plan_item.wms_bn = item.batchNo; -- WMS批次编号 |
| | | count_plan_item.batch_no = item.produceCode; -- 生产批次 |
| | | count_plan_item.prd_date = item.productDate; -- 生产日期 |
| | | count_plan_item.exp_date = item.expiryDate; -- 有效期 |
| | | count_plan_item.reg_no = item.registerNo; -- 商品编码 |
| | | count_plan_item.cp_no = mainData.taskId |
| | | count_plan_item.row_no = item.orderItemId |
| | | count_plan_item.qty = lua.Get_NumAttrValue(item.qty) or 0 |
| | | count_plan_item.wms_bn = item.batchNo |
| | | count_plan_item.batch_no = item.produceCode or "" |
| | | count_plan_item.prd_date = item.productDate or "" |
| | | count_plan_item.exp_date = item.expiryDate or "" |
| | | count_plan_item.reg_no = item.registerNo or "" |
| | | |
| | | local nRet, ret_info = m3.CreateDataObj(strLuaDEID, count_plan_item) |
| | | if nRet ~= 0 then |
| | | result.flag = "failure" |
| | | 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) |
| | | lua.Stop(strLuaDEID, "创建明细表数据失败" .. count_plan.cp_no, ret_info) |
| | | else |
| | | lua.DebugEx(strLuaDEID, "明细数据创建成功!", count_plan_item) |
| | | 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 |
| | | end |
| | | |
| | | -- 8. 返回成功 |
| | | FinalRes = Create_result("success", "0", "盘点任务创建成功") |
| | | local xml_result = xml.json_to_xml(FinalRes, "response") |
| | | mobox.returnValue(strLuaDEID, 0, xml_result, 0) |
| | | end |