@baseworks/invoice 1.2.3 → 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/README.md +37 -27
- package/dist/{chunk-JS6GUXCC.js → chunk-3MXVPVAV.js} +55 -17
- package/dist/cli.d.ts +3 -2
- package/dist/cli.js +45 -26
- package/dist/client.d.ts +93 -45
- package/dist/client.js +20 -10
- package/dist/events.d.ts +131 -19
- package/dist/events.js +1 -1
- package/dist/graphql.d.ts +5 -2
- package/dist/graphql.js +4 -2
- package/dist/mcp.d.ts +1 -0
- package/dist/mcp.js +54 -23
- package/dist/projections.js +25 -10
- package/package.json +2 -1
- package/src/__tests__/cli.test.ts +43 -6
- package/src/__tests__/client.test.ts +36 -2
- package/src/__tests__/mcp.test.ts +51 -8
- package/src/cli.ts +69 -33
- package/src/client.ts +31 -12
- package/src/compat.ts +2 -3
- package/src/events.ts +45 -14
- package/src/graphql.ts +4 -2
- package/src/mcp.ts +62 -33
- package/src/projections.ts +21 -7
- package/src/types.ts +76 -26
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
|
-
|
|
99
|
-
|
|
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
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
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
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
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
package/dist/mcp.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// src/mcp.ts
|
|
2
2
|
import { z } from "zod";
|
|
3
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,6 +9,15 @@ 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.");
|
|
12
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`.";
|
|
13
23
|
var Offset = z.number().int().min(0).optional().describe("Deprecated: use `cursor`. How many to skip; not together with `cursor`.");
|
|
@@ -24,8 +34,9 @@ function invoiceTools() {
|
|
|
24
34
|
defineTool({
|
|
25
35
|
name: "invoice_list",
|
|
26
36
|
title: "List invoices",
|
|
27
|
-
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, next }\`. ${PAGED} Read only.`,
|
|
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.`,
|
|
28
38
|
input: z.object(withPaging(z, {
|
|
39
|
+
...documentDateRange(z),
|
|
29
40
|
status: z.enum(["draft", "finalized", "paid", "void"]).optional().describe("Only invoices in this status."),
|
|
30
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."),
|
|
31
42
|
overdue: z.boolean().optional().describe("true: only what is owed and past its due date."),
|
|
@@ -41,6 +52,8 @@ function invoiceTools() {
|
|
|
41
52
|
const page = pageOf(a);
|
|
42
53
|
const res = await ctx.clients.invoice.listPage({
|
|
43
54
|
limit: page.limit,
|
|
55
|
+
...a.from !== void 0 ? { from: a.from } : {},
|
|
56
|
+
...a.to !== void 0 ? { to: a.to } : {},
|
|
44
57
|
...page.offset ? { offset: page.offset } : {},
|
|
45
58
|
...a.status ? { status: a.status } : {},
|
|
46
59
|
...a.payment_status ? { payment_status: a.payment_status } : {},
|
|
@@ -56,7 +69,7 @@ function invoiceTools() {
|
|
|
56
69
|
defineTool({
|
|
57
70
|
name: "invoice_get",
|
|
58
71
|
title: "Get one invoice",
|
|
59
|
-
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,
|
|
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.",
|
|
60
73
|
input: z.object({ ref: REF }),
|
|
61
74
|
access: "read",
|
|
62
75
|
permission: "invoice.read",
|
|
@@ -65,7 +78,7 @@ function invoiceTools() {
|
|
|
65
78
|
defineTool({
|
|
66
79
|
name: "invoice_numbering",
|
|
67
80
|
title: "Show the numbering series",
|
|
68
|
-
description: "Shows how the organization numbers its invoices: `{ prefix, digits, yearly,
|
|
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.",
|
|
69
82
|
input: z.object({}),
|
|
70
83
|
access: "read",
|
|
71
84
|
permission: "invoice.read",
|
|
@@ -74,16 +87,17 @@ function invoiceTools() {
|
|
|
74
87
|
defineTool({
|
|
75
88
|
name: "invoice_transactions",
|
|
76
89
|
title: "List payments",
|
|
77
|
-
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. ${PAGED} Read only.`,
|
|
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.`,
|
|
78
91
|
input: z.object(withPaging(z, {
|
|
79
92
|
ref: REF.optional().describe("An invoice id or short id; leave out for the whole organization."),
|
|
93
|
+
...documentDateRange(z),
|
|
80
94
|
offset: Offset
|
|
81
95
|
})),
|
|
82
96
|
access: "read",
|
|
83
97
|
permission: "invoice.read",
|
|
84
98
|
run: async (ctx, a) => {
|
|
85
99
|
const page = pageOf(a);
|
|
86
|
-
const q = { limit: page.limit, ...page.offset ? { offset: page.offset } : {} };
|
|
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 } : {} };
|
|
87
101
|
const res = a.ref ? await ctx.clients.invoice.invoiceTransactionsPage(a.ref, q) : await ctx.clients.invoice.transactionsPage(q);
|
|
88
102
|
return { transactions: res.transactions, next: nextOf(res.next_offset) };
|
|
89
103
|
}
|
|
@@ -91,12 +105,12 @@ function invoiceTools() {
|
|
|
91
105
|
defineTool({
|
|
92
106
|
name: "invoice_create",
|
|
93
107
|
title: "Draft an invoice",
|
|
94
|
-
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
|
|
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.",
|
|
95
109
|
input: z.object({
|
|
96
110
|
customer_id,
|
|
97
111
|
currency: z.string().length(3).describe("ISO 4217 code, e.g. EUR, USD, TRY."),
|
|
98
112
|
lines: z.array(Line).min(1).describe("At least one line."),
|
|
99
|
-
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:
|
|
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`).")
|
|
100
114
|
}),
|
|
101
115
|
access: "write",
|
|
102
116
|
permission: "invoice.write",
|
|
@@ -124,12 +138,14 @@ function invoiceTools() {
|
|
|
124
138
|
defineTool({
|
|
125
139
|
name: "invoice_finalize",
|
|
126
140
|
title: "Finalize an invoice",
|
|
127
|
-
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.
|
|
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`.",
|
|
128
142
|
input: z.object({
|
|
129
143
|
ref: REF,
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
external_number: z.string().min(1).max(64).optional().describe("A number another system already gave this invoice.
|
|
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`.")
|
|
133
149
|
}),
|
|
134
150
|
access: "write",
|
|
135
151
|
permission: "invoice.finalize",
|
|
@@ -140,22 +156,36 @@ function invoiceTools() {
|
|
|
140
156
|
}),
|
|
141
157
|
defineTool({
|
|
142
158
|
name: "invoice_set_external_number",
|
|
143
|
-
title: "Set the portal number
|
|
144
|
-
description: "
|
|
145
|
-
input: z.object({
|
|
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 }),
|
|
146
175
|
access: "write",
|
|
147
176
|
permission: "invoice.fiscal_update",
|
|
148
|
-
run: async (ctx, a) => ({ invoice: await ctx.clients.invoice.
|
|
177
|
+
run: async (ctx, a) => ({ invoice: await ctx.clients.invoice.clearExternalNumber(a.ref) })
|
|
149
178
|
}),
|
|
150
179
|
defineTool({
|
|
151
180
|
name: "invoice_record_payment",
|
|
152
181
|
title: "Record a payment",
|
|
153
|
-
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.",
|
|
154
183
|
input: z.object({
|
|
155
184
|
ref: REF,
|
|
156
185
|
amount_cents: z.number().int().positive().describe("Integer cents received: 100.00 is 10000."),
|
|
157
186
|
method: z.string().optional().describe("e.g. bank, cash, card."),
|
|
158
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),
|
|
159
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.")
|
|
160
190
|
}),
|
|
161
191
|
access: "write",
|
|
@@ -168,13 +198,14 @@ function invoiceTools() {
|
|
|
168
198
|
defineTool({
|
|
169
199
|
name: "invoice_configure_numbering",
|
|
170
200
|
title: "Set the numbering series",
|
|
171
|
-
description: "Sets how the organization numbers its invoices from now on: a `prefix` of 3 uppercase letters or digits, then the
|
|
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.",
|
|
172
202
|
input: z.object({
|
|
173
203
|
prefix: z.string().regex(/^[A-Z0-9]{3}$/).describe("Exactly 3 uppercase letters or digits."),
|
|
174
204
|
digits: z.number().int().min(1).max(12).describe("Counter width: 9 for e-Fatura."),
|
|
175
|
-
yearly: z.boolean().describe("true: the counter restarts each calendar year (of
|
|
176
|
-
time_zone: z.string().
|
|
177
|
-
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).")
|
|
178
209
|
}),
|
|
179
210
|
access: "write",
|
|
180
211
|
permission: "invoice.configure",
|
|
@@ -183,11 +214,11 @@ function invoiceTools() {
|
|
|
183
214
|
defineTool({
|
|
184
215
|
name: "invoice_void",
|
|
185
216
|
title: "Void an invoice",
|
|
186
|
-
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`.",
|
|
187
|
-
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.") }),
|
|
188
219
|
access: "write",
|
|
189
220
|
permission: "invoice.void",
|
|
190
|
-
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 } : {}) })
|
|
191
222
|
})
|
|
192
223
|
];
|
|
193
224
|
}
|