@baseworks/invoice 1.2.2 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/client.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { DocumentDate } from '@baseworks/core/document-date';
2
+ export { DocumentDate } from '@baseworks/core/document-date';
1
3
  import { FiscalStatus, InvoiceStatus, InvoiceSource } from './events.js';
2
4
  import '@baseworks/foldbase';
3
5
  import 'zod';
@@ -14,8 +16,8 @@ interface InvoiceRow {
14
16
  subtotal_cents: number;
15
17
  total_cents: number;
16
18
  amount_paid_cents: number;
17
- issued_at: number;
18
- due_at: number;
19
+ issued_at: number | null;
20
+ due_at: number | null;
19
21
  created_at: number;
20
22
  updated_at: number;
21
23
  source: string | null;
@@ -34,6 +36,11 @@ interface InvoiceRow {
34
36
  number_text: string | null;
35
37
  series_key: string | null;
36
38
  external_number: string | null;
39
+ document_date: string | null;
40
+ due_date: string | null;
41
+ paid_date: string | null;
42
+ void_date: string | null;
43
+ void_reason: string | null;
37
44
  }
38
45
  interface InvoiceLineDto {
39
46
  description: string;
@@ -82,7 +89,10 @@ interface InvoicePayment {
82
89
  currency: string;
83
90
  method: string | null;
84
91
  reference: string | null;
85
- paid_at: number;
92
+ /** The day of the payment. */
93
+ document_date: DocumentDate;
94
+ /** When it was recorded (epoch millis): audit and the order inside one day. */
95
+ created_at: number;
86
96
  }
87
97
  interface InvoiceDto {
88
98
  id: string;
@@ -99,8 +109,13 @@ interface InvoiceDto {
99
109
  amount_paid_cents: number;
100
110
  /** total_cents − amount_paid_cents (0 for draft/void semantics aside). */
101
111
  balance_cents: number;
102
- issued_at: number | null;
103
- due_at: number | null;
112
+ /** The invoice date (calendar day); `null` while a draft. Every period, number and list window follows it. */
113
+ document_date: DocumentDate | null;
114
+ due_date: DocumentDate | null;
115
+ /** The day of the payment that settled it; `null` unless paid (and for a mirror paid with no date and no payment). */
116
+ paid_date: DocumentDate | null;
117
+ void_date: DocumentDate | null;
118
+ void_reason: string | null;
104
119
  created_at: number;
105
120
  updated_at: number;
106
121
  /** `local` (made here), `stripe`, `manual` (entered from a document issued elsewhere). Old invoices are `local`. */
@@ -111,12 +126,15 @@ interface InvoiceDto {
111
126
  payment_status: PaymentStatus;
112
127
  fiscal: Fiscal | null;
113
128
  /**
114
- * The number as printed: `BSF2026000000148` in a series, `7` without one, the external number when the invoice has one.
115
- * `null` while a draft. `number` is the series' counter (`null` when the number is the external one).
129
+ * The internal number as printed: `BSF2026000000148` in a series, `7` without one. `null` while a draft and for a
130
+ * mirror. (An invoice finalized with an external number before 2.0 took no internal one: `number` is `null` and this
131
+ * holds that external number.)
116
132
  */
117
133
  number_text: string | null;
118
- /** A number given by another system (an e-Fatura portal, say), up to 64 characters. Unique among non-void invoices. */
134
+ /** A number given by another system (an e-Fatura portal, say), up to 64 characters. Unique among the invoices that hold one. */
119
135
  external_number: string | null;
136
+ /** Read only: `external_number` if there is one, else `number_text`. */
137
+ display_number: string | null;
120
138
  /** Only `GET /invoices/:ref` includes the payments. */
121
139
  payments?: InvoicePayment[];
122
140
  }
@@ -135,10 +153,6 @@ interface InvoiceDeprecated {
135
153
  amountPaidCents: number;
136
154
  /** @deprecated Read `balance_cents`. */
137
155
  balanceCents: number;
138
- /** @deprecated Read `issued_at`. */
139
- issuedAt: number | null;
140
- /** @deprecated Read `due_at`. */
141
- dueAt: number | null;
142
156
  /** @deprecated Read `created_at`. */
143
157
  createdAt: number;
144
158
  /** @deprecated Read `updated_at`. */
@@ -165,7 +179,7 @@ interface CreateInvoiceInput {
165
179
  currency: string;
166
180
  lines: InvoiceLineInput[];
167
181
  metadata?: Metadata;
168
- /** A number from another system; the invoice keeps it and takes none from the series at finalize. */
182
+ /** A number from another system; the invoice keeps it beside the internal number it gets at finalize. */
169
183
  external_number?: string;
170
184
  /** @deprecated Send `customer_id`. */
171
185
  customerId?: string;
@@ -181,15 +195,19 @@ interface UpdateInvoiceInput {
181
195
  customerId?: string;
182
196
  }
183
197
  interface FinalizeInvoiceInput {
184
- /** Epoch millis; defaults to now server-side. */
198
+ /** `YYYY-MM-DD`; defaults to today (UTC) server-side; a future date is refused. */
199
+ document_date?: DocumentDate | string;
200
+ /** `YYYY-MM-DD`; omitted = no due date. */
201
+ due_date?: DocumentDate | string;
202
+ /** The number another system gave it, kept beside the internal number. */
203
+ external_number?: string;
204
+ /** @deprecated Send `document_date`. Epoch millis, read as its UTC day; not together with `document_date`. */
185
205
  issued_at?: number;
186
- /** Epoch millis; 0/omitted = no due date. */
206
+ /** @deprecated Send `due_date`. Epoch millis, read as its UTC day (0 = none); not together with `due_date`. */
187
207
  due_at?: number;
188
- /** The number another system gave it. Then it is the invoice's number and the series does not advance. */
189
- external_number?: string;
190
- /** @deprecated Send `issued_at`. */
208
+ /** @deprecated Send `document_date`. */
191
209
  issuedAt?: number;
192
- /** @deprecated Send `due_at`. */
210
+ /** @deprecated Send `due_date`. */
193
211
  dueAt?: number;
194
212
  }
195
213
  interface RecordPaymentInput {
@@ -198,16 +216,31 @@ interface RecordPaymentInput {
198
216
  reference?: string;
199
217
  /** The pay record this settles. Idempotency key: the same id twice changes nothing. */
200
218
  payment_id?: string;
219
+ /** The day of the payment, `YYYY-MM-DD`; defaults to today (UTC); a future date is refused. */
220
+ document_date?: DocumentDate | string;
201
221
  /** @deprecated Send `amount_cents`. */
202
222
  amountCents?: number;
203
223
  }
224
+ interface VoidInvoiceInput {
225
+ /** `YYYY-MM-DD`; defaults to today (UTC); a future date is refused. */
226
+ void_date?: DocumentDate | string;
227
+ }
228
+ /** The `from` / `to` window of a list: calendar days, both included, compared with `document_date`. */
229
+ interface DocumentDateRangeInput {
230
+ from?: DocumentDate | string;
231
+ to?: DocumentDate | string;
232
+ }
204
233
  interface ListInvoicesOptions {
234
+ /** Only invoices dated this day or later (`document_date`); drafts have no date and are left out. */
235
+ from?: DocumentDate | string;
236
+ /** Only invoices dated this day or earlier. */
237
+ to?: DocumentDate | string;
205
238
  limit?: number;
206
239
  offset?: number;
207
240
  status?: string;
208
241
  /** One or several (an array is sent comma-separated). */
209
242
  payment_status?: PaymentStatus | PaymentStatus[];
210
- /** Unpaid or partially paid, with a due date in the past. */
243
+ /** Unpaid or partially paid, with a due date before today (UTC). */
211
244
  overdue?: boolean;
212
245
  source?: InvoiceSource;
213
246
  fiscal_status?: FiscalStatus;
@@ -232,9 +265,18 @@ interface MirrorInvoiceInput {
232
265
  tax_cents: number;
233
266
  total_cents: number;
234
267
  status: 'finalized' | 'paid' | 'void';
235
- issued_at: number;
268
+ /** The provider's invoice date, `YYYY-MM-DD`. Required (unless the deprecated `issued_at` is sent). */
269
+ document_date?: DocumentDate | string;
270
+ due_date?: DocumentDate | string;
271
+ paid_date?: DocumentDate | string;
272
+ void_date?: DocumentDate | string;
273
+ /** @deprecated Send `document_date`. Epoch millis, read as its UTC day; never together with the new name. */
274
+ issued_at?: number;
275
+ /** @deprecated Send `due_date`. */
236
276
  due_at?: number;
277
+ /** @deprecated Send `paid_date`. */
237
278
  paid_at?: number;
279
+ /** @deprecated Send `void_date`. */
238
280
  voided_at?: number;
239
281
  /** The provider's own "updated" time: an upsert older than what is stored is ignored. */
240
282
  provider_updated_at: number;
@@ -247,13 +289,11 @@ interface MirrorResult {
247
289
  /** True when the document was older than the one stored and was ignored. */
248
290
  stale?: boolean;
249
291
  }
250
- /** An organization's numbering series. `number_text` is `${prefix}${issue year}${counter, zero-padded to digits}`. */
292
+ /** An organization's numbering series. `number_text` is `${prefix}${year of document_date}${counter, zero-padded to digits}`. */
251
293
  interface Numbering {
252
294
  prefix: string;
253
295
  digits: number;
254
296
  yearly: boolean;
255
- /** IANA name (`Europe/Istanbul`); `UTC` when none is set. The year in the number and the yearly reset follow the issue date in it. */
256
- time_zone: string;
257
297
  /** The counter the next invoice finalized now would get. */
258
298
  next: number;
259
299
  /** What that number would read, for the current year. */
@@ -265,12 +305,14 @@ interface NumberingInput {
265
305
  prefix: string;
266
306
  /** 1 to 12; 9 for e-Fatura. */
267
307
  digits: number;
268
- /** true: the counter restarts each calendar year (of the issue date). */
308
+ /** true: the counter restarts each calendar year (of `document_date`). */
269
309
  yearly: boolean;
270
- /** IANA name. Omitted: stays as it was (UTC if never set). */
310
+ /** @deprecated Accepted and ignored in 2.0, to be removed: the year is that of `document_date`, a calendar day. */
271
311
  time_zone?: string;
272
312
  /** The counter to continue from (this year's, when yearly). Omitted: the series continues where it is. It can only go forward. */
273
313
  next?: number;
314
+ /** Which year's series `next` applies to, for a yearly series. Default: the current year (UTC). */
315
+ year?: number;
274
316
  }
275
317
  interface FiscalInput {
276
318
  status: FiscalStatus;
@@ -290,6 +332,8 @@ interface TransactionRow {
290
332
  method: string;
291
333
  reference: string;
292
334
  payment_id: string | null;
335
+ /** 2.0; NULL on rows folded from older events (the day of `created_at`). */
336
+ document_date: string | null;
293
337
  created_at: number;
294
338
  updated_at: number;
295
339
  }
@@ -305,6 +349,8 @@ interface TransactionDto {
305
349
  method: string | null;
306
350
  reference: string | null;
307
351
  payment_id: string | null;
352
+ /** The day of the payment. */
353
+ document_date: DocumentDate;
308
354
  created_at: number;
309
355
  }
310
356
  interface TransactionDeprecated {
@@ -372,6 +418,11 @@ declare class InvoiceClient {
372
418
  /** Generic ApiClient (get/post/patch/del) — used by the CLI plugin. */
373
419
  get http(): HttpClient;
374
420
  list(opts?: ListInvoicesOptions): Promise<Invoice[]>;
421
+ /** One page and `next_offset` (null on the last page): pass it back as `offset` to read on. */
422
+ listPage(opts?: ListInvoicesOptions): Promise<{
423
+ invoices: Invoice[];
424
+ next_offset?: number | null;
425
+ }>;
375
426
  get(ref: string): Promise<Invoice>;
376
427
  create(input: CreateInvoiceInput): Promise<Invoice>;
377
428
  update(ref: string, input: UpdateInvoiceInput): Promise<Invoice>;
@@ -389,23 +440,42 @@ declare class InvoiceClient {
389
440
  /** Record where the invoice stands with a tax system. Needs `invoice.fiscal_update`; nothing is sent to any tax system. */
390
441
  setFiscal(ref: string, input: FiscalInput): Promise<Invoice>;
391
442
  /**
392
- * Record the number another system gave an invoice that is already finalized or paid (the e-Arşiv / GİB portal's).
393
- * Once: the same number again changes nothing, another one is `409 external_number_set`. Needs `invoice.fiscal_update`.
394
- * A draft is `409 use_update`.
443
+ * Set or change the number another system gave an issued invoice (finalized, paid or void; the e-Arşiv / GİB portal's).
444
+ * The same number again changes nothing; one another invoice holds is `409 external_number_taken`. A draft is
445
+ * `409 use_update`. Needs `invoice.fiscal_update`.
446
+ * `from_invoice_id` MOVES the number from that void invoice to this one in one operation (nobody can take it in between;
447
+ * `409 from_not_void`, `409 from_number_mismatch`).
395
448
  */
396
- setExternalNumber(ref: string, external_number: string): Promise<Invoice>;
449
+ setExternalNumber(ref: string, external_number: string, opts?: {
450
+ from_invoice_id?: string;
451
+ }): Promise<Invoice>;
452
+ /** Take the external number off an issued invoice, which frees it for another. Needs `invoice.fiscal_update`. */
453
+ clearExternalNumber(ref: string): Promise<Invoice>;
397
454
  finalize(ref: string, input?: FinalizeInvoiceInput): Promise<Invoice>;
398
455
  recordPayment(ref: string, input: RecordPaymentInput): Promise<Invoice>;
399
- void(ref: string, reason?: string): Promise<Invoice>;
400
- /** All transactions for the org, newest first. */
401
- transactions(opts?: {
402
- limit?: number;
403
- offset?: number;
404
- }): Promise<Transaction[]>;
405
- /** Transactions recorded against one invoice. */
456
+ /** `void_date` defaults to today (UTC) server-side. */
457
+ void(ref: string, reason?: string, input?: VoidInvoiceInput): Promise<Invoice>;
458
+ /** All transactions for the org, newest first by (document_date, created_at, id). */
459
+ transactions(opts?: TransactionListOptions): Promise<Transaction[]>;
460
+ /** One page of the organization's transactions and `next_offset` (null on the last page). */
461
+ transactionsPage(opts?: TransactionListOptions): Promise<{
462
+ transactions: Transaction[];
463
+ next_offset?: number | null;
464
+ }>;
465
+ private transactionList;
466
+ /** Transactions recorded against one invoice: its first 100. `invoiceTransactionsPage` reads on. */
406
467
  invoiceTransactions(ref: string): Promise<Transaction[]>;
468
+ /** One page of an invoice's transactions and `next_offset` (null on the last page). */
469
+ invoiceTransactionsPage(ref: string, opts?: TransactionListOptions): Promise<{
470
+ transactions: Transaction[];
471
+ next_offset?: number | null;
472
+ }>;
407
473
  }
474
+ type TransactionListOptions = DocumentDateRangeInput & {
475
+ limit?: number;
476
+ offset?: number;
477
+ };
408
478
  /** Factory used by the SDK capability registry. */
409
479
  declare function createInvoiceClient(opts: InvoiceClientOptions): InvoiceClient;
410
480
 
411
- export { type CreateInvoiceInput, type ExternalRef, type FinalizeInvoiceInput, type Fiscal, type FiscalInput, FiscalStatus, type HttpClient, type Invoice, InvoiceClient, type InvoiceClientOptions, type InvoiceDeprecated, type InvoiceDto, InvoiceError, type InvoiceLine, type InvoiceLineDeprecated, type InvoiceLineDto, type InvoiceLineInput, type InvoicePayment, type InvoiceRow, InvoiceSource, InvoiceStatus, type ListInvoicesOptions, type Metadata, type MirrorInvoiceInput, type MirrorResult, type Numbering, type NumberingInput, type PaymentStatus, type RecordPaymentInput, type Transaction, type TransactionDeprecated, type TransactionDto, type TransactionRow, type UpdateInvoiceInput, createInvoiceClient };
481
+ export { type CreateInvoiceInput, type DocumentDateRangeInput, type ExternalRef, type FinalizeInvoiceInput, type Fiscal, type FiscalInput, FiscalStatus, type HttpClient, type Invoice, InvoiceClient, type InvoiceClientOptions, type InvoiceDeprecated, type InvoiceDto, InvoiceError, type InvoiceLine, type InvoiceLineDeprecated, type InvoiceLineDto, type InvoiceLineInput, type InvoicePayment, type InvoiceRow, InvoiceSource, InvoiceStatus, type ListInvoicesOptions, type Metadata, type MirrorInvoiceInput, type MirrorResult, type Numbering, type NumberingInput, type PaymentStatus, type RecordPaymentInput, type Transaction, type TransactionDeprecated, type TransactionDto, type TransactionListOptions, type TransactionRow, type UpdateInvoiceInput, type VoidInvoiceInput, createInvoiceClient };
package/dist/client.js CHANGED
@@ -7,8 +7,6 @@ var INVOICE = {
7
7
  totalCents: "total_cents",
8
8
  amountPaidCents: "amount_paid_cents",
9
9
  balanceCents: "balance_cents",
10
- issuedAt: "issued_at",
11
- dueAt: "due_at",
12
10
  createdAt: "created_at",
13
11
  updatedAt: "updated_at"
14
12
  };
@@ -115,7 +113,13 @@ var InvoiceClient = class _InvoiceClient {
115
113
  }
116
114
  // ── invoices ──────────────────────────────────────────────────────────────
117
115
  async list(opts = {}) {
116
+ return (await this.listPage(opts)).invoices;
117
+ }
118
+ /** One page and `next_offset` (null on the last page): pass it back as `offset` to read on. */
119
+ async listPage(opts = {}) {
118
120
  const q = new URLSearchParams();
121
+ if (opts.from !== void 0) q.set("from", opts.from);
122
+ if (opts.to !== void 0) q.set("to", opts.to);
119
123
  if (opts.limit !== void 0) q.set("limit", String(opts.limit));
120
124
  if (opts.offset !== void 0) q.set("offset", String(opts.offset));
121
125
  if (opts.status) q.set("status", opts.status);
@@ -126,8 +130,8 @@ var InvoiceClient = class _InvoiceClient {
126
130
  if (opts.customer_id) q.set("customer_id", opts.customer_id);
127
131
  if (opts.external_number) q.set("external_number", opts.external_number);
128
132
  const qs = q.toString();
129
- const { invoices } = await this.api("GET", `/invoices${qs ? `?${qs}` : ""}`);
130
- return invoices.map(oldNamesOnInvoice);
133
+ const { invoices, next_offset } = await this.api("GET", `/invoices${qs ? `?${qs}` : ""}`);
134
+ return { invoices: invoices.map(oldNamesOnInvoice), next_offset };
131
135
  }
132
136
  async get(ref) {
133
137
  const { invoice } = await this.api("GET", `/invoices/${ref}`);
@@ -167,12 +171,19 @@ var InvoiceClient = class _InvoiceClient {
167
171
  return oldNamesOnInvoice(invoice);
168
172
  }
169
173
  /**
170
- * Record the number another system gave an invoice that is already finalized or paid (the e-Arşiv / GİB portal's).
171
- * Once: the same number again changes nothing, another one is `409 external_number_set`. Needs `invoice.fiscal_update`.
172
- * A draft is `409 use_update`.
174
+ * Set or change the number another system gave an issued invoice (finalized, paid or void; the e-Arşiv / GİB portal's).
175
+ * The same number again changes nothing; one another invoice holds is `409 external_number_taken`. A draft is
176
+ * `409 use_update`. Needs `invoice.fiscal_update`.
177
+ * `from_invoice_id` MOVES the number from that void invoice to this one in one operation (nobody can take it in between;
178
+ * `409 from_not_void`, `409 from_number_mismatch`).
173
179
  */
174
- async setExternalNumber(ref, external_number) {
175
- const { invoice } = await this.api("PUT", `/invoices/${ref}/external-number`, { external_number });
180
+ async setExternalNumber(ref, external_number, opts = {}) {
181
+ const { invoice } = await this.api("PUT", `/invoices/${ref}/external-number`, { external_number, ...opts.from_invoice_id ? { from_invoice_id: opts.from_invoice_id } : {} });
182
+ return oldNamesOnInvoice(invoice);
183
+ }
184
+ /** Take the external number off an issued invoice, which frees it for another. Needs `invoice.fiscal_update`. */
185
+ async clearExternalNumber(ref) {
186
+ const { invoice } = await this.api("DELETE", `/invoices/${ref}/external-number`);
176
187
  return oldNamesOnInvoice(invoice);
177
188
  }
178
189
  async finalize(ref, input = {}) {
@@ -183,24 +194,37 @@ var InvoiceClient = class _InvoiceClient {
183
194
  const { invoice } = await this.api("POST", `/invoices/${ref}/payments`, newNamesOnPayment(input));
184
195
  return oldNamesOnInvoice(invoice);
185
196
  }
186
- async void(ref, reason) {
187
- const { invoice } = await this.api("POST", `/invoices/${ref}/void`, { reason: reason ?? "" });
197
+ /** `void_date` defaults to today (UTC) server-side. */
198
+ async void(ref, reason, input = {}) {
199
+ const { invoice } = await this.api("POST", `/invoices/${ref}/void`, { reason: reason ?? "", ...input.void_date !== void 0 ? { void_date: input.void_date } : {} });
188
200
  return oldNamesOnInvoice(invoice);
189
201
  }
190
202
  // ── transactions (the payment ledger) ───────────────────────────────────────
191
- /** All transactions for the org, newest first. */
203
+ /** All transactions for the org, newest first by (document_date, created_at, id). */
192
204
  async transactions(opts = {}) {
205
+ return (await this.transactionsPage(opts)).transactions;
206
+ }
207
+ /** One page of the organization's transactions and `next_offset` (null on the last page). */
208
+ transactionsPage(opts = {}) {
209
+ return this.transactionList("/transactions", opts);
210
+ }
211
+ async transactionList(path, opts) {
193
212
  const q = new URLSearchParams();
213
+ if (opts.from !== void 0) q.set("from", opts.from);
214
+ if (opts.to !== void 0) q.set("to", opts.to);
194
215
  if (opts.limit !== void 0) q.set("limit", String(opts.limit));
195
216
  if (opts.offset !== void 0) q.set("offset", String(opts.offset));
196
217
  const qs = q.toString();
197
- const { transactions } = await this.api("GET", `/transactions${qs ? `?${qs}` : ""}`);
198
- return transactions.map(oldNamesOnTransaction);
218
+ const { transactions, next_offset } = await this.api("GET", `${path}${qs ? `?${qs}` : ""}`);
219
+ return { transactions: transactions.map(oldNamesOnTransaction), next_offset };
199
220
  }
200
- /** Transactions recorded against one invoice. */
221
+ /** Transactions recorded against one invoice: its first 100. `invoiceTransactionsPage` reads on. */
201
222
  async invoiceTransactions(ref) {
202
- const { transactions } = await this.api("GET", `/invoices/${ref}/payments`);
203
- return transactions.map(oldNamesOnTransaction);
223
+ return (await this.invoiceTransactionsPage(ref)).transactions;
224
+ }
225
+ /** One page of an invoice's transactions and `next_offset` (null on the last page). */
226
+ invoiceTransactionsPage(ref, opts = {}) {
227
+ return this.transactionList(`/invoices/${ref}/payments`, opts);
204
228
  }
205
229
  };
206
230
  function createInvoiceClient(opts) {