@baseworks/invoice 1.0.0 → 1.2.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 +42 -0
- package/dist/chunk-THLNZVGO.js +162 -0
- package/dist/cli.js +57 -12
- package/dist/client.d.ts +167 -7
- package/dist/client.js +31 -0
- package/dist/events.d.ts +203 -3
- package/dist/events.js +7 -1
- package/dist/mcp.js +51 -8
- package/dist/projections.js +43 -3
- package/package.json +1 -1
- package/src/__tests__/cli.test.ts +55 -1
- package/src/__tests__/client.test.ts +59 -1
- package/src/__tests__/mcp.test.ts +55 -6
- package/src/cli.ts +92 -18
- package/src/client.ts +51 -1
- package/src/events.ts +72 -0
- package/src/mcp.ts +58 -10
- package/src/projections.ts +40 -0
- package/src/types.ts +160 -2
- package/dist/chunk-ITMZVGLQ.js +0 -78
package/dist/events.d.ts
CHANGED
|
@@ -33,6 +33,9 @@ declare const InvoiceEvents: _baseworks_foldbase.EventCatalog<{
|
|
|
33
33
|
subtotal_cents: z.ZodNumber;
|
|
34
34
|
total_cents: z.ZodNumber;
|
|
35
35
|
short_id: z.ZodString;
|
|
36
|
+
source: z.ZodOptional<z.ZodString>;
|
|
37
|
+
metadata: z.ZodOptional<z.ZodString>;
|
|
38
|
+
external_number: z.ZodOptional<z.ZodString>;
|
|
36
39
|
at: z.ZodNumber;
|
|
37
40
|
}, "strip", z.ZodTypeAny, {
|
|
38
41
|
at: number;
|
|
@@ -43,6 +46,9 @@ declare const InvoiceEvents: _baseworks_foldbase.EventCatalog<{
|
|
|
43
46
|
currency: string;
|
|
44
47
|
subtotal_cents: number;
|
|
45
48
|
total_cents: number;
|
|
49
|
+
source?: string | undefined;
|
|
50
|
+
metadata?: string | undefined;
|
|
51
|
+
external_number?: string | undefined;
|
|
46
52
|
}, {
|
|
47
53
|
at: number;
|
|
48
54
|
lines: string;
|
|
@@ -52,6 +58,9 @@ declare const InvoiceEvents: _baseworks_foldbase.EventCatalog<{
|
|
|
52
58
|
currency: string;
|
|
53
59
|
subtotal_cents: number;
|
|
54
60
|
total_cents: number;
|
|
61
|
+
source?: string | undefined;
|
|
62
|
+
metadata?: string | undefined;
|
|
63
|
+
external_number?: string | undefined;
|
|
55
64
|
}>;
|
|
56
65
|
InvoiceUpdated: z.ZodObject<{
|
|
57
66
|
customer_id: z.ZodString;
|
|
@@ -59,6 +68,8 @@ declare const InvoiceEvents: _baseworks_foldbase.EventCatalog<{
|
|
|
59
68
|
lines: z.ZodString;
|
|
60
69
|
subtotal_cents: z.ZodNumber;
|
|
61
70
|
total_cents: z.ZodNumber;
|
|
71
|
+
metadata: z.ZodOptional<z.ZodString>;
|
|
72
|
+
external_number: z.ZodOptional<z.ZodString>;
|
|
62
73
|
at: z.ZodNumber;
|
|
63
74
|
}, "strip", z.ZodTypeAny, {
|
|
64
75
|
at: number;
|
|
@@ -67,6 +78,8 @@ declare const InvoiceEvents: _baseworks_foldbase.EventCatalog<{
|
|
|
67
78
|
customer_name: string;
|
|
68
79
|
subtotal_cents: number;
|
|
69
80
|
total_cents: number;
|
|
81
|
+
metadata?: string | undefined;
|
|
82
|
+
external_number?: string | undefined;
|
|
70
83
|
}, {
|
|
71
84
|
at: number;
|
|
72
85
|
lines: string;
|
|
@@ -74,6 +87,8 @@ declare const InvoiceEvents: _baseworks_foldbase.EventCatalog<{
|
|
|
74
87
|
customer_name: string;
|
|
75
88
|
subtotal_cents: number;
|
|
76
89
|
total_cents: number;
|
|
90
|
+
metadata?: string | undefined;
|
|
91
|
+
external_number?: string | undefined;
|
|
77
92
|
}>;
|
|
78
93
|
InvoiceFinalized: z.ZodObject<{
|
|
79
94
|
number: z.ZodNumber;
|
|
@@ -82,6 +97,9 @@ declare const InvoiceEvents: _baseworks_foldbase.EventCatalog<{
|
|
|
82
97
|
total_cents: z.ZodNumber;
|
|
83
98
|
issued_at: z.ZodNumber;
|
|
84
99
|
due_at: z.ZodNumber;
|
|
100
|
+
number_text: z.ZodOptional<z.ZodString>;
|
|
101
|
+
series_key: z.ZodOptional<z.ZodString>;
|
|
102
|
+
external_number: z.ZodOptional<z.ZodString>;
|
|
85
103
|
at: z.ZodNumber;
|
|
86
104
|
}, "strip", z.ZodTypeAny, {
|
|
87
105
|
number: number;
|
|
@@ -91,6 +109,9 @@ declare const InvoiceEvents: _baseworks_foldbase.EventCatalog<{
|
|
|
91
109
|
total_cents: number;
|
|
92
110
|
issued_at: number;
|
|
93
111
|
due_at: number;
|
|
112
|
+
number_text?: string | undefined;
|
|
113
|
+
external_number?: string | undefined;
|
|
114
|
+
series_key?: string | undefined;
|
|
94
115
|
}, {
|
|
95
116
|
number: number;
|
|
96
117
|
at: number;
|
|
@@ -99,6 +120,9 @@ declare const InvoiceEvents: _baseworks_foldbase.EventCatalog<{
|
|
|
99
120
|
total_cents: number;
|
|
100
121
|
issued_at: number;
|
|
101
122
|
due_at: number;
|
|
123
|
+
number_text?: string | undefined;
|
|
124
|
+
external_number?: string | undefined;
|
|
125
|
+
series_key?: string | undefined;
|
|
102
126
|
}>;
|
|
103
127
|
PaymentRecorded: z.ZodObject<{
|
|
104
128
|
amount_cents: z.ZodNumber;
|
|
@@ -106,21 +130,24 @@ declare const InvoiceEvents: _baseworks_foldbase.EventCatalog<{
|
|
|
106
130
|
status: z.ZodEnum<["draft", "finalized", "paid", "void"]>;
|
|
107
131
|
method: z.ZodString;
|
|
108
132
|
reference: z.ZodString;
|
|
133
|
+
payment_id: z.ZodOptional<z.ZodString>;
|
|
109
134
|
at: z.ZodNumber;
|
|
110
135
|
}, "strip", z.ZodTypeAny, {
|
|
111
136
|
method: string;
|
|
112
137
|
at: number;
|
|
113
|
-
status: "
|
|
138
|
+
status: "paid" | "draft" | "finalized" | "void";
|
|
114
139
|
amount_paid_cents: number;
|
|
115
140
|
amount_cents: number;
|
|
116
141
|
reference: string;
|
|
142
|
+
payment_id?: string | undefined;
|
|
117
143
|
}, {
|
|
118
144
|
method: string;
|
|
119
145
|
at: number;
|
|
120
|
-
status: "
|
|
146
|
+
status: "paid" | "draft" | "finalized" | "void";
|
|
121
147
|
amount_paid_cents: number;
|
|
122
148
|
amount_cents: number;
|
|
123
149
|
reference: string;
|
|
150
|
+
payment_id?: string | undefined;
|
|
124
151
|
}>;
|
|
125
152
|
InvoiceVoided: z.ZodObject<{
|
|
126
153
|
reason: z.ZodString;
|
|
@@ -132,9 +159,179 @@ declare const InvoiceEvents: _baseworks_foldbase.EventCatalog<{
|
|
|
132
159
|
at: number;
|
|
133
160
|
reason: string;
|
|
134
161
|
}>;
|
|
162
|
+
InvoiceMirrored: z.ZodObject<{
|
|
163
|
+
source: z.ZodString;
|
|
164
|
+
mirror_key: z.ZodString;
|
|
165
|
+
external_refs: z.ZodString;
|
|
166
|
+
customer_id: z.ZodString;
|
|
167
|
+
customer_name: z.ZodString;
|
|
168
|
+
currency: z.ZodString;
|
|
169
|
+
lines: z.ZodString;
|
|
170
|
+
subtotal_cents: z.ZodNumber;
|
|
171
|
+
tax_cents: z.ZodNumber;
|
|
172
|
+
total_cents: z.ZodNumber;
|
|
173
|
+
amount_paid_cents: z.ZodNumber;
|
|
174
|
+
status: z.ZodEnum<["draft", "finalized", "paid", "void"]>;
|
|
175
|
+
issued_at: z.ZodNumber;
|
|
176
|
+
due_at: z.ZodNumber;
|
|
177
|
+
paid_at: z.ZodNumber;
|
|
178
|
+
voided_at: z.ZodNumber;
|
|
179
|
+
provider_updated_at: z.ZodNumber;
|
|
180
|
+
metadata: z.ZodString;
|
|
181
|
+
short_id: z.ZodString;
|
|
182
|
+
created_at: z.ZodNumber;
|
|
183
|
+
at: z.ZodNumber;
|
|
184
|
+
}, "strip", z.ZodTypeAny, {
|
|
185
|
+
at: number;
|
|
186
|
+
lines: string;
|
|
187
|
+
short_id: string;
|
|
188
|
+
status: "paid" | "draft" | "finalized" | "void";
|
|
189
|
+
customer_id: string;
|
|
190
|
+
customer_name: string;
|
|
191
|
+
currency: string;
|
|
192
|
+
subtotal_cents: number;
|
|
193
|
+
total_cents: number;
|
|
194
|
+
amount_paid_cents: number;
|
|
195
|
+
issued_at: number;
|
|
196
|
+
due_at: number;
|
|
197
|
+
created_at: number;
|
|
198
|
+
source: string;
|
|
199
|
+
external_refs: string;
|
|
200
|
+
metadata: string;
|
|
201
|
+
tax_cents: number;
|
|
202
|
+
mirror_key: string;
|
|
203
|
+
paid_at: number;
|
|
204
|
+
voided_at: number;
|
|
205
|
+
provider_updated_at: number;
|
|
206
|
+
}, {
|
|
207
|
+
at: number;
|
|
208
|
+
lines: string;
|
|
209
|
+
short_id: string;
|
|
210
|
+
status: "paid" | "draft" | "finalized" | "void";
|
|
211
|
+
customer_id: string;
|
|
212
|
+
customer_name: string;
|
|
213
|
+
currency: string;
|
|
214
|
+
subtotal_cents: number;
|
|
215
|
+
total_cents: number;
|
|
216
|
+
amount_paid_cents: number;
|
|
217
|
+
issued_at: number;
|
|
218
|
+
due_at: number;
|
|
219
|
+
created_at: number;
|
|
220
|
+
source: string;
|
|
221
|
+
external_refs: string;
|
|
222
|
+
metadata: string;
|
|
223
|
+
tax_cents: number;
|
|
224
|
+
mirror_key: string;
|
|
225
|
+
paid_at: number;
|
|
226
|
+
voided_at: number;
|
|
227
|
+
provider_updated_at: number;
|
|
228
|
+
}>;
|
|
229
|
+
InvoiceFiscalChanged: z.ZodObject<{
|
|
230
|
+
status: z.ZodString;
|
|
231
|
+
system: z.ZodString;
|
|
232
|
+
external_id: z.ZodString;
|
|
233
|
+
message: z.ZodString;
|
|
234
|
+
at: z.ZodNumber;
|
|
235
|
+
}, "strip", z.ZodTypeAny, {
|
|
236
|
+
at: number;
|
|
237
|
+
status: string;
|
|
238
|
+
message: string;
|
|
239
|
+
system: string;
|
|
240
|
+
external_id: string;
|
|
241
|
+
}, {
|
|
242
|
+
at: number;
|
|
243
|
+
status: string;
|
|
244
|
+
message: string;
|
|
245
|
+
system: string;
|
|
246
|
+
external_id: string;
|
|
247
|
+
}>;
|
|
248
|
+
}> & {
|
|
249
|
+
newId(): string;
|
|
250
|
+
};
|
|
251
|
+
/**
|
|
252
|
+
* 1.2.0. An organization's numbering series, and the claims that keep numbers unique. One aggregate, many streams:
|
|
253
|
+
* `numbering-settings` holds the series; one stream per (series key, counter) serializes who takes that number; one per
|
|
254
|
+
* external number serializes who holds it.
|
|
255
|
+
*/
|
|
256
|
+
declare const NumberingEvents: _baseworks_foldbase.EventCatalog<{
|
|
257
|
+
NumberingConfigured: z.ZodObject<{
|
|
258
|
+
prefix: z.ZodString;
|
|
259
|
+
digits: z.ZodNumber;
|
|
260
|
+
yearly: z.ZodBoolean;
|
|
261
|
+
next: z.ZodNumber;
|
|
262
|
+
next_key: z.ZodString;
|
|
263
|
+
time_zone: z.ZodOptional<z.ZodString>;
|
|
264
|
+
at: z.ZodNumber;
|
|
265
|
+
}, "strip", z.ZodTypeAny, {
|
|
266
|
+
at: number;
|
|
267
|
+
prefix: string;
|
|
268
|
+
digits: number;
|
|
269
|
+
yearly: boolean;
|
|
270
|
+
next: number;
|
|
271
|
+
next_key: string;
|
|
272
|
+
time_zone?: string | undefined;
|
|
273
|
+
}, {
|
|
274
|
+
at: number;
|
|
275
|
+
prefix: string;
|
|
276
|
+
digits: number;
|
|
277
|
+
yearly: boolean;
|
|
278
|
+
next: number;
|
|
279
|
+
next_key: string;
|
|
280
|
+
time_zone?: string | undefined;
|
|
281
|
+
}>;
|
|
282
|
+
NumberingCleared: z.ZodObject<{
|
|
283
|
+
at: z.ZodNumber;
|
|
284
|
+
}, "strip", z.ZodTypeAny, {
|
|
285
|
+
at: number;
|
|
286
|
+
}, {
|
|
287
|
+
at: number;
|
|
288
|
+
}>;
|
|
289
|
+
NumberClaimed: z.ZodObject<{
|
|
290
|
+
invoice_id: z.ZodString;
|
|
291
|
+
series_key: z.ZodString;
|
|
292
|
+
number: z.ZodNumber;
|
|
293
|
+
at: z.ZodNumber;
|
|
294
|
+
}, "strip", z.ZodTypeAny, {
|
|
295
|
+
number: number;
|
|
296
|
+
at: number;
|
|
297
|
+
invoice_id: string;
|
|
298
|
+
series_key: string;
|
|
299
|
+
}, {
|
|
300
|
+
number: number;
|
|
301
|
+
at: number;
|
|
302
|
+
invoice_id: string;
|
|
303
|
+
series_key: string;
|
|
304
|
+
}>;
|
|
305
|
+
NumberReleased: z.ZodObject<{
|
|
306
|
+
invoice_id: z.ZodString;
|
|
307
|
+
at: z.ZodNumber;
|
|
308
|
+
}, "strip", z.ZodTypeAny, {
|
|
309
|
+
at: number;
|
|
310
|
+
invoice_id: string;
|
|
311
|
+
}, {
|
|
312
|
+
at: number;
|
|
313
|
+
invoice_id: string;
|
|
314
|
+
}>;
|
|
315
|
+
ExternalNumberClaimed: z.ZodObject<{
|
|
316
|
+
external_number: z.ZodString;
|
|
317
|
+
invoice_id: z.ZodString;
|
|
318
|
+
at: z.ZodNumber;
|
|
319
|
+
}, "strip", z.ZodTypeAny, {
|
|
320
|
+
at: number;
|
|
321
|
+
external_number: string;
|
|
322
|
+
invoice_id: string;
|
|
323
|
+
}, {
|
|
324
|
+
at: number;
|
|
325
|
+
external_number: string;
|
|
326
|
+
invoice_id: string;
|
|
327
|
+
}>;
|
|
135
328
|
}> & {
|
|
136
329
|
newId(): string;
|
|
137
330
|
};
|
|
331
|
+
declare const FISCAL_STATUSES: readonly ["not_submitted", "queued", "submitted", "accepted", "rejected", "cancelled"];
|
|
332
|
+
type FiscalStatus = (typeof FISCAL_STATUSES)[number];
|
|
333
|
+
declare const INVOICE_SOURCES: readonly ["local", "stripe", "manual"];
|
|
334
|
+
type InvoiceSource = (typeof INVOICE_SOURCES)[number];
|
|
138
335
|
/**
|
|
139
336
|
* The transaction ledger — one stream (and one read-model row) PER money movement,
|
|
140
337
|
* unlike the invoice's cached `amount_paid`. A payment emits BOTH `PaymentRecorded`
|
|
@@ -155,6 +352,7 @@ declare const TransactionEvents: _baseworks_foldbase.EventCatalog<{
|
|
|
155
352
|
currency: z.ZodString;
|
|
156
353
|
method: z.ZodString;
|
|
157
354
|
reference: z.ZodString;
|
|
355
|
+
payment_id: z.ZodOptional<z.ZodString>;
|
|
158
356
|
at: z.ZodNumber;
|
|
159
357
|
}, "strip", z.ZodTypeAny, {
|
|
160
358
|
method: string;
|
|
@@ -167,6 +365,7 @@ declare const TransactionEvents: _baseworks_foldbase.EventCatalog<{
|
|
|
167
365
|
invoice_number: number;
|
|
168
366
|
reference: string;
|
|
169
367
|
kind: "payment" | "refund" | "adjustment";
|
|
368
|
+
payment_id?: string | undefined;
|
|
170
369
|
}, {
|
|
171
370
|
method: string;
|
|
172
371
|
at: number;
|
|
@@ -178,9 +377,10 @@ declare const TransactionEvents: _baseworks_foldbase.EventCatalog<{
|
|
|
178
377
|
invoice_number: number;
|
|
179
378
|
reference: string;
|
|
180
379
|
kind: "payment" | "refund" | "adjustment";
|
|
380
|
+
payment_id?: string | undefined;
|
|
181
381
|
}>;
|
|
182
382
|
}> & {
|
|
183
383
|
newId(): string;
|
|
184
384
|
};
|
|
185
385
|
|
|
186
|
-
export { InvoiceEvents, InvoiceStatus, TransactionEvents, TransactionKind };
|
|
386
|
+
export { FISCAL_STATUSES, type FiscalStatus, INVOICE_SOURCES, InvoiceEvents, type InvoiceSource, InvoiceStatus, NumberingEvents, TransactionEvents, TransactionKind };
|
package/dist/events.js
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
import {
|
|
2
|
+
FISCAL_STATUSES,
|
|
3
|
+
INVOICE_SOURCES,
|
|
2
4
|
InvoiceEvents,
|
|
3
5
|
InvoiceStatus,
|
|
6
|
+
NumberingEvents,
|
|
4
7
|
TransactionEvents,
|
|
5
8
|
TransactionKind
|
|
6
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-THLNZVGO.js";
|
|
7
10
|
export {
|
|
11
|
+
FISCAL_STATUSES,
|
|
12
|
+
INVOICE_SOURCES,
|
|
8
13
|
InvoiceEvents,
|
|
9
14
|
InvoiceStatus,
|
|
15
|
+
NumberingEvents,
|
|
10
16
|
TransactionEvents,
|
|
11
17
|
TransactionKind
|
|
12
18
|
};
|
package/dist/mcp.js
CHANGED
|
@@ -14,27 +14,52 @@ function invoiceTools() {
|
|
|
14
14
|
defineTool({
|
|
15
15
|
name: "invoice_list",
|
|
16
16
|
title: "List invoices",
|
|
17
|
-
description:
|
|
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
18
|
input: z.object({
|
|
19
19
|
status: z.enum(["draft", "finalized", "paid", "void"]).optional().describe("Only invoices in this status."),
|
|
20
|
+
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
|
+
overdue: z.boolean().optional().describe("true: only what is owed and past its due date."),
|
|
22
|
+
source: z.enum(["local", "stripe", "manual"]).optional().describe("`local` was made here, `stripe` mirrors a Stripe invoice, `manual` was entered from a document issued elsewhere."),
|
|
23
|
+
fiscal_status: z.enum(["not_submitted", "queued", "submitted", "accepted", "rejected", "cancelled"]).optional().describe("Only invoices in this state with a tax system."),
|
|
24
|
+
customer_id: z.string().optional().describe("Only this customer's invoices: the customer's id."),
|
|
25
|
+
external_number: z.string().optional().describe("Only the invoice with this exact external number (the number another system gave it)."),
|
|
20
26
|
limit: z.number().int().min(1).max(100).optional().describe("Page size, 1 to 100 (default 25)."),
|
|
21
27
|
offset: z.number().int().min(0).optional().describe("How many to skip, for the next page.")
|
|
22
28
|
}),
|
|
23
29
|
access: "read",
|
|
24
30
|
permission: "invoice.read",
|
|
25
31
|
run: async (ctx, a) => ({
|
|
26
|
-
invoices: await ctx.clients.invoice.list({
|
|
32
|
+
invoices: await ctx.clients.invoice.list({
|
|
33
|
+
limit: a.limit ?? 25,
|
|
34
|
+
...a.offset ? { offset: a.offset } : {},
|
|
35
|
+
...a.status ? { status: a.status } : {},
|
|
36
|
+
...a.payment_status ? { payment_status: a.payment_status } : {},
|
|
37
|
+
...a.overdue !== void 0 ? { overdue: a.overdue } : {},
|
|
38
|
+
...a.source ? { source: a.source } : {},
|
|
39
|
+
...a.fiscal_status ? { fiscal_status: a.fiscal_status } : {},
|
|
40
|
+
...a.customer_id ? { customer_id: a.customer_id } : {},
|
|
41
|
+
...a.external_number ? { external_number: a.external_number } : {}
|
|
42
|
+
})
|
|
27
43
|
})
|
|
28
44
|
}),
|
|
29
45
|
defineTool({
|
|
30
46
|
name: "invoice_get",
|
|
31
47
|
title: "Get one invoice",
|
|
32
|
-
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, total, amount paid, balance, issue and due dates. `ref` is the invoice's id or short id. Fails with `not_found` if there is no such invoice. Read only.",
|
|
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.",
|
|
33
49
|
input: z.object({ ref: REF }),
|
|
34
50
|
access: "read",
|
|
35
51
|
permission: "invoice.read",
|
|
36
52
|
run: async (ctx, a) => ({ invoice: await ctx.clients.invoice.get(a.ref) })
|
|
37
53
|
}),
|
|
54
|
+
defineTool({
|
|
55
|
+
name: "invoice_numbering",
|
|
56
|
+
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.",
|
|
58
|
+
input: z.object({}),
|
|
59
|
+
access: "read",
|
|
60
|
+
permission: "invoice.read",
|
|
61
|
+
run: async (ctx) => ({ numbering: await ctx.clients.invoice.getNumbering() })
|
|
62
|
+
}),
|
|
38
63
|
defineTool({
|
|
39
64
|
name: "invoice_transactions",
|
|
40
65
|
title: "List payments",
|
|
@@ -53,11 +78,12 @@ function invoiceTools() {
|
|
|
53
78
|
defineTool({
|
|
54
79
|
name: "invoice_create",
|
|
55
80
|
title: "Draft an invoice",
|
|
56
|
-
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. It is recorded as you, the agent acting for the person who connected you.",
|
|
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.",
|
|
57
82
|
input: z.object({
|
|
58
83
|
customer_id,
|
|
59
84
|
currency: z.string().length(3).describe("ISO 4217 code, e.g. EUR, USD, TRY."),
|
|
60
|
-
lines: z.array(Line).min(1).describe("At least one line.")
|
|
85
|
+
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`).")
|
|
61
87
|
}),
|
|
62
88
|
access: "write",
|
|
63
89
|
permission: "invoice.write",
|
|
@@ -85,11 +111,12 @@ function invoiceTools() {
|
|
|
85
111
|
defineTool({
|
|
86
112
|
name: "invoice_finalize",
|
|
87
113
|
title: "Finalize an invoice",
|
|
88
|
-
description: "Issues a draft: it gets the next number of the organization, 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). Only a draft can be finalized: otherwise `not_draft`.",
|
|
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`.",
|
|
89
115
|
input: z.object({
|
|
90
116
|
ref: REF,
|
|
91
117
|
issued_at: z.number().int().optional().describe("Epoch milliseconds; default now."),
|
|
92
|
-
due_at: z.number().int().optional().describe("Epoch milliseconds; omit for no due date.")
|
|
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.")
|
|
93
120
|
}),
|
|
94
121
|
access: "write",
|
|
95
122
|
permission: "invoice.finalize",
|
|
@@ -106,7 +133,8 @@ function invoiceTools() {
|
|
|
106
133
|
ref: REF,
|
|
107
134
|
amount_cents: z.number().int().positive().describe("Integer cents received: 100.00 is 10000."),
|
|
108
135
|
method: z.string().optional().describe("e.g. bank, cash, card."),
|
|
109
|
-
reference: z.string().optional().describe("The payer's or processor's own id for this payment; makes a retry safe.")
|
|
136
|
+
reference: z.string().optional().describe("The payer's or processor's own id for this payment; makes a retry safe."),
|
|
137
|
+
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.")
|
|
110
138
|
}),
|
|
111
139
|
access: "write",
|
|
112
140
|
permission: "invoice.record_payment",
|
|
@@ -115,6 +143,21 @@ function invoiceTools() {
|
|
|
115
143
|
return { invoice: await ctx.clients.invoice.recordPayment(ref, input) };
|
|
116
144
|
}
|
|
117
145
|
}),
|
|
146
|
+
defineTool({
|
|
147
|
+
name: "invoice_configure_numbering",
|
|
148
|
+
title: "Set the numbering series",
|
|
149
|
+
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.",
|
|
150
|
+
input: z.object({
|
|
151
|
+
prefix: z.string().regex(/^[A-Z0-9]{3}$/).describe("Exactly 3 uppercase letters or digits."),
|
|
152
|
+
digits: z.number().int().min(1).max(12).describe("Counter width: 9 for e-Fatura."),
|
|
153
|
+
yearly: z.boolean().describe("true: the counter restarts each calendar year (of the issue date)."),
|
|
154
|
+
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)."),
|
|
155
|
+
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.")
|
|
156
|
+
}),
|
|
157
|
+
access: "write",
|
|
158
|
+
permission: "invoice.configure",
|
|
159
|
+
run: async (ctx, a) => ({ numbering: await ctx.clients.invoice.setNumbering(a) })
|
|
160
|
+
}),
|
|
118
161
|
defineTool({
|
|
119
162
|
name: "invoice_void",
|
|
120
163
|
title: "Void an invoice",
|
package/dist/projections.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
InvoiceEvents,
|
|
3
3
|
TransactionEvents
|
|
4
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-THLNZVGO.js";
|
|
5
5
|
|
|
6
6
|
// src/projections.ts
|
|
7
7
|
import { defineProjection } from "@baseworks/foldbase";
|
|
@@ -19,6 +19,9 @@ var readInvoices = defineProjection("invoices", InvoiceEvents, (on) => ({
|
|
|
19
19
|
issued_at: 0,
|
|
20
20
|
due_at: 0,
|
|
21
21
|
short_id: e.short_id,
|
|
22
|
+
source: e.source ?? null,
|
|
23
|
+
metadata: e.metadata ?? null,
|
|
24
|
+
external_number: e.external_number ?? null,
|
|
22
25
|
created_at: e.at
|
|
23
26
|
})),
|
|
24
27
|
InvoiceUpdated: on.InvoiceUpdated.upsert((e) => ({
|
|
@@ -26,7 +29,9 @@ var readInvoices = defineProjection("invoices", InvoiceEvents, (on) => ({
|
|
|
26
29
|
customer_name: e.customer_name,
|
|
27
30
|
lines: e.lines,
|
|
28
31
|
subtotal_cents: e.subtotal_cents,
|
|
29
|
-
total_cents: e.total_cents
|
|
32
|
+
total_cents: e.total_cents,
|
|
33
|
+
metadata: e.metadata ?? null,
|
|
34
|
+
external_number: e.external_number ?? null
|
|
30
35
|
})),
|
|
31
36
|
InvoiceFinalized: on.InvoiceFinalized.upsert((e) => ({
|
|
32
37
|
number: e.number,
|
|
@@ -35,7 +40,10 @@ var readInvoices = defineProjection("invoices", InvoiceEvents, (on) => ({
|
|
|
35
40
|
subtotal_cents: e.subtotal_cents,
|
|
36
41
|
total_cents: e.total_cents,
|
|
37
42
|
issued_at: e.issued_at,
|
|
38
|
-
due_at: e.due_at
|
|
43
|
+
due_at: e.due_at,
|
|
44
|
+
number_text: e.number_text ?? null,
|
|
45
|
+
series_key: e.series_key ?? null,
|
|
46
|
+
external_number: e.external_number ?? null
|
|
39
47
|
})),
|
|
40
48
|
PaymentRecorded: on.PaymentRecorded.upsert((e) => ({
|
|
41
49
|
amount_paid_cents: e.amount_paid_cents,
|
|
@@ -43,6 +51,37 @@ var readInvoices = defineProjection("invoices", InvoiceEvents, (on) => ({
|
|
|
43
51
|
})),
|
|
44
52
|
InvoiceVoided: on.InvoiceVoided.upsert(() => ({
|
|
45
53
|
status: "void"
|
|
54
|
+
})),
|
|
55
|
+
// The provider's whole document. `number` is 0 and stays 0: a mirror has no number of the organization's own.
|
|
56
|
+
InvoiceMirrored: on.InvoiceMirrored.upsert((e) => ({
|
|
57
|
+
source: e.source,
|
|
58
|
+
mirror_key: e.mirror_key,
|
|
59
|
+
external_refs: e.external_refs,
|
|
60
|
+
customer_id: e.customer_id,
|
|
61
|
+
customer_name: e.customer_name,
|
|
62
|
+
currency: e.currency,
|
|
63
|
+
lines: e.lines,
|
|
64
|
+
number: 0,
|
|
65
|
+
status: e.status,
|
|
66
|
+
subtotal_cents: e.subtotal_cents,
|
|
67
|
+
tax_cents: e.tax_cents,
|
|
68
|
+
total_cents: e.total_cents,
|
|
69
|
+
amount_paid_cents: e.amount_paid_cents,
|
|
70
|
+
issued_at: e.issued_at,
|
|
71
|
+
due_at: e.due_at,
|
|
72
|
+
paid_at: e.paid_at,
|
|
73
|
+
voided_at: e.voided_at,
|
|
74
|
+
provider_updated_at: e.provider_updated_at,
|
|
75
|
+
metadata: e.metadata,
|
|
76
|
+
short_id: e.short_id,
|
|
77
|
+
created_at: e.created_at
|
|
78
|
+
})),
|
|
79
|
+
InvoiceFiscalChanged: on.InvoiceFiscalChanged.upsert((e) => ({
|
|
80
|
+
fiscal_status: e.status,
|
|
81
|
+
fiscal_system: e.system,
|
|
82
|
+
fiscal_external_id: e.external_id,
|
|
83
|
+
fiscal_message: e.message,
|
|
84
|
+
fiscal_updated_at: e.at
|
|
46
85
|
}))
|
|
47
86
|
}));
|
|
48
87
|
var readTransactions = defineProjection("transactions", TransactionEvents, (on) => ({
|
|
@@ -56,6 +95,7 @@ var readTransactions = defineProjection("transactions", TransactionEvents, (on)
|
|
|
56
95
|
currency: e.currency,
|
|
57
96
|
method: e.method,
|
|
58
97
|
reference: e.reference,
|
|
98
|
+
payment_id: e.payment_id ?? null,
|
|
59
99
|
created_at: e.at
|
|
60
100
|
}))
|
|
61
101
|
}));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@baseworks/invoice",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Invoice capability: typed HTTP client, event catalog, projections, and CLI plugin for invoice-service (AR ledger). Contract-first, versioned with the service.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"publishConfig": {
|
|
@@ -12,6 +12,8 @@ const DRAFT = {
|
|
|
12
12
|
id: '019f6a3e-1111-7c71-beff-496d1be3fd1f',
|
|
13
13
|
short_id: 'inv9x2',
|
|
14
14
|
number: null,
|
|
15
|
+
number_text: null,
|
|
16
|
+
external_number: null,
|
|
15
17
|
status: 'draft',
|
|
16
18
|
customer_id: 'cst1',
|
|
17
19
|
customer_name: 'Acme Co',
|
|
@@ -26,9 +28,27 @@ const DRAFT = {
|
|
|
26
28
|
created_at: 1784200000000,
|
|
27
29
|
updatedAt: 1784200000000,
|
|
28
30
|
}
|
|
29
|
-
const FINAL = { ...DRAFT, number: 1, status: 'finalized', issued_at: 1784200000000 }
|
|
31
|
+
const FINAL = { ...DRAFT, number: 1, number_text: '1', status: 'finalized', issued_at: 1784200000000 }
|
|
30
32
|
const PAID = { ...FINAL, status: 'paid', amount_paid_cents: 150000, balance_cents: 0 }
|
|
31
33
|
|
|
34
|
+
describe('dtab invoices (list filters)', () => {
|
|
35
|
+
it('sends every filter, on the bare command and on ls, and shows the payment column', async () => {
|
|
36
|
+
let seen = ''
|
|
37
|
+
server.use(http.get(`${TEST_BASE}/invoices`, ({ request }) => {
|
|
38
|
+
seen = new URL(request.url).search
|
|
39
|
+
return HttpResponse.json({ invoices: [{ ...DRAFT, status: 'finalized', payment_status: 'unpaid' }] })
|
|
40
|
+
}))
|
|
41
|
+
const r = await run(['invoices', '--payment-status', 'unpaid,partially_paid', '--overdue', '--source', 'stripe', '--fiscal-status', 'queued', '--customer-id', 'cst1', '--status', 'finalized'])
|
|
42
|
+
expect(r.exitCode).toBe(0)
|
|
43
|
+
const q = Object.fromEntries(new URLSearchParams(seen))
|
|
44
|
+
expect(q).toEqual({ status: 'finalized', payment_status: 'unpaid,partially_paid', overdue: 'true', source: 'stripe', fiscal_status: 'queued', customer_id: 'cst1' })
|
|
45
|
+
expect(r.stdout).toContain('PAYMENT')
|
|
46
|
+
expect(r.stdout).toContain('unpaid')
|
|
47
|
+
await run(['invoices', 'ls', '--overdue', '--payment-status', 'paid'])
|
|
48
|
+
expect(Object.fromEntries(new URLSearchParams(seen))).toEqual({ payment_status: 'paid', overdue: 'true' })
|
|
49
|
+
})
|
|
50
|
+
})
|
|
51
|
+
|
|
32
52
|
describe('dtab invoices (list)', () => {
|
|
33
53
|
it('columns incl. NO/STATUS/TOTAL/BALANCE, draft NO shows —', async () => {
|
|
34
54
|
mocks.mockGet('/invoices', { invoices: [DRAFT, FINAL] })
|
|
@@ -115,3 +135,37 @@ describe('dtab invoices get / finalize / pay / void', () => {
|
|
|
115
135
|
expect(r.stdout).toContain('inv9x2')
|
|
116
136
|
})
|
|
117
137
|
})
|
|
138
|
+
|
|
139
|
+
describe('dtab invoices (numbers)', () => {
|
|
140
|
+
it('create and finalize pass --number as the external number; the list filters by --external-number', async () => {
|
|
141
|
+
const cap = mocks.mockPostCapture('/invoices', { invoice: DRAFT })
|
|
142
|
+
await run(['invoices', 'create', '--customer', 'cst1', '--currency', 'USD', '--line', 'x|1|1.00', '--number', 'BSF2026000000148'])
|
|
143
|
+
expect(cap.body).toMatchObject({ external_number: 'BSF2026000000148' })
|
|
144
|
+
const fin = mocks.mockPostCapture('/invoices/inv9x2/finalize', { invoice: { ...FINAL, number: null, number_text: 'PORTAL-1', external_number: 'PORTAL-1' } })
|
|
145
|
+
const r = await run(['invoices', 'finalize', 'inv9x2', '--number', 'PORTAL-1'])
|
|
146
|
+
expect(fin.body).toMatchObject({ external_number: 'PORTAL-1' })
|
|
147
|
+
expect(r.stdout).toContain('PORTAL-1')
|
|
148
|
+
let seen = ''
|
|
149
|
+
server.use(http.get(`${TEST_BASE}/invoices`, ({ request }) => { seen = new URL(request.url).search; return HttpResponse.json({ invoices: [{ ...FINAL, number_text: 'BSF2026000000148' }] }) }))
|
|
150
|
+
const list = await run(['invoices', '--external-number', 'BSF2026000000148'])
|
|
151
|
+
expect(Object.fromEntries(new URLSearchParams(seen))).toEqual({ external_number: 'BSF2026000000148' })
|
|
152
|
+
expect(list.stdout).toContain('BSF2026000000148')
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
it('numbering shows the series, sets it, clears it', async () => {
|
|
156
|
+
mocks.mockGet('/settings/numbering', { numbering: { prefix: 'BSF', digits: 9, yearly: true, next: 149, next_text: 'BSF2026000000149', updated_at: 1 } })
|
|
157
|
+
const shown = await run(['invoices', 'numbering'])
|
|
158
|
+
expect(shown.exitCode).toBe(0)
|
|
159
|
+
expect(shown.stdout).toContain('BSF2026000000149')
|
|
160
|
+
let body: unknown
|
|
161
|
+
server.use(http.put(`${TEST_BASE}/settings/numbering`, async ({ request }) => {
|
|
162
|
+
body = await request.json()
|
|
163
|
+
return HttpResponse.json({ numbering: { prefix: 'BSF', digits: 9, yearly: true, next: 148, next_text: 'BSF2026000000148', updated_at: 2 } })
|
|
164
|
+
}))
|
|
165
|
+
const set = await run(['invoices', 'numbering', 'set', '--prefix', 'BSF', '--digits', '9', '--yearly', '--next', '148', '--time-zone', 'Europe/Istanbul'])
|
|
166
|
+
expect(set.exitCode).toBe(0)
|
|
167
|
+
expect(body).toEqual({ prefix: 'BSF', digits: 9, yearly: true, next: 148, time_zone: 'Europe/Istanbul' })
|
|
168
|
+
mocks.mockGet('/settings/numbering', { numbering: null })
|
|
169
|
+
expect((await run(['invoices', 'numbering'])).stdout).toContain('1, 2, 3')
|
|
170
|
+
})
|
|
171
|
+
})
|