opc-finance-mcp 0.6.0 → 0.7.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/package.json +1 -1
- package/src/index.js +138 -0
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -1084,6 +1084,144 @@ 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
|
+
|
|
1087
1225
|
/* ================================================================
|
|
1088
1226
|
* 启动
|
|
1089
1227
|
* ================================================================ */
|