opc-finance-mcp 0.6.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -0
- package/package.json +1 -1
- package/src/index.js +243 -0
package/README.md
CHANGED
|
@@ -91,6 +91,15 @@ npm run inspect
|
|
|
91
91
|
| `finance_save_payroll` | 保存一期工资表 | 可写 |
|
|
92
92
|
| `finance_upload_attachment` | 上传本地文件到票据库 | 传票 |
|
|
93
93
|
| `finance_link_attachments` | 把票据挂到凭证(只加不减) | 可写 |
|
|
94
|
+
| `finance_list_invoices` | 发票清单(销项/进项、按期间与状态筛) | 只读 |
|
|
95
|
+
| `finance_list_bank_flows` | 银行/第三方流水,含流入流出小计 | 只读 |
|
|
96
|
+
| `finance_import_bank_flow` | 导入一批流水到核对清单(按指纹去重) | 可写 |
|
|
97
|
+
| `finance_suggest_voucher` | **起草凭证建议**:给发票或流水算出分录,**不写账** | 只读 |
|
|
98
|
+
|
|
99
|
+
> 0.7.0 起多了「读票、读流水、起草」四件工具。它们刻意停在建议那一步:
|
|
100
|
+
> AI 可以算出借贷,但过账仍然要走 `finance_create_voucher` 并由用户确认。
|
|
101
|
+
> 依据依次是「同一对手方的历史记法 → 摘要关键词 → 收入/费用兜底」,
|
|
102
|
+
> 每条建议都带 `reason`,把理由念给用户听比让用户猜更省事。
|
|
94
103
|
|
|
95
104
|
工资与个税的口径见 `finance_calc_payroll` 的描述,或直接看 Skill 的
|
|
96
105
|
`workflows/工资与个税.md` —— 个税算错不会让账不平,但年底汇算会爆。
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -1084,6 +1084,249 @@ tool('finance_save_tax_params', {
|
|
|
1084
1084
|
].join('\n'));
|
|
1085
1085
|
});
|
|
1086
1086
|
|
|
1087
|
+
/* ================================================================
|
|
1088
|
+
* 发票 / 流水 / 起草凭证(第 2 期)
|
|
1089
|
+
*
|
|
1090
|
+
* 这一组工具刻意停在「建议」那一步:AI 可以读票、读流水、算出借贷,
|
|
1091
|
+
* 但过账必须走 finance_create_voucher,且要用户确认。
|
|
1092
|
+
* 读接口与写接口分开,是为了让「先看清再动手」成为默认姿势。
|
|
1093
|
+
* ================================================================ */
|
|
1094
|
+
|
|
1095
|
+
const INVOICE_STATUS_CN = { new: '待处理', posted: '已入账', ignored: '已忽略' };
|
|
1096
|
+
|
|
1097
|
+
tool('finance_list_invoices', {
|
|
1098
|
+
title: '查询发票',
|
|
1099
|
+
description: '按期间 / 方向 / 状态查发票,返回销项进项的价税合计与税额小计。'
|
|
1100
|
+
+ '核对「账上收入 vs 开票额」、回答「这个季度开了多少票」都用它。',
|
|
1101
|
+
inputSchema: {
|
|
1102
|
+
from: z.string().optional().describe('起始期间 YYYY-MM'),
|
|
1103
|
+
to: z.string().optional().describe('结束期间 YYYY-MM'),
|
|
1104
|
+
direction: z.enum(['销项', '进项']).optional().describe('销项=我开出去的,进项=我收到的'),
|
|
1105
|
+
status: z.enum(['new', 'posted', 'ignored']).optional().describe('核对状态'),
|
|
1106
|
+
q: z.string().optional().describe('发票号 / 对方 / 品名关键字'),
|
|
1107
|
+
limit: z.number().int().min(1).max(200).optional().describe('最多返回多少张,默认 50'),
|
|
1108
|
+
},
|
|
1109
|
+
}, async (a) => {
|
|
1110
|
+
const d = await request('/api/invoices' + qs({ ...a, limit: a.limit || 50 }));
|
|
1111
|
+
if (!d.rows.length) return ok('没有符合条件的发票。');
|
|
1112
|
+
const s = d.summary;
|
|
1113
|
+
return ok([
|
|
1114
|
+
`# 发票(共 ${d.total} 张,显示 ${d.rows.length} 张)`,
|
|
1115
|
+
`销项价税合计 ${fmt(s.outIncl)}(税额 ${fmt(s.outTax)})|进项价税合计 ${fmt(s.inIncl)}(税额 ${fmt(s.inTax)})|待处理 ${s.pending} 张`,
|
|
1116
|
+
'',
|
|
1117
|
+
'| id | 开票日期 | 发票号 | 方向 | 对方 | 不含税 | 税额 | 价税合计 | 状态 |',
|
|
1118
|
+
'| --- | --- | --- | --- | --- | --- | --- | --- | --- |',
|
|
1119
|
+
...d.rows.map(r => `| ${r.id} | ${String(r.invoice_date || '').slice(0, 10)} | ${r.invoice_no} | ${r.direction} | `
|
|
1120
|
+
+ `${String((r.direction === '销项' ? r.buyer : r.seller) || '').replace(/\|/g, '/').slice(0, 24)} | `
|
|
1121
|
+
+ `${fmt(r.amount_excl_tax)} | ${fmt(r.tax)} | ${fmt(r.amount_incl_tax)} | ${INVOICE_STATUS_CN[r.status] || r.status} |`),
|
|
1122
|
+
'',
|
|
1123
|
+
'要把它变成凭证,用 finance_suggest_voucher 传 invoice_id 拿建议分录,确认后再 finance_create_voucher。',
|
|
1124
|
+
].join('\n'));
|
|
1125
|
+
});
|
|
1126
|
+
|
|
1127
|
+
tool('finance_list_bank_flows', {
|
|
1128
|
+
title: '查询银行流水',
|
|
1129
|
+
description: '按日期区间 / 账户 / 状态查流水,返回流入流出小计。'
|
|
1130
|
+
+ '对「这个月钱都去哪了」「哪几笔还没入账」这类问题,先看它再决定记哪几笔。',
|
|
1131
|
+
inputSchema: {
|
|
1132
|
+
from: z.string().optional().describe('起始日期 YYYY-MM-DD'),
|
|
1133
|
+
to: z.string().optional().describe('结束日期 YYYY-MM-DD'),
|
|
1134
|
+
account: z.string().optional().describe('账户名,如「交通银行2668」'),
|
|
1135
|
+
status: z.enum(['new', 'posted', 'ignored']).optional().describe('核对状态'),
|
|
1136
|
+
q: z.string().optional().describe('对方或摘要关键字'),
|
|
1137
|
+
limit: z.number().int().min(1).max(200).optional().describe('最多返回多少条,默认 50'),
|
|
1138
|
+
},
|
|
1139
|
+
}, async (a) => {
|
|
1140
|
+
const d = await request('/api/bank-flows' + qs({ ...a, limit: a.limit || 50 }));
|
|
1141
|
+
if (!d.rows.length) return ok('没有符合条件的流水。');
|
|
1142
|
+
const s = d.summary;
|
|
1143
|
+
return ok([
|
|
1144
|
+
`# 流水(共 ${d.total} 条,显示 ${d.rows.length} 条)`,
|
|
1145
|
+
`流入 ${fmt(s.inflow)}|流出 ${fmt(s.outflow)}|净额 ${fmt(s.net)}|待处理 ${s.pending} 条`,
|
|
1146
|
+
'',
|
|
1147
|
+
'| id | 日期 | 账户 | 方向 | 金额 | 对方 | 摘要 | 状态 |',
|
|
1148
|
+
'| --- | --- | --- | --- | --- | --- | --- | --- |',
|
|
1149
|
+
...d.rows.map(r => `| ${r.id} | ${String(r.txn_date || '').slice(0, 10)} | ${r.account} | ${r.direction} | `
|
|
1150
|
+
+ `${fmt(r.amount)} | ${String(r.counterparty || '').replace(/\|/g, '/').slice(0, 20)} | `
|
|
1151
|
+
+ `${String(r.memo || '').replace(/\|/g, '/').slice(0, 24)} | ${INVOICE_STATUS_CN[r.status] || r.status} |`),
|
|
1152
|
+
'',
|
|
1153
|
+
'下一步:finance_suggest_voucher 传 flow_ids 拿建议分录。',
|
|
1154
|
+
].join('\n'));
|
|
1155
|
+
});
|
|
1156
|
+
|
|
1157
|
+
tool('finance_import_bank_flow', {
|
|
1158
|
+
title: '导入银行流水',
|
|
1159
|
+
description: '把一批流水写进核对清单。⚠️ 会写库(但不是记账)。'
|
|
1160
|
+
+ '同一账户+日期+方向+金额+对方方视为同一条,重复导入会被跳过而不是翻倍。'
|
|
1161
|
+
+ '导入后还要用 finance_suggest_voucher 起草、finance_create_voucher 过账。',
|
|
1162
|
+
inputSchema: {
|
|
1163
|
+
rows: z.array(z.object({
|
|
1164
|
+
account: z.string().describe('账户名,如「交通银行2668」'),
|
|
1165
|
+
txn_date: z.string().describe('交易日期 YYYY-MM-DD'),
|
|
1166
|
+
direction: z.enum(['收', '支']).optional().describe('收支方向;不填按金额正负推断'),
|
|
1167
|
+
amount: z.number().describe('金额(正数;填负数会按方向推断)'),
|
|
1168
|
+
counterparty: z.string().optional().describe('对方户名'),
|
|
1169
|
+
memo: z.string().optional().describe('摘要 / 商品说明'),
|
|
1170
|
+
balance: z.number().optional().describe('账户余额'),
|
|
1171
|
+
})).min(1).max(500).describe('流水明细'),
|
|
1172
|
+
},
|
|
1173
|
+
}, async ({ rows }) => {
|
|
1174
|
+
const d = await request('/api/bank-flows', { method: 'POST', body: { rows } });
|
|
1175
|
+
const bad = d.errors || [];
|
|
1176
|
+
return ok([
|
|
1177
|
+
`✅ 导入完成:新增 ${d.inserted || 0} 条,跳过重复 ${d.skipped || 0} 条`
|
|
1178
|
+
+ (bad.length ? `,${bad.length} 条没通过校验` : '') + '。',
|
|
1179
|
+
...bad.slice(0, 5).map(e => ` · 第 ${e.row} 行:${e.msg}`),
|
|
1180
|
+
'',
|
|
1181
|
+
'接着可以对这批流水调 finance_suggest_voucher 看建议分录。',
|
|
1182
|
+
].join('\n'));
|
|
1183
|
+
});
|
|
1184
|
+
|
|
1185
|
+
tool('finance_suggest_voucher', {
|
|
1186
|
+
title: '起草凭证建议(不记账)',
|
|
1187
|
+
description: '给发票或流水算出建议分录,**只返回建议,不写账**。'
|
|
1188
|
+
+ '依据三档:同一对手方的历史记法 > 摘要关键词对应科目 > 收入/费用兜底。'
|
|
1189
|
+
+ '拿到结果后把分录念给用户确认,再用 finance_create_voucher 保存。',
|
|
1190
|
+
inputSchema: {
|
|
1191
|
+
flow_ids: z.array(z.number().int().positive()).max(20).optional()
|
|
1192
|
+
.describe('要起草的流水 id(来自 finance_list_bank_flows)'),
|
|
1193
|
+
invoice_id: z.number().int().positive().optional().describe('要起草的发票 id'),
|
|
1194
|
+
},
|
|
1195
|
+
}, async ({ flow_ids, invoice_id }) => {
|
|
1196
|
+
if (!flow_ids?.length && !invoice_id) return ok('请给 flow_ids 或 invoice_id。');
|
|
1197
|
+
|
|
1198
|
+
let body;
|
|
1199
|
+
if (invoice_id) {
|
|
1200
|
+
const inv = await request('/api/invoice/' + invoice_id).catch(() => null);
|
|
1201
|
+
if (!inv || !inv.id) return ok(`没找到 id=${invoice_id} 的发票,先用 finance_list_invoices 确认。`);
|
|
1202
|
+
body = { rows: [{ type: 'invoice', direction: inv.direction, amount_incl_tax: inv.amount_incl_tax,
|
|
1203
|
+
tax: inv.tax, is_red: inv.is_red, memo: inv.item_name, counterparty: inv.buyer }] };
|
|
1204
|
+
} else {
|
|
1205
|
+
body = { flow_ids };
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1208
|
+
const d = await request('/api/voucher/suggest', { method: 'POST', body });
|
|
1209
|
+
const list = d.suggestions || [];
|
|
1210
|
+
if (!list.length) return ok('没算出来。检查科目表是否为空。');
|
|
1211
|
+
|
|
1212
|
+
const out = ['# 建议分录(尚未入账)'];
|
|
1213
|
+
list.forEach((s, i) => {
|
|
1214
|
+
out.push('', `## 第 ${i + 1} 条${s.key ? `(来源 id=${s.key})` : ''}`, `依据:${s.reason}`);
|
|
1215
|
+
if (!s.entries.length) { out.push('没有可用科目,无法建议。'); return; }
|
|
1216
|
+
out.push('', '| 科目 | 名称 | 借方 | 贷方 |', '| --- | --- | --- | --- |',
|
|
1217
|
+
...s.entries.map(e => `| ${e.account_code} | ${e.account_name || ''} | `
|
|
1218
|
+
+ `${e.debit ? fmt(e.debit) : ''} | ${e.credit ? fmt(e.credit) : ''} |`));
|
|
1219
|
+
out.push(`合计:借 ${fmt(s.debit)}|贷 ${fmt(s.credit)}${s.balanced ? '(平)' : '(不平,需手工调整)'}`);
|
|
1220
|
+
});
|
|
1221
|
+
out.push('', '把上面每一行念给用户确认后,再用 finance_create_voucher 提交;不要跳过确认。');
|
|
1222
|
+
return ok(out.join('\n'));
|
|
1223
|
+
});
|
|
1224
|
+
|
|
1225
|
+
tool('finance_get_attachment_text', {
|
|
1226
|
+
title: '读票据原件的文字(PDF)',
|
|
1227
|
+
description: '把「票据资料」里的一个 PDF 转成纯文本,用来读票面字段。'
|
|
1228
|
+
+ '服务端只做 PDF→文本这一件确定性的事,认发票号/金额/日期交给模型判断。'
|
|
1229
|
+
+ '扫描件(没有文字层)会返回提示,这时让用户直接看原件。',
|
|
1230
|
+
inputSchema: {
|
|
1231
|
+
attach_id: z.number().int().positive().describe('附件 id(来自 finance_list_attachments)'),
|
|
1232
|
+
},
|
|
1233
|
+
}, async ({ attach_id }) => {
|
|
1234
|
+
const d = await request('/api/attach/' + attach_id + '/text');
|
|
1235
|
+
return ok([
|
|
1236
|
+
`# ${d.title || d.filename}(attach_id=${d.id})`,
|
|
1237
|
+
`字符数 ${d.chars}`,
|
|
1238
|
+
'',
|
|
1239
|
+
'```text',
|
|
1240
|
+
String(d.text || '').slice(0, 8000),
|
|
1241
|
+
'```',
|
|
1242
|
+
'',
|
|
1243
|
+
'把上面识别出的发票号、开票日期、购销方、不含税金额、税额交给 finance_import_invoices 入库;',
|
|
1244
|
+
'金额认不准就只传能确定的字段,不要编。',
|
|
1245
|
+
].join('\n'));
|
|
1246
|
+
});
|
|
1247
|
+
|
|
1248
|
+
tool('finance_import_invoices', {
|
|
1249
|
+
title: '导入发票(可批量)',
|
|
1250
|
+
description: '⚠️ 会写库(不是记账)。同一发票号默认覆盖,传 mode="skip" 则跳过重复。'
|
|
1251
|
+
+ '金额只认「价税合计 / 税额 / 不含税金额」三者中的任意一个以上,其余自动推算。',
|
|
1252
|
+
inputSchema: {
|
|
1253
|
+
direction: z.enum(['销项', '进项']).describe('整批方向:销项=我开出去,进项=我收到'),
|
|
1254
|
+
rows: z.array(z.object({
|
|
1255
|
+
invoice_no: z.string().describe('发票号码'),
|
|
1256
|
+
invoice_date: z.string().describe('开票日期 YYYY-MM-DD'),
|
|
1257
|
+
buyer: z.string().optional().describe('购方名称'),
|
|
1258
|
+
seller: z.string().optional().describe('销方名称'),
|
|
1259
|
+
item_name: z.string().optional().describe('货物或服务名称'),
|
|
1260
|
+
amount_excl_tax: z.number().optional().describe('不含税金额'),
|
|
1261
|
+
tax: z.number().optional().describe('税额'),
|
|
1262
|
+
amount_incl_tax: z.number().optional().describe('价税合计'),
|
|
1263
|
+
is_red: z.boolean().optional().describe('是否红字发票'),
|
|
1264
|
+
attach_id: z.number().int().optional().describe('关联的原件 attach_id'),
|
|
1265
|
+
})).min(1).max(200),
|
|
1266
|
+
mode: z.enum(['merge', 'skip']).optional().describe('发票号重复时覆盖还是跳过,默认覆盖'),
|
|
1267
|
+
},
|
|
1268
|
+
}, async ({ direction, rows, mode }) => {
|
|
1269
|
+
const d = await request('/api/invoices', { method: 'POST', body: { direction, rows, mode: mode || 'merge' } });
|
|
1270
|
+
const bad = d.errors || [];
|
|
1271
|
+
return ok([
|
|
1272
|
+
`✅ 导入完成:新增 ${d.inserted || 0},更新 ${d.updated || 0},跳过重复 ${d.skipped || 0}`
|
|
1273
|
+
+ (bad.length ? `,${bad.length} 行有问题` : '') + '。',
|
|
1274
|
+
...bad.slice(0, 5).map(e => ` · 第 ${e.row} 行:${e.msg}`),
|
|
1275
|
+
'',
|
|
1276
|
+
'要生成凭证:finance_suggest_voucher 传 invoice_id 取建议分录,确认后 finance_create_voucher。',
|
|
1277
|
+
].join('\n'));
|
|
1278
|
+
});
|
|
1279
|
+
|
|
1280
|
+
tool('finance_link_invoice_attachment', {
|
|
1281
|
+
title: '给发票挂原件',
|
|
1282
|
+
description: '把一张发票和「票据资料」里的一个文件关联起来,之后在页面上点「原件」就能看原票。'
|
|
1283
|
+
+ '只改发票上的指针,不影响文件与凭证的既有关系。',
|
|
1284
|
+
inputSchema: {
|
|
1285
|
+
invoice_id: z.number().int().positive(),
|
|
1286
|
+
attach_id: z.number().int().positive(),
|
|
1287
|
+
},
|
|
1288
|
+
}, async ({ invoice_id, attach_id }) => {
|
|
1289
|
+
await request('/api/invoice/' + invoice_id + '/attach', { method: 'POST', body: { attach_id } });
|
|
1290
|
+
return ok(`✅ 发票 ${invoice_id} 已挂上附件 ${attach_id}。`);
|
|
1291
|
+
});
|
|
1292
|
+
|
|
1293
|
+
tool('finance_reconcile_documents', {
|
|
1294
|
+
title: '自动对账(找已有凭证)',
|
|
1295
|
+
description: '给待处理的发票/流水找「可能已经记过的那张凭证」,只返回候选,不写库。'
|
|
1296
|
+
+ '「先按流水记了账、票后来才到」时用它,避免重复入账。'
|
|
1297
|
+
+ '拿到候选后要把几条念给用户,确认了再 finance_link_documents 关联。',
|
|
1298
|
+
inputSchema: {
|
|
1299
|
+
kinds: z.array(z.enum(['invoice', 'flow'])).optional().describe('默认两种都查'),
|
|
1300
|
+
days: z.number().int().min(0).max(60).optional().describe('日期容差天数,默认 15'),
|
|
1301
|
+
},
|
|
1302
|
+
}, async ({ kinds, days }) => {
|
|
1303
|
+
const d = await request('/api/reconcile', { method: 'POST', body: { kinds, days } });
|
|
1304
|
+
if (!d.items?.length) return ok(d.note || '没有找到能对上的凭证 —— 这些单据的账大概还没记。');
|
|
1305
|
+
const out = ['# 候选关联(尚未写入)', d.note || ''];
|
|
1306
|
+
d.items.slice(0, 20).forEach((it) => {
|
|
1307
|
+
out.push('', `## ${it.label}`, `单据:${it.kind} id=${it.id}`);
|
|
1308
|
+
it.candidates.forEach(c => out.push(
|
|
1309
|
+
`- 凭证 #${c.voucher_no}(id=${c.voucher_id},${c.voucher_date})${c.summary} — 得分 ${c.score},${c.reason}`));
|
|
1310
|
+
});
|
|
1311
|
+
out.push('', '确认后用 finance_link_documents 传 [{kind,id,voucher_id}]。');
|
|
1312
|
+
return ok(out.join('\n'));
|
|
1313
|
+
});
|
|
1314
|
+
|
|
1315
|
+
tool('finance_link_documents', {
|
|
1316
|
+
title: '把单据关联到已有凭证',
|
|
1317
|
+
description: '⚠️ 会写库(不是记账)。把发票/流水标成「已入账」并挂上凭证号,不新建凭证。',
|
|
1318
|
+
inputSchema: {
|
|
1319
|
+
items: z.array(z.object({
|
|
1320
|
+
kind: z.enum(['invoice', 'flow']),
|
|
1321
|
+
id: z.number().int().positive().describe('发票或流水的 id'),
|
|
1322
|
+
voucher_id: z.number().int().positive().describe('要关联的凭证 id'),
|
|
1323
|
+
})).min(1).max(100),
|
|
1324
|
+
},
|
|
1325
|
+
}, async ({ items }) => {
|
|
1326
|
+
const d = await request('/api/reconcile/apply', { method: 'POST', body: { items } });
|
|
1327
|
+
return ok(`✅ 已关联 ${d.linked} 条${d.skipped ? `,${d.skipped} 条跳过(凭证不存在)` : ''}。`);
|
|
1328
|
+
});
|
|
1329
|
+
|
|
1087
1330
|
/* ================================================================
|
|
1088
1331
|
* 启动
|
|
1089
1332
|
* ================================================================ */
|