import { createBillDraft, prepareBillDraft } from "../../../domains/bill/create.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 create --template <模板ID> [--title=...] [--amount=0.01] [--reason=...] [--dry-run] --yes` * * 纯接口创建草稿:最小表头 + 一条费用明细,创建后不提交。 */ export async function handleCreate(ctx: BillActionContext): Promise { const { parsed, runtime, corpId } = ctx; const templateId = stringOption(parsed.options, ["template", "spec", "specification"]); if (!templateId) { throw new Error("请通过 --template 指定单据模板 ID: hose-use bill create --template --yes"); } const title = stringOption(parsed.options, ["title"]); const amount = stringOption(parsed.options, ["amount"]) ?? "0.01"; const reason = stringOption(parsed.options, ["reason"]); const feeTypeName = stringOption(parsed.options, ["fee-type", "feeType"]); if (isDryRun(parsed, runtime)) { const prepared = await prepareBillDraft(await ctx.getClient(), corpId, { templateId, title, amount, reason, feeTypeName }); const preview = { template: templateId, title: prepared.title, amount: prepared.amount, reason: reason ?? "", feeType: prepared.feeType }; if (runtime.jsonMode) { return { stdout: printJson({ dryRun: true, ...preview }, runtime.outputOptions), stderr: "", exitCode: 0 }; } return { stdout: `====== 创建单据(dry-run,未创建) ======\n` + ` 模板 : ${preview.template}\n` + ` 标题 : ${preview.title}\n` + ` 金额 : ${preview.amount} CNY\n` + ` 费用类型: ${preview.feeType}\n` + ` 事由 : ${preview.reason || "-"}\n` + `\n提示: 追加 --yes 执行创建(仍为草稿,不会提交)\n`, stderr: "", exitCode: 0, }; } await ctx.requireWriteConfirmation(); const result = await createBillDraft(await ctx.getClient(), corpId, { templateId, title, amount, reason, feeTypeName, }); if (runtime.jsonMode) { return { stdout: printJson({ ...result, submitted: false }, runtime.outputOptions), stderr: "", exitCode: 0 }; } return { stdout: `创建成功(草稿,未提交)\n` + ` 单号 : ${result.code}\n` + ` 流程 ID : ${result.flowId}\n` + ` 标题 : ${result.title}\n` + ` 金额 : ${result.amount} CNY(${result.feeType})\n`, stderr: "", exitCode: 0, }; }