facturas 0.22.0 → 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",
@@ -2499,7 +2529,11 @@ function mapWsfexVoucherInfo(raw) {
2499
2529
  );
2500
2530
  assignWsfexValue(voucher, "paymentTerms", readWsfexText(raw.Forma_pago));
2501
2531
  assignWsfexValue(voucher, "incoterms", readWsfexText(raw.Incoterms));
2502
- assignWsfexValue(voucher, "incotermsDetail", readWsfexText(raw.Incoterms_Ds));
2532
+ assignWsfexValue(
2533
+ voucher,
2534
+ "incotermsDetail",
2535
+ readWsfexIncotermsDetail(raw.Incoterms_Ds)
2536
+ );
2503
2537
  assignWsfexValue(voucher, "language", readWsfexNumber(raw.Idioma_cbte));
2504
2538
  assignWsfexValue(voucher, "paymentDate", readWsfexText(raw.Fecha_pago));
2505
2539
  assignWsfexValue(
@@ -2588,6 +2622,10 @@ function readWsfexTaxId(value) {
2588
2622
  const text2 = readWsfexText(value);
2589
2623
  return text2 !== void 0 && /^\d+$/.test(text2) && Number(text2) !== 0 ? text2 : void 0;
2590
2624
  }
2625
+ function readWsfexIncotermsDetail(value) {
2626
+ const text2 = readWsfexText(value);
2627
+ return text2 === "0" ? void 0 : text2;
2628
+ }
2591
2629
  function readWsfexFlag(value) {
2592
2630
  const flag = normalizeWsfexResult(value);
2593
2631
  return flag === "S" || flag === "N" ? flag : void 0;
@@ -4722,7 +4760,8 @@ function fiscalIdentity(voucher) {
4722
4760
  ]),
4723
4761
  paymentTerms: trimmed(voucher.paymentTerms),
4724
4762
  incoterms: trimmed(voucher.incoterms),
4725
- incotermsDetail: trimmed(voucher.incotermsDetail),
4763
+ // ARCA stores no detail as "0", and the lookup reads "0" as absent.
4764
+ incotermsDetail: trimmed(voucher.incotermsDetail) === "0" ? void 0 : trimmed(voucher.incotermsDetail),
4726
4765
  language: voucher.language,
4727
4766
  items: voucher.items.map((item) => [
4728
4767
  trimmed(item.code),
@@ -7594,9 +7633,37 @@ function issuedVoucher2({
7594
7633
  cae,
7595
7634
  caeExpiry: toIsoDate(caeExpiry) ?? caeExpiry,
7596
7635
  amounts,
7636
+ totals: voucherTotals(data),
7597
7637
  ...qr
7598
7638
  };
7599
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
+ }
7600
7667
  function fiscalHeader(data, lookup) {
7601
7668
  const sent = normalizeWsfeVoucherInput(data);
7602
7669
  const header = {
@@ -8237,6 +8304,8 @@ export {
8237
8304
  ARCA_QR_URL,
8238
8305
  arcaQrUrl,
8239
8306
  arcaQrPayload,
8307
+ calculateWsfeAmounts,
8308
+ settleVatItems,
8240
8309
  voucherDateWindow,
8241
8310
  createWsfexService,
8242
8311
  matchWsfeVoucherIdentity,
@@ -8247,4 +8316,4 @@ export {
8247
8316
  createArcaClient,
8248
8317
  createFileStore
8249
8318
  };
8250
- //# sourceMappingURL=chunk-R4MOVPYT.mjs.map
8319
+ //# sourceMappingURL=chunk-HSC3KLPL.mjs.map