import { getFinReimbursementDetail } from "@/api/officeProcessAutomation/finReimbursement.js"; import { matchBusinessTypeValue } from "../../ApproveManage/approve-list/approveListConstants.js"; import { APPROVAL_MODULE_KEYS, getApprovalModuleConfig, } from "../../ApproveManage/approve-shared/approvalModuleRegistry.js"; import { getModuleKeyByReimbursementType, mapFinReimbursementDetailRow, resolveReimbursementType, unwrapFinReimbursementDetail, } from "./finReimbursementMappers.js"; export const REIMBURSE_EDIT_FROM_APPROVE_KEY = "oa_reimburse_edit_from_approve"; const REIMBURSE_MODULE_KEYS = [ APPROVAL_MODULE_KEYS.TRAVEL_REIMBURSE, APPROVAL_MODULE_KEYS.COST_REIMBURSE, ]; /** 审批实例是否差旅/费用报销 */ export function inferReimburseModuleKeyFromInstance(row) { if (!row) return ""; for (const moduleKey of REIMBURSE_MODULE_KEYS) { const cfg = getApprovalModuleConfig(moduleKey); if (!cfg) continue; if ( cfg.businessType != null && cfg.businessType !== "" && matchBusinessTypeValue(row.businessType, cfg.businessType) ) { return moduleKey; } if (matchBusinessTypeValue(row.businessType, cfg.approvalType)) { return moduleKey; } const text = `${row.templateName || ""}${row.title || ""}${row.businessName || ""}`; if ((cfg.typeLabels || []).some((l) => l && text.includes(l))) { return moduleKey; } } return ""; } export function isReimburseApprovalInstance(row) { return Boolean(inferReimburseModuleKeyFromInstance(row)); } /** 审批实例关联的 fin_reimbursement.id */ export function resolveFinReimbursementIdFromInstance(row) { const raw = row?.businessId ?? row?.formPayload?.reimbursementId; if (raw == null || raw === "") return undefined; const n = Number(raw); return Number.isNaN(n) ? raw : n; } /** 拉取报销详情并映射为差旅/费用页面行(以接口 reimbursementType 为准) */ export async function loadReimburseDetailForInstance(instanceRow, moduleKey) { const mk = moduleKey || inferReimburseModuleKeyFromInstance(instanceRow); const id = resolveFinReimbursementIdFromInstance(instanceRow); if (id == null) { throw new Error("missing reimbursement id"); } const res = await getFinReimbursementDetail(id); const raw = unwrapFinReimbursementDetail(res); const reimburseRow = mapFinReimbursementDetailRow(raw, mk); const reimbursementType = resolveReimbursementType(raw, mk); const resolvedMk = getModuleKeyByReimbursementType(reimbursementType) || mk; return { reimburseRow, instanceRow, moduleKey: resolvedMk, reimbursementType, }; } export function stashReimburseEditFromApprove(moduleKey, reimbursementId) { sessionStorage.setItem( REIMBURSE_EDIT_FROM_APPROVE_KEY, JSON.stringify({ moduleKey, reimbursementId }) ); } export function consumeReimburseEditFromApprove() { const raw = sessionStorage.getItem(REIMBURSE_EDIT_FROM_APPROVE_KEY); if (!raw) return null; sessionStorage.removeItem(REIMBURSE_EDIT_FROM_APPROVE_KEY); try { return JSON.parse(raw); } catch { return null; } } /** 从已注册路由解析差旅/费用报销菜单 path(避免写死 path 导致 404) */ export function resolveReimburseManageRoutePath(router, moduleKey) { if (!router?.getRoutes) return ""; const needle = moduleKey === APPROVAL_MODULE_KEYS.TRAVEL_REIMBURSE ? "travel-reimburse" : moduleKey === APPROVAL_MODULE_KEYS.COST_REIMBURSE ? "cost-reimburse" : ""; if (!needle) return ""; const labelHint = moduleKey === APPROVAL_MODULE_KEYS.TRAVEL_REIMBURSE ? "差旅" : "费用"; const hit = router.getRoutes().find((r) => { const path = r.path || ""; if (path.includes(needle)) return true; const title = r.meta?.title || ""; return title.includes(labelHint) && title.includes("报销"); }); return hit?.path || ""; } export async function navigateToReimburseManageForEdit(router, moduleKey, reimbursementId) { stashReimburseEditFromApprove(moduleKey, reimbursementId); const path = resolveReimburseManageRoutePath(router, moduleKey); if (!path) { throw new Error("route not found"); } await router.push(path); }