Merge pull request #23 from yunyingk/feat/issue-14-bill-relations
feat: trace linked bill relations
This commit is contained in:
8 files changed
+163
-3
No files matched your search
@@ -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. 待办与消息
|
||||
|
||||
@@ -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`)
|
||||
|
||||
用于探查当前企业所有单据实体、字段类型与扩展属性定义:
|
||||
|
||||
@@ -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 <单号>`);
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -119,7 +119,7 @@ export function printHelp(): string {
|
||||
corps 多企业租户管理 (list, switch, current)
|
||||
config 全局运行模式与路径诊断 (show, paths, mode <stateful|stateless>, 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 <staffId>)
|
||||
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 # 根据单号精准查询单据结构化详情
|
||||
|
||||
@@ -25,3 +25,5 @@ src/domains/bill/
|
||||
4. **关联引用二级解析 (`getBillReferences`)**:
|
||||
- 通过审批详情、组织架构和费用类型目录批量解析提交人、归属人、费用承担部门与费用类型;
|
||||
- 保留原始 ID,并在 JSON 中提供 `name`、`code`、部门 `path` 与未解析 ID;目录在进程内按企业缓存 10 分钟。
|
||||
5. **单据关联链 (`getBillRelations`)**:
|
||||
- 从审批详情中的 `expenseLink`、`loan`、`requisition`、`contract` 等关系字段抽取前置单据树,保留原始对象供 JSON 下钻。
|
||||
@@ -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");
|
||||
});
|
||||
});
|
||||
@@ -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, unknown>): 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<string, unknown>;
|
||||
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<string, unknown>;
|
||||
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<BillRelationsReport | undefined> {
|
||||
const raw = await getRawBillFlow(client, corpId, target);
|
||||
if (!raw || !raw.flowId || typeof raw.flowId !== "object") return undefined;
|
||||
const flow = raw.flowId as Record<string, unknown>;
|
||||
const form = flow.form && typeof flow.form === "object" ? flow.form as Record<string, unknown> : {};
|
||||
const relations: BillRelationNode[] = [];
|
||||
const seen = new Set<string>();
|
||||
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(
|
||||
|
||||
@@ -179,3 +179,22 @@ export interface BillReferenceReport {
|
||||
unresolved: string[];
|
||||
raw: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export interface BillRelationNode {
|
||||
relation: string;
|
||||
id?: string;
|
||||
code?: string;
|
||||
title?: string;
|
||||
amount?: string;
|
||||
children: BillRelationNode[];
|
||||
raw: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export interface BillRelationsReport {
|
||||
target: string;
|
||||
billId?: string;
|
||||
billCode?: string;
|
||||
title?: string;
|
||||
relations: BillRelationNode[];
|
||||
raw: Record<string, unknown>;
|
||||
}
|
||||
Reference in new issue
Block a user