yyb
4 小时以前 df5efb2ca2b0cf74d9160ffe2b6c215c4ddc9c99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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);
}