ownledger 2.2.0 → 2.3.1

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/AGENTS.md CHANGED
@@ -126,7 +126,9 @@ Propose the fixes to the owner as **one batch** (the connector's `batch` tool: o
126
126
  cover a gap**: find the real rows. If the closing figure isn't found or rows look wrong, add the layout fields to
127
127
  `sources.yaml` (above) rather than working around it. `--save-bank-amounts` saves each month's bank figure in
128
128
  `data/cards/statements.yaml` (preview; `--yes` after the owner's go) so `cards` / `card_statement` show the gap
129
- without the PDFs.
129
+ without the PDFs. When a bank prints its closing figure without a label (reconcile says "no closing figure found"),
130
+ read the figures off the PDFs and save them as `statement_amount` ops in the same `batch` (`account`,
131
+ `statement_date` = the close date, `amount`); reconcile then compares those months too.
130
132
 
131
133
  `npx ownledger statements check <account-id> <YYYY-MM>` shows one statement: ownledger's figures and transactions for
132
134
  the period, the rows read from the PDF, and its text with columns kept.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # Changelog
2
2
 
3
+ ## 2.3.1 (2026-11-23)
4
+
5
+ - **Fix: editing only the amount of a 0-amount row** booked it as income (the kind came from the sign of 0), so the
6
+ account went up instead of down. The kind now comes from the category's side (income or expense; expense for system
7
+ categories), and a 0 transfer keeps its lines' order.
8
+ - **Fix: `statements reconcile` read a statement's period line as a row** ("21/06/2025 - 20/07/2025 100,000", the
9
+ dates and the minimum due on MSB), giving a false "missing" row each month, sometimes matched to another account.
10
+ Lines holding a date range are skipped.
11
+
12
+ ## 2.3.0 (2026-11-23)
13
+
14
+ - **`batch` saves bank statement amounts too:** op `statement_amount` (`account`, `statement_date`, `amount`; 0
15
+ allowed), the same as `add_statement_amount`, so a whole statement check (the transaction fixes and every month's bank
16
+ figure) is one preview and one commit. For banks whose PDFs print the closing figure without a label.
17
+
3
18
  ## 2.2.0 (2026-11-23)
4
19
 
5
20
  Batch writes, and checking every statement of a card in one run (plan 005). Data format stays 1.
@@ -55,6 +55,14 @@ export type BatchOp = ({
55
55
  op: "void";
56
56
  id: string;
57
57
  reason?: string;
58
+ }
59
+ /** The bank's amount for a card statement (data/cards/statements.yaml), like add_statement_amount. */
60
+ | {
61
+ op: "statement_amount";
62
+ account: string;
63
+ statement_date: string;
64
+ amount: number;
65
+ reason?: string;
58
66
  };
59
67
  export declare const BATCH_MAX = 200;
60
68
  export interface BatchPlanned extends Planned {
@@ -64,6 +72,7 @@ export interface BatchPlanned extends Planned {
64
72
  add: number;
65
73
  edit: number;
66
74
  void: number;
75
+ statement_amount: number;
67
76
  };
68
77
  }
69
78
  /**
@@ -4,6 +4,7 @@ import { flattenCategories } from "./categories.js";
4
4
  import { isDate } from "./dates.js";
5
5
  import { LEDGER_FILE_RE, loadLedger, parseJsonl, PATHS } from "./load.js";
6
6
  import { applyChange, describeChange, yamlText } from "./write.js";
7
+ import { STATEMENTS_FILE, withBankAmounts } from "../modules/cards/terms.js";
7
8
  export class ActionError extends Error {
8
9
  }
9
10
  function newId(prefix = "t_") {
@@ -109,28 +110,43 @@ function buildEdit(files, id, patch, meta) {
109
110
  next[k] = patch[k];
110
111
  }
111
112
  if (patch.lines || patch.amount || patch.kind || patch.account || patch.category || patch.to_account) {
112
- next.lines = linesOf({ ...simpleOf(old), ...patch });
113
+ next.lines = linesOf({ ...simpleOf(old, files), ...patch });
113
114
  }
114
115
  if (!isDate(next.date))
115
116
  throw new ActionError("date must be yyyy-mm-dd");
116
117
  const done = trailed(next, "edit", meta);
117
118
  return { change: { replaceTransactions: [done] }, message: withMeta(`Edit: ${next.date} ${next.note ?? ""}`.trim(), meta), tx: done, old };
118
119
  }
119
- /** Best-effort simple view of a transaction (to let an edit change just one field). */
120
- function simpleOf(t) {
120
+ /**
121
+ * Best-effort simple view of a transaction (to let an edit change just one field).
122
+ * A 0 amount has no sign, so the kind comes from the category's side (income/expense),
123
+ * and a 0 transfer keeps its lines' order (first line is the source).
124
+ */
125
+ function simpleOf(t, files) {
121
126
  const acc = t.lines.filter((l) => l.account);
122
127
  const cat = t.lines.filter((l) => l.category);
123
128
  if (t.lines.length === 2 && acc.length === 1 && cat.length === 1) {
124
129
  const a = acc[0];
125
- return { kind: a.amount < 0 ? "expense" : "income", amount: Math.abs(a.amount), account: a.account, category: cat[0].category };
130
+ const category = cat[0].category;
131
+ const kind = a.amount < 0 ? "expense" : a.amount > 0 ? "income" : categoryKindOf(files, category);
132
+ return { kind, amount: Math.abs(a.amount), account: a.account, category };
126
133
  }
127
134
  if (t.lines.length === 2 && acc.length === 2) {
128
- const from = acc.find((l) => l.amount < 0);
129
- const to = acc.find((l) => l.amount > 0);
130
- return { kind: "transfer", amount: to.amount, account: from.account, to_account: to.account };
135
+ const from = acc.find((l) => l.amount < 0) ?? acc[0];
136
+ const to = acc.find((l) => l !== from);
137
+ return { kind: "transfer", amount: Math.abs(to.amount), account: from.account, to_account: to.account };
131
138
  }
132
139
  return { lines: t.lines };
133
140
  }
141
+ /** A category's side from data/categories.yaml; expense when unknown (system categories, a missing file). */
142
+ function categoryKindOf(files, path) {
143
+ try {
144
+ return flattenCategories(readCats(files)).map.get(path)?.kind === "income" ? "income" : "expense";
145
+ }
146
+ catch {
147
+ return "expense";
148
+ }
149
+ }
134
150
  export function voidTransaction(files, id, reason, meta) {
135
151
  const b = buildVoid(files, id, reason, meta);
136
152
  return plan(files, b.change, b.message);
@@ -164,10 +180,21 @@ export function batch(files, ops, meta) {
164
180
  const changed = new Set();
165
181
  const finals = new Map(); // id -> final version
166
182
  const originals = new Map(); // id -> version before the batch (null: added here)
167
- const counts = { add: 0, edit: 0, void: 0 };
183
+ const counts = { add: 0, edit: 0, void: 0, statement_amount: 0 };
184
+ const amounts = [];
168
185
  ops.forEach((o, i) => {
169
186
  const m = meta ? { ...meta, reason: joinReason(meta.reason, o.reason) } : undefined;
170
187
  let b;
188
+ if (o.op === "statement_amount") {
189
+ if (!isDate(o.statement_date) || !Number.isInteger(o.amount))
190
+ throw new ActionError(`batch refused: op ${i + 1} (statement_amount) needs statement_date yyyy-mm-dd and a whole amount`);
191
+ const k = amounts.findIndex((x) => x.account === o.account && x.statement_date === o.statement_date);
192
+ if (k >= 0)
193
+ amounts.splice(k, 1);
194
+ amounts.push({ account: o.account, statement_date: o.statement_date, amount: o.amount });
195
+ counts.statement_amount++;
196
+ return;
197
+ }
171
198
  try {
172
199
  if (o.op === "add") {
173
200
  const { op: _op, reason: _r, ...t } = o;
@@ -195,6 +222,13 @@ export function batch(files, ops, meta) {
195
222
  changed.add(p);
196
223
  cur = { ...cur, ...out };
197
224
  });
225
+ if (amounts.length) {
226
+ const unknown = amounts.find((x) => !loadLedger(cur).ledger.accounts.some((a) => a.id === x.account));
227
+ if (unknown)
228
+ throw new ActionError(`batch refused: statement_amount: no account ${unknown.account}`);
229
+ cur = { ...cur, [STATEMENTS_FILE]: yamlText(withBankAmounts(loadLedger(cur).ledger, amounts)) };
230
+ changed.add(STATEMENTS_FILE);
231
+ }
198
232
  const r = checkFiles(cur);
199
233
  if (!r.ok)
200
234
  throw new ActionError(`batch refused, the result would not pass check:\n${r.errors.slice(0, 10).map((e) => `- ${e.where ? `[${e.where}] ` : ""}${e.message}`).join("\n")}`);
@@ -222,12 +256,12 @@ export function batch(files, ops, meta) {
222
256
  for (const [k, v] of Object.entries(balanceChange))
223
257
  if (v === 0)
224
258
  delete balanceChange[k];
225
- const parts = [counts.add && `${counts.add} add${counts.add > 1 ? "s" : ""}`, counts.edit && `${counts.edit} edit${counts.edit > 1 ? "s" : ""}`, counts.void && `${counts.void} void${counts.void > 1 ? "s" : ""}`].filter(Boolean);
259
+ const parts = [counts.add && `${counts.add} add${counts.add > 1 ? "s" : ""}`, counts.edit && `${counts.edit} edit${counts.edit > 1 ? "s" : ""}`, counts.void && `${counts.void} void${counts.void > 1 ? "s" : ""}`, counts.statement_amount && `${counts.statement_amount} bank amount${counts.statement_amount > 1 ? "s" : ""}`].filter(Boolean);
226
260
  const change = { ...(add.length ? { addTransactions: add } : {}), ...(rep.length ? { replaceTransactions: rep } : {}) };
227
261
  return {
228
262
  change,
229
263
  files: out,
230
- summary: [`batch: ${parts.join(", ")}`, ...describeChange(change)],
264
+ summary: [`batch: ${parts.join(", ")}`, ...describeChange(change), ...amounts.map((x) => `bank amount ${x.account} ${x.statement_date}: ${x.amount}`)],
231
265
  message: withMeta(`Batch: ${parts.join(", ")}`, meta),
232
266
  warnings: r.warnings.filter((w) => !before.has(`${w.file}|${w.where}|${w.message}`)),
233
267
  balanceChange,
@@ -124,6 +124,8 @@ export function amountOf(text) {
124
124
  }
125
125
  /** A cell that starts with a date. */
126
126
  const STARTS_WITH_DATE = /^(\d{1,2}[/.-]\d{1,2}([/.-](\d{4}|\d{2}))?|\d{4}[/.-]\d{1,2}[/.-]\d{1,2})(?![\d])/;
127
+ /** A period line ("21/06/2025 - 20/07/2025", "01/06/2025 đến 30/06/2025"): a statement's header, never a row. */
128
+ const DATE_RANGE_RE = /\d{1,2}[/.]\d{1,2}[/.]\d{2,4}\s*(?:-|–|~|to|den)\s*\d{1,2}[/.]\d{1,2}[/.]\d{2,4}/;
127
129
  const hasWord = (text, words) => {
128
130
  const p = plain(text);
129
131
  return words.some((w) => {
@@ -200,7 +202,7 @@ export function parseStatement(lines, period, layout = {}, base) {
200
202
  if (!date || date < lo || date > hi)
201
203
  return;
202
204
  const text = l.map((c) => c.text).join(" ");
203
- if (hasWord(text, skips))
205
+ if (hasWord(text, skips) || DATE_RANGE_RE.test(plain(text)))
204
206
  return;
205
207
  const amounts = [];
206
208
  const desc = [];
@@ -332,18 +332,25 @@ export function buildServer(env, props, src = sourceFor(env), deps = {}) {
332
332
  server.registerTool("edit_transaction", { description: "PREVIEW changes to a transaction by id (only the fields given change).", inputSchema: z.object({ id: z.string(), ...txFields, date: z.string().optional(), reason }), annotations: pv }, wrap(async ({ id, reason, ...patch }) => preview((f) => A.editTransaction(f, id, patch, meta(reason)))));
333
333
  server.registerTool("void_transaction", { description: "PREVIEW voiding a transaction (kept in history, excluded everywhere).", inputSchema: z.object({ id: z.string(), reason }), annotations: pv }, wrap(async ({ id, reason }) => preview((f) => A.voidTransaction(f, id, reason, meta(reason)))));
334
334
  const batchOp = z.object(txFields).partial().extend({
335
- op: z.enum(["add", "edit", "void"]),
335
+ op: z.enum(["add", "edit", "void", "statement_amount"]),
336
336
  id: z.string().optional().describe("edit / void: the transaction id"),
337
+ amount: z.number().int().optional().describe("add/edit: positive, whole base-currency units; statement_amount: the bank's amount (0 allowed)"),
338
+ statement_date: z.string().optional().describe("statement_amount: the statement's closing date yyyy-mm-dd (with account and amount: the bank's amount, like add_statement_amount)"),
337
339
  reason: z.string().trim().max(A.REASON_MAX).optional().describe("optional: this op's own reason, added to the batch's"),
338
340
  });
339
341
  server.registerTool("batch", {
340
- description: `PREVIEW many adds, edits and voids as ONE change (writes nothing): one preview, one go, one commit. Use it for more than ~3 writes (e.g. fixes from a statement check). Each op takes the same fields as add_transaction / edit_transaction / void_transaction plus op. Ops run in order; if any op fails, the whole batch is refused and the reply names the op. Max ${A.BATCH_MAX} ops.`,
342
+ description: `PREVIEW many adds, edits and voids as ONE change (writes nothing): one preview, one go, one commit. Use it for more than ~3 writes (e.g. fixes from a statement check). Each op takes the same fields as add_transaction / edit_transaction / void_transaction plus op; op statement_amount (account, statement_date, amount) saves the bank's amount for a card statement like add_statement_amount. Ops run in order; if any op fails, the whole batch is refused and the reply names the op. Max ${A.BATCH_MAX} ops.`,
341
343
  inputSchema: z.object({ ops: z.array(batchOp).min(1).max(A.BATCH_MAX), reason }),
342
344
  annotations: pv,
343
345
  }, wrap(async ({ ops, reason }) => {
344
346
  const s = await snap();
345
347
  const p = poolOf(s.ledger, dashboardOf(s.files, props.login)?.prefs?.pool, now());
346
348
  const list = ops.map((o, i) => {
349
+ if (o.op === "statement_amount") {
350
+ if (!o.account || !o.statement_date || o.amount === undefined)
351
+ throw new A.ActionError(`batch refused: op ${i + 1} (statement_amount) needs account, statement_date and amount`);
352
+ return o;
353
+ }
347
354
  if (o.op !== "add" && !o.id)
348
355
  throw new A.ActionError(`batch refused: op ${i + 1} (${o.op}) needs an id`);
349
356
  if (o.op === "add" && !o.date)
@@ -1 +1 @@
1
- export declare const VERSION = "2.2.0";
1
+ export declare const VERSION = "2.3.1";
@@ -1,2 +1,2 @@
1
1
  // generated by scripts/version.mjs
2
- export const VERSION = "2.2.0";
2
+ export const VERSION = "2.3.1";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ownledger",
3
- "version": "2.2.0",
3
+ "version": "2.3.1",
4
4
  "description": "Personal finance ledger used through AI chat (MCP). Your data in your own private GitHub repo, served by your own Cloudflare Worker.",
5
5
  "license": "MIT",
6
6
  "author": "phucbm",