feat(bill): fix code lookup and add draft validate/copy/delete
- fix: resolve bill by code/ID via flows/my + flowId detail, covering draft and paid bills - feat: draft validation engine (currency consistency, foreign rate, totals, draft status) - feat: bill validate, bill copy, bill delete commands - docs: write API contracts (freeflow.edit/delete/submit, plans/computed) and skill routing
This commit is contained in:
1 parent
4d921c09b3
commit
6628a546c5
13 files changed
+1274
-8
No files matched your search
@@ -0,0 +1,105 @@
|
||||
import { getRawBillFlow } from "../../../domains/bill/tools.js";
|
||||
import { buildBillCopyForm, copyBillAsDraft } from "../../../domains/bill/copy.js";
|
||||
import type { BillDraftForm } from "../../../domains/bill/draft-validation.js";
|
||||
import { isDryRun } from "../../mutation.js";
|
||||
import { booleanOption, stringOption } from "../../parser.js";
|
||||
import { printJson } from "../../printer.js";
|
||||
import type { CliResult } from "../../types.js";
|
||||
import type { BillActionContext } from "./context.js";
|
||||
|
||||
/**
|
||||
* `hose-use bill copy <单号|ID> [--title=...] [--drop-attachments] [--dry-run] --yes`
|
||||
*
|
||||
* 纯接口复制:读取服务端最新表单 → 清空发票绑定/重算金额 → 创建新草稿。
|
||||
* 只创建草稿,不提交。
|
||||
*/
|
||||
export async function handleCopy(ctx: BillActionContext): Promise<CliResult> {
|
||||
const { parsed, runtime, corpId } = ctx;
|
||||
const target = parsed.args[0] || stringOption(parsed.options, ["code", "id"]);
|
||||
if (!target) {
|
||||
throw new Error("请提供要复制的单号或 ID: hose-use bill copy <单号|ID> [--title=...] --yes");
|
||||
}
|
||||
|
||||
const title = stringOption(parsed.options, ["title"]);
|
||||
const keepAttachments = !booleanOption(parsed.options, ["drop-attachments", "dropAttachments"]);
|
||||
const client = await ctx.getClient();
|
||||
|
||||
if (isDryRun(parsed, runtime)) {
|
||||
const raw = await getRawBillFlow(client, corpId, target);
|
||||
const flow = raw && typeof raw.flowId === "object" ? (raw.flowId as Record<string, unknown>) : undefined;
|
||||
const form = flow && typeof flow.form === "object" ? (flow.form as BillDraftForm) : undefined;
|
||||
if (!form) {
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: `[NOT FOUND] 在企业下未找到单号或 ID 为 [${target}] 的单据,无法复制\n`,
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
const copy = buildBillCopyForm(form, { title, keepAttachments });
|
||||
if (runtime.jsonMode) {
|
||||
return {
|
||||
stdout: printJson(
|
||||
{
|
||||
dryRun: true,
|
||||
source: target,
|
||||
sourceTitle: form.title,
|
||||
title: copy.title,
|
||||
detailCount: copy.details?.length ?? 0,
|
||||
expenseMoney: copy.expenseMoney?.standard,
|
||||
keepAttachments,
|
||||
invoicesCleared: true,
|
||||
},
|
||||
runtime.outputOptions
|
||||
),
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
return {
|
||||
stdout:
|
||||
`====== 复制单据(dry-run,未创建) ======\n` +
|
||||
` 源单 : ${target}(${form.title ?? "-"})\n` +
|
||||
` 新标题 : ${copy.title ?? "-"}\n` +
|
||||
` 明细 : ${copy.details?.length ?? 0} 条\n` +
|
||||
` 金额 : ${copy.expenseMoney?.standard ?? "0"} CNY\n` +
|
||||
` 附件 : ${keepAttachments ? "保留" : "清空"}\n` +
|
||||
` 发票 : 清空绑定,发票总额归零\n` +
|
||||
`\n提示: 确认无误后追加 --yes 执行创建(仍为草稿,不会提交)\n`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
await ctx.requireWriteConfirmation();
|
||||
const result = await copyBillAsDraft(client, corpId, target, { title, keepAttachments });
|
||||
|
||||
if (runtime.jsonMode) {
|
||||
return {
|
||||
stdout: printJson(
|
||||
{
|
||||
source: target,
|
||||
flowId: result.flowId,
|
||||
code: result.code,
|
||||
title: result.title,
|
||||
detailCount: result.detailCount,
|
||||
expenseMoney: result.expenseMoney,
|
||||
submitted: false,
|
||||
},
|
||||
runtime.outputOptions
|
||||
),
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
stdout:
|
||||
`复制成功(已创建草稿,未提交)\n` +
|
||||
` 新单号 : ${result.code}\n` +
|
||||
` 单据 ID : ${result.flowId}\n` +
|
||||
` 标题 : ${result.title}\n` +
|
||||
` 明细 : ${result.detailCount} 条 / ${result.expenseMoney} CNY\n`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
import { getRawBillFlow } from "../../../domains/bill/tools.js";
|
||||
import { deleteBillDraft } from "../../../domains/bill/write.js";
|
||||
import { isDryRun } from "../../mutation.js";
|
||||
import { stringOption } from "../../parser.js";
|
||||
import { printJson } from "../../printer.js";
|
||||
import type { CliResult } from "../../types.js";
|
||||
import type { BillActionContext } from "./context.js";
|
||||
|
||||
/**
|
||||
* `hose-use bill delete <单号|ID> [--dry-run] --yes`
|
||||
*
|
||||
* 仅允许删除 `draft` 状态单据(`freeflow.delete`)。
|
||||
* 已提交/审批中/已支付的单据会被拒绝,避免误删真实业务单据。
|
||||
*/
|
||||
export async function handleDelete(ctx: BillActionContext): Promise<CliResult> {
|
||||
const { parsed, runtime, corpId } = ctx;
|
||||
const target = parsed.args[0] || stringOption(parsed.options, ["code", "id"]);
|
||||
if (!target) {
|
||||
throw new Error("请提供要删除的单号或 ID: hose-use bill delete <单号|ID> --yes");
|
||||
}
|
||||
|
||||
const client = await ctx.getClient();
|
||||
const raw = await getRawBillFlow(client, corpId, target);
|
||||
const flow = raw && typeof raw.flowId === "object" ? (raw.flowId as Record<string, unknown>) : undefined;
|
||||
const form = flow && typeof flow.form === "object" ? (flow.form as Record<string, unknown>) : undefined;
|
||||
|
||||
if (!flow || !form) {
|
||||
if (runtime.jsonMode) {
|
||||
return {
|
||||
stdout: printJson({ found: false, code: target, message: "未找到匹配单据" }),
|
||||
stderr: "",
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: `[NOT FOUND] 在企业下未找到单号或 ID 为 [${target}] 的单据\n`,
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
|
||||
const flowId = String(flow.id ?? "");
|
||||
const code = String(form.code ?? target);
|
||||
const state = String(flow.state ?? "");
|
||||
if (state !== "draft") {
|
||||
return {
|
||||
stdout: "",
|
||||
stderr:
|
||||
`[拒绝删除] 单据 ${code} 当前状态为 ${state || "unknown"},仅允许删除 draft 草稿。\n` +
|
||||
`已提交单据请走撤回/作废流程,避免误删真实业务数据。\n`,
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
|
||||
if (isDryRun(parsed, runtime)) {
|
||||
if (runtime.jsonMode) {
|
||||
return {
|
||||
stdout: printJson({ dryRun: true, code, flowId, state }, runtime.outputOptions),
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
return {
|
||||
stdout:
|
||||
`====== 删除单据(dry-run,未执行) ======\n` +
|
||||
` 单号 : ${code}\n` +
|
||||
` 流程 ID : ${flowId}\n` +
|
||||
` 状态 : ${state}\n` +
|
||||
`\n提示: 确认无误后追加 --yes 执行删除\n`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
await ctx.requireWriteConfirmation();
|
||||
await deleteBillDraft(client, corpId, flowId);
|
||||
|
||||
if (runtime.jsonMode) {
|
||||
return {
|
||||
stdout: printJson({ deleted: true, code, flowId }, runtime.outputOptions),
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
return { stdout: `删除成功\n 单号: ${code}\n 流程 ID: ${flowId}\n`, stderr: "", exitCode: 0 };
|
||||
}
|
||||
@@ -5,6 +5,9 @@ import { handleList, handleGet, handleTrace } from "./search.js";
|
||||
import { handleFlow, handleRisk, handleBudget } from "./flow.js";
|
||||
import { handleRefs, handleRelations, handleAttachments, handleDownload } from "./related.js";
|
||||
import { handleSchema, handleLink, handleSpec } from "./meta.js";
|
||||
import { handleValidate } from "./validate.js";
|
||||
import { handleCopy } from "./copy.js";
|
||||
import { handleDelete } from "./delete.js";
|
||||
|
||||
export async function handleBillCommand(
|
||||
parsed: CliParsedArgs,
|
||||
@@ -104,7 +107,22 @@ export async function handleBillCommand(
|
||||
case "specification":
|
||||
return handleSpec(ctx);
|
||||
|
||||
case "validate":
|
||||
case "check":
|
||||
case "lint":
|
||||
return handleValidate(ctx);
|
||||
|
||||
case "copy":
|
||||
case "duplicate":
|
||||
case "clone":
|
||||
return handleCopy(ctx);
|
||||
|
||||
case "delete":
|
||||
case "rm":
|
||||
case "remove":
|
||||
return handleDelete(ctx);
|
||||
|
||||
default:
|
||||
throw new Error(`未知 bill 子命令: ${action}。支持: list, get <单号> [--risk], trace <单号|ID>, spec <specificationId>, refs <单号>, relations <单号>, attachments <单号>, download <单号>, flow <单号>, risk <单号>, budget <单号>, schema, link <单号>`);
|
||||
throw new Error(`未知 bill 子命令: ${action}。支持: list, get <单号> [--risk], trace <单号|ID>, spec <specificationId>, refs <单号>, relations <单号>, attachments <单号>, download <单号>, flow <单号>, risk <单号>, budget <单号>, schema, link <单号>, validate <单号>, copy <单号> --yes, delete <单号> --yes`);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
import { getRawBillFlow } from "../../../domains/bill/tools.js";
|
||||
import {
|
||||
formatValidationIssues,
|
||||
validateBillDraft,
|
||||
type BillDraftForm,
|
||||
} from "../../../domains/bill/draft-validation.js";
|
||||
import { stringOption } from "../../parser.js";
|
||||
import { printJson } from "../../printer.js";
|
||||
import type { CliResult } from "../../types.js";
|
||||
import type { BillActionContext } from "./context.js";
|
||||
|
||||
/**
|
||||
* `hose-use bill validate <单号|ID>`
|
||||
*
|
||||
* 读取单据的原始表单,校验派生字段(金额/币种/发票总额/明细草稿状态)是否自洽。
|
||||
* 这是写入前的最后一道闸门:合思服务端只做弱校验,币种混加、发票总额不同步都能存进去。
|
||||
*/
|
||||
export async function handleValidate(ctx: BillActionContext): Promise<CliResult> {
|
||||
const { parsed, runtime, corp, corpId } = ctx;
|
||||
const target = parsed.args[0] || stringOption(parsed.options, ["code", "id"]);
|
||||
if (!target) {
|
||||
throw new Error("请提供要校验的单号或 ID: hose-use bill validate <单号|ID>");
|
||||
}
|
||||
|
||||
const raw = await getRawBillFlow(await ctx.getClient(), corpId, target);
|
||||
const flow = raw && typeof raw.flowId === "object" ? (raw.flowId as Record<string, unknown>) : undefined;
|
||||
const form = flow && typeof flow.form === "object" ? (flow.form as BillDraftForm) : undefined;
|
||||
|
||||
if (!flow || !form) {
|
||||
if (runtime.jsonMode) {
|
||||
return {
|
||||
stdout: printJson({ found: false, code: target, message: "未找到匹配单据" }),
|
||||
stderr: "",
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: `[NOT FOUND] 在企业 [${corp.name}] 下未找到单号或 ID 为 [${target}] 的单据\n`,
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
|
||||
const issues = validateBillDraft(form);
|
||||
const errors = issues.filter((item) => item.severity === "error");
|
||||
const warnings = issues.filter((item) => item.severity === "warning");
|
||||
const billCode = typeof form.title === "string" ? form.title : target;
|
||||
const code = typeof (form as Record<string, unknown>).code === "string"
|
||||
? String((form as Record<string, unknown>).code)
|
||||
: target;
|
||||
|
||||
if (runtime.jsonMode) {
|
||||
return {
|
||||
stdout: printJson(
|
||||
{
|
||||
found: true,
|
||||
corpId,
|
||||
code,
|
||||
title: billCode,
|
||||
ok: errors.length === 0,
|
||||
errorCount: errors.length,
|
||||
warningCount: warnings.length,
|
||||
issues,
|
||||
},
|
||||
runtime.outputOptions
|
||||
),
|
||||
stderr: "",
|
||||
exitCode: errors.length === 0 ? 0 : 1,
|
||||
};
|
||||
}
|
||||
|
||||
const header =
|
||||
`====== 单据校验 (${code}) ======\n` +
|
||||
` 企业 : ${corp.name}\n` +
|
||||
` 标题 : ${billCode}\n` +
|
||||
` 结论 : ${errors.length === 0 ? "✅ 通过" : "❌ 未通过"}(错误 ${errors.length} / 警告 ${warnings.length})\n`;
|
||||
const body = issues.length > 0 ? formatValidationIssues(issues) : " ✅ 派生字段与必填项一致";
|
||||
return {
|
||||
stdout: `${header}\n${body}\n`,
|
||||
stderr: "",
|
||||
exitCode: errors.length === 0 ? 0 : 1,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user