@baseworks/invoice 1.2.2 → 2.0.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/dist/events.d.ts CHANGED
@@ -13,7 +13,10 @@ import { z } from 'zod';
13
13
  * amounts, subtotal/total, the running amount-paid, the per-org invoice number,
14
14
  * and the resulting status — is computed by the STORE before emit and carried as
15
15
  * absolute values in the event, so the projection is a plain upsert (no
16
- * accumulation). `lines` is a serialized JSON array. Timestamps are epoch millis.
16
+ * accumulation). `lines` is a serialized JSON array. Timestamps (`at`, `created_at`) are epoch millis; business dates
17
+ * (`document_date`, `due_date`, `paid_date`, `void_date`, since 2.0) are calendar days `YYYY-MM-DD` (workspace ADR 0010).
18
+ * Events written before 2.0 say `issued_at`, `due_at`, `paid_at`, `voided_at` in epoch millis; they stay in the log and
19
+ * the service reads them as UTC days.
17
20
  * A FINALIZED invoice is immutable: only PaymentRecorded / InvoiceVoided follow.
18
21
  *
19
22
  * Payload fields are snake_case (0.3.0). Events written before are camelCase
@@ -70,6 +73,7 @@ declare const InvoiceEvents: _baseworks_foldbase.EventCatalog<{
70
73
  total_cents: z.ZodNumber;
71
74
  metadata: z.ZodOptional<z.ZodString>;
72
75
  external_number: z.ZodOptional<z.ZodString>;
76
+ previous_external_number: z.ZodOptional<z.ZodString>;
73
77
  at: z.ZodNumber;
74
78
  }, "strip", z.ZodTypeAny, {
75
79
  at: number;
@@ -80,6 +84,7 @@ declare const InvoiceEvents: _baseworks_foldbase.EventCatalog<{
80
84
  total_cents: number;
81
85
  metadata?: string | undefined;
82
86
  external_number?: string | undefined;
87
+ previous_external_number?: string | undefined;
83
88
  }, {
84
89
  at: number;
85
90
  customer_id: string;
@@ -89,14 +94,17 @@ declare const InvoiceEvents: _baseworks_foldbase.EventCatalog<{
89
94
  total_cents: number;
90
95
  metadata?: string | undefined;
91
96
  external_number?: string | undefined;
97
+ previous_external_number?: string | undefined;
92
98
  }>;
93
99
  InvoiceFinalized: z.ZodObject<{
94
100
  number: z.ZodNumber;
95
101
  lines: z.ZodString;
96
102
  subtotal_cents: z.ZodNumber;
97
103
  total_cents: z.ZodNumber;
98
- issued_at: z.ZodNumber;
99
- due_at: z.ZodNumber;
104
+ document_date: z.ZodOptional<z.ZodString>;
105
+ due_date: z.ZodOptional<z.ZodString>;
106
+ issued_at: z.ZodOptional<z.ZodNumber>;
107
+ due_at: z.ZodOptional<z.ZodNumber>;
100
108
  number_text: z.ZodOptional<z.ZodString>;
101
109
  series_key: z.ZodOptional<z.ZodString>;
102
110
  external_number: z.ZodOptional<z.ZodString>;
@@ -107,9 +115,11 @@ declare const InvoiceEvents: _baseworks_foldbase.EventCatalog<{
107
115
  lines: string;
108
116
  subtotal_cents: number;
109
117
  total_cents: number;
110
- issued_at: number;
111
- due_at: number;
112
118
  external_number?: string | undefined;
119
+ document_date?: string | undefined;
120
+ due_date?: string | undefined;
121
+ issued_at?: number | undefined;
122
+ due_at?: number | undefined;
113
123
  number_text?: string | undefined;
114
124
  series_key?: string | undefined;
115
125
  }, {
@@ -118,9 +128,11 @@ declare const InvoiceEvents: _baseworks_foldbase.EventCatalog<{
118
128
  lines: string;
119
129
  subtotal_cents: number;
120
130
  total_cents: number;
121
- issued_at: number;
122
- due_at: number;
123
131
  external_number?: string | undefined;
132
+ document_date?: string | undefined;
133
+ due_date?: string | undefined;
134
+ issued_at?: number | undefined;
135
+ due_at?: number | undefined;
124
136
  number_text?: string | undefined;
125
137
  series_key?: string | undefined;
126
138
  }>;
@@ -131,6 +143,8 @@ declare const InvoiceEvents: _baseworks_foldbase.EventCatalog<{
131
143
  method: z.ZodString;
132
144
  reference: z.ZodString;
133
145
  payment_id: z.ZodOptional<z.ZodString>;
146
+ document_date: z.ZodOptional<z.ZodString>;
147
+ paid_date: z.ZodOptional<z.ZodString>;
134
148
  at: z.ZodNumber;
135
149
  }, "strip", z.ZodTypeAny, {
136
150
  at: number;
@@ -139,7 +153,9 @@ declare const InvoiceEvents: _baseworks_foldbase.EventCatalog<{
139
153
  amount_paid_cents: number;
140
154
  method: string;
141
155
  reference: string;
156
+ document_date?: string | undefined;
142
157
  payment_id?: string | undefined;
158
+ paid_date?: string | undefined;
143
159
  }, {
144
160
  at: number;
145
161
  status: "draft" | "finalized" | "paid" | "void";
@@ -147,27 +163,51 @@ declare const InvoiceEvents: _baseworks_foldbase.EventCatalog<{
147
163
  amount_paid_cents: number;
148
164
  method: string;
149
165
  reference: string;
166
+ document_date?: string | undefined;
150
167
  payment_id?: string | undefined;
168
+ paid_date?: string | undefined;
151
169
  }>;
152
170
  InvoiceVoided: z.ZodObject<{
153
171
  reason: z.ZodString;
172
+ void_date: z.ZodOptional<z.ZodString>;
154
173
  at: z.ZodNumber;
155
174
  }, "strip", z.ZodTypeAny, {
156
175
  at: number;
157
176
  reason: string;
177
+ void_date?: string | undefined;
158
178
  }, {
159
179
  at: number;
160
180
  reason: string;
181
+ void_date?: string | undefined;
161
182
  }>;
162
183
  InvoiceExternalNumberSet: z.ZodObject<{
163
184
  external_number: z.ZodString;
185
+ previous: z.ZodOptional<z.ZodString>;
186
+ moved_from: z.ZodOptional<z.ZodString>;
164
187
  at: z.ZodNumber;
165
188
  }, "strip", z.ZodTypeAny, {
166
189
  at: number;
167
190
  external_number: string;
191
+ previous?: string | undefined;
192
+ moved_from?: string | undefined;
168
193
  }, {
169
194
  at: number;
170
195
  external_number: string;
196
+ previous?: string | undefined;
197
+ moved_from?: string | undefined;
198
+ }>;
199
+ InvoiceExternalNumberCleared: z.ZodObject<{
200
+ previous: z.ZodString;
201
+ moved_to: z.ZodOptional<z.ZodString>;
202
+ at: z.ZodNumber;
203
+ }, "strip", z.ZodTypeAny, {
204
+ at: number;
205
+ previous: string;
206
+ moved_to?: string | undefined;
207
+ }, {
208
+ at: number;
209
+ previous: string;
210
+ moved_to?: string | undefined;
171
211
  }>;
172
212
  InvoiceMirrored: z.ZodObject<{
173
213
  source: z.ZodString;
@@ -182,10 +222,14 @@ declare const InvoiceEvents: _baseworks_foldbase.EventCatalog<{
182
222
  total_cents: z.ZodNumber;
183
223
  amount_paid_cents: z.ZodNumber;
184
224
  status: z.ZodEnum<["draft", "finalized", "paid", "void"]>;
185
- issued_at: z.ZodNumber;
186
- due_at: z.ZodNumber;
187
- paid_at: z.ZodNumber;
188
- voided_at: z.ZodNumber;
225
+ document_date: z.ZodOptional<z.ZodString>;
226
+ due_date: z.ZodOptional<z.ZodString>;
227
+ paid_date: z.ZodOptional<z.ZodString>;
228
+ void_date: z.ZodOptional<z.ZodString>;
229
+ issued_at: z.ZodOptional<z.ZodNumber>;
230
+ due_at: z.ZodOptional<z.ZodNumber>;
231
+ paid_at: z.ZodOptional<z.ZodNumber>;
232
+ voided_at: z.ZodOptional<z.ZodNumber>;
189
233
  provider_updated_at: z.ZodNumber;
190
234
  metadata: z.ZodString;
191
235
  short_id: z.ZodString;
@@ -203,16 +247,20 @@ declare const InvoiceEvents: _baseworks_foldbase.EventCatalog<{
203
247
  short_id: string;
204
248
  source: string;
205
249
  metadata: string;
206
- issued_at: number;
207
- due_at: number;
208
250
  amount_paid_cents: number;
209
251
  mirror_key: string;
210
252
  external_refs: string;
211
253
  tax_cents: number;
212
- paid_at: number;
213
- voided_at: number;
214
254
  provider_updated_at: number;
215
255
  created_at: number;
256
+ document_date?: string | undefined;
257
+ due_date?: string | undefined;
258
+ issued_at?: number | undefined;
259
+ due_at?: number | undefined;
260
+ paid_date?: string | undefined;
261
+ void_date?: string | undefined;
262
+ paid_at?: number | undefined;
263
+ voided_at?: number | undefined;
216
264
  }, {
217
265
  at: number;
218
266
  status: "draft" | "finalized" | "paid" | "void";
@@ -225,16 +273,20 @@ declare const InvoiceEvents: _baseworks_foldbase.EventCatalog<{
225
273
  short_id: string;
226
274
  source: string;
227
275
  metadata: string;
228
- issued_at: number;
229
- due_at: number;
230
276
  amount_paid_cents: number;
231
277
  mirror_key: string;
232
278
  external_refs: string;
233
279
  tax_cents: number;
234
- paid_at: number;
235
- voided_at: number;
236
280
  provider_updated_at: number;
237
281
  created_at: number;
282
+ document_date?: string | undefined;
283
+ due_date?: string | undefined;
284
+ issued_at?: number | undefined;
285
+ due_at?: number | undefined;
286
+ paid_date?: string | undefined;
287
+ void_date?: string | undefined;
288
+ paid_at?: number | undefined;
289
+ voided_at?: number | undefined;
238
290
  }>;
239
291
  InvoiceFiscalChanged: z.ZodObject<{
240
292
  status: z.ZodString;
@@ -300,17 +352,20 @@ declare const NumberingEvents: _baseworks_foldbase.EventCatalog<{
300
352
  invoice_id: z.ZodString;
301
353
  series_key: z.ZodString;
302
354
  number: z.ZodNumber;
355
+ claim_id: z.ZodOptional<z.ZodString>;
303
356
  at: z.ZodNumber;
304
357
  }, "strip", z.ZodTypeAny, {
305
358
  number: number;
306
359
  at: number;
307
360
  series_key: string;
308
361
  invoice_id: string;
362
+ claim_id?: string | undefined;
309
363
  }, {
310
364
  number: number;
311
365
  at: number;
312
366
  series_key: string;
313
367
  invoice_id: string;
368
+ claim_id?: string | undefined;
314
369
  }>;
315
370
  NumberReleased: z.ZodObject<{
316
371
  invoice_id: z.ZodString;
@@ -326,18 +381,72 @@ declare const NumberingEvents: _baseworks_foldbase.EventCatalog<{
326
381
  external_number: z.ZodString;
327
382
  invoice_id: z.ZodString;
328
383
  invoice_version: z.ZodOptional<z.ZodNumber>;
384
+ claim_id: z.ZodOptional<z.ZodString>;
385
+ at: z.ZodNumber;
386
+ }, "strip", z.ZodTypeAny, {
387
+ at: number;
388
+ external_number: string;
389
+ invoice_id: string;
390
+ claim_id?: string | undefined;
391
+ invoice_version?: number | undefined;
392
+ }, {
393
+ at: number;
394
+ external_number: string;
395
+ invoice_id: string;
396
+ claim_id?: string | undefined;
397
+ invoice_version?: number | undefined;
398
+ }>;
399
+ NumberConfirmed: z.ZodObject<{
400
+ invoice_id: z.ZodString;
401
+ series_key: z.ZodString;
402
+ number: z.ZodNumber;
403
+ claim_id: z.ZodOptional<z.ZodString>;
404
+ at: z.ZodNumber;
405
+ }, "strip", z.ZodTypeAny, {
406
+ number: number;
407
+ at: number;
408
+ series_key: string;
409
+ invoice_id: string;
410
+ claim_id?: string | undefined;
411
+ }, {
412
+ number: number;
413
+ at: number;
414
+ series_key: string;
415
+ invoice_id: string;
416
+ claim_id?: string | undefined;
417
+ }>;
418
+ ExternalNumberConfirmed: z.ZodObject<{
419
+ external_number: z.ZodString;
420
+ invoice_id: z.ZodString;
421
+ invoice_version: z.ZodOptional<z.ZodNumber>;
422
+ claim_id: z.ZodOptional<z.ZodString>;
329
423
  at: z.ZodNumber;
330
424
  }, "strip", z.ZodTypeAny, {
331
425
  at: number;
332
426
  external_number: string;
333
427
  invoice_id: string;
428
+ claim_id?: string | undefined;
334
429
  invoice_version?: number | undefined;
335
430
  }, {
336
431
  at: number;
337
432
  external_number: string;
338
433
  invoice_id: string;
434
+ claim_id?: string | undefined;
339
435
  invoice_version?: number | undefined;
340
436
  }>;
437
+ ExternalNumberReleased: z.ZodObject<{
438
+ external_number: z.ZodString;
439
+ invoice_id: z.ZodString;
440
+ at: z.ZodNumber;
441
+ }, "strip", z.ZodTypeAny, {
442
+ at: number;
443
+ external_number: string;
444
+ invoice_id: string;
445
+ }, {
446
+ at: number;
447
+ external_number: string;
448
+ invoice_id: string;
449
+ }>;
341
450
  }> & {
342
451
  newId(): string;
343
452
  };
@@ -366,6 +475,7 @@ declare const TransactionEvents: _baseworks_foldbase.EventCatalog<{
366
475
  method: z.ZodString;
367
476
  reference: z.ZodString;
368
477
  payment_id: z.ZodOptional<z.ZodString>;
478
+ document_date: z.ZodOptional<z.ZodString>;
369
479
  at: z.ZodNumber;
370
480
  }, "strip", z.ZodTypeAny, {
371
481
  at: number;
@@ -378,6 +488,7 @@ declare const TransactionEvents: _baseworks_foldbase.EventCatalog<{
378
488
  invoice_id: string;
379
489
  invoice_number: number;
380
490
  kind: "payment" | "refund" | "adjustment";
491
+ document_date?: string | undefined;
381
492
  payment_id?: string | undefined;
382
493
  }, {
383
494
  at: number;
@@ -390,6 +501,7 @@ declare const TransactionEvents: _baseworks_foldbase.EventCatalog<{
390
501
  invoice_id: string;
391
502
  invoice_number: number;
392
503
  kind: "payment" | "refund" | "adjustment";
504
+ document_date?: string | undefined;
393
505
  payment_id?: string | undefined;
394
506
  }>;
395
507
  }> & {
package/dist/events.js CHANGED
@@ -6,7 +6,7 @@ import {
6
6
  NumberingEvents,
7
7
  TransactionEvents,
8
8
  TransactionKind
9
- } from "./chunk-JS6GUXCC.js";
9
+ } from "./chunk-3MXVPVAV.js";
10
10
  export {
11
11
  FISCAL_STATUSES,
12
12
  INVOICE_SOURCES,
package/dist/graphql.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  import { createInvoiceClient, Invoice, Transaction } from './client.js';
2
+ import * as _baseworks_core from '@baseworks/core';
3
+ import '@baseworks/core/document-date';
2
4
  import './events.js';
3
5
  import '@baseworks/foldbase';
4
6
  import 'zod';
@@ -10,7 +12,7 @@ interface InvoiceGraphContext {
10
12
  load(id: string): Promise<unknown>;
11
13
  };
12
14
  }
13
- declare const typeDefs = "\n type Invoice {\n id: ID!\n number: Int\n status: String!\n currency: String!\n subtotalCents: Int!\n totalCents: Int!\n amountPaidCents: Int!\n balanceCents: Int!\n dueAt: Float\n createdAt: Float!\n lines: [InvoiceLine!]!\n customer: Customer\n transactions: [Transaction!]!\n }\n type InvoiceLine {\n description: String!\n quantity: Float!\n unitPriceCents: Int!\n amountCents: Int!\n }\n type Transaction {\n id: ID!\n kind: String!\n amountCents: Int!\n currency: String!\n method: String\n reference: String\n createdAt: Float!\n }\n extend type Query {\n invoice(ref: String!): Invoice\n invoices(status: String): [Invoice!]!\n }\n extend type Customer {\n invoices(status: String): [Invoice!]!\n }\n";
15
+ declare const typeDefs = "\n type Invoice {\n id: ID!\n number: Int\n status: String!\n currency: String!\n subtotalCents: Int!\n totalCents: Int!\n amountPaidCents: Int!\n balanceCents: Int!\n documentDate: String\n dueDate: String\n createdAt: Float!\n lines: [InvoiceLine!]!\n customer: Customer\n transactions: [Transaction!]!\n }\n type InvoiceLine {\n description: String!\n quantity: Float!\n unitPriceCents: Int!\n amountCents: Int!\n }\n type Transaction {\n id: ID!\n kind: String!\n amountCents: Int!\n currency: String!\n method: String\n reference: String\n createdAt: Float!\n }\n extend type Query {\n invoice(ref: String!): Invoice\n invoices(status: String): [Invoice!]!\n }\n extend type Customer {\n invoices(status: String): [Invoice!]!\n }\n";
14
16
  declare const resolvers: {
15
17
  Query: {
16
18
  invoice: (_: unknown, { ref }: {
@@ -48,7 +50,8 @@ declare const resolvers: {
48
50
  totalCents: (p: Invoice) => number;
49
51
  amountPaidCents: (p: Invoice) => number;
50
52
  balanceCents: (p: Invoice) => number;
51
- dueAt: (p: Invoice) => number | null;
53
+ documentDate: (p: Invoice) => _baseworks_core.DocumentDate | null;
54
+ dueDate: (p: Invoice) => _baseworks_core.DocumentDate | null;
52
55
  createdAt: (p: Invoice) => number;
53
56
  customer: (parent: Invoice, _: unknown, ctx: InvoiceGraphContext) => Promise<unknown>;
54
57
  transactions: (parent: Invoice, _: unknown, ctx: InvoiceGraphContext) => Promise<never[] | Transaction[]>;
package/dist/graphql.js CHANGED
@@ -11,7 +11,8 @@ var typeDefs = (
11
11
  totalCents: Int!
12
12
  amountPaidCents: Int!
13
13
  balanceCents: Int!
14
- dueAt: Float
14
+ documentDate: String
15
+ dueDate: String
15
16
  createdAt: Float!
16
17
  lines: [InvoiceLine!]!
17
18
  customer: Customer
@@ -63,7 +64,8 @@ var resolvers = {
63
64
  totalCents: (p) => p.total_cents,
64
65
  amountPaidCents: (p) => p.amount_paid_cents,
65
66
  balanceCents: (p) => p.balance_cents,
66
- dueAt: (p) => p.due_at,
67
+ documentDate: (p) => p.document_date,
68
+ dueDate: (p) => p.due_date,
67
69
  createdAt: (p) => p.created_at,
68
70
  customer: (parent, _, ctx) => ctx.customerLoader.load(parent.customer_id),
69
71
  transactions: (parent, _, ctx) => ctx.invoice.invoiceTransactions(parent.id).catch(() => [])
package/dist/mcp.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { McpTool } from '@baseworks/mcp';
2
2
  import { InvoiceClient } from './client.js';
3
+ import '@baseworks/core/document-date';
3
4
  import './events.js';
4
5
  import '@baseworks/foldbase';
5
6
  import 'zod';
package/dist/mcp.js CHANGED
@@ -1,6 +1,7 @@
1
1
  // src/mcp.ts
2
2
  import { z } from "zod";
3
- import { defineTool } from "@baseworks/mcp";
3
+ import { defineTool, encodeCursor, pageArgs, withPaging } from "@baseworks/mcp";
4
+ import { documentDate, documentDateRange, parseDocumentDate } from "@baseworks/core";
4
5
  var REF = z.string().min(1).describe("The invoice's id or its short id.");
5
6
  var Line = z.object({
6
7
  description: z.string().min(1).describe("What is being charged for."),
@@ -8,14 +9,34 @@ var Line = z.object({
8
9
  unit_price_cents: z.number().int().min(0).describe("Price of one unit in integer cents: 150.00 is 15000."),
9
10
  product_id: z.string().optional().describe("The id of a catalog product, if the line is one.")
10
11
  });
12
+ var isDay = (v) => {
13
+ try {
14
+ parseDocumentDate(v);
15
+ return true;
16
+ } catch {
17
+ return false;
18
+ }
19
+ };
20
+ var due_date = z.string().refine(isDay, { message: "not a calendar day YYYY-MM-DD" }).optional().describe("The due date, a calendar day YYYY-MM-DD; omit for none.");
11
21
  var customer_id = z.string().min(1).describe("The customer: its id, short id or external id in the customer directory (see customers_list). It must exist.");
22
+ var PAGED = "Paged: at most `limit` (default 100, max 500); when `next` is not null there is more: call again with the same filters and `cursor: next`.";
23
+ var Offset = z.number().int().min(0).optional().describe("Deprecated: use `cursor`. How many to skip; not together with `cursor`.");
24
+ function pageOf(a) {
25
+ if (a.offset === void 0) return pageArgs(a);
26
+ if (a.cursor !== void 0) {
27
+ throw Object.assign(new Error("give `cursor` or `offset`, not both: `offset` is deprecated, pass the `next` of the previous page as `cursor`"), { code: "bad_request", status: 400 });
28
+ }
29
+ return { limit: pageArgs({ limit: a.limit }).limit, offset: a.offset };
30
+ }
31
+ var nextOf = (next_offset) => next_offset == null ? null : encodeCursor(next_offset);
12
32
  function invoiceTools() {
13
33
  return [
14
34
  defineTool({
15
35
  name: "invoice_list",
16
36
  title: "List invoices",
17
- description: 'Lists the organization\'s invoices, newest first. Narrow with `status`: draft (being prepared, no number yet), finalized (issued, owed), paid or void. Each invoice has its totals, the amount paid, the `balance_cents` still owed and a `payment_status` (unpaid, partially_paid, paid; not_due for a draft or void invoice), so the answer to "what is unpaid" is one call: filter with `payment_status`, `overdue`, `source`, `fiscal_status`, `customer_id` and `external_number` (exact: find the invoice a number belongs to); filters combine. Money is integer cents. Returns `{ invoices }`. Read only.',
18
- input: z.object({
37
+ description: `Lists the organization's invoices, newest \`document_date\` first (drafts, which have none yet, before them). Narrow with \`from\` / \`to\` (invoice dates, both included; drafts are then left out), with \`status\`: draft (being prepared, no number yet), finalized (issued, owed), paid or void. Each invoice has its totals, the amount paid, the \`balance_cents\` still owed and a \`payment_status\` (unpaid, partially_paid, paid; not_due for a draft or void invoice), so the answer to "what is unpaid" is one call: filter with \`payment_status\`, \`overdue\`, \`source\`, \`fiscal_status\`, \`customer_id\` and \`external_number\` (exact: find the invoice a number belongs to); filters combine. Money is integer cents. Returns \`{ invoices, next }\`. ${PAGED} Read only.`,
38
+ input: z.object(withPaging(z, {
39
+ ...documentDateRange(z),
19
40
  status: z.enum(["draft", "finalized", "paid", "void"]).optional().describe("Only invoices in this status."),
20
41
  payment_status: z.array(z.enum(["unpaid", "partially_paid", "paid", "not_due"])).min(1).optional().describe("Only invoices in one of these payment states. `unpaid` and `partially_paid` are what is still owed; `not_due` is a draft or void invoice."),
21
42
  overdue: z.boolean().optional().describe("true: only what is owed and past its due date."),
@@ -23,15 +44,17 @@ function invoiceTools() {
23
44
  fiscal_status: z.enum(["not_submitted", "queued", "submitted", "accepted", "rejected", "cancelled"]).optional().describe("Only invoices in this state with a tax system."),
24
45
  customer_id: z.string().optional().describe("Only this customer's invoices: the customer's id."),
25
46
  external_number: z.string().optional().describe("Only the invoice with this exact external number (the number another system gave it)."),
26
- limit: z.number().int().min(1).max(100).optional().describe("Page size, 1 to 100 (default 25)."),
27
- offset: z.number().int().min(0).optional().describe("How many to skip, for the next page.")
28
- }),
47
+ offset: Offset
48
+ })),
29
49
  access: "read",
30
50
  permission: "invoice.read",
31
- run: async (ctx, a) => ({
32
- invoices: await ctx.clients.invoice.list({
33
- limit: a.limit ?? 25,
34
- ...a.offset ? { offset: a.offset } : {},
51
+ run: async (ctx, a) => {
52
+ const page = pageOf(a);
53
+ const res = await ctx.clients.invoice.listPage({
54
+ limit: page.limit,
55
+ ...a.from !== void 0 ? { from: a.from } : {},
56
+ ...a.to !== void 0 ? { to: a.to } : {},
57
+ ...page.offset ? { offset: page.offset } : {},
35
58
  ...a.status ? { status: a.status } : {},
36
59
  ...a.payment_status ? { payment_status: a.payment_status } : {},
37
60
  ...a.overdue !== void 0 ? { overdue: a.overdue } : {},
@@ -39,13 +62,14 @@ function invoiceTools() {
39
62
  ...a.fiscal_status ? { fiscal_status: a.fiscal_status } : {},
40
63
  ...a.customer_id ? { customer_id: a.customer_id } : {},
41
64
  ...a.external_number ? { external_number: a.external_number } : {}
42
- })
43
- })
65
+ });
66
+ return { invoices: res.invoices, next: nextOf(res.next_offset) };
67
+ }
44
68
  }),
45
69
  defineTool({
46
70
  name: "invoice_get",
47
71
  title: "Get one invoice",
48
- description: "Returns one invoice in full: status, number (null while a draft), the customer (id and the name as it was when the invoice was drafted), every line, subtotal, tax, total, amount paid, balance, payment status, issue and due dates, where it came from (`source`, `external_refs`) and every payment recorded on it. `ref` is the invoice's id or short id. Fails with `not_found` if there is no such invoice. Read only.",
72
+ description: "Returns one invoice in full: status, the internal number (`number`, `number_text`; null while a draft), `external_number` and `display_number` (the external one if there is one, else the internal), the customer (id and the name as it was when the invoice was drafted), every line, subtotal, tax, total, amount paid, balance, payment status, `document_date` (the invoice date), `due_date`, `paid_date`, `void_date` and `void_reason`, where it came from (`source`, `external_refs`) and every payment recorded on it. `ref` is the invoice's id or short id. Fails with `not_found` if there is no such invoice. Read only.",
49
73
  input: z.object({ ref: REF }),
50
74
  access: "read",
51
75
  permission: "invoice.read",
@@ -54,7 +78,7 @@ function invoiceTools() {
54
78
  defineTool({
55
79
  name: "invoice_numbering",
56
80
  title: "Show the numbering series",
57
- description: "Shows how the organization numbers its invoices: `{ prefix, digits, yearly, time_zone, next, next_text }`, or `null` when it has no series and invoices are numbered 1, 2, 3. `next_text` is the number the next invoice finalized now would get (for example BSF2026000000149: the prefix, the issue year, then the counter). Read it before you finalize or before you tell someone what an invoice's number will be. Changing the series is invoice_configure_numbering, which is for administrators. Read only.",
81
+ description: "Shows how the organization numbers its invoices: `{ prefix, digits, yearly, next, next_text }`, or `null` when it has no series and invoices are numbered 1, 2, 3. `next_text` is the number the next invoice finalized now would get (for example BSF2026000000149: the prefix, the year of the invoice date, then the counter). Read it before you finalize or before you tell someone what an invoice's number will be. Changing the series is invoice_configure_numbering, which is for administrators. Read only.",
58
82
  input: z.object({}),
59
83
  access: "read",
60
84
  permission: "invoice.read",
@@ -63,27 +87,30 @@ function invoiceTools() {
63
87
  defineTool({
64
88
  name: "invoice_transactions",
65
89
  title: "List payments",
66
- description: "The money ledger: one entry per payment recorded, newest first. Give `ref` for one invoice's payments, or leave it out for every payment of the organization. Each entry says the invoice number, the customer, the amount in cents, the method and the reference. Read only.",
67
- input: z.object({
90
+ description: `The money ledger: one entry per payment recorded, newest \`document_date\` (day of the payment) first. Narrow with \`from\` / \`to\`. Give \`ref\` for one invoice's payments, or leave it out for every payment of the organization. Each entry says the invoice number, the customer, the amount in cents, the method and the reference. ${PAGED} Read only.`,
91
+ input: z.object(withPaging(z, {
68
92
  ref: REF.optional().describe("An invoice id or short id; leave out for the whole organization."),
69
- limit: z.number().int().min(1).max(100).optional().describe("Page size when listing the whole organization, 1 to 100 (default 25)."),
70
- offset: z.number().int().min(0).optional()
71
- }),
93
+ ...documentDateRange(z),
94
+ offset: Offset
95
+ })),
72
96
  access: "read",
73
97
  permission: "invoice.read",
74
- run: async (ctx, a) => ({
75
- transactions: a.ref ? await ctx.clients.invoice.invoiceTransactions(a.ref) : await ctx.clients.invoice.transactions({ limit: a.limit ?? 25, ...a.offset ? { offset: a.offset } : {} })
76
- })
98
+ run: async (ctx, a) => {
99
+ const page = pageOf(a);
100
+ const q = { limit: page.limit, ...page.offset ? { offset: page.offset } : {}, ...a.from !== void 0 ? { from: a.from } : {}, ...a.to !== void 0 ? { to: a.to } : {} };
101
+ const res = a.ref ? await ctx.clients.invoice.invoiceTransactionsPage(a.ref, q) : await ctx.clients.invoice.transactionsPage(q);
102
+ return { transactions: res.transactions, next: nextOf(res.next_offset) };
103
+ }
77
104
  }),
78
105
  defineTool({
79
106
  name: "invoice_create",
80
107
  title: "Draft an invoice",
81
- description: "Creates a DRAFT invoice for a customer and returns it. WRITES. The draft has no number and nothing is owed until it is finalized (invoice_finalize), so it is safe to prepare and then show the user. The customer must already exist: find it with customers_list; an unknown one is refused with `customer_not_found`. Lines carry a description, a quantity and a unit price in integer cents; the amounts and totals are computed for you. There is no tax in this version. Give `external_number` only when another system (an e-Fatura portal, say) has already given this invoice a number: the invoice keeps it and takes none from the organization's series. Otherwise leave it out; the number comes when the invoice is finalized. It is recorded as you, the agent acting for the person who connected you.",
108
+ description: "Creates a DRAFT invoice for a customer and returns it. WRITES. The draft has no number and nothing is owed until it is finalized (invoice_finalize), so it is safe to prepare and then show the user. The customer must already exist: find it with customers_list; an unknown one is refused with `customer_not_found`. Lines carry a description, a quantity and a unit price in integer cents; the amounts and totals are computed for you. There is no tax in this version. Give `external_number` only when another system (an e-Fatura portal, say) has already given this invoice a number: the invoice keeps it beside the internal number it gets when finalized. Otherwise leave it out. It is recorded as you, the agent acting for the person who connected you.",
82
109
  input: z.object({
83
110
  customer_id,
84
111
  currency: z.string().length(3).describe("ISO 4217 code, e.g. EUR, USD, TRY."),
85
112
  lines: z.array(Line).min(1).describe("At least one line."),
86
- external_number: z.string().min(1).max(64).optional().describe("A number another system already gave this invoice (an e-Fatura portal's, say), up to 64 characters. Give it only when you have one: the invoice then keeps it as its number and takes none from the organization's series. Unique among the organization's invoices (`external_number_taken`).")
113
+ external_number: z.string().min(1).max(64).optional().describe("A number another system already gave this invoice (an e-Fatura portal's, say), up to 64 characters. Give it only when you have one: it is kept beside the internal number the invoice gets at finalize. Unique among the organization's invoices (`external_number_taken`).")
87
114
  }),
88
115
  access: "write",
89
116
  permission: "invoice.write",
@@ -111,12 +138,14 @@ function invoiceTools() {
111
138
  defineTool({
112
139
  name: "invoice_finalize",
113
140
  title: "Finalize an invoice",
114
- description: "Issues a draft: it gets the next number of the organization's series (see invoice_numbering for what that reads like), its lines and totals are frozen for good, and it becomes owed. WRITES, and it cannot be undone (only voided). Ask the user to confirm before you do it. Optionally set `issued_at` and `due_at` (epoch milliseconds; the issue date defaults to now, no due date by default). If another system already numbered it, give `external_number`: that becomes the invoice's number and the series does not advance. Only a draft can be finalized: otherwise `not_draft`.",
141
+ description: "Issues a draft: it gets the next internal number of the organization's series (see invoice_numbering for what that reads like), always; its lines and totals are frozen for good, and it becomes owed. WRITES, and it cannot be undone (only voided). Ask the user to confirm before you do it. `document_date` is the invoice date: send the day the user means: omitted, it is today in UTC, which is yesterday in T\xFCrkiye between 00:00 and 03:00. It is never in the future and decides the year and series of the number: a date before the latest numbered invoice of that series is refused (`date_before_series`, naming it). `due_date` is optional and not before `document_date`. If another system already numbered it, give `external_number`: it is kept beside the internal number and shown as the `display_number`. Only a draft can be finalized: otherwise `not_draft`.",
115
142
  input: z.object({
116
143
  ref: REF,
117
- issued_at: z.number().int().optional().describe("Epoch milliseconds; default now."),
118
- due_at: z.number().int().optional().describe("Epoch milliseconds; omit for no due date."),
119
- external_number: z.string().min(1).max(64).optional().describe("A number another system already gave this invoice. Then it IS the invoice's number and the organization's series does not advance. Leave it out to take the next number of the series (a configured one such as BSF2026000000149, else 1, 2, 3). Refused with `external_number_set` if the invoice already has a different one.")
144
+ document_date: documentDate(z),
145
+ due_date,
146
+ external_number: z.string().min(1).max(64).optional().describe("A number another system already gave this invoice. Refused with `external_number_set` if the draft already has a different one."),
147
+ issued_at: z.number().int().optional().describe("Deprecated: use `document_date`. Epoch milliseconds, read as its UTC day; not together with `document_date`."),
148
+ due_at: z.number().int().optional().describe("Deprecated: use `due_date`. Epoch milliseconds; not together with `due_date`.")
120
149
  }),
121
150
  access: "write",
122
151
  permission: "invoice.finalize",
@@ -127,22 +156,36 @@ function invoiceTools() {
127
156
  }),
128
157
  defineTool({
129
158
  name: "invoice_set_external_number",
130
- title: "Set the portal number of an issued invoice",
131
- description: "Records the number another system (the e-Ar\u015Fiv / G\u0130B portal, say) gave an invoice that is ALREADY finalized or paid, and returns the invoice. WRITES, and it is one-time: once set it cannot be changed or removed here (a wrong number is corrected only by voiding the invoice and issuing it again). It needs the fiscal permission (`invoice.fiscal_update`). Ask the user to confirm the number first. The same number again changes nothing; a different one is `external_number_set`; a number another invoice holds is `external_number_taken`. The invoice's own `number` and `number_text` stay as issued. A draft is `use_update` (give `external_number` on invoice_finalize instead), a void invoice `void`, a Stripe mirror `mirrored`.",
132
- input: z.object({ ref: REF, external_number: z.string().min(1).max(64).describe("The number exactly as the other system printed it, up to 64 characters, no leading or trailing spaces.") }),
159
+ title: "Set or change the portal number",
160
+ description: "Sets or CHANGES the number another system (the e-Ar\u015Fiv / G\u0130B portal, say) gave an invoice that is already finalized, paid or void, and returns the invoice. WRITES; every change is kept on record with the previous value. It needs the fiscal permission (`invoice.fiscal_update`). Ask the user to confirm the number first. The same number again changes nothing; a number another invoice holds (a void one included) is `external_number_taken`: to move it from a void invoice give `from_invoice_id` (one operation), or take it off that one first (invoice_clear_external_number). The invoice's internal `number` and `number_text` never change; `display_number` shows the external one. A draft is `use_update` (give `external_number` on invoice_finalize instead), a Stripe mirror `mirrored`.",
161
+ input: z.object({
162
+ ref: REF,
163
+ external_number: z.string().min(1).max(64).describe("The number exactly as the other system printed it, up to 64 characters, no leading or trailing spaces."),
164
+ from_invoice_id: z.string().min(1).optional().describe("To MOVE the number: the VOID invoice that holds it now (its id or short id). The number leaves it and arrives on `ref` in one operation, so nobody can take it in between. Both changes are recorded.")
165
+ }),
166
+ access: "write",
167
+ permission: "invoice.fiscal_update",
168
+ run: async (ctx, a) => ({ invoice: await ctx.clients.invoice.setExternalNumber(a.ref, a.external_number, a.from_invoice_id ? { from_invoice_id: a.from_invoice_id } : {}) })
169
+ }),
170
+ defineTool({
171
+ name: "invoice_clear_external_number",
172
+ title: "Take the portal number off",
173
+ description: "Removes the external number from a finalized, paid or void invoice, and returns the invoice. WRITES; the change is kept on record with the previous value. The number is then free for another invoice (how a portal number is moved from a void invoice to the one that replaces it). It needs the fiscal permission (`invoice.fiscal_update`); ask the user to confirm first. The internal number stays. Nothing to remove changes nothing; a draft is `use_update`, a Stripe mirror `mirrored`.",
174
+ input: z.object({ ref: REF }),
133
175
  access: "write",
134
176
  permission: "invoice.fiscal_update",
135
- run: async (ctx, a) => ({ invoice: await ctx.clients.invoice.setExternalNumber(a.ref, a.external_number) })
177
+ run: async (ctx, a) => ({ invoice: await ctx.clients.invoice.clearExternalNumber(a.ref) })
136
178
  }),
137
179
  defineTool({
138
180
  name: "invoice_record_payment",
139
181
  title: "Record a payment",
140
- description: "Records that money was received against a FINALIZED invoice, and returns the invoice with its new balance; when the balance reaches zero it becomes `paid`. WRITES to the books. The amount is integer cents and cannot exceed the balance (`overpayment`); a draft, void or already paid invoice is refused. Give `reference` (the bank or processor's own id) whenever you have one: a second call with the same reference on the same invoice changes nothing, so a retry is safe. This records a payment that happened; it does not charge anybody.",
182
+ description: "Records that money was received against a FINALIZED invoice, and returns the invoice with its new balance; when the balance reaches zero it becomes `paid`. WRITES to the books. The amount is integer cents and cannot exceed the balance (`overpayment`); a draft, void or already paid invoice is refused. Give `reference` (the bank or processor's own id) whenever you have one: a second call with the same reference on the same invoice changes nothing, so a retry is safe. `document_date` is the day the money was received: send the day the user means: omitted, it is today in UTC, which is yesterday in T\xFCrkiye between 00:00 and 03:00. It is not before the invoice's own `document_date` (`date_before_invoice`); the payment that settles the invoice gives its `paid_date`. This records a payment that happened; it does not charge anybody.",
141
183
  input: z.object({
142
184
  ref: REF,
143
185
  amount_cents: z.number().int().positive().describe("Integer cents received: 100.00 is 10000."),
144
186
  method: z.string().optional().describe("e.g. bank, cash, card."),
145
187
  reference: z.string().optional().describe("The payer's or processor's own id for this payment; makes a retry safe."),
188
+ document_date: documentDate(z),
146
189
  payment_id: z.string().optional().describe("The id of the payment record this settles (from pay-service); the same payment_id twice records it once. Needed to record a payment on an invoice that mirrors Stripe's.")
147
190
  }),
148
191
  access: "write",
@@ -155,13 +198,14 @@ function invoiceTools() {
155
198
  defineTool({
156
199
  name: "invoice_configure_numbering",
157
200
  title: "Set the numbering series",
158
- description: "Sets how the organization numbers its invoices from now on: a `prefix` of 3 uppercase letters or digits, then the issue year, then a counter zero-padded to `digits` (prefix BSF, 9 digits: BSF2026000000148). WRITES, and it is for an administrator (permission `invoice.configure`). It never renumbers a finalized invoice. `yearly: true` restarts the counter each calendar year; the year (in the number and for the restart) is that of the issue date in `time_zone`, so set the organization's zone (e.g. Europe/Istanbul) or numbers issued around New Year land in the wrong year (UTC when never set); `next` continues an existing series from that counter and can only go forward (`next_behind` otherwise). Ask the person to confirm the prefix, the width and the next number before you change it: these are legal numbers. Use invoice_numbering to read the current series; to give one invoice a number from another system use `external_number` on invoice_create or invoice_finalize instead.",
201
+ description: "Sets how the organization numbers its invoices from now on: a `prefix` of 3 uppercase letters or digits, then the year of the invoice date, then a counter zero-padded to `digits` (prefix BSF, 9 digits: BSF2026000000148). WRITES, and it is for an administrator (permission `invoice.configure`). It never renumbers a finalized invoice. `yearly: true` restarts the counter each calendar year; the year (in the number and for the restart) is that of the invoice's `document_date`; `next` continues an existing series from that counter and can only go forward (`next_behind` otherwise). Ask the person to confirm the prefix, the width and the next number before you change it: these are legal numbers. Use invoice_numbering to read the current series; to give one invoice a number from another system use `external_number` on invoice_create or invoice_finalize instead.",
159
202
  input: z.object({
160
203
  prefix: z.string().regex(/^[A-Z0-9]{3}$/).describe("Exactly 3 uppercase letters or digits."),
161
204
  digits: z.number().int().min(1).max(12).describe("Counter width: 9 for e-Fatura."),
162
- yearly: z.boolean().describe("true: the counter restarts each calendar year (of the issue date)."),
163
- time_zone: z.string().min(1).max(64).optional().describe("IANA time zone name, e.g. Europe/Istanbul. Omit to keep the current one (UTC if never set)."),
164
- next: z.number().int().min(1).optional().describe("The counter to continue from (this year's, when yearly). Omit to continue where the series is.")
205
+ yearly: z.boolean().describe("true: the counter restarts each calendar year (of `document_date`)."),
206
+ time_zone: z.string().optional().describe("Deprecated: accepted and ignored."),
207
+ next: z.number().int().min(1).optional().describe("The counter to continue from (this year's, when yearly). Omit to continue where the series is."),
208
+ year: z.number().int().min(1).max(9999).optional().describe("Which year's series `next` applies to (yearly series only). Omit for the current year (UTC).")
165
209
  }),
166
210
  access: "write",
167
211
  permission: "invoice.configure",
@@ -170,11 +214,11 @@ function invoiceTools() {
170
214
  defineTool({
171
215
  name: "invoice_void",
172
216
  title: "Void an invoice",
173
- description: "Cancels a FINALIZED invoice that has no payments: its status becomes `void` and it stays on record. WRITES, and it cannot be undone. Ask the user to confirm first. Drafts are not voided (edit them or leave them); an invoice with payments or already paid is refused (`has_payments`, `already_paid`). Give a `reason`.",
174
- input: z.object({ ref: REF, reason: z.string().optional().describe("Why it is being voided; kept on the record.") }),
217
+ description: "Cancels a FINALIZED invoice that has no payments: its status becomes `void` and it stays on record. WRITES, and it cannot be undone. Ask the user to confirm first. Drafts are not voided (edit them or leave them); an invoice with payments or already paid is refused (`has_payments`, `already_paid`). Give a `reason`. `void_date` is the day it is voided, not before the invoice date: send the day the user means: omitted, it is today in UTC, which is yesterday in T\xFCrkiye between 00:00 and 03:00. The invoice keeps its numbers.",
218
+ input: z.object({ ref: REF, reason: z.string().optional().describe("Why it is being voided; kept on the record."), void_date: documentDate(z).describe("The day it is voided, YYYY-MM-DD. Defaults to today in UTC; a future date is refused.") }),
175
219
  access: "write",
176
220
  permission: "invoice.void",
177
- run: async (ctx, a) => ({ invoice: await ctx.clients.invoice.void(a.ref, a.reason) })
221
+ run: async (ctx, a) => ({ invoice: await ctx.clients.invoice.void(a.ref, a.reason, a.void_date !== void 0 ? { void_date: a.void_date } : {}) })
178
222
  })
179
223
  ];
180
224
  }