import { matchBusinessTypeValue } from "./approvalTemplateType.js"; import { APPROVAL_MODULE_KEYS, getApprovalModuleConfig, } from "./approvalModuleRegistry.js"; import { fetchFinReimbursementListItemDetail } from "./finReimbursementMappers.js"; export const REIMBURSE_EDIT_FROM_APPROVE_KEY = "oa_reimburse_edit_from_approve"; export const FIN_REIMBURSE_FORM_ACTION_KEY = "oa_fin_reimburse_form_action"; 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)); } 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; } 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 reimburseRow = await fetchFinReimbursementListItemDetail( { reimbursementId: id }, mk ); return { reimburseRow, instanceRow, moduleKey: reimburseRow.moduleKey || mk, reimbursementType: reimburseRow.reimbursementType, }; } export function stashReimburseEditFromApprove(moduleKey, reimbursementId) { uni.setStorageSync( REIMBURSE_EDIT_FROM_APPROVE_KEY, JSON.stringify({ moduleKey, reimbursementId }) ); } export function consumeReimburseEditFromApprove() { const raw = uni.getStorageSync(REIMBURSE_EDIT_FROM_APPROVE_KEY); if (!raw) return null; uni.removeStorageSync(REIMBURSE_EDIT_FROM_APPROVE_KEY); try { return typeof raw === "string" ? JSON.parse(raw) : raw; } catch { return null; } } export function stashFinReimburseFormAction(payload) { uni.setStorageSync(FIN_REIMBURSE_FORM_ACTION_KEY, JSON.stringify(payload)); } export function consumeFinReimburseFormAction() { const raw = uni.getStorageSync(FIN_REIMBURSE_FORM_ACTION_KEY); if (!raw) return null; uni.removeStorageSync(FIN_REIMBURSE_FORM_ACTION_KEY); try { return typeof raw === "string" ? JSON.parse(raw) : raw; } catch { return null; } }