@scell/sdk 1.2.0 → 1.4.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.
@@ -0,0 +1,360 @@
1
+ /**
2
+ * Tenant Direct Credit Notes Resource
3
+ *
4
+ * Manage credit notes for direct invoices issued by a tenant.
5
+ * Direct credit notes are linked to direct invoices (not sub-tenant invoices)
6
+ * and allow partial or total refunds.
7
+ *
8
+ * @packageDocumentation
9
+ */
10
+
11
+ import type { HttpClient, RequestOptions } from '../client.js';
12
+ import type {
13
+ MessageResponse,
14
+ PaginatedResponse,
15
+ SingleResponse,
16
+ } from '../types/common.js';
17
+ import type {
18
+ CreateTenantDirectCreditNoteParams,
19
+ TenantCreditNote,
20
+ TenantCreditNoteFilters,
21
+ UpdateTenantCreditNoteParams,
22
+ } from '../types/tenant-invoices.js';
23
+
24
+ /**
25
+ * Remaining creditable information for a direct invoice
26
+ */
27
+ export interface DirectInvoiceRemainingCreditable {
28
+ /** Invoice UUID */
29
+ invoice_id: string;
30
+ /** Original invoice total (TTC) */
31
+ invoice_total: number;
32
+ /** Total already credited */
33
+ credited_total: number;
34
+ /** Remaining amount that can be credited */
35
+ remaining_total: number;
36
+ /** Line-by-line breakdown */
37
+ lines: DirectInvoiceRemainingLine[];
38
+ }
39
+
40
+ /**
41
+ * Remaining creditable information for an invoice line
42
+ */
43
+ export interface DirectInvoiceRemainingLine {
44
+ /** Invoice line UUID */
45
+ invoice_line_id: string;
46
+ /** Line description */
47
+ description: string;
48
+ /** Original quantity */
49
+ original_quantity: number;
50
+ /** Quantity already credited */
51
+ credited_quantity: number;
52
+ /** Remaining quantity that can be credited */
53
+ remaining_quantity: number;
54
+ /** Unit price */
55
+ unit_price: number;
56
+ }
57
+
58
+ /**
59
+ * Tenant Direct Credit Notes API resource
60
+ *
61
+ * Provides operations for managing credit notes linked to direct invoices.
62
+ * Use this for refunds or corrections on invoices issued to external buyers.
63
+ *
64
+ * @example
65
+ * ```typescript
66
+ * import { ScellTenantClient } from '@scell/sdk';
67
+ *
68
+ * const client = new ScellTenantClient('your-tenant-key');
69
+ *
70
+ * // Create a partial credit note
71
+ * const { data: creditNote } = await client.directCreditNotes.create({
72
+ * invoice_id: 'invoice-uuid',
73
+ * reason: 'Product returned - damaged in transit',
74
+ * type: 'partial',
75
+ * items: [
76
+ * { invoice_line_id: 'line-uuid', quantity: 2 }
77
+ * ]
78
+ * });
79
+ *
80
+ * // Create a total credit note (full refund)
81
+ * const { data: totalCreditNote } = await client.directCreditNotes.create({
82
+ * invoice_id: 'invoice-uuid',
83
+ * reason: 'Order cancelled by customer',
84
+ * type: 'total'
85
+ * });
86
+ *
87
+ * // List all credit notes
88
+ * const { data: creditNotes } = await client.directCreditNotes.list({
89
+ * status: 'sent'
90
+ * });
91
+ * ```
92
+ */
93
+ export class TenantDirectCreditNotesResource {
94
+ constructor(private readonly http: HttpClient) {}
95
+
96
+ /**
97
+ * Create a new credit note for a direct invoice
98
+ *
99
+ * Creates a credit note (avoir) linked to a direct invoice.
100
+ * - For partial credit notes, specify the items and quantities to credit.
101
+ * - For total credit notes, all items are automatically credited.
102
+ *
103
+ * @param params - Credit note creation parameters
104
+ * @param requestOptions - Optional request configuration
105
+ * @returns Created credit note
106
+ *
107
+ * @example
108
+ * ```typescript
109
+ * // Partial credit note
110
+ * const { data: creditNote } = await client.directCreditNotes.create({
111
+ * invoice_id: 'invoice-uuid',
112
+ * reason: 'Returned items',
113
+ * type: 'partial',
114
+ * items: [
115
+ * { invoice_line_id: 'line-1-uuid', quantity: 2 },
116
+ * { invoice_line_id: 'line-2-uuid', quantity: 1 }
117
+ * ]
118
+ * });
119
+ *
120
+ * // Total credit note (cancels entire invoice)
121
+ * const { data: totalCreditNote } = await client.directCreditNotes.create({
122
+ * invoice_id: 'invoice-uuid',
123
+ * reason: 'Invoice issued in error',
124
+ * type: 'total'
125
+ * });
126
+ *
127
+ * console.log('Credit note number:', creditNote.credit_note_number);
128
+ * console.log('Amount:', creditNote.total, creditNote.currency);
129
+ * ```
130
+ */
131
+ async create(
132
+ params: CreateTenantDirectCreditNoteParams,
133
+ requestOptions?: RequestOptions
134
+ ): Promise<SingleResponse<TenantCreditNote>> {
135
+ return this.http.post<SingleResponse<TenantCreditNote>>(
136
+ '/tenant/credit-notes',
137
+ params,
138
+ requestOptions
139
+ );
140
+ }
141
+
142
+ /**
143
+ * List all direct credit notes
144
+ *
145
+ * Returns a paginated list of credit notes for direct invoices.
146
+ *
147
+ * @param filters - Filter and pagination options
148
+ * @param requestOptions - Optional request configuration
149
+ * @returns Paginated list of credit notes
150
+ *
151
+ * @example
152
+ * ```typescript
153
+ * // List all sent credit notes
154
+ * const { data: creditNotes, meta } = await client.directCreditNotes.list({
155
+ * status: 'sent',
156
+ * per_page: 50
157
+ * });
158
+ *
159
+ * console.log(`Found ${meta.total} sent credit notes`);
160
+ *
161
+ * // Filter by date range
162
+ * const januaryCreditNotes = await client.directCreditNotes.list({
163
+ * date_from: '2026-01-01',
164
+ * date_to: '2026-01-31'
165
+ * });
166
+ * ```
167
+ */
168
+ async list(
169
+ filters: TenantCreditNoteFilters = {},
170
+ requestOptions?: RequestOptions
171
+ ): Promise<PaginatedResponse<TenantCreditNote>> {
172
+ return this.http.get<PaginatedResponse<TenantCreditNote>>(
173
+ '/tenant/credit-notes',
174
+ filters as Record<string, string | number | boolean | undefined>,
175
+ requestOptions
176
+ );
177
+ }
178
+
179
+ /**
180
+ * Get a specific credit note by ID
181
+ *
182
+ * @param creditNoteId - Credit note UUID
183
+ * @param requestOptions - Optional request configuration
184
+ * @returns Credit note details
185
+ *
186
+ * @example
187
+ * ```typescript
188
+ * const { data: creditNote } = await client.directCreditNotes.get('credit-note-uuid');
189
+ *
190
+ * console.log('Credit note:', creditNote.credit_note_number);
191
+ * console.log('Status:', creditNote.status);
192
+ * console.log('Reason:', creditNote.reason);
193
+ * console.log('Total:', creditNote.total, creditNote.currency);
194
+ * ```
195
+ */
196
+ async get(
197
+ creditNoteId: string,
198
+ requestOptions?: RequestOptions
199
+ ): Promise<SingleResponse<TenantCreditNote>> {
200
+ return this.http.get<SingleResponse<TenantCreditNote>>(
201
+ `/tenant/credit-notes/${creditNoteId}`,
202
+ undefined,
203
+ requestOptions
204
+ );
205
+ }
206
+
207
+ /**
208
+ * Update a credit note
209
+ *
210
+ * Only credit notes in 'draft' status can be updated.
211
+ *
212
+ * @param creditNoteId - Credit note UUID
213
+ * @param params - Update parameters
214
+ * @param requestOptions - Optional request configuration
215
+ * @returns Updated credit note
216
+ *
217
+ * @example
218
+ * ```typescript
219
+ * const { data: creditNote } = await client.directCreditNotes.update(
220
+ * 'credit-note-uuid',
221
+ * {
222
+ * reason: 'Updated reason: Customer complaint resolved',
223
+ * items: [
224
+ * { invoice_line_id: 'line-uuid', quantity: 3 }
225
+ * ]
226
+ * }
227
+ * );
228
+ * ```
229
+ */
230
+ async update(
231
+ creditNoteId: string,
232
+ params: UpdateTenantCreditNoteParams,
233
+ requestOptions?: RequestOptions
234
+ ): Promise<SingleResponse<TenantCreditNote>> {
235
+ return this.http.patch<SingleResponse<TenantCreditNote>>(
236
+ `/tenant/credit-notes/${creditNoteId}`,
237
+ params,
238
+ requestOptions
239
+ );
240
+ }
241
+
242
+ /**
243
+ * Delete a credit note
244
+ *
245
+ * Only credit notes in 'draft' status can be deleted.
246
+ *
247
+ * @param creditNoteId - Credit note UUID
248
+ * @param requestOptions - Optional request configuration
249
+ *
250
+ * @example
251
+ * ```typescript
252
+ * await client.directCreditNotes.delete('draft-credit-note-uuid');
253
+ * console.log('Credit note deleted');
254
+ * ```
255
+ */
256
+ async delete(
257
+ creditNoteId: string,
258
+ requestOptions?: RequestOptions
259
+ ): Promise<MessageResponse> {
260
+ return this.http.delete<MessageResponse>(
261
+ `/tenant/credit-notes/${creditNoteId}`,
262
+ requestOptions
263
+ );
264
+ }
265
+
266
+ /**
267
+ * Send a credit note
268
+ *
269
+ * Validates and sends the credit note, changing status from 'draft' to 'sent'.
270
+ *
271
+ * @param creditNoteId - Credit note UUID
272
+ * @param requestOptions - Optional request configuration
273
+ * @returns Sent credit note
274
+ *
275
+ * @example
276
+ * ```typescript
277
+ * const { data: creditNote } = await client.directCreditNotes.send('credit-note-uuid');
278
+ * console.log('Credit note sent:', creditNote.status); // 'sent'
279
+ * ```
280
+ */
281
+ async send(
282
+ creditNoteId: string,
283
+ requestOptions?: RequestOptions
284
+ ): Promise<SingleResponse<TenantCreditNote>> {
285
+ return this.http.post<SingleResponse<TenantCreditNote>>(
286
+ `/tenant/credit-notes/${creditNoteId}/send`,
287
+ undefined,
288
+ requestOptions
289
+ );
290
+ }
291
+
292
+ /**
293
+ * Download credit note as PDF
294
+ *
295
+ * @param creditNoteId - Credit note UUID
296
+ * @param requestOptions - Optional request configuration
297
+ * @returns ArrayBuffer containing the PDF file
298
+ *
299
+ * @example
300
+ * ```typescript
301
+ * const pdfBuffer = await client.directCreditNotes.download('credit-note-uuid');
302
+ *
303
+ * // In Node.js:
304
+ * import { writeFileSync } from 'fs';
305
+ * writeFileSync('credit-note.pdf', Buffer.from(pdfBuffer));
306
+ *
307
+ * // In browser:
308
+ * const blob = new Blob([pdfBuffer], { type: 'application/pdf' });
309
+ * const url = URL.createObjectURL(blob);
310
+ * window.open(url);
311
+ * ```
312
+ */
313
+ async download(
314
+ creditNoteId: string,
315
+ requestOptions?: RequestOptions
316
+ ): Promise<ArrayBuffer> {
317
+ return this.http.getRaw(
318
+ `/tenant/credit-notes/${creditNoteId}/download`,
319
+ undefined,
320
+ requestOptions
321
+ );
322
+ }
323
+
324
+ /**
325
+ * Get remaining creditable amount for a direct invoice
326
+ *
327
+ * Returns information about how much can still be credited,
328
+ * including line-by-line breakdown.
329
+ *
330
+ * @param invoiceId - Invoice UUID
331
+ * @param requestOptions - Optional request configuration
332
+ * @returns Remaining creditable information
333
+ *
334
+ * @example
335
+ * ```typescript
336
+ * const remaining = await client.directCreditNotes.remainingCreditable('invoice-uuid');
337
+ *
338
+ * console.log('Invoice total:', remaining.invoice_total);
339
+ * console.log('Already credited:', remaining.credited_total);
340
+ * console.log('Can still credit:', remaining.remaining_total);
341
+ *
342
+ * // Check per-line
343
+ * remaining.lines.forEach(line => {
344
+ * if (line.remaining_quantity > 0) {
345
+ * console.log(`${line.description}: ${line.remaining_quantity} units remaining`);
346
+ * }
347
+ * });
348
+ * ```
349
+ */
350
+ async remainingCreditable(
351
+ invoiceId: string,
352
+ requestOptions?: RequestOptions
353
+ ): Promise<DirectInvoiceRemainingCreditable> {
354
+ return this.http.get<DirectInvoiceRemainingCreditable>(
355
+ `/tenant/invoices/${invoiceId}/remaining-creditable`,
356
+ undefined,
357
+ requestOptions
358
+ );
359
+ }
360
+ }