@baseworks/invoice 0.1.1 → 1.1.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 +81 -14
- package/dist/chunk-335MPCTE.js +133 -0
- package/dist/cli.js +58 -42
- package/dist/client.d.ts +216 -29
- package/dist/client.js +99 -23
- package/dist/events.d.ts +180 -69
- package/dist/events.js +5 -1
- package/dist/graphql.d.ts +22 -0
- package/dist/graphql.js +17 -2
- package/dist/mcp.d.ts +13 -0
- package/dist/mcp.js +146 -0
- package/dist/projections.js +55 -20
- package/package.json +14 -3
- package/src/__tests__/cli.test.ts +43 -25
- package/src/__tests__/client.test.ts +147 -0
- package/src/__tests__/dts-exports.test.ts +75 -0
- package/src/__tests__/mcp.test.ts +163 -0
- package/src/cli.ts +85 -60
- package/src/client.ts +78 -26
- package/src/compat.ts +61 -0
- package/src/events.ts +72 -22
- package/src/graphql.ts +17 -2
- package/src/mcp.ts +191 -0
- package/src/projections.ts +54 -19
- package/src/types.ts +208 -22
- package/dist/chunk-7P7ARN3X.js +0 -78
package/dist/events.d.ts
CHANGED
|
@@ -15,106 +15,124 @@ import { z } from 'zod';
|
|
|
15
15
|
* absolute values in the event, so the projection is a plain upsert (no
|
|
16
16
|
* accumulation). `lines` is a serialized JSON array. Timestamps are epoch millis.
|
|
17
17
|
* A FINALIZED invoice is immutable: only PaymentRecorded / InvoiceVoided follow.
|
|
18
|
+
*
|
|
19
|
+
* Payload fields are snake_case (0.3.0). Events written before are camelCase
|
|
20
|
+
* (`customerId`, `totalCents`, …) and stay so in the log: foldbase reads a
|
|
21
|
+
* snake_case path from a camelCase payload when the snake_case key is absent, so
|
|
22
|
+
* the projections fold both. The `lines` JSON inside a payload is the same: old
|
|
23
|
+
* lines carry `unitPriceCents`, `amountCents`, `productId`; the service reads both.
|
|
18
24
|
*/
|
|
19
25
|
declare const InvoiceStatus: z.ZodEnum<["draft", "finalized", "paid", "void"]>;
|
|
20
26
|
type InvoiceStatus = z.infer<typeof InvoiceStatus>;
|
|
21
27
|
declare const InvoiceEvents: _baseworks_foldbase.EventCatalog<{
|
|
22
28
|
InvoiceDrafted: z.ZodObject<{
|
|
23
|
-
|
|
24
|
-
|
|
29
|
+
customer_id: z.ZodString;
|
|
30
|
+
customer_name: z.ZodString;
|
|
25
31
|
currency: z.ZodString;
|
|
26
32
|
lines: z.ZodString;
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
33
|
+
subtotal_cents: z.ZodNumber;
|
|
34
|
+
total_cents: z.ZodNumber;
|
|
35
|
+
short_id: z.ZodString;
|
|
36
|
+
source: z.ZodOptional<z.ZodString>;
|
|
37
|
+
metadata: z.ZodOptional<z.ZodString>;
|
|
30
38
|
at: z.ZodNumber;
|
|
31
39
|
}, "strip", z.ZodTypeAny, {
|
|
32
40
|
at: number;
|
|
33
|
-
customerId: string;
|
|
34
|
-
customerName: string;
|
|
35
|
-
currency: string;
|
|
36
41
|
lines: string;
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
42
|
+
short_id: string;
|
|
43
|
+
customer_id: string;
|
|
44
|
+
customer_name: string;
|
|
45
|
+
currency: string;
|
|
46
|
+
subtotal_cents: number;
|
|
47
|
+
total_cents: number;
|
|
48
|
+
source?: string | undefined;
|
|
49
|
+
metadata?: string | undefined;
|
|
40
50
|
}, {
|
|
41
51
|
at: number;
|
|
42
|
-
customerId: string;
|
|
43
|
-
customerName: string;
|
|
44
|
-
currency: string;
|
|
45
52
|
lines: string;
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
53
|
+
short_id: string;
|
|
54
|
+
customer_id: string;
|
|
55
|
+
customer_name: string;
|
|
56
|
+
currency: string;
|
|
57
|
+
subtotal_cents: number;
|
|
58
|
+
total_cents: number;
|
|
59
|
+
source?: string | undefined;
|
|
60
|
+
metadata?: string | undefined;
|
|
49
61
|
}>;
|
|
50
62
|
InvoiceUpdated: z.ZodObject<{
|
|
51
|
-
|
|
52
|
-
|
|
63
|
+
customer_id: z.ZodString;
|
|
64
|
+
customer_name: z.ZodString;
|
|
53
65
|
lines: z.ZodString;
|
|
54
|
-
|
|
55
|
-
|
|
66
|
+
subtotal_cents: z.ZodNumber;
|
|
67
|
+
total_cents: z.ZodNumber;
|
|
68
|
+
metadata: z.ZodOptional<z.ZodString>;
|
|
56
69
|
at: z.ZodNumber;
|
|
57
70
|
}, "strip", z.ZodTypeAny, {
|
|
58
71
|
at: number;
|
|
59
|
-
customerId: string;
|
|
60
|
-
customerName: string;
|
|
61
72
|
lines: string;
|
|
62
|
-
|
|
63
|
-
|
|
73
|
+
customer_id: string;
|
|
74
|
+
customer_name: string;
|
|
75
|
+
subtotal_cents: number;
|
|
76
|
+
total_cents: number;
|
|
77
|
+
metadata?: string | undefined;
|
|
64
78
|
}, {
|
|
65
79
|
at: number;
|
|
66
|
-
customerId: string;
|
|
67
|
-
customerName: string;
|
|
68
80
|
lines: string;
|
|
69
|
-
|
|
70
|
-
|
|
81
|
+
customer_id: string;
|
|
82
|
+
customer_name: string;
|
|
83
|
+
subtotal_cents: number;
|
|
84
|
+
total_cents: number;
|
|
85
|
+
metadata?: string | undefined;
|
|
71
86
|
}>;
|
|
72
87
|
InvoiceFinalized: z.ZodObject<{
|
|
73
88
|
number: z.ZodNumber;
|
|
74
89
|
lines: z.ZodString;
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
90
|
+
subtotal_cents: z.ZodNumber;
|
|
91
|
+
total_cents: z.ZodNumber;
|
|
92
|
+
issued_at: z.ZodNumber;
|
|
93
|
+
due_at: z.ZodNumber;
|
|
79
94
|
at: z.ZodNumber;
|
|
80
95
|
}, "strip", z.ZodTypeAny, {
|
|
81
96
|
number: number;
|
|
82
97
|
at: number;
|
|
83
98
|
lines: string;
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
99
|
+
subtotal_cents: number;
|
|
100
|
+
total_cents: number;
|
|
101
|
+
issued_at: number;
|
|
102
|
+
due_at: number;
|
|
88
103
|
}, {
|
|
89
104
|
number: number;
|
|
90
105
|
at: number;
|
|
91
106
|
lines: string;
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
107
|
+
subtotal_cents: number;
|
|
108
|
+
total_cents: number;
|
|
109
|
+
issued_at: number;
|
|
110
|
+
due_at: number;
|
|
96
111
|
}>;
|
|
97
112
|
PaymentRecorded: z.ZodObject<{
|
|
98
|
-
|
|
99
|
-
|
|
113
|
+
amount_cents: z.ZodNumber;
|
|
114
|
+
amount_paid_cents: z.ZodNumber;
|
|
100
115
|
status: z.ZodEnum<["draft", "finalized", "paid", "void"]>;
|
|
101
116
|
method: z.ZodString;
|
|
102
117
|
reference: z.ZodString;
|
|
118
|
+
payment_id: z.ZodOptional<z.ZodString>;
|
|
103
119
|
at: z.ZodNumber;
|
|
104
120
|
}, "strip", z.ZodTypeAny, {
|
|
105
|
-
at: number;
|
|
106
|
-
status: "draft" | "finalized" | "paid" | "void";
|
|
107
|
-
amountCents: number;
|
|
108
|
-
amountPaidCents: number;
|
|
109
121
|
method: string;
|
|
122
|
+
at: number;
|
|
123
|
+
status: "paid" | "draft" | "finalized" | "void";
|
|
124
|
+
amount_paid_cents: number;
|
|
125
|
+
amount_cents: number;
|
|
110
126
|
reference: string;
|
|
127
|
+
payment_id?: string | undefined;
|
|
111
128
|
}, {
|
|
112
|
-
at: number;
|
|
113
|
-
status: "draft" | "finalized" | "paid" | "void";
|
|
114
|
-
amountCents: number;
|
|
115
|
-
amountPaidCents: number;
|
|
116
129
|
method: string;
|
|
130
|
+
at: number;
|
|
131
|
+
status: "paid" | "draft" | "finalized" | "void";
|
|
132
|
+
amount_paid_cents: number;
|
|
133
|
+
amount_cents: number;
|
|
117
134
|
reference: string;
|
|
135
|
+
payment_id?: string | undefined;
|
|
118
136
|
}>;
|
|
119
137
|
InvoiceVoided: z.ZodObject<{
|
|
120
138
|
reason: z.ZodString;
|
|
@@ -126,9 +144,99 @@ declare const InvoiceEvents: _baseworks_foldbase.EventCatalog<{
|
|
|
126
144
|
at: number;
|
|
127
145
|
reason: string;
|
|
128
146
|
}>;
|
|
147
|
+
InvoiceMirrored: z.ZodObject<{
|
|
148
|
+
source: z.ZodString;
|
|
149
|
+
mirror_key: z.ZodString;
|
|
150
|
+
external_refs: z.ZodString;
|
|
151
|
+
customer_id: z.ZodString;
|
|
152
|
+
customer_name: z.ZodString;
|
|
153
|
+
currency: z.ZodString;
|
|
154
|
+
lines: z.ZodString;
|
|
155
|
+
subtotal_cents: z.ZodNumber;
|
|
156
|
+
tax_cents: z.ZodNumber;
|
|
157
|
+
total_cents: z.ZodNumber;
|
|
158
|
+
amount_paid_cents: z.ZodNumber;
|
|
159
|
+
status: z.ZodEnum<["draft", "finalized", "paid", "void"]>;
|
|
160
|
+
issued_at: z.ZodNumber;
|
|
161
|
+
due_at: z.ZodNumber;
|
|
162
|
+
paid_at: z.ZodNumber;
|
|
163
|
+
voided_at: z.ZodNumber;
|
|
164
|
+
provider_updated_at: z.ZodNumber;
|
|
165
|
+
metadata: z.ZodString;
|
|
166
|
+
short_id: z.ZodString;
|
|
167
|
+
created_at: z.ZodNumber;
|
|
168
|
+
at: z.ZodNumber;
|
|
169
|
+
}, "strip", z.ZodTypeAny, {
|
|
170
|
+
at: number;
|
|
171
|
+
lines: string;
|
|
172
|
+
short_id: string;
|
|
173
|
+
status: "paid" | "draft" | "finalized" | "void";
|
|
174
|
+
customer_id: string;
|
|
175
|
+
customer_name: string;
|
|
176
|
+
currency: string;
|
|
177
|
+
subtotal_cents: number;
|
|
178
|
+
total_cents: number;
|
|
179
|
+
amount_paid_cents: number;
|
|
180
|
+
issued_at: number;
|
|
181
|
+
due_at: number;
|
|
182
|
+
created_at: number;
|
|
183
|
+
source: string;
|
|
184
|
+
external_refs: string;
|
|
185
|
+
metadata: string;
|
|
186
|
+
tax_cents: number;
|
|
187
|
+
mirror_key: string;
|
|
188
|
+
paid_at: number;
|
|
189
|
+
voided_at: number;
|
|
190
|
+
provider_updated_at: number;
|
|
191
|
+
}, {
|
|
192
|
+
at: number;
|
|
193
|
+
lines: string;
|
|
194
|
+
short_id: string;
|
|
195
|
+
status: "paid" | "draft" | "finalized" | "void";
|
|
196
|
+
customer_id: string;
|
|
197
|
+
customer_name: string;
|
|
198
|
+
currency: string;
|
|
199
|
+
subtotal_cents: number;
|
|
200
|
+
total_cents: number;
|
|
201
|
+
amount_paid_cents: number;
|
|
202
|
+
issued_at: number;
|
|
203
|
+
due_at: number;
|
|
204
|
+
created_at: number;
|
|
205
|
+
source: string;
|
|
206
|
+
external_refs: string;
|
|
207
|
+
metadata: string;
|
|
208
|
+
tax_cents: number;
|
|
209
|
+
mirror_key: string;
|
|
210
|
+
paid_at: number;
|
|
211
|
+
voided_at: number;
|
|
212
|
+
provider_updated_at: number;
|
|
213
|
+
}>;
|
|
214
|
+
InvoiceFiscalChanged: z.ZodObject<{
|
|
215
|
+
status: z.ZodString;
|
|
216
|
+
system: z.ZodString;
|
|
217
|
+
external_id: z.ZodString;
|
|
218
|
+
message: z.ZodString;
|
|
219
|
+
at: z.ZodNumber;
|
|
220
|
+
}, "strip", z.ZodTypeAny, {
|
|
221
|
+
at: number;
|
|
222
|
+
status: string;
|
|
223
|
+
message: string;
|
|
224
|
+
system: string;
|
|
225
|
+
external_id: string;
|
|
226
|
+
}, {
|
|
227
|
+
at: number;
|
|
228
|
+
status: string;
|
|
229
|
+
message: string;
|
|
230
|
+
system: string;
|
|
231
|
+
external_id: string;
|
|
232
|
+
}>;
|
|
129
233
|
}> & {
|
|
130
234
|
newId(): string;
|
|
131
235
|
};
|
|
236
|
+
declare const FISCAL_STATUSES: readonly ["not_submitted", "queued", "submitted", "accepted", "rejected", "cancelled"];
|
|
237
|
+
type FiscalStatus = (typeof FISCAL_STATUSES)[number];
|
|
238
|
+
declare const INVOICE_SOURCES: readonly ["local", "stripe", "manual"];
|
|
239
|
+
type InvoiceSource = (typeof INVOICE_SOURCES)[number];
|
|
132
240
|
/**
|
|
133
241
|
* The transaction ledger — one stream (and one read-model row) PER money movement,
|
|
134
242
|
* unlike the invoice's cached `amount_paid`. A payment emits BOTH `PaymentRecorded`
|
|
@@ -140,41 +248,44 @@ declare const TransactionKind: z.ZodEnum<["payment", "refund", "adjustment"]>;
|
|
|
140
248
|
type TransactionKind = z.infer<typeof TransactionKind>;
|
|
141
249
|
declare const TransactionEvents: _baseworks_foldbase.EventCatalog<{
|
|
142
250
|
TransactionRecorded: z.ZodObject<{
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
251
|
+
invoice_id: z.ZodString;
|
|
252
|
+
invoice_number: z.ZodNumber;
|
|
253
|
+
customer_id: z.ZodString;
|
|
254
|
+
customer_name: z.ZodString;
|
|
147
255
|
kind: z.ZodEnum<["payment", "refund", "adjustment"]>;
|
|
148
|
-
|
|
256
|
+
amount_cents: z.ZodNumber;
|
|
149
257
|
currency: z.ZodString;
|
|
150
258
|
method: z.ZodString;
|
|
151
259
|
reference: z.ZodString;
|
|
260
|
+
payment_id: z.ZodOptional<z.ZodString>;
|
|
152
261
|
at: z.ZodNumber;
|
|
153
262
|
}, "strip", z.ZodTypeAny, {
|
|
263
|
+
method: string;
|
|
154
264
|
at: number;
|
|
155
|
-
|
|
156
|
-
|
|
265
|
+
customer_id: string;
|
|
266
|
+
customer_name: string;
|
|
157
267
|
currency: string;
|
|
158
|
-
|
|
159
|
-
|
|
268
|
+
amount_cents: number;
|
|
269
|
+
invoice_id: string;
|
|
270
|
+
invoice_number: number;
|
|
160
271
|
reference: string;
|
|
161
|
-
invoiceId: string;
|
|
162
|
-
invoiceNumber: number;
|
|
163
272
|
kind: "payment" | "refund" | "adjustment";
|
|
273
|
+
payment_id?: string | undefined;
|
|
164
274
|
}, {
|
|
275
|
+
method: string;
|
|
165
276
|
at: number;
|
|
166
|
-
|
|
167
|
-
|
|
277
|
+
customer_id: string;
|
|
278
|
+
customer_name: string;
|
|
168
279
|
currency: string;
|
|
169
|
-
|
|
170
|
-
|
|
280
|
+
amount_cents: number;
|
|
281
|
+
invoice_id: string;
|
|
282
|
+
invoice_number: number;
|
|
171
283
|
reference: string;
|
|
172
|
-
invoiceId: string;
|
|
173
|
-
invoiceNumber: number;
|
|
174
284
|
kind: "payment" | "refund" | "adjustment";
|
|
285
|
+
payment_id?: string | undefined;
|
|
175
286
|
}>;
|
|
176
287
|
}> & {
|
|
177
288
|
newId(): string;
|
|
178
289
|
};
|
|
179
290
|
|
|
180
|
-
export { InvoiceEvents, InvoiceStatus, TransactionEvents, TransactionKind };
|
|
291
|
+
export { FISCAL_STATUSES, type FiscalStatus, INVOICE_SOURCES, InvoiceEvents, type InvoiceSource, InvoiceStatus, TransactionEvents, TransactionKind };
|
package/dist/events.js
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
import {
|
|
2
|
+
FISCAL_STATUSES,
|
|
3
|
+
INVOICE_SOURCES,
|
|
2
4
|
InvoiceEvents,
|
|
3
5
|
InvoiceStatus,
|
|
4
6
|
TransactionEvents,
|
|
5
7
|
TransactionKind
|
|
6
|
-
} from "./chunk-
|
|
8
|
+
} from "./chunk-335MPCTE.js";
|
|
7
9
|
export {
|
|
10
|
+
FISCAL_STATUSES,
|
|
11
|
+
INVOICE_SOURCES,
|
|
8
12
|
InvoiceEvents,
|
|
9
13
|
InvoiceStatus,
|
|
10
14
|
TransactionEvents,
|
package/dist/graphql.d.ts
CHANGED
|
@@ -27,7 +27,29 @@ declare const resolvers: {
|
|
|
27
27
|
status?: string;
|
|
28
28
|
}, ctx: InvoiceGraphContext) => Promise<Invoice[]>;
|
|
29
29
|
};
|
|
30
|
+
InvoiceLine: {
|
|
31
|
+
unitPriceCents: (l: {
|
|
32
|
+
unit_price_cents: number;
|
|
33
|
+
}) => number;
|
|
34
|
+
amountCents: (l: {
|
|
35
|
+
amount_cents: number;
|
|
36
|
+
}) => number;
|
|
37
|
+
};
|
|
38
|
+
Transaction: {
|
|
39
|
+
amountCents: (t: {
|
|
40
|
+
amount_cents: number;
|
|
41
|
+
}) => number;
|
|
42
|
+
createdAt: (t: {
|
|
43
|
+
created_at: number;
|
|
44
|
+
}) => number;
|
|
45
|
+
};
|
|
30
46
|
Invoice: {
|
|
47
|
+
subtotalCents: (p: Invoice) => number;
|
|
48
|
+
totalCents: (p: Invoice) => number;
|
|
49
|
+
amountPaidCents: (p: Invoice) => number;
|
|
50
|
+
balanceCents: (p: Invoice) => number;
|
|
51
|
+
dueAt: (p: Invoice) => number | null;
|
|
52
|
+
createdAt: (p: Invoice) => number;
|
|
31
53
|
customer: (parent: Invoice, _: unknown, ctx: InvoiceGraphContext) => Promise<unknown>;
|
|
32
54
|
transactions: (parent: Invoice, _: unknown, ctx: InvoiceGraphContext) => Promise<never[] | Transaction[]>;
|
|
33
55
|
};
|
package/dist/graphql.js
CHANGED
|
@@ -47,10 +47,25 @@ var resolvers = {
|
|
|
47
47
|
invoices: (_, { status }, ctx) => ctx.invoice.list(status ? { status } : {})
|
|
48
48
|
},
|
|
49
49
|
Customer: {
|
|
50
|
-
invoices: async (parent, { status }, ctx) => (await ctx.invoice.list(status ? { status } : {})).filter((i) => i.
|
|
50
|
+
invoices: async (parent, { status }, ctx) => (await ctx.invoice.list(status ? { status } : {})).filter((i) => i.customer_id === parent.id)
|
|
51
|
+
},
|
|
52
|
+
InvoiceLine: {
|
|
53
|
+
unitPriceCents: (l) => l.unit_price_cents,
|
|
54
|
+
amountCents: (l) => l.amount_cents
|
|
55
|
+
},
|
|
56
|
+
Transaction: {
|
|
57
|
+
amountCents: (t) => t.amount_cents,
|
|
58
|
+
createdAt: (t) => t.created_at
|
|
51
59
|
},
|
|
52
60
|
Invoice: {
|
|
53
|
-
|
|
61
|
+
// The schema is camelCase (that contract's own); the data is snake_case.
|
|
62
|
+
subtotalCents: (p) => p.subtotal_cents,
|
|
63
|
+
totalCents: (p) => p.total_cents,
|
|
64
|
+
amountPaidCents: (p) => p.amount_paid_cents,
|
|
65
|
+
balanceCents: (p) => p.balance_cents,
|
|
66
|
+
dueAt: (p) => p.due_at,
|
|
67
|
+
createdAt: (p) => p.created_at,
|
|
68
|
+
customer: (parent, _, ctx) => ctx.customerLoader.load(parent.customer_id),
|
|
54
69
|
transactions: (parent, _, ctx) => ctx.invoice.invoiceTransactions(parent.id).catch(() => [])
|
|
55
70
|
}
|
|
56
71
|
};
|
package/dist/mcp.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { McpTool } from '@baseworks/mcp';
|
|
2
|
+
import { InvoiceClient } from './client.js';
|
|
3
|
+
import './events.js';
|
|
4
|
+
import '@baseworks/foldbase';
|
|
5
|
+
import 'zod';
|
|
6
|
+
|
|
7
|
+
/** What `ctx.clients` must hold for these tools. */
|
|
8
|
+
interface InvoiceMcpClients {
|
|
9
|
+
invoice: InvoiceClient;
|
|
10
|
+
}
|
|
11
|
+
declare function invoiceTools(): McpTool<InvoiceMcpClients>[];
|
|
12
|
+
|
|
13
|
+
export { type InvoiceMcpClients, invoiceTools };
|
package/dist/mcp.js
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
// src/mcp.ts
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { defineTool } from "@baseworks/mcp";
|
|
4
|
+
var REF = z.string().min(1).describe("The invoice's id or its short id.");
|
|
5
|
+
var Line = z.object({
|
|
6
|
+
description: z.string().min(1).describe("What is being charged for."),
|
|
7
|
+
quantity: z.number().positive().describe("How many (may be fractional, e.g. 2.5 hours)."),
|
|
8
|
+
unit_price_cents: z.number().int().min(0).describe("Price of one unit in integer cents: 150.00 is 15000."),
|
|
9
|
+
product_id: z.string().optional().describe("The id of a catalog product, if the line is one.")
|
|
10
|
+
});
|
|
11
|
+
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
|
+
function invoiceTools() {
|
|
13
|
+
return [
|
|
14
|
+
defineTool({
|
|
15
|
+
name: "invoice_list",
|
|
16
|
+
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` and `customer_id`; filters combine. Money is integer cents. Returns `{ invoices }`. Read only.',
|
|
18
|
+
input: z.object({
|
|
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
|
+
limit: z.number().int().min(1).max(100).optional().describe("Page size, 1 to 100 (default 25)."),
|
|
26
|
+
offset: z.number().int().min(0).optional().describe("How many to skip, for the next page.")
|
|
27
|
+
}),
|
|
28
|
+
access: "read",
|
|
29
|
+
permission: "invoice.read",
|
|
30
|
+
run: async (ctx, a) => ({
|
|
31
|
+
invoices: await ctx.clients.invoice.list({
|
|
32
|
+
limit: a.limit ?? 25,
|
|
33
|
+
...a.offset ? { offset: a.offset } : {},
|
|
34
|
+
...a.status ? { status: a.status } : {},
|
|
35
|
+
...a.payment_status ? { payment_status: a.payment_status } : {},
|
|
36
|
+
...a.overdue !== void 0 ? { overdue: a.overdue } : {},
|
|
37
|
+
...a.source ? { source: a.source } : {},
|
|
38
|
+
...a.fiscal_status ? { fiscal_status: a.fiscal_status } : {},
|
|
39
|
+
...a.customer_id ? { customer_id: a.customer_id } : {}
|
|
40
|
+
})
|
|
41
|
+
})
|
|
42
|
+
}),
|
|
43
|
+
defineTool({
|
|
44
|
+
name: "invoice_get",
|
|
45
|
+
title: "Get one invoice",
|
|
46
|
+
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.",
|
|
47
|
+
input: z.object({ ref: REF }),
|
|
48
|
+
access: "read",
|
|
49
|
+
permission: "invoice.read",
|
|
50
|
+
run: async (ctx, a) => ({ invoice: await ctx.clients.invoice.get(a.ref) })
|
|
51
|
+
}),
|
|
52
|
+
defineTool({
|
|
53
|
+
name: "invoice_transactions",
|
|
54
|
+
title: "List payments",
|
|
55
|
+
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.",
|
|
56
|
+
input: z.object({
|
|
57
|
+
ref: REF.optional().describe("An invoice id or short id; leave out for the whole organization."),
|
|
58
|
+
limit: z.number().int().min(1).max(100).optional().describe("Page size when listing the whole organization, 1 to 100 (default 25)."),
|
|
59
|
+
offset: z.number().int().min(0).optional()
|
|
60
|
+
}),
|
|
61
|
+
access: "read",
|
|
62
|
+
permission: "invoice.read",
|
|
63
|
+
run: async (ctx, a) => ({
|
|
64
|
+
transactions: a.ref ? await ctx.clients.invoice.invoiceTransactions(a.ref) : await ctx.clients.invoice.transactions({ limit: a.limit ?? 25, ...a.offset ? { offset: a.offset } : {} })
|
|
65
|
+
})
|
|
66
|
+
}),
|
|
67
|
+
defineTool({
|
|
68
|
+
name: "invoice_create",
|
|
69
|
+
title: "Draft an invoice",
|
|
70
|
+
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.",
|
|
71
|
+
input: z.object({
|
|
72
|
+
customer_id,
|
|
73
|
+
currency: z.string().length(3).describe("ISO 4217 code, e.g. EUR, USD, TRY."),
|
|
74
|
+
lines: z.array(Line).min(1).describe("At least one line.")
|
|
75
|
+
}),
|
|
76
|
+
access: "write",
|
|
77
|
+
permission: "invoice.write",
|
|
78
|
+
run: async (ctx, a) => ({
|
|
79
|
+
invoice: await ctx.clients.invoice.create(a)
|
|
80
|
+
})
|
|
81
|
+
}),
|
|
82
|
+
defineTool({
|
|
83
|
+
name: "invoice_update",
|
|
84
|
+
title: "Edit a draft invoice",
|
|
85
|
+
description: "Changes a DRAFT invoice's customer and/or replaces ALL its lines, and returns it. WRITES. Send the full new list of lines, not a delta. Only drafts can be edited: a finalized, paid or void invoice is refused with `not_draft` (a finalized invoice is a legal record; to undo one, void it and draft a new one).",
|
|
86
|
+
input: z.object({
|
|
87
|
+
ref: REF,
|
|
88
|
+
customer_id: customer_id.optional(),
|
|
89
|
+
lines: z.array(Line).min(1).optional().describe("The complete new set of lines.")
|
|
90
|
+
}),
|
|
91
|
+
access: "write",
|
|
92
|
+
permission: "invoice.write",
|
|
93
|
+
async run(ctx, a) {
|
|
94
|
+
const { ref, ...patch } = a;
|
|
95
|
+
if (patch.customer_id === void 0 && patch.lines === void 0) throw Object.assign(new Error("Send customer_id and/or lines"), { code: "nothing_to_update", status: 400 });
|
|
96
|
+
return { invoice: await ctx.clients.invoice.update(ref, patch) };
|
|
97
|
+
}
|
|
98
|
+
}),
|
|
99
|
+
defineTool({
|
|
100
|
+
name: "invoice_finalize",
|
|
101
|
+
title: "Finalize an invoice",
|
|
102
|
+
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`.",
|
|
103
|
+
input: z.object({
|
|
104
|
+
ref: REF,
|
|
105
|
+
issued_at: z.number().int().optional().describe("Epoch milliseconds; default now."),
|
|
106
|
+
due_at: z.number().int().optional().describe("Epoch milliseconds; omit for no due date.")
|
|
107
|
+
}),
|
|
108
|
+
access: "write",
|
|
109
|
+
permission: "invoice.finalize",
|
|
110
|
+
async run(ctx, a) {
|
|
111
|
+
const { ref, ...input } = a;
|
|
112
|
+
return { invoice: await ctx.clients.invoice.finalize(ref, input) };
|
|
113
|
+
}
|
|
114
|
+
}),
|
|
115
|
+
defineTool({
|
|
116
|
+
name: "invoice_record_payment",
|
|
117
|
+
title: "Record a payment",
|
|
118
|
+
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.",
|
|
119
|
+
input: z.object({
|
|
120
|
+
ref: REF,
|
|
121
|
+
amount_cents: z.number().int().positive().describe("Integer cents received: 100.00 is 10000."),
|
|
122
|
+
method: z.string().optional().describe("e.g. bank, cash, card."),
|
|
123
|
+
reference: z.string().optional().describe("The payer's or processor's own id for this payment; makes a retry safe."),
|
|
124
|
+
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.")
|
|
125
|
+
}),
|
|
126
|
+
access: "write",
|
|
127
|
+
permission: "invoice.record_payment",
|
|
128
|
+
async run(ctx, a) {
|
|
129
|
+
const { ref, ...input } = a;
|
|
130
|
+
return { invoice: await ctx.clients.invoice.recordPayment(ref, input) };
|
|
131
|
+
}
|
|
132
|
+
}),
|
|
133
|
+
defineTool({
|
|
134
|
+
name: "invoice_void",
|
|
135
|
+
title: "Void an invoice",
|
|
136
|
+
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`.",
|
|
137
|
+
input: z.object({ ref: REF, reason: z.string().optional().describe("Why it is being voided; kept on the record.") }),
|
|
138
|
+
access: "write",
|
|
139
|
+
permission: "invoice.void",
|
|
140
|
+
run: async (ctx, a) => ({ invoice: await ctx.clients.invoice.void(a.ref, a.reason) })
|
|
141
|
+
})
|
|
142
|
+
];
|
|
143
|
+
}
|
|
144
|
+
export {
|
|
145
|
+
invoiceTools
|
|
146
|
+
};
|