facturas 0.22.1 → 0.23.1

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.
@@ -548,7 +548,7 @@ function validateFiscalHeader(data) {
548
548
  bad("service");
549
549
  }
550
550
  if (data.concept === 2 || data.concept === 3) {
551
- if (!(data.serviceStartDate && data.serviceEndDate && data.paymentDueDate)) {
551
+ if (!(data.serviceStartDate && data.serviceEndDate && (isNonAnnulmentFceNote(data) || data.paymentDueDate))) {
552
552
  bad("service");
553
553
  }
554
554
  const start = normalizeWsfeDateInput(data.serviceStartDate, "service.from");
@@ -564,6 +564,10 @@ function validateFiscalHeader(data) {
564
564
  bad("paidInForeignCurrency");
565
565
  }
566
566
  }
567
+ function isNonAnnulmentFceNote(data) {
568
+ const { family, types } = voucherFamily(data.voucherType);
569
+ return family === "fce" && data.voucherType !== types[0] && normalizedFceAnnulment({ optionalFields: data.optionalFields }) === false;
570
+ }
567
571
  function applyFceFields(data, fce) {
568
572
  if (fce === void 0) {
569
573
  return;
@@ -629,6 +633,9 @@ function validateFceHeader(data) {
629
633
  } else if (!["S", "N"].includes(options.get("22") ?? "") || options.has("2101") || options.has("2102") || options.has("27")) {
630
634
  bad("fce.annulment");
631
635
  }
636
+ if (isNonAnnulmentFceNote(data) && data.paymentDueDate !== void 0) {
637
+ bad("dueDate");
638
+ }
632
639
  }
633
640
  }
634
641
  function validateFceOptions(fce) {
@@ -758,6 +765,14 @@ var RATES = {
758
765
  21: { id: ARCA_VAT_RATES.IVA_21, basisPoints: 2100n },
759
766
  27: { id: ARCA_VAT_RATES.IVA_27, basisPoints: 2700n }
760
767
  };
768
+ function vatRatePercent(id) {
769
+ for (const [rate, entry] of Object.entries(RATES)) {
770
+ if (entry.id === id) {
771
+ return Number(rate);
772
+ }
773
+ }
774
+ return void 0;
775
+ }
761
776
  function calculateWsfeAmounts(input) {
762
777
  if (!Array.isArray(input.items) || input.items.length === 0) {
763
778
  invalidItem("items", "a non-empty array of items");
@@ -919,28 +934,29 @@ function deriveWsmtxcaSettlement(input, vatAdjustment) {
919
934
  }
920
935
  function lineDraft(item, path, groups) {
921
936
  const line = assertItemLine(item, path);
937
+ const money = moneyDraft(item, path, groups);
938
+ const vatCondition = money.rate === void 0 ? money.vatRate === "exempt" ? EXEMPT_CONDITION : UNTAXED_CONDITION : RATES[money.rate].id;
939
+ return { ...money, line: { ...line, vatCondition, amount: 0 } };
940
+ }
941
+ function moneyDraft(item, path, groups) {
922
942
  const { amount, field, rate } = vatItemAmount(item, path);
923
943
  if (rate === "exempt" || rate === "untaxed") {
924
944
  return {
925
- line: {
926
- ...line,
927
- vatCondition: rate === "exempt" ? EXEMPT_CONDITION : UNTAXED_CONDITION,
928
- amount: 0
929
- },
930
945
  vat: 0n,
931
946
  amount,
947
+ vatRate: rate,
932
948
  amountFormula: { numerator: amount, denominator: 1n }
933
949
  };
934
950
  }
935
- const { id, basisPoints } = RATES[rate];
951
+ const { basisPoints } = RATES[rate];
936
952
  const group = groups.get(rate) ?? { net: 0n, gross: 0n };
937
953
  group[field] += amount;
938
954
  groups.set(rate, group);
939
955
  const vat = field === "gross" ? amount - roundHalfEvenRatio(amount * 10000n, 10000n + basisPoints) : roundHalfEvenRatio(amount * basisPoints, 10000n);
940
956
  return {
941
- line: { ...line, vatCondition: id, amount: 0 },
942
957
  vat,
943
958
  amount: field === "gross" ? amount : amount + vat,
959
+ vatRate: rate,
944
960
  rate,
945
961
  field,
946
962
  vatFormula: field === "gross" ? {
@@ -1024,8 +1040,19 @@ function applyMove(draft, step, movesItem) {
1024
1040
  }
1025
1041
  }
1026
1042
  function absorbAdjustment(drafts, adjustment) {
1043
+ if (absorb(drafts, adjustment) !== 0n) {
1044
+ throw new ArcaInputError(
1045
+ "total cannot be reconciled with WSMTXCA item formulas within ARCA's tolerance.",
1046
+ {
1047
+ code: "ARCA_INPUT_AMOUNT_MISMATCH",
1048
+ field: "total"
1049
+ }
1050
+ );
1051
+ }
1052
+ }
1053
+ function absorb(drafts, adjustment) {
1027
1054
  if (adjustment === 0n) {
1028
- return;
1055
+ return 0n;
1029
1056
  }
1030
1057
  const step = adjustment > 0n ? 1n : -1n;
1031
1058
  let remaining = adjustment;
@@ -1046,16 +1073,26 @@ function absorbAdjustment(drafts, adjustment) {
1046
1073
  applyMove(target, step, true);
1047
1074
  remaining -= step;
1048
1075
  if (remaining === 0n) {
1049
- return;
1076
+ return 0n;
1050
1077
  }
1051
1078
  }
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
- }
1079
+ return remaining;
1080
+ }
1081
+ function settleVatItems(input, vatAdjustment) {
1082
+ const groups = /* @__PURE__ */ new Map();
1083
+ const drafts = input.items.map(
1084
+ (item, index) => moneyDraft(item, `items[${index}]`, groups)
1058
1085
  );
1086
+ reconcileGroups(drafts, groups);
1087
+ const unabsorbed = absorb(drafts, BigInt(vatAdjustment));
1088
+ return {
1089
+ items: drafts.map(({ amount, vat, vatRate }) => ({
1090
+ amount: Number(amount),
1091
+ vat: Number(vat),
1092
+ rate: vatRate
1093
+ })),
1094
+ unabsorbed: Number(unabsorbed)
1095
+ };
1059
1096
  }
1060
1097
  var ITEM_KEYS = [
1061
1098
  "net",
@@ -5852,6 +5889,9 @@ function copyServiceDates(original, header) {
5852
5889
  original.serviceEndDate,
5853
5890
  "serviceEndDate"
5854
5891
  );
5892
+ if (isNonAnnulmentFceNote(header)) {
5893
+ return;
5894
+ }
5855
5895
  const due = normalizeWsfeDateInput(
5856
5896
  required(original.paymentDueDate, "paymentDueDate"),
5857
5897
  "original.paymentDueDate"
@@ -7603,9 +7643,37 @@ function issuedVoucher2({
7603
7643
  cae,
7604
7644
  caeExpiry: toIsoDate(caeExpiry) ?? caeExpiry,
7605
7645
  amounts,
7646
+ totals: voucherTotals(data),
7606
7647
  ...qr
7607
7648
  };
7608
7649
  }
7650
+ function voucherTotals(data) {
7651
+ const minor2 = (value, field) => Number(normalizeArcaAmountToMinorUnits(value ?? 0, field));
7652
+ return {
7653
+ total: minor2(data.totalAmount, "totalAmount"),
7654
+ netTaxed: minor2(data.netAmount, "netAmount"),
7655
+ untaxed: minor2(data.nonTaxableAmount, "nonTaxableAmount"),
7656
+ exempt: minor2(data.exemptAmount, "exemptAmount"),
7657
+ vat: minor2(data.vatAmount, "vatAmount"),
7658
+ otherTaxes: minor2(data.taxAmount, "taxAmount"),
7659
+ vatRates: (data.vatRates ?? []).map((row) => {
7660
+ const rate = vatRatePercent(row.id);
7661
+ return {
7662
+ id: row.id,
7663
+ ...rate === void 0 ? {} : { rate },
7664
+ base: minor2(row.baseAmount, "vatRates.baseAmount"),
7665
+ amount: minor2(row.amount, "vatRates.amount")
7666
+ };
7667
+ }),
7668
+ taxes: (data.taxes ?? []).map((tax) => ({
7669
+ id: tax.id,
7670
+ ...tax.description === void 0 ? {} : { description: tax.description },
7671
+ base: minor2(tax.baseAmount, "taxes.baseAmount"),
7672
+ rate: tax.rate,
7673
+ amount: minor2(tax.amount, "taxes.amount")
7674
+ }))
7675
+ };
7676
+ }
7609
7677
  function fiscalHeader(data, lookup) {
7610
7678
  const sent = normalizeWsfeVoucherInput(data);
7611
7679
  const header = {
@@ -8246,6 +8314,8 @@ export {
8246
8314
  ARCA_QR_URL,
8247
8315
  arcaQrUrl,
8248
8316
  arcaQrPayload,
8317
+ calculateWsfeAmounts,
8318
+ settleVatItems,
8249
8319
  voucherDateWindow,
8250
8320
  createWsfexService,
8251
8321
  matchWsfeVoucherIdentity,
@@ -8256,4 +8326,4 @@ export {
8256
8326
  createArcaClient,
8257
8327
  createFileStore
8258
8328
  };
8259
- //# sourceMappingURL=chunk-6BLEIM5F.mjs.map
8329
+ //# sourceMappingURL=chunk-GERGSYQC.mjs.map