@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/src/mcp.ts CHANGED
@@ -14,7 +14,7 @@
14
14
  */
15
15
  import { z } from 'zod'
16
16
  import { defineTool, type McpTool } from '@baseworks/mcp'
17
- import type { InvoiceClient } from './client.js'
17
+ import type { FiscalStatus, InvoiceClient, InvoiceSource, PaymentStatus } from './client.js'
18
18
 
19
19
  /** What `ctx.clients` must hold for these tools. */
20
20
  export interface InvoiceMcpClients { invoice: InvoiceClient }
@@ -37,16 +37,28 @@ export function invoiceTools(): McpTool<InvoiceMcpClients>[] {
37
37
  title: 'List invoices',
38
38
  description:
39
39
  'Lists the organization\'s invoices, newest first. Narrow with `status`: draft (being prepared, no number yet), finalized (issued, owed), paid or void. ' +
40
- 'Each invoice has its totals, the amount paid and the `balance_cents` still owed; money is integer cents. Returns `{ invoices }`. Read only.',
40
+ '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: ' +
41
+ '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.',
41
42
  input: z.object({
42
43
  status: z.enum(['draft', 'finalized', 'paid', 'void']).optional().describe('Only invoices in this status.'),
44
+ 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.'),
45
+ overdue: z.boolean().optional().describe('true: only what is owed and past its due date.'),
46
+ 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.'),
47
+ fiscal_status: z.enum(['not_submitted', 'queued', 'submitted', 'accepted', 'rejected', 'cancelled']).optional().describe('Only invoices in this state with a tax system.'),
48
+ customer_id: z.string().optional().describe('Only this customer\'s invoices: the customer\'s id.'),
49
+ external_number: z.string().optional().describe('Only the invoice with this exact external number (the number another system gave it).'),
43
50
  limit: z.number().int().min(1).max(100).optional().describe('Page size, 1 to 100 (default 25).'),
44
51
  offset: z.number().int().min(0).optional().describe('How many to skip, for the next page.'),
45
52
  }),
46
53
  access: 'read',
47
54
  permission: 'invoice.read',
48
- run: async (ctx: { clients: InvoiceMcpClients }, a: { status?: string; limit?: number; offset?: number }) => ({
49
- invoices: await ctx.clients.invoice.list({ limit: a.limit ?? 25, ...(a.offset ? { offset: a.offset } : {}), ...(a.status ? { status: a.status } : {}) }),
55
+ run: async (ctx: { clients: InvoiceMcpClients }, a: { status?: string; payment_status?: PaymentStatus[]; overdue?: boolean; source?: InvoiceSource; fiscal_status?: FiscalStatus; customer_id?: string; external_number?: string; limit?: number; offset?: number }) => ({
56
+ invoices: await ctx.clients.invoice.list({
57
+ limit: a.limit ?? 25,
58
+ ...(a.offset ? { offset: a.offset } : {}), ...(a.status ? { status: a.status } : {}),
59
+ ...(a.payment_status ? { payment_status: a.payment_status } : {}), ...(a.overdue !== undefined ? { overdue: a.overdue } : {}),
60
+ ...(a.source ? { source: a.source } : {}), ...(a.fiscal_status ? { fiscal_status: a.fiscal_status } : {}), ...(a.customer_id ? { customer_id: a.customer_id } : {}), ...(a.external_number ? { external_number: a.external_number } : {}),
61
+ }),
50
62
  }),
51
63
  }),
52
64
 
@@ -54,7 +66,7 @@ export function invoiceTools(): McpTool<InvoiceMcpClients>[] {
54
66
  name: 'invoice_get',
55
67
  title: 'Get one invoice',
56
68
  description:
57
- '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. ' +
69
+ '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. ' +
58
70
  '`ref` is the invoice\'s id or short id. Fails with `not_found` if there is no such invoice. Read only.',
59
71
  input: z.object({ ref: REF }),
60
72
  access: 'read',
@@ -62,6 +74,19 @@ export function invoiceTools(): McpTool<InvoiceMcpClients>[] {
62
74
  run: async (ctx: { clients: InvoiceMcpClients }, a: { ref: string }) => ({ invoice: await ctx.clients.invoice.get(a.ref) }),
63
75
  }),
64
76
 
77
+ defineTool({
78
+ name: 'invoice_numbering',
79
+ title: 'Show the numbering series',
80
+ description:
81
+ '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. ' +
82
+ '`next_text` is the number the next invoice finalized now would get (for example BSF2026000000149: the prefix, the issue year, then the counter). ' +
83
+ '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.',
84
+ input: z.object({}),
85
+ access: 'read',
86
+ permission: 'invoice.read',
87
+ run: async (ctx: { clients: InvoiceMcpClients }) => ({ numbering: await ctx.clients.invoice.getNumbering() }),
88
+ }),
89
+
65
90
  defineTool({
66
91
  name: 'invoice_transactions',
67
92
  title: 'List payments',
@@ -89,15 +114,17 @@ export function invoiceTools(): McpTool<InvoiceMcpClients>[] {
89
114
  '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. ' +
90
115
  'The customer must already exist: find it with customers_list; an unknown one is refused with `customer_not_found`. ' +
91
116
  '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. ' +
117
+ '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. ' +
92
118
  'It is recorded as you, the agent acting for the person who connected you.',
93
119
  input: z.object({
94
120
  customer_id,
95
121
  currency: z.string().length(3).describe('ISO 4217 code, e.g. EUR, USD, TRY.'),
96
122
  lines: z.array(Line).min(1).describe('At least one line.'),
123
+ 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`).'),
97
124
  }),
98
125
  access: 'write',
99
126
  permission: 'invoice.write',
100
- run: async (ctx: { clients: InvoiceMcpClients }, a: { customer_id: string; currency: string; lines: z.infer<typeof Line>[] }) => ({
127
+ run: async (ctx: { clients: InvoiceMcpClients }, a: { customer_id: string; currency: string; lines: z.infer<typeof Line>[]; external_number?: string }) => ({
101
128
  invoice: await ctx.clients.invoice.create(a),
102
129
  }),
103
130
  }),
@@ -126,17 +153,18 @@ export function invoiceTools(): McpTool<InvoiceMcpClients>[] {
126
153
  name: 'invoice_finalize',
127
154
  title: 'Finalize an invoice',
128
155
  description:
129
- '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). ' +
156
+ '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). ' +
130
157
  '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). ' +
131
- 'Only a draft can be finalized: otherwise `not_draft`.',
158
+ '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`.',
132
159
  input: z.object({
133
160
  ref: REF,
134
161
  issued_at: z.number().int().optional().describe('Epoch milliseconds; default now.'),
135
162
  due_at: z.number().int().optional().describe('Epoch milliseconds; omit for no due date.'),
163
+ 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.'),
136
164
  }),
137
165
  access: 'write',
138
166
  permission: 'invoice.finalize',
139
- async run(ctx: { clients: InvoiceMcpClients }, a: { ref: string; issued_at?: number; due_at?: number }) {
167
+ async run(ctx: { clients: InvoiceMcpClients }, a: { ref: string; issued_at?: number; due_at?: number; external_number?: string }) {
140
168
  const { ref, ...input } = a
141
169
  return { invoice: await ctx.clients.invoice.finalize(ref, input) }
142
170
  },
@@ -155,15 +183,35 @@ export function invoiceTools(): McpTool<InvoiceMcpClients>[] {
155
183
  amount_cents: z.number().int().positive().describe('Integer cents received: 100.00 is 10000.'),
156
184
  method: z.string().optional().describe('e.g. bank, cash, card.'),
157
185
  reference: z.string().optional().describe('The payer\'s or processor\'s own id for this payment; makes a retry safe.'),
186
+ 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.'),
158
187
  }),
159
188
  access: 'write',
160
189
  permission: 'invoice.record_payment',
161
- async run(ctx: { clients: InvoiceMcpClients }, a: { ref: string; amount_cents: number; method?: string; reference?: string }) {
190
+ async run(ctx: { clients: InvoiceMcpClients }, a: { ref: string; amount_cents: number; method?: string; reference?: string; payment_id?: string }) {
162
191
  const { ref, ...input } = a
163
192
  return { invoice: await ctx.clients.invoice.recordPayment(ref, input) }
164
193
  },
165
194
  }),
166
195
 
196
+ defineTool({
197
+ name: 'invoice_configure_numbering',
198
+ title: 'Set the numbering series',
199
+ description:
200
+ '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). ' +
201
+ '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). ' +
202
+ '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.',
203
+ input: z.object({
204
+ prefix: z.string().regex(/^[A-Z0-9]{3}$/).describe('Exactly 3 uppercase letters or digits.'),
205
+ digits: z.number().int().min(1).max(12).describe('Counter width: 9 for e-Fatura.'),
206
+ yearly: z.boolean().describe('true: the counter restarts each calendar year (of the issue date).'),
207
+ 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).'),
208
+ 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.'),
209
+ }),
210
+ access: 'write',
211
+ permission: 'invoice.configure',
212
+ run: async (ctx: { clients: InvoiceMcpClients }, a: { prefix: string; digits: number; yearly: boolean; time_zone?: string; next?: number }) => ({ numbering: await ctx.clients.invoice.setNumbering(a) }),
213
+ }),
214
+
167
215
  defineTool({
168
216
  name: 'invoice_void',
169
217
  title: 'Void an invoice',
@@ -23,6 +23,9 @@ export const readInvoices = defineProjection('invoices', InvoiceEvents, (on) =>
23
23
  issued_at: 0,
24
24
  due_at: 0,
25
25
  short_id: e.short_id,
26
+ source: e.source ?? null,
27
+ metadata: e.metadata ?? null,
28
+ external_number: e.external_number ?? null,
26
29
  created_at: e.at,
27
30
  })),
28
31
  InvoiceUpdated: on.InvoiceUpdated.upsert((e) => ({
@@ -31,6 +34,8 @@ export const readInvoices = defineProjection('invoices', InvoiceEvents, (on) =>
31
34
  lines: e.lines,
32
35
  subtotal_cents: e.subtotal_cents,
33
36
  total_cents: e.total_cents,
37
+ metadata: e.metadata ?? null,
38
+ external_number: e.external_number ?? null,
34
39
  })),
35
40
  InvoiceFinalized: on.InvoiceFinalized.upsert((e) => ({
36
41
  number: e.number,
@@ -40,6 +45,9 @@ export const readInvoices = defineProjection('invoices', InvoiceEvents, (on) =>
40
45
  total_cents: e.total_cents,
41
46
  issued_at: e.issued_at,
42
47
  due_at: e.due_at,
48
+ number_text: e.number_text ?? null,
49
+ series_key: e.series_key ?? null,
50
+ external_number: e.external_number ?? null,
43
51
  })),
44
52
  PaymentRecorded: on.PaymentRecorded.upsert((e) => ({
45
53
  amount_paid_cents: e.amount_paid_cents,
@@ -48,6 +56,37 @@ export const readInvoices = defineProjection('invoices', InvoiceEvents, (on) =>
48
56
  InvoiceVoided: on.InvoiceVoided.upsert(() => ({
49
57
  status: 'void',
50
58
  })),
59
+ // The provider's whole document. `number` is 0 and stays 0: a mirror has no number of the organization's own.
60
+ InvoiceMirrored: on.InvoiceMirrored.upsert((e) => ({
61
+ source: e.source,
62
+ mirror_key: e.mirror_key,
63
+ external_refs: e.external_refs,
64
+ customer_id: e.customer_id,
65
+ customer_name: e.customer_name,
66
+ currency: e.currency,
67
+ lines: e.lines,
68
+ number: 0,
69
+ status: e.status,
70
+ subtotal_cents: e.subtotal_cents,
71
+ tax_cents: e.tax_cents,
72
+ total_cents: e.total_cents,
73
+ amount_paid_cents: e.amount_paid_cents,
74
+ issued_at: e.issued_at,
75
+ due_at: e.due_at,
76
+ paid_at: e.paid_at,
77
+ voided_at: e.voided_at,
78
+ provider_updated_at: e.provider_updated_at,
79
+ metadata: e.metadata,
80
+ short_id: e.short_id,
81
+ created_at: e.created_at,
82
+ })),
83
+ InvoiceFiscalChanged: on.InvoiceFiscalChanged.upsert((e) => ({
84
+ fiscal_status: e.status,
85
+ fiscal_system: e.system,
86
+ fiscal_external_id: e.external_id,
87
+ fiscal_message: e.message,
88
+ fiscal_updated_at: e.at,
89
+ })),
51
90
  }))
52
91
 
53
92
  /**
@@ -65,6 +104,7 @@ export const readTransactions = defineProjection('transactions', TransactionEven
65
104
  currency: e.currency,
66
105
  method: e.method,
67
106
  reference: e.reference,
107
+ payment_id: e.payment_id ?? null,
68
108
  created_at: e.at,
69
109
  })),
70
110
  }))
package/src/types.ts CHANGED
@@ -1,5 +1,5 @@
1
- import type { InvoiceStatus } from './events.js'
2
- export type { InvoiceStatus } from './events.js'
1
+ import type { FiscalStatus, InvoiceSource, InvoiceStatus } from './events.js'
2
+ export type { FiscalStatus, InvoiceSource, InvoiceStatus } from './events.js'
3
3
 
4
4
  // ── read-model row (snake_case — foldbase read_invoices table) ────────────────
5
5
 
@@ -19,6 +19,24 @@ export interface InvoiceRow {
19
19
  due_at: number // 0 = none
20
20
  created_at: number
21
21
  updated_at: number
22
+ // 1.1.0: NULL on rows written before it (read as `local`, no refs, `{}`, no tax, no fiscal state).
23
+ source: string | null
24
+ mirror_key: string | null
25
+ external_refs: string | null // JSON array
26
+ metadata: string | null // JSON object
27
+ tax_cents: number | null
28
+ paid_at: number | null
29
+ voided_at: number | null
30
+ provider_updated_at: number | null
31
+ fiscal_status: string | null
32
+ fiscal_system: string | null
33
+ fiscal_external_id: string | null
34
+ fiscal_message: string | null
35
+ fiscal_updated_at: number | null
36
+ // 1.2.0
37
+ number_text: string | null
38
+ series_key: string | null
39
+ external_number: string | null
22
40
  }
23
41
 
24
42
  // ── REST DTOs (snake_case, as every service in the realm speaks: ADR-0003) ───
@@ -51,6 +69,40 @@ export interface InvoiceLineDeprecated {
51
69
  }
52
70
  export type InvoiceLine = InvoiceLineDto & InvoiceLineDeprecated
53
71
 
72
+ /** A reference to the same document in another system. At most 10 per invoice. */
73
+ export interface ExternalRef {
74
+ system: string
75
+ id: string
76
+ number?: string
77
+ url?: string
78
+ pdf_url?: string
79
+ account_id?: string
80
+ livemode?: boolean
81
+ }
82
+
83
+ export type Metadata = Record<string, string | number | boolean | null>
84
+
85
+ /** `unpaid`, `partially_paid`, `paid`; `not_due` for a draft and a void invoice. Derived, never stored. */
86
+ export type PaymentStatus = 'unpaid' | 'partially_paid' | 'paid' | 'not_due'
87
+
88
+ export interface Fiscal {
89
+ status: FiscalStatus
90
+ system: string | null
91
+ external_id: string | null
92
+ message: string | null
93
+ updated_at: number
94
+ }
95
+
96
+ /** One payment on an invoice, as `GET /invoices/:ref` lists them. */
97
+ export interface InvoicePayment {
98
+ payment_id: string
99
+ amount_cents: number
100
+ currency: string
101
+ method: string | null
102
+ reference: string | null
103
+ paid_at: number
104
+ }
105
+
54
106
  export interface InvoiceDto {
55
107
  id: string
56
108
  short_id: string
@@ -70,6 +122,22 @@ export interface InvoiceDto {
70
122
  due_at: number | null
71
123
  created_at: number
72
124
  updated_at: number
125
+ /** `local` (made here), `stripe`, `manual` (entered from a document issued elsewhere). Old invoices are `local`. */
126
+ source: InvoiceSource
127
+ external_refs: ExternalRef[]
128
+ metadata: Metadata
129
+ tax_cents: number
130
+ payment_status: PaymentStatus
131
+ fiscal: Fiscal | null
132
+ /**
133
+ * The number as printed: `BSF2026000000148` in a series, `7` without one, the external number when the invoice has one.
134
+ * `null` while a draft. `number` is the series' counter (`null` when the number is the external one).
135
+ */
136
+ number_text: string | null
137
+ /** A number given by another system (an e-Fatura portal, say), up to 64 characters. Unique among non-void invoices. */
138
+ external_number: string | null
139
+ /** Only `GET /invoices/:ref` includes the payments. */
140
+ payments?: InvoicePayment[]
73
141
  }
74
142
 
75
143
  export interface InvoiceDeprecated {
@@ -116,6 +184,9 @@ export interface CreateInvoiceInput {
116
184
  customer_id?: string
117
185
  currency: string
118
186
  lines: InvoiceLineInput[]
187
+ metadata?: Metadata
188
+ /** A number from another system; the invoice keeps it and takes none from the series at finalize. */
189
+ external_number?: string
119
190
  /** @deprecated Send `customer_id`. */
120
191
  customerId?: string
121
192
  }
@@ -123,6 +194,10 @@ export interface CreateInvoiceInput {
123
194
  export interface UpdateInvoiceInput {
124
195
  customer_id?: string
125
196
  lines?: InvoiceLineInput[]
197
+ /** Replaces the invoice's metadata. */
198
+ metadata?: Metadata
199
+ /** Sets (or, with null, clears) the external number of a draft. */
200
+ external_number?: string | null
126
201
  /** @deprecated Send `customer_id`. */
127
202
  customerId?: string
128
203
  }
@@ -132,6 +207,8 @@ export interface FinalizeInvoiceInput {
132
207
  issued_at?: number
133
208
  /** Epoch millis; 0/omitted = no due date. */
134
209
  due_at?: number
210
+ /** The number another system gave it. Then it is the invoice's number and the series does not advance. */
211
+ external_number?: string
135
212
  /** @deprecated Send `issued_at`. */
136
213
  issuedAt?: number
137
214
  /** @deprecated Send `due_at`. */
@@ -142,10 +219,89 @@ export interface RecordPaymentInput {
142
219
  amount_cents?: number
143
220
  method?: string
144
221
  reference?: string
222
+ /** The pay record this settles. Idempotency key: the same id twice changes nothing. */
223
+ payment_id?: string
145
224
  /** @deprecated Send `amount_cents`. */
146
225
  amountCents?: number
147
226
  }
148
227
 
228
+ export interface ListInvoicesOptions {
229
+ limit?: number
230
+ offset?: number
231
+ status?: string
232
+ /** One or several (an array is sent comma-separated). */
233
+ payment_status?: PaymentStatus | PaymentStatus[]
234
+ /** Unpaid or partially paid, with a due date in the past. */
235
+ overdue?: boolean
236
+ source?: InvoiceSource
237
+ fiscal_status?: FiscalStatus
238
+ customer_id?: string
239
+ /** Exact. */
240
+ external_number?: string
241
+ }
242
+
243
+ /** The provider's whole document, for `PUT /invoices/mirror`. */
244
+ export interface MirrorInvoiceInput {
245
+ source: 'stripe' | 'manual'
246
+ external_ref: ExternalRef
247
+ customer_id: string
248
+ currency: string
249
+ lines: Array<{ description: string; quantity: number; unit_price_cents: number; amount_cents: number; product_id?: string }>
250
+ subtotal_cents: number
251
+ tax_cents: number
252
+ total_cents: number
253
+ status: 'finalized' | 'paid' | 'void'
254
+ issued_at: number
255
+ due_at?: number
256
+ paid_at?: number
257
+ voided_at?: number
258
+ /** The provider's own "updated" time: an upsert older than what is stored is ignored. */
259
+ provider_updated_at: number
260
+ metadata?: Metadata
261
+ }
262
+
263
+ export interface MirrorResult {
264
+ invoice: Invoice
265
+ /** True when this call created the invoice. */
266
+ created: boolean
267
+ /** True when the document was older than the one stored and was ignored. */
268
+ stale?: boolean
269
+ }
270
+
271
+ /** An organization's numbering series. `number_text` is `${prefix}${issue year}${counter, zero-padded to digits}`. */
272
+ export interface Numbering {
273
+ prefix: string
274
+ digits: number
275
+ yearly: boolean
276
+ /** IANA name (`Europe/Istanbul`); `UTC` when none is set. The year in the number and the yearly reset follow the issue date in it. */
277
+ time_zone: string
278
+ /** The counter the next invoice finalized now would get. */
279
+ next: number
280
+ /** What that number would read, for the current year. */
281
+ next_text: string
282
+ updated_at: number
283
+ }
284
+
285
+ export interface NumberingInput {
286
+ /** 3 uppercase letters or digits. */
287
+ prefix: string
288
+ /** 1 to 12; 9 for e-Fatura. */
289
+ digits: number
290
+ /** true: the counter restarts each calendar year (of the issue date). */
291
+ yearly: boolean
292
+ /** IANA name. Omitted: stays as it was (UTC if never set). */
293
+ time_zone?: string
294
+ /** The counter to continue from (this year's, when yearly). Omitted: the series continues where it is. It can only go forward. */
295
+ next?: number
296
+ }
297
+
298
+ export interface FiscalInput {
299
+ status: FiscalStatus
300
+ system?: string
301
+ external_id?: string
302
+ message?: string
303
+ }
304
+
149
305
  // ── transactions (the ledger) ─────────────────────────────────────────────────
150
306
 
151
307
  export interface TransactionRow {
@@ -159,6 +315,7 @@ export interface TransactionRow {
159
315
  currency: string
160
316
  method: string
161
317
  reference: string
318
+ payment_id: string | null
162
319
  created_at: number
163
320
  updated_at: number
164
321
  }
@@ -174,6 +331,7 @@ export interface TransactionDto {
174
331
  currency: string
175
332
  method: string | null
176
333
  reference: string | null
334
+ payment_id: string | null
177
335
  created_at: number
178
336
  }
179
337
 
@@ -1,78 +0,0 @@
1
- // src/events.ts
2
- import { z } from "zod";
3
- import { defineAggregate } from "@baseworks/foldbase";
4
- var InvoiceStatus = z.enum(["draft", "finalized", "paid", "void"]);
5
- var InvoiceEvents = defineAggregate("invoice", {
6
- // Draft created (with its initial lines + computed totals). Numeric fields are
7
- // initialised to 0 so their read-model columns are stable integers.
8
- InvoiceDrafted: z.object({
9
- customer_id: z.string().min(1),
10
- customer_name: z.string(),
11
- // snapshot at draft time; '' if unknown
12
- currency: z.string().length(3),
13
- lines: z.string(),
14
- // JSON array of { description, quantity, unit_price_cents, amount_cents, product_id }
15
- subtotal_cents: z.number().int(),
16
- total_cents: z.number().int(),
17
- short_id: z.string().min(1),
18
- at: z.number().int()
19
- }),
20
- // Draft edited (customer / lines). Only valid while status = draft.
21
- InvoiceUpdated: z.object({
22
- customer_id: z.string().min(1),
23
- customer_name: z.string(),
24
- lines: z.string(),
25
- subtotal_cents: z.number().int(),
26
- total_cents: z.number().int(),
27
- at: z.number().int()
28
- }),
29
- // Finalize: assign the per-org number, freeze lines + totals, set issue/due.
30
- InvoiceFinalized: z.object({
31
- number: z.number().int(),
32
- lines: z.string(),
33
- subtotal_cents: z.number().int(),
34
- total_cents: z.number().int(),
35
- issued_at: z.number().int(),
36
- due_at: z.number().int(),
37
- // 0 = no due date
38
- at: z.number().int()
39
- }),
40
- // A payment applied to a finalized invoice. `amount_paid_cents` is the new running
41
- // total; `status` is the resulting status ('paid' when balance ≤ 0, else
42
- // 'finalized') — both computed by the store.
43
- PaymentRecorded: z.object({
44
- amount_cents: z.number().int(),
45
- amount_paid_cents: z.number().int(),
46
- status: InvoiceStatus,
47
- method: z.string(),
48
- reference: z.string(),
49
- at: z.number().int()
50
- }),
51
- InvoiceVoided: z.object({
52
- reason: z.string(),
53
- at: z.number().int()
54
- })
55
- });
56
- var TransactionKind = z.enum(["payment", "refund", "adjustment"]);
57
- var TransactionEvents = defineAggregate("transaction", {
58
- TransactionRecorded: z.object({
59
- invoice_id: z.string().min(1),
60
- invoice_number: z.number().int(),
61
- // the invoice's number at the time
62
- customer_id: z.string(),
63
- customer_name: z.string(),
64
- kind: TransactionKind,
65
- amount_cents: z.number().int(),
66
- currency: z.string().length(3),
67
- method: z.string(),
68
- reference: z.string(),
69
- at: z.number().int()
70
- })
71
- });
72
-
73
- export {
74
- InvoiceStatus,
75
- InvoiceEvents,
76
- TransactionKind,
77
- TransactionEvents
78
- };