feat(bill): add pure-API create and submit commands
- bill create: 按模板创建草稿(最小表单 + 一条费用明细,版本化 specificationId) - bill submit: plans/computed 计算审批计划 → freeflow.submit(未指派节点需显式 --approver) - 文档与 Skill 路由同步
This commit is contained in:
1 parent
1de9e3f552
commit
3c00a0c0a6
9 files changed
+920
-442
No files matched your search
@@ -0,0 +1,192 @@
|
||||
import type { KyInstance } from "ky";
|
||||
import { NotFoundError, ValidationError } from "../../core/errors.js";
|
||||
import { getDepartmentTree, getFeeTypeTree, getFormSpecification } from "./api.js";
|
||||
import { listBillTemplates } from "./templates.js";
|
||||
|
||||
/**
|
||||
* 纯接口创建单据草稿。
|
||||
*
|
||||
* 模板决定必填字段;这里构造最小可用表单:
|
||||
* - 表头:specificationId / title / submitterId / expenseDate / expenseDepartment
|
||||
* - 明细:选中一个费用类型(默认「简单费用」或第一个可用类型),金额可配置
|
||||
*
|
||||
* 创建后仍为 draft,不会提交。
|
||||
*/
|
||||
|
||||
export interface CreateBillOptions {
|
||||
/** 模板 ID(bill templates 输出的 id) */
|
||||
templateId: string;
|
||||
title?: string;
|
||||
/** 本位币金额,默认 0.01 */
|
||||
amount?: string;
|
||||
reason?: string;
|
||||
/** 指定费用类型名称,默认「简单费用」,找不到则取第一个可用类型 */
|
||||
feeTypeName?: string;
|
||||
submitterId?: string;
|
||||
expenseDepartment?: string;
|
||||
expenseDate?: number;
|
||||
}
|
||||
|
||||
export interface CreateBillResult {
|
||||
flowId: string;
|
||||
code: string;
|
||||
title: string;
|
||||
amount: string;
|
||||
feeType: string;
|
||||
}
|
||||
|
||||
interface FeeTypeNode {
|
||||
id?: string;
|
||||
name?: string;
|
||||
active?: boolean;
|
||||
expenseSpecificationId?: string;
|
||||
children?: FeeTypeNode[];
|
||||
}
|
||||
|
||||
function flattenFeeTypes(nodes: FeeTypeNode[]): FeeTypeNode[] {
|
||||
const out: FeeTypeNode[] = [];
|
||||
const walk = (list: FeeTypeNode[]) => {
|
||||
for (const node of list) {
|
||||
out.push(node);
|
||||
if (Array.isArray(node.children)) walk(node.children);
|
||||
}
|
||||
};
|
||||
walk(nodes);
|
||||
return out;
|
||||
}
|
||||
|
||||
function zeroMoney(): Record<string, unknown> {
|
||||
return {
|
||||
standard: "0",
|
||||
standardUnit: "元",
|
||||
standardScale: 2,
|
||||
standardSymbol: "¥",
|
||||
standardNumCode: "156",
|
||||
standardStrCode: "CNY",
|
||||
};
|
||||
}
|
||||
|
||||
export async function createBillDraft(
|
||||
client: KyInstance,
|
||||
corpId: string,
|
||||
options: CreateBillOptions
|
||||
): Promise<CreateBillResult> {
|
||||
const templateId = options.templateId.trim();
|
||||
if (!templateId) throw new ValidationError("请通过 --template 指定单据模板 ID");
|
||||
|
||||
const specification = await getFormSpecification(client, corpId, templateId);
|
||||
if (!specification) {
|
||||
throw new NotFoundError(`未找到单据模板: ${templateId}`);
|
||||
}
|
||||
|
||||
// 创建接口要求版本化 specificationId(`<originalId>:<hash>`);
|
||||
// 用户传入的通常是 bill templates 输出的原始 ID。
|
||||
let versionedSpecificationId = templateId;
|
||||
if (!/:[0-9a-f]{32,64}$/i.test(templateId)) {
|
||||
const templates = await listBillTemplates(client, corpId);
|
||||
const hit = templates.groups
|
||||
.flatMap((group) => group.specifications)
|
||||
.find((item) => item.id === templateId || item.name === templateId);
|
||||
if (!hit) throw new NotFoundError(`未找到单据模板: ${templateId}`);
|
||||
versionedSpecificationId = hit.versionedId;
|
||||
}
|
||||
|
||||
const meResponse = await client.get("api/v1/organization/staffs/me", {
|
||||
searchParams: { corpId },
|
||||
});
|
||||
const meJson = (await meResponse.json()) as Record<string, any>;
|
||||
const staff = meJson.value?.staff ?? meJson;
|
||||
const submitterId = options.submitterId || staff.id;
|
||||
const corpDeptCandidate = (value: unknown): string | undefined => {
|
||||
if (typeof value === "string" && value && value !== corpId) return value;
|
||||
if (value && typeof value === "object" && typeof (value as any).id === "string") {
|
||||
const id = (value as any).id as string;
|
||||
return id === corpId ? undefined : id;
|
||||
}
|
||||
return undefined;
|
||||
};
|
||||
let expenseDepartment =
|
||||
options.expenseDepartment ||
|
||||
corpDeptCandidate(staff.defaultDepartment) ||
|
||||
corpDeptCandidate(staff.defaultDepartmentId) ||
|
||||
corpDeptCandidate(Array.isArray(staff.departments) ? staff.departments[0] : undefined);
|
||||
if (!expenseDepartment) {
|
||||
// OpenAPI/协助身份可能只返回企业 ID 作为部门;回退到部门树第一个有效节点。
|
||||
const tree = (await getDepartmentTree(client, corpId)) as Array<Record<string, any>>;
|
||||
const flat: Array<Record<string, any>> = [];
|
||||
const walk = (nodes: Array<Record<string, any>>) => {
|
||||
for (const node of nodes) {
|
||||
flat.push(node);
|
||||
if (Array.isArray(node.children)) walk(node.children);
|
||||
}
|
||||
};
|
||||
walk(tree);
|
||||
expenseDepartment = corpDeptCandidate(flat.find((node) => corpDeptCandidate(node.id))?.id);
|
||||
}
|
||||
if (!submitterId || !expenseDepartment) {
|
||||
throw new ValidationError("无法确定提交人或报销部门,请通过 --submitter / --department 指定");
|
||||
}
|
||||
|
||||
const feeTypes = flattenFeeTypes((await getFeeTypeTree(client, corpId)) as FeeTypeNode[]);
|
||||
const preferred = options.feeTypeName?.trim();
|
||||
const feeType =
|
||||
(preferred
|
||||
? feeTypes.find((item) => item.name === preferred && item.expenseSpecificationId)
|
||||
: undefined) ||
|
||||
feeTypes.find((item) => item.name === "简单费用" && item.expenseSpecificationId) ||
|
||||
feeTypes.find((item) => item.active !== false && item.expenseSpecificationId);
|
||||
if (!feeType?.id || !feeType.expenseSpecificationId) {
|
||||
throw new NotFoundError("未找到可用的费用类型(缺少 expenseSpecificationId)");
|
||||
}
|
||||
|
||||
const amount = options.amount?.trim() || "0.01";
|
||||
if (!Number.isFinite(Number(amount)) || Number(amount) <= 0) {
|
||||
throw new ValidationError(`金额不合法: ${amount}`);
|
||||
}
|
||||
const expenseDate = options.expenseDate ?? Date.now();
|
||||
const title = options.title?.trim() || specification.name;
|
||||
|
||||
const detail = {
|
||||
feeTypeId: feeType.id,
|
||||
specificationId: feeType.expenseSpecificationId,
|
||||
feeTypeForm: {
|
||||
amount: {
|
||||
standard: amount,
|
||||
standardUnit: "元",
|
||||
standardScale: 2,
|
||||
standardSymbol: "¥",
|
||||
standardNumCode: "156",
|
||||
standardStrCode: "CNY",
|
||||
},
|
||||
feeDate: expenseDate,
|
||||
},
|
||||
};
|
||||
|
||||
const form = {
|
||||
specificationId: versionedSpecificationId,
|
||||
title,
|
||||
submitterId,
|
||||
expenseDate,
|
||||
expenseDepartment,
|
||||
...(options.reason?.trim() ? { description: options.reason.trim() } : {}),
|
||||
details: [detail],
|
||||
};
|
||||
|
||||
const response = await client.post("api/flow/v1/flows", {
|
||||
searchParams: { corpId },
|
||||
json: { name: "freeflow.edit", form },
|
||||
});
|
||||
const json = (await response.json()) as Record<string, any>;
|
||||
const value =
|
||||
(json.value && typeof json.value === "object" ? json.value : undefined) ??
|
||||
json.flow ??
|
||||
json;
|
||||
|
||||
return {
|
||||
flowId: String(value?.id ?? ""),
|
||||
code: String(value?.form?.code ?? ""),
|
||||
title,
|
||||
amount,
|
||||
feeType: feeType.name ?? "",
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user