facturas 0.25.1 → 0.26.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/cli.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { A as ArcaClient } from './client-BK-aaWiG.js';
1
+ import { A as ArcaClient } from './client-n_Bkci4D.js';
2
2
  import { A as ArcaClientOptions, a as ArcaClientConfig } from './types-CbvsjIhf.js';
3
3
  import { W as WsaaAuthModule } from './index-BSTxy3nN.js';
4
4
  import './padron.js';
package/dist/cli.mjs CHANGED
@@ -7,7 +7,7 @@ import {
7
7
  createWsaaAuthModule,
8
8
  createWsaaStoreAdapter,
9
9
  discoverArcaClientConfig
10
- } from "./chunk-EL7OXUFE.mjs";
10
+ } from "./chunk-NZ6KEUSM.mjs";
11
11
  import "./chunk-5CGIRDZY.mjs";
12
12
  import "./chunk-FSJHE4VS.mjs";
13
13
  import "./chunk-67ISWKGE.mjs";
@@ -1034,7 +1034,8 @@ declare function voucherDateWindow(input: VoucherDateWindowInput): VoucherDateWi
1034
1034
  type CreditNoteInput = Pick<IssuanceFields, "taxes" | "optionalFields" | "fce"> & {
1035
1035
  /**
1036
1036
  * The authorized invoice or debit note the note corrects, or a non-empty
1037
- * list of them. Every original is consulted and every one is associated to
1037
+ * list of them. WSFE debit notes also accept ordinary credit notes (3, 8,
1038
+ * 13). Every original is consulted and every one is associated to
1038
1039
  * the note; they must agree on everything the note inherits.
1039
1040
  */
1040
1041
  for: VoucherCoordinates | readonly VoucherCoordinates[];
@@ -1072,6 +1073,7 @@ type PeriodNoteInput = IssueInput & {
1072
1073
  };
1073
1074
  for?: never;
1074
1075
  };
1076
+ /** WSFE linked notes also accept authorized ordinary credit notes in `for`. */
1075
1077
  type DebitNoteInput = (CreditNoteInput & {
1076
1078
  all?: never;
1077
1079
  }) | PeriodNoteInput;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { V as VatRate, I as IssuedVoucher, a as VatItem, b as AmountItem } from './client-BK-aaWiG.js';
2
- export { c as ARCA_QR_URL, A as ArcaClient, d as ArcaClientConfigView, e as ArcaQrInput, f as ArcaQrPayload, C as CreateWsfexServiceOptions, g as CreditNoteInput, D as DebitNoteInput, E as ExportCreditNoteInput, h as ExportDebitNoteInput, i as ExportHeader, j as ExportIssueInput, k as ExportIssueOutcome, l as ExportIssueRequest, m as ExportIssuedVoucher, n as ExportItem, o as ExportOfGoods, p as ExportOfServices, q as ExportPreview, r as ExportReceiver, s as ExportVoucherSummary, t as ExportVoucherType, F as FceOptions, u as FiscalHeader, v as InvoiceFamily, w as IssuanceFields, x as IssueAmounts, y as IssueCommon, z as IssueInput, B as IssueOptions, G as IssueOutcome, H as IssuePreview, J as IssueRequest, K as ItemLine, N as NotePreview, P as PeriodNoteInput, L as PreviewOptions, R as Receiver, M as RecoveryOptions, T as Tribute, O as VoucherAmounts, Q as VoucherCoordinates, S as VoucherDateWindowInput, U as VoucherSummary, W as VoucherTotals, X as VoucherTypeInfo, Y as VouchersService, Z as WsfeIdentityMatch, _ as WsfexAssociatedVoucher, $ as WsfexAuthorizationOutcome, a0 as WsfexCatalogEntry, a1 as WsfexCountryTaxId, a2 as WsfexExchangeRate, a3 as WsfexExportType, a4 as WsfexIssueInput, a5 as WsfexItem, a6 as WsfexLanguage, a7 as WsfexPermit, a8 as WsfexSalesPoint, a9 as WsfexService, aa as WsfexVoucherInfo, ab as WsfexVoucherInput, ac as WsfexVoucherLookupResult, ad as WsfexVoucherType, ae as WsmtxcaIssueRequest, af as WsmtxcaLine, ag as arcaQrPayload, ah as arcaQrUrl, ai as createArcaClient, aj as createWsfexService, ak as describeVoucherType, al as matchWsfeVoucherIdentity, am as voucherDateWindow } from './client-BK-aaWiG.js';
1
+ import { V as VatRate, I as IssuedVoucher, a as VatItem, b as AmountItem } from './client-n_Bkci4D.js';
2
+ export { c as ARCA_QR_URL, A as ArcaClient, d as ArcaClientConfigView, e as ArcaQrInput, f as ArcaQrPayload, C as CreateWsfexServiceOptions, g as CreditNoteInput, D as DebitNoteInput, E as ExportCreditNoteInput, h as ExportDebitNoteInput, i as ExportHeader, j as ExportIssueInput, k as ExportIssueOutcome, l as ExportIssueRequest, m as ExportIssuedVoucher, n as ExportItem, o as ExportOfGoods, p as ExportOfServices, q as ExportPreview, r as ExportReceiver, s as ExportVoucherSummary, t as ExportVoucherType, F as FceOptions, u as FiscalHeader, v as InvoiceFamily, w as IssuanceFields, x as IssueAmounts, y as IssueCommon, z as IssueInput, B as IssueOptions, G as IssueOutcome, H as IssuePreview, J as IssueRequest, K as ItemLine, N as NotePreview, P as PeriodNoteInput, L as PreviewOptions, R as Receiver, M as RecoveryOptions, T as Tribute, O as VoucherAmounts, Q as VoucherCoordinates, S as VoucherDateWindowInput, U as VoucherSummary, W as VoucherTotals, X as VoucherTypeInfo, Y as VouchersService, Z as WsfeIdentityMatch, _ as WsfexAssociatedVoucher, $ as WsfexAuthorizationOutcome, a0 as WsfexCatalogEntry, a1 as WsfexCountryTaxId, a2 as WsfexExchangeRate, a3 as WsfexExportType, a4 as WsfexIssueInput, a5 as WsfexItem, a6 as WsfexLanguage, a7 as WsfexPermit, a8 as WsfexSalesPoint, a9 as WsfexService, aa as WsfexVoucherInfo, ab as WsfexVoucherInput, ac as WsfexVoucherLookupResult, ad as WsfexVoucherType, ae as WsmtxcaIssueRequest, af as WsmtxcaLine, ag as arcaQrPayload, ah as arcaQrUrl, ai as createArcaClient, aj as createWsfexService, ak as describeVoucherType, al as matchWsfeVoucherIdentity, am as voucherDateWindow } from './client-n_Bkci4D.js';
3
3
  import { h as ArcaEnvironment, a as ArcaClientConfig, i as ArcaStore, j as ArcaWsaaSessionStore } from './types-CbvsjIhf.js';
4
4
  export { g as ArcaAuthCredentials, f as ArcaAuthOptions, A as ArcaClientConfig, k as ArcaLockOptions, l as ArcaLogLevel, m as ArcaLoggerConfig, n as ArcaPadronServiceName, o as ArcaRepresentedTaxId, b as ArcaServiceName, p as ArcaServiceTarget, e as ArcaWsaaServiceId, q as ArcaWsaaSessionKey } from './types-CbvsjIhf.js';
5
5
  export { ARCA_FINAL_CONSUMER_IDENTIFICATION_THRESHOLD_MINOR_UNITS, ARCA_INVOICE_CLASS_BY_ISSUER, ARCA_ISSUER_CONDITION_IDS, ARCA_RECEIVER_CONDITION_IDS, ARCA_WSMTXCA_GENERIC_CODES, IssuerCondition, IssuerConditionId, ReceiverCondition, VoucherClass } from './constants.js';
@@ -63,12 +63,18 @@ declare const PRINTED_VOUCHER_TEXT: {
63
63
  declare const PRINTED_CAE_DUE_DATE_MIN_FONT_SIZE_PT = 12;
64
64
  /** The issuer conditions a printed voucher can name (C5). */
65
65
  type PrintedIssuerCondition = "responsable_inscripto" | "exento" | "no_alcanzado" | "monotributo" | "monotributo_social" | "monotributo_trabajador_independiente_promovido";
66
+ /** A labeled change already included in the authorized voucher's money. */
67
+ type VoucherDocumentAdjustment = {
68
+ label: string;
69
+ /** Signed minor units: net by VAT rate id on A, gross by id on B, one amount on C. */
70
+ amount: number | Readonly<Record<number, number>>;
71
+ };
66
72
  type VoucherDocumentInput = {
67
73
  /** The voucher `issue()`, a note method or `recover()` answered authorized. */
68
74
  voucher: IssuedVoucher;
69
75
  /**
70
76
  * The same items the voucher was issued from, each with its description.
71
- * With a `globalDiscount`, the items before it.
77
+ * With a `globalDiscount` or `adjustments`, the items before those changes.
72
78
  */
73
79
  items: readonly (VatItem | AmountItem)[];
74
80
  /**
@@ -79,6 +85,13 @@ type VoucherDocumentInput = {
79
85
  * less its discount, must add up to the authorized base of that rate.
80
86
  */
81
87
  globalDiscount?: number | Readonly<Record<number, number>>;
88
+ /**
89
+ * Signed, labeled totals rows, already included when issuing. Like the
90
+ * global discount, amounts are net by ARCA VAT rate id on A, with VAT by id
91
+ * on B, and one amount on C. Each rate's lines less its global discount
92
+ * plus its adjustments must equal its authorized base. No automatic split.
93
+ */
94
+ adjustments?: readonly VoucherDocumentAdjustment[];
82
95
  issuer: {
83
96
  /** C1. */
84
97
  legalName: string;
@@ -191,9 +204,14 @@ type VoucherDocument = {
191
204
  * with VAT on class B. The lines are before it and the subtotal after it.
192
205
  */
193
206
  globalDiscount: number;
207
+ /** Signed issuer-labeled rows, in input order. Absent without adjustments. */
208
+ adjustments?: {
209
+ label: string;
210
+ amount: number;
211
+ }[];
194
212
  /**
195
213
  * Class A: net taxed. Class B: the lines with VAT. Class C: the lines.
196
- * Classes B and C take the global discount off the lines.
214
+ * Includes the global discount and the labeled adjustments.
197
215
  */
198
216
  subtotal: number;
199
217
  exempt: number;
@@ -276,4 +294,4 @@ declare function createRedisStore(client: RedisClient, options?: {
276
294
 
277
295
  declare function createMemoryWsaaSessionStore(): ArcaWsaaSessionStore;
278
296
 
279
- export { ARCA_ENVIRONMENTS, ARCA_ENV_VARIABLES, AmountItem, ArcaEnvironment, ArcaStore, ArcaWsaaSessionStore, type CreateArcaClientConfigFromEnvOptions, IssuedVoucher, MONOTRIBUTO_CREDIT_LEGEND, PRINTED_CAE_DUE_DATE_MIN_FONT_SIZE_PT, PRINTED_VOUCHER_TEXT, type PrintedIssuerCondition, VatItem, VatRate, type VoucherDocument, type VoucherDocumentInput, type VoucherDocumentLine, assertArcaClientConfig, buildVoucherDocument, createArcaClientConfigFromEnv, createFileStore, createMemoryStore, createMemoryWsaaSessionStore, createPostgresStore, createRedisStore };
297
+ export { ARCA_ENVIRONMENTS, ARCA_ENV_VARIABLES, AmountItem, ArcaEnvironment, ArcaStore, ArcaWsaaSessionStore, type CreateArcaClientConfigFromEnvOptions, IssuedVoucher, MONOTRIBUTO_CREDIT_LEGEND, PRINTED_CAE_DUE_DATE_MIN_FONT_SIZE_PT, PRINTED_VOUCHER_TEXT, type PrintedIssuerCondition, VatItem, VatRate, type VoucherDocument, type VoucherDocumentAdjustment, type VoucherDocumentInput, type VoucherDocumentLine, assertArcaClientConfig, buildVoucherDocument, createArcaClientConfigFromEnv, createFileStore, createMemoryStore, createMemoryWsaaSessionStore, createPostgresStore, createRedisStore };
package/dist/index.mjs CHANGED
@@ -22,7 +22,7 @@ import {
22
22
  vatRatePercent,
23
23
  voucherDateWindow,
24
24
  withLease
25
- } from "./chunk-EL7OXUFE.mjs";
25
+ } from "./chunk-NZ6KEUSM.mjs";
26
26
  import {
27
27
  ARCA_FINAL_CONSUMER_IDENTIFICATION_THRESHOLD_MINOR_UNITS,
28
28
  ARCA_INVOICE_CLASS_BY_ISSUER,
@@ -192,6 +192,7 @@ function buildVoucherDocument(input) {
192
192
  lines: lines.lines,
193
193
  totals: {
194
194
  globalDiscount: lines.globalDiscount,
195
+ ...lines.adjustments.length > 0 ? { adjustments: lines.adjustments } : {},
195
196
  subtotal: lines.subtotal,
196
197
  exempt: voucher.totals.exempt,
197
198
  untaxed: voucher.totals.untaxed,
@@ -305,27 +306,16 @@ function documentLines(input, voucherClass) {
305
306
  invalid("items", "the items the voucher was issued from");
306
307
  }
307
308
  const discount = globalDiscount(input.globalDiscount, voucherClass);
308
- let computed;
309
- try {
310
- computed = calculateWsfeAmounts({
311
- voucherClass,
312
- items,
313
- // Items before a global discount add up to more than the authorized total.
314
- ...discount ? {} : { total: totals.total - totals.otherTaxes }
315
- });
316
- } catch (error) {
317
- if (error instanceof ArcaInputError && error.code === "ARCA_INPUT_AMOUNT_MISMATCH") {
318
- mismatch("total");
319
- }
320
- throw error;
321
- }
322
- if (!discount) {
309
+ const adjustments = documentAdjustments(input.adjustments, voucherClass);
310
+ const modified = discount !== void 0 || adjustments.length > 0;
311
+ const computed = documentAmounts(input, voucherClass, modified);
312
+ if (!modified) {
323
313
  assertItemsMatchTotals(computed.data, totals, voucherClass);
324
314
  }
325
315
  const settled = voucherClass === "C" ? void 0 : settleVatItems(
326
316
  { voucherClass, items },
327
- // The header's VAT adjustment belongs to the discounted money.
328
- discount ? 0 : computed.amounts.vatAdjustment
317
+ // The header's VAT residual belongs to the money after changes.
318
+ modified ? 0 : computed.amounts.vatAdjustment
329
319
  );
330
320
  const lines = items.map((item, index) => {
331
321
  const path = `items[${index}]`;
@@ -343,19 +333,32 @@ function documentLines(input, voucherClass) {
343
333
  });
344
334
  const sum = lines.reduce((total, line) => total + line.amount, 0);
345
335
  const rows = totals.vatRates.reduce((total, row) => total + row.amount, 0);
346
- if (discount) {
347
- assertDiscountedLinesMatchTotals(
336
+ const adjustmentRows = adjustments.map(({ label, total }) => ({
337
+ label,
338
+ amount: total
339
+ }));
340
+ if (modified) {
341
+ assertModifiedLinesMatchTotals(
348
342
  voucherClass,
349
343
  lines,
350
344
  settled?.items,
351
345
  computed.data,
352
346
  totals,
353
- discount.byRate
347
+ discount?.byRate ?? /* @__PURE__ */ new Map(),
348
+ adjustments
354
349
  );
355
350
  return {
356
351
  lines,
357
- globalDiscount: discount.total,
358
- subtotal: voucherClass === "A" ? totals.netTaxed : sum - discount.total,
352
+ globalDiscount: discount?.total ?? 0,
353
+ adjustments: adjustmentRows,
354
+ subtotal: voucherClass === "A" ? totals.netTaxed : sumMinorUnits(
355
+ [
356
+ sum,
357
+ -(discount?.total ?? 0),
358
+ ...adjustmentRows.map((row) => row.amount)
359
+ ],
360
+ "adjustments"
361
+ ),
359
362
  adjustment: voucherClass === "C" ? 0 : totals.vat - rows
360
363
  };
361
364
  }
@@ -363,6 +366,7 @@ function documentLines(input, voucherClass) {
363
366
  return {
364
367
  lines,
365
368
  globalDiscount: 0,
369
+ adjustments: [],
366
370
  subtotal: totals.netTaxed,
367
371
  adjustment: totals.vat - rows
368
372
  };
@@ -370,10 +374,88 @@ function documentLines(input, voucherClass) {
370
374
  return {
371
375
  lines,
372
376
  globalDiscount: 0,
377
+ adjustments: [],
373
378
  subtotal: sum,
374
379
  adjustment: settled?.unabsorbed ?? 0
375
380
  };
376
381
  }
382
+ function documentAmounts(input, voucherClass, modified) {
383
+ try {
384
+ return calculateWsfeAmounts({
385
+ voucherClass,
386
+ items: input.items,
387
+ // The printed items precede changes already in the authorized total.
388
+ ...modified ? {} : {
389
+ total: input.voucher.totals.total - input.voucher.totals.otherTaxes
390
+ }
391
+ });
392
+ } catch (error) {
393
+ if (error instanceof ArcaInputError && error.code === "ARCA_INPUT_AMOUNT_MISMATCH") {
394
+ mismatch("total");
395
+ }
396
+ throw error;
397
+ }
398
+ }
399
+ function documentAdjustments(value, voucherClass) {
400
+ if (value === void 0) {
401
+ return [];
402
+ }
403
+ if (!Array.isArray(value)) {
404
+ invalid("adjustments", "an array of labeled signed adjustments");
405
+ }
406
+ return value.flatMap((entry, index) => {
407
+ const field = `adjustments[${index}].amount`;
408
+ if (entry === null || typeof entry !== "object" || Array.isArray(entry)) {
409
+ invalid(`adjustments[${index}]`, "a labeled signed adjustment");
410
+ }
411
+ const label = required(entry.label, `adjustments[${index}].label`);
412
+ const byRate = adjustmentByRate(entry.amount, field, voucherClass);
413
+ return byRate.size === 0 ? [] : [
414
+ {
415
+ label,
416
+ field,
417
+ byRate,
418
+ total: sumMinorUnits([...byRate.values()], field)
419
+ }
420
+ ];
421
+ });
422
+ }
423
+ function adjustmentByRate(amount, field, voucherClass) {
424
+ if (voucherClass === "C") {
425
+ const money = signedAmount(amount, field);
426
+ return money === 0 ? /* @__PURE__ */ new Map() : /* @__PURE__ */ new Map([[0, money]]);
427
+ }
428
+ if (amount === null || typeof amount !== "object" || Array.isArray(amount)) {
429
+ invalid(field, "signed amounts by ARCA VAT rate id on class A or B");
430
+ }
431
+ const byRate = /* @__PURE__ */ new Map();
432
+ for (const [key, money] of Object.entries(amount)) {
433
+ const id = Number(key);
434
+ if (String(id) !== key || vatRatePercent(id) === void 0) {
435
+ invalid(`${field}[${key}]`, "keyed by an ARCA VAT rate id");
436
+ }
437
+ const checked = signedAmount(money, `${field}[${key}]`);
438
+ if (checked !== 0) {
439
+ byRate.set(id, checked);
440
+ }
441
+ }
442
+ return byRate;
443
+ }
444
+ function signedAmount(value, field) {
445
+ if (!Number.isSafeInteger(value)) {
446
+ invalid(field, "a signed safe integer in minor units");
447
+ }
448
+ return value;
449
+ }
450
+ function sumMinorUnits(values, field) {
451
+ const sum = Number(
452
+ values.reduce((total, amount) => total + BigInt(amount), 0n)
453
+ );
454
+ if (!Number.isSafeInteger(sum)) {
455
+ invalid(field, "a sum within the safe integer range in minor units");
456
+ }
457
+ return sum;
458
+ }
377
459
  function globalDiscount(value, voucherClass) {
378
460
  if (value === void 0) {
379
461
  return;
@@ -414,7 +496,7 @@ function discountAmount(value, field) {
414
496
  }
415
497
  return value;
416
498
  }
417
- function assertDiscountedLinesMatchTotals(voucherClass, lines, settled, data, totals, discount) {
499
+ function assertModifiedLinesMatchTotals(voucherClass, lines, settled, data, totals, discount, adjustments) {
418
500
  const minor = (value) => Number(normalizeArcaAmountToMinorUnits(value, "items"));
419
501
  if (minor(data.exemptAmount) !== totals.exempt) {
420
502
  mismatch("exempt");
@@ -427,30 +509,43 @@ function assertDiscountedLinesMatchTotals(voucherClass, lines, settled, data, to
427
509
  [0, lines.reduce((sum, line) => sum + line.amount, 0)]
428
510
  ]),
429
511
  authorized: /* @__PURE__ */ new Map([[0, totals.netTaxed]])
430
- } : discountedRates(voucherClass, lines, settled, totals);
431
- for (const id of discount.keys()) {
432
- if (!byLines.has(id)) {
433
- invalid(
434
- `globalDiscount[${id}]`,
435
- "keyed by a VAT rate id the items carry"
436
- );
437
- }
512
+ } : modifiedRates(voucherClass, lines, settled, totals);
513
+ assertRatesPresent(byLines, discount, "globalDiscount");
514
+ for (const adjustment of adjustments) {
515
+ assertRatesPresent(byLines, adjustment.byRate, adjustment.field);
438
516
  }
439
517
  for (const id of /* @__PURE__ */ new Set([...byLines.keys(), ...authorized.keys()])) {
440
518
  const expected = authorized.get(id) ?? 0;
441
- if ((byLines.get(id) ?? 0) - (discount.get(id) ?? 0) !== expected) {
519
+ const actual = BigInt(byLines.get(id) ?? 0) - BigInt(discount.get(id) ?? 0) + adjustments.reduce(
520
+ (sum, adjustment) => sum + BigInt(adjustment.byRate.get(id) ?? 0),
521
+ 0n
522
+ );
523
+ if (actual !== BigInt(expected)) {
442
524
  throw new ArcaInputError(
443
- "items less the global discount do not add up to the authorized voucher.",
525
+ "items less the global discount plus adjustments do not add up to the authorized voucher.",
444
526
  {
445
527
  code: "ARCA_INPUT_AMOUNT_MISMATCH",
446
- field: voucherClass === "C" ? "globalDiscount" : `globalDiscount[${id}]`,
447
- expected: `${expected} minor units of lines less discount`
528
+ field: modificationField(voucherClass, id, adjustments.length > 0),
529
+ expected: `${expected} minor units of lines less discount plus adjustments`
448
530
  }
449
531
  );
450
532
  }
451
533
  }
452
534
  }
453
- function discountedRates(voucherClass, lines, settled, totals) {
535
+ function assertRatesPresent(byLines, byRate, field) {
536
+ for (const id of byRate.keys()) {
537
+ if (!byLines.has(id)) {
538
+ invalid(`${field}[${id}]`, "keyed by a VAT rate id the items carry");
539
+ }
540
+ }
541
+ }
542
+ function modificationField(voucherClass, id, adjusted) {
543
+ if (adjusted) {
544
+ return voucherClass === "C" ? "adjustments" : `adjustments (VAT rate ${id})`;
545
+ }
546
+ return voucherClass === "C" ? "globalDiscount" : `globalDiscount[${id}]`;
547
+ }
548
+ function modifiedRates(voucherClass, lines, settled, totals) {
454
549
  const byLines = /* @__PURE__ */ new Map();
455
550
  for (const [index, line] of lines.entries()) {
456
551
  const rate = settled[index]?.rate;