facturas 0.22.1 → 0.23.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.
@@ -758,6 +758,14 @@ var RATES = {
758
758
  21: { id: ARCA_VAT_RATES.IVA_21, basisPoints: 2100n },
759
759
  27: { id: ARCA_VAT_RATES.IVA_27, basisPoints: 2700n }
760
760
  };
761
+ function vatRatePercent(id) {
762
+ for (const [rate, entry] of Object.entries(RATES)) {
763
+ if (entry.id === id) {
764
+ return Number(rate);
765
+ }
766
+ }
767
+ return void 0;
768
+ }
761
769
  function calculateWsfeAmounts(input) {
762
770
  if (!Array.isArray(input.items) || input.items.length === 0) {
763
771
  invalidItem("items", "a non-empty array of items");
@@ -919,28 +927,29 @@ function deriveWsmtxcaSettlement(input, vatAdjustment) {
919
927
  }
920
928
  function lineDraft(item, path, groups) {
921
929
  const line = assertItemLine(item, path);
930
+ const money = moneyDraft(item, path, groups);
931
+ const vatCondition = money.rate === void 0 ? money.vatRate === "exempt" ? EXEMPT_CONDITION : UNTAXED_CONDITION : RATES[money.rate].id;
932
+ return { ...money, line: { ...line, vatCondition, amount: 0 } };
933
+ }
934
+ function moneyDraft(item, path, groups) {
922
935
  const { amount, field, rate } = vatItemAmount(item, path);
923
936
  if (rate === "exempt" || rate === "untaxed") {
924
937
  return {
925
- line: {
926
- ...line,
927
- vatCondition: rate === "exempt" ? EXEMPT_CONDITION : UNTAXED_CONDITION,
928
- amount: 0
929
- },
930
938
  vat: 0n,
931
939
  amount,
940
+ vatRate: rate,
932
941
  amountFormula: { numerator: amount, denominator: 1n }
933
942
  };
934
943
  }
935
- const { id, basisPoints } = RATES[rate];
944
+ const { basisPoints } = RATES[rate];
936
945
  const group = groups.get(rate) ?? { net: 0n, gross: 0n };
937
946
  group[field] += amount;
938
947
  groups.set(rate, group);
939
948
  const vat = field === "gross" ? amount - roundHalfEvenRatio(amount * 10000n, 10000n + basisPoints) : roundHalfEvenRatio(amount * basisPoints, 10000n);
940
949
  return {
941
- line: { ...line, vatCondition: id, amount: 0 },
942
950
  vat,
943
951
  amount: field === "gross" ? amount : amount + vat,
952
+ vatRate: rate,
944
953
  rate,
945
954
  field,
946
955
  vatFormula: field === "gross" ? {
@@ -1024,8 +1033,19 @@ function applyMove(draft, step, movesItem) {
1024
1033
  }
1025
1034
  }
1026
1035
  function absorbAdjustment(drafts, adjustment) {
1036
+ if (absorb(drafts, adjustment) !== 0n) {
1037
+ throw new ArcaInputError(
1038
+ "total cannot be reconciled with WSMTXCA item formulas within ARCA's tolerance.",
1039
+ {
1040
+ code: "ARCA_INPUT_AMOUNT_MISMATCH",
1041
+ field: "total"
1042
+ }
1043
+ );
1044
+ }
1045
+ }
1046
+ function absorb(drafts, adjustment) {
1027
1047
  if (adjustment === 0n) {
1028
- return;
1048
+ return 0n;
1029
1049
  }
1030
1050
  const step = adjustment > 0n ? 1n : -1n;
1031
1051
  let remaining = adjustment;
@@ -1046,16 +1066,26 @@ function absorbAdjustment(drafts, adjustment) {
1046
1066
  applyMove(target, step, true);
1047
1067
  remaining -= step;
1048
1068
  if (remaining === 0n) {
1049
- return;
1069
+ return 0n;
1050
1070
  }
1051
1071
  }
1052
- throw new ArcaInputError(
1053
- "total cannot be reconciled with WSMTXCA item formulas within ARCA's tolerance.",
1054
- {
1055
- code: "ARCA_INPUT_AMOUNT_MISMATCH",
1056
- field: "total"
1057
- }
1072
+ return remaining;
1073
+ }
1074
+ function settleVatItems(input, vatAdjustment) {
1075
+ const groups = /* @__PURE__ */ new Map();
1076
+ const drafts = input.items.map(
1077
+ (item, index) => moneyDraft(item, `items[${index}]`, groups)
1058
1078
  );
1079
+ reconcileGroups(drafts, groups);
1080
+ const unabsorbed = absorb(drafts, BigInt(vatAdjustment));
1081
+ return {
1082
+ items: drafts.map(({ amount, vat, vatRate }) => ({
1083
+ amount: Number(amount),
1084
+ vat: Number(vat),
1085
+ rate: vatRate
1086
+ })),
1087
+ unabsorbed: Number(unabsorbed)
1088
+ };
1059
1089
  }
1060
1090
  var ITEM_KEYS = [
1061
1091
  "net",
@@ -7603,9 +7633,37 @@ function issuedVoucher2({
7603
7633
  cae,
7604
7634
  caeExpiry: toIsoDate(caeExpiry) ?? caeExpiry,
7605
7635
  amounts,
7636
+ totals: voucherTotals(data),
7606
7637
  ...qr
7607
7638
  };
7608
7639
  }
7640
+ function voucherTotals(data) {
7641
+ const minor2 = (value, field) => Number(normalizeArcaAmountToMinorUnits(value ?? 0, field));
7642
+ return {
7643
+ total: minor2(data.totalAmount, "totalAmount"),
7644
+ netTaxed: minor2(data.netAmount, "netAmount"),
7645
+ untaxed: minor2(data.nonTaxableAmount, "nonTaxableAmount"),
7646
+ exempt: minor2(data.exemptAmount, "exemptAmount"),
7647
+ vat: minor2(data.vatAmount, "vatAmount"),
7648
+ otherTaxes: minor2(data.taxAmount, "taxAmount"),
7649
+ vatRates: (data.vatRates ?? []).map((row) => {
7650
+ const rate = vatRatePercent(row.id);
7651
+ return {
7652
+ id: row.id,
7653
+ ...rate === void 0 ? {} : { rate },
7654
+ base: minor2(row.baseAmount, "vatRates.baseAmount"),
7655
+ amount: minor2(row.amount, "vatRates.amount")
7656
+ };
7657
+ }),
7658
+ taxes: (data.taxes ?? []).map((tax) => ({
7659
+ id: tax.id,
7660
+ ...tax.description === void 0 ? {} : { description: tax.description },
7661
+ base: minor2(tax.baseAmount, "taxes.baseAmount"),
7662
+ rate: tax.rate,
7663
+ amount: minor2(tax.amount, "taxes.amount")
7664
+ }))
7665
+ };
7666
+ }
7609
7667
  function fiscalHeader(data, lookup) {
7610
7668
  const sent = normalizeWsfeVoucherInput(data);
7611
7669
  const header = {
@@ -8246,6 +8304,8 @@ export {
8246
8304
  ARCA_QR_URL,
8247
8305
  arcaQrUrl,
8248
8306
  arcaQrPayload,
8307
+ calculateWsfeAmounts,
8308
+ settleVatItems,
8249
8309
  voucherDateWindow,
8250
8310
  createWsfexService,
8251
8311
  matchWsfeVoucherIdentity,
@@ -8256,4 +8316,4 @@ export {
8256
8316
  createArcaClient,
8257
8317
  createFileStore
8258
8318
  };
8259
- //# sourceMappingURL=chunk-6BLEIM5F.mjs.map
8319
+ //# sourceMappingURL=chunk-HSC3KLPL.mjs.map