From 7a8f2f693132f2272dd8b4ff1e35e30e0051f814 Mon Sep 17 00:00:00 2001 From: yunyingk Date: Sat, 5 Sep 2026 20:04:08 +0800 Subject: [PATCH] feat(bill): trace linked loan and requisition records --- skills/hose-use/SKILL.md | 2 + skills/hose-use/references/bill.md | 11 +++++ src/cli/commands/bill.ts | 36 ++++++++++++++- src/cli/printer.ts | 3 +- src/domains/bill/README.bill.md | 2 + src/domains/bill/bill.test.ts | 22 ++++++++- src/domains/bill/tools.ts | 71 ++++++++++++++++++++++++++++++ src/domains/bill/types.ts | 19 ++++++++ 8 files changed, 163 insertions(+), 3 deletions(-) diff --git a/skills/hose-use/SKILL.md b/skills/hose-use/SKILL.md index b6bac2e..27ecb7f 100644 --- a/skills/hose-use/SKILL.md +++ b/skills/hose-use/SKILL.md @@ -36,6 +36,7 @@ metadata: | 单据审批历史 / 当前节点 / 后续流程 | `hose bill flow <单号|ID>` | [references/bill.md](references/bill.md) | | 单据风控预警 / 预算状态 / 发票风险 | `hose bill risk <单号|ID>` | [references/bill.md](references/bill.md) | | 单据引用 ID 二级解析(人员/部门/费用类型) | `hose bill refs <单号|ID>` | [references/bill.md](references/bill.md) | +| 单据关联关系 / 冲借款与前置申请追溯 | `hose bill relations <单号|ID>` | [references/bill.md](references/bill.md) | | 单据 Web 详情直达链接生成 | `hose bill link <单号|ID>` | [references/bill.md](references/bill.md) | | 单据实体模型与字段元数据查询 | `hose bill schema` | [references/bill.md](references/bill.md) | | 企业通讯录检索 / 员工档案 / 防爆保护 | `hose contact search`, `hose contact list`, `hose contact get` | [references/contact.md](references/contact.md) | @@ -74,6 +75,7 @@ hose bill link CL20260901001 # 生成官方 Web 详情 hose bill list --json -o /tmp/bills.json # 导出全量单据报文至本地磁盘 hose bill get n0GMIM0dkQmKbS # 精准查询单据详情 (中文字段映射) hose bill refs S26000001 # 解析提交人、归属人、部门、法人实体与费用类型 +hose bill relations S26000001 # 追溯冲借款、事前申请和合同关联 hose bill schema # 查询单据实体元数据字典 # 4. 待办与消息 diff --git a/skills/hose-use/references/bill.md b/skills/hose-use/references/bill.md index 15c6f69..788fe50 100644 --- a/skills/hose-use/references/bill.md +++ b/skills/hose-use/references/bill.md @@ -72,6 +72,17 @@ hose bill refs S26000001 --json JSON 输出包含 `submitter`、`owner`、`department`、`legalEntity`、`feeTypes` 和 `unresolved`。目录在当前进程内按企业缓存 10 分钟,避免对同一企业重复拉取。 +### 单据关联链 (`bill relations`) + +从审批详情中的冲借款、前置申请、合同和费用关联字段抽取关系树: + +```bash +hose bill relations S26000001 +hose bill relations S26000001 --json +``` + +表格模式按树形缩进显示关联 ID、单号、标题和金额;JSON 模式保留每个节点的原始字段,便于继续下钻。 + ## 4. 单据实体元数据字典 (`bill schema`) 用于探查当前企业所有单据实体、字段类型与扩展属性定义: diff --git a/src/cli/commands/bill.ts b/src/cli/commands/bill.ts index 0b3dc66..e36a354 100644 --- a/src/cli/commands/bill.ts +++ b/src/cli/commands/bill.ts @@ -8,6 +8,7 @@ import { getBillFlow, getBillRisk, getBillReferences, + getBillRelations, getFieldDictionary, } from "../../domains/bill/tools.js"; @@ -297,6 +298,39 @@ export async function handleBillCommand( return { stdout: output, stderr: "", exitCode: 0 }; } + case "relations": + case "relation": + case "links": { + const target = parsed.args[0] || stringOption(parsed.options, ["code", "id"]); + if (!target) throw new Error("请提供要查询关联关系的单号或单据 ID: hose bill relations <单号|ID>"); + const report = await getBillRelations(client, corpId, target); + if (!report) { + if (runtime.jsonMode) return { stdout: printJson({ found: false, target }, runtime.outputOptions), stderr: "", exitCode: 1 }; + return { stdout: "", stderr: `[NOT FOUND] 未找到单据关联关系: ${target}\n`, exitCode: 1 }; + } + if (runtime.jsonMode) { + return { stdout: printJson({ corpId, corpName: corp.name, ...report }, runtime.outputOptions), stderr: "", exitCode: 0 }; + } + const rows: string[][] = []; + const visit = (node: typeof report.relations[number], depth = 0) => { + rows.push([ + `${" ".repeat(depth)}${node.relation}`, + node.code || node.id || "-", + node.title || "-", + node.amount || "-", + ]); + for (const child of node.children) visit(child, depth + 1); + }; + for (const node of report.relations) visit(node); + const output = [ + `====== 单据关联关系 (单号: ${report.billCode || target}) ======`, + `标题: ${report.title || "-"}`, + rows.length ? printTable(["关系", "关联 ID/单号", "标题/名称", "金额"], rows) : "暂无可识别的前置单据、冲借款或合同关联", + "", + ].join("\n"); + return { stdout: output, stderr: "", exitCode: 0 }; + } + case "link": case "url": { const target = parsed.args[0] || stringOption(parsed.options, ["code", "id", "c"]); @@ -359,6 +393,6 @@ export async function handleBillCommand( } default: - throw new Error(`未知 bill 子命令: ${action}。支持: list, get <单号>, refs <单号>, flow <单号>, risk <单号>, schema, link <单号>`); + throw new Error(`未知 bill 子命令: ${action}。支持: list, get <单号>, refs <单号>, relations <单号>, flow <单号>, risk <单号>, schema, link <单号>`); } } diff --git a/src/cli/printer.ts b/src/cli/printer.ts index 75ddc38..bc11792 100644 --- a/src/cli/printer.ts +++ b/src/cli/printer.ts @@ -119,7 +119,7 @@ export function printHelp(): string { corps 多企业租户管理 (list, switch, current) config 全局运行模式与路径诊断 (show, paths, mode , set) workflow 工作流管理 (list, get, find, toggle) - bill 单据与费用明细检索 (list, get <单号|ID>, refs <单号|ID>, flow <单号|ID>, risk <单号|ID>, schema, link <单号|ID>) + bill 单据与费用明细检索 (list, get <单号|ID>, refs <单号|ID>, relations <单号|ID>, flow <单号|ID>, risk <单号|ID>, schema, link <单号|ID>) contact 企业通讯录与人员检索 (list, search <关键词>, get ) task 待办中心与审批任务 (todo, approved, records, submitted, cc) message 消息中心与通知管理 (list, count, read, read-all) @@ -163,6 +163,7 @@ export function printHelp(): string { hose-use bill flow S26000001 # 查看单据审批历史与后续节点 hose-use bill risk S26000001 # 查看风控预警、预算与发票风险 hose-use bill refs S26000001 # 二级解析提交人、部门、法人实体与费用类型引用 + hose-use bill relations S26000001 # 追溯冲借款、事前申请和合同关联 hose-use bill link CL20260901001 # 获取单据 Web 详情直达免密跳转链接 hose-use bill list --json -o /tmp/bills.json # 导出单据报文到本地磁盘并防爆 hose-use bill get n0GMIM0dkQmKbS # 根据单号精准查询单据结构化详情 diff --git a/src/domains/bill/README.bill.md b/src/domains/bill/README.bill.md index ba9c0cd..7c1e21b 100644 --- a/src/domains/bill/README.bill.md +++ b/src/domains/bill/README.bill.md @@ -25,3 +25,5 @@ src/domains/bill/ 4. **关联引用二级解析 (`getBillReferences`)**: - 通过审批详情、组织架构和费用类型目录批量解析提交人、归属人、费用承担部门与费用类型; - 保留原始 ID,并在 JSON 中提供 `name`、`code`、部门 `path` 与未解析 ID;目录在进程内按企业缓存 10 分钟。 +5. **单据关联链 (`getBillRelations`)**: + - 从审批详情中的 `expenseLink`、`loan`、`requisition`、`contract` 等关系字段抽取前置单据树,保留原始对象供 JSON 下钻。 diff --git a/src/domains/bill/bill.test.ts b/src/domains/bill/bill.test.ts index 6740e48..3532a31 100644 --- a/src/domains/bill/bill.test.ts +++ b/src/domains/bill/bill.test.ts @@ -1,5 +1,5 @@ import { describe, it, expect } from "bun:test"; -import { formatSingleBill, getBillReferences } from "./tools.js"; +import { formatSingleBill, getBillReferences, getBillRelations } from "./tools.js"; import { RawBillItemSchema, EntitySchema } from "./types.js"; describe("Bill Domain", () => { @@ -103,4 +103,24 @@ describe("Bill Domain", () => { expect(result?.feeTypes[0]?.name).toBe("差旅费"); expect(result?.unresolved).toEqual([]); }); + + it("extracts linked loan and requisition nodes as a relation tree", async () => { + const client = { + post: async () => new Response(JSON.stringify({ value: { + id: "backlog-2", + flowId: { + id: "flow-2", + form: { + code: "E26000001", + title: "费用报销", + expenseLink: { id: "loan-1", code: "J26000001", title: "员工借款", loanMoney: { standard: "500" } }, + linkRequisitionInfo: { id: "req-1", code: "Q26000001", title: "出差申请" }, + }, + }, + } })), + } as any; + const result = await getBillRelations(client, "corp-relations", "E26000001"); + expect(result?.relations.map((item) => item.id)).toEqual(["loan-1", "req-1"]); + expect(result?.relations[0]?.amount).toContain("500"); + }); }); diff --git a/src/domains/bill/tools.ts b/src/domains/bill/tools.ts index 6189627..5914596 100644 --- a/src/domains/bill/tools.ts +++ b/src/domains/bill/tools.ts @@ -23,6 +23,8 @@ import { type BillRiskWarning, type BillReference, type BillReferenceReport, + type BillRelationNode, + type BillRelationsReport, } from "./types.js"; import { CorpContext } from "../../core/config/manager.js"; import { createEkbClient } from "../../core/auth/ekb.js"; @@ -266,6 +268,75 @@ export async function getBillReferences( }; } +function relationAmount(value: Record): string | undefined { + for (const key of ["amount", "money", "expenseMoney", "loanMoney", "requisitionMoney", "payMoney", "writtenOffAmount"]) { + const candidate = value[key]; + if (typeof candidate === "number" || typeof candidate === "string") return String(candidate); + if (candidate && typeof candidate === "object") { + const money = candidate as Record; + if (money.standard !== undefined) return `${money.standardSymbol || "¥"}${money.standard} ${money.standardStrCode || "CNY"}`.trim(); + } + } + return undefined; +} + +function relationNode(relation: string, value: unknown): BillRelationNode | undefined { + if (!value || typeof value !== "object") return undefined; + const object = value as Record; + const children: BillRelationNode[] = []; + for (const [key, nested] of Object.entries(object)) { + if (key === "id" || key === "code" || key === "title" || key === "name") continue; + if (Array.isArray(nested)) { + for (const item of nested) { + const arrayChild = relationNode(key, item); + if (arrayChild) children.push(arrayChild); + } + } else if (nested && typeof nested === "object") { + const child = relationNode(key, nested); + if (child) children.push(child); + } + } + const id = typeof object.id === "string" ? object.id : undefined; + const code = typeof object.code === "string" ? object.code : undefined; + const title = typeof object.title === "string" ? object.title : typeof object.name === "string" ? object.name : undefined; + if (!id && !code && !title && children.length === 0) return undefined; + return { relation, id, code, title, amount: relationAmount(object), children, raw: object }; +} + +export async function getBillRelations( + client: KyInstance, + corpId: string, + target: string, +): Promise { + const raw = await getRawBillFlow(client, corpId, target); + if (!raw || !raw.flowId || typeof raw.flowId !== "object") return undefined; + const flow = raw.flowId as Record; + const form = flow.form && typeof flow.form === "object" ? flow.form as Record : {}; + const relations: BillRelationNode[] = []; + const seen = new Set(); + const relationPattern = /(link|loan|requisition|contract|pre.?apply|advance)/i; + for (const [key, value] of Object.entries(form)) { + if (!relationPattern.test(key)) continue; + const values = Array.isArray(value) ? value : [value]; + for (const candidate of values) { + const node = relationNode(key, candidate); + if (!node) continue; + const dedupeKey = `${node.relation}:${node.id || node.code || node.title || JSON.stringify(node.raw)}`; + if (seen.has(dedupeKey)) continue; + seen.add(dedupeKey); + relations.push(node); + } + } + return { + target, + billId: typeof raw.id === "string" ? raw.id : undefined, + billCode: typeof form.code === "string" ? form.code : undefined, + title: typeof form.title === "string" ? form.title : undefined, + relations, + raw: { form }, + }; +} + // ── 格式化单据数据 ───────────────────────────────────────────────────────────── export function formatSingleBill( diff --git a/src/domains/bill/types.ts b/src/domains/bill/types.ts index 55145f7..0655bea 100644 --- a/src/domains/bill/types.ts +++ b/src/domains/bill/types.ts @@ -179,3 +179,22 @@ export interface BillReferenceReport { unresolved: string[]; raw: Record; } + +export interface BillRelationNode { + relation: string; + id?: string; + code?: string; + title?: string; + amount?: string; + children: BillRelationNode[]; + raw: Record; +} + +export interface BillRelationsReport { + target: string; + billId?: string; + billCode?: string; + title?: string; + relations: BillRelationNode[]; + raw: Record; +}