facturas 0.18.0 → 0.19.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.
@@ -2959,6 +2959,50 @@ function buenosAiresDate(now) {
2959
2959
  }).formatToParts(now);
2960
2960
  return ["year", "month", "day"].map((part) => parts.find((entry) => entry.type === part)?.value).join("");
2961
2961
  }
2962
+ var FCE_TYPES = Object.values(FAMILIES.fce);
2963
+ var FCE_INVOICE_TYPES = FCE_TYPES.map(([invoice]) => invoice);
2964
+ var FCE_NOTE_TYPES = FCE_TYPES.flatMap(([, ...notes]) => notes);
2965
+ function assertVoucherDateWindow(data, service, today) {
2966
+ const products = data.concept === 1;
2967
+ let from = addDays(today, products ? -5 : -10);
2968
+ let to = addDays(today, products ? 5 : 10);
2969
+ if (products) {
2970
+ to = minDate(to, endOfMonth(today));
2971
+ }
2972
+ if (service === "wsfe" && FCE_INVOICE_TYPES.includes(data.voucherType)) {
2973
+ from = maxDate(from, addDays(today, -5));
2974
+ to = minDate(to, addDays(today, 1));
2975
+ }
2976
+ if (service === "wsfe" && FCE_NOTE_TYPES.includes(data.voucherType)) {
2977
+ from = maxDate(from, addDays(today, -5));
2978
+ }
2979
+ if (data.voucherDate < from || data.voucherDate > to) {
2980
+ invalid3(
2981
+ "date",
2982
+ `from ${toIsoDate(from)} through ${toIsoDate(to)}, the window ARCA accepts on ${toIsoDate(today)}`
2983
+ );
2984
+ }
2985
+ }
2986
+ function shiftDate(date, days, months = 0) {
2987
+ const utc = Date.UTC(
2988
+ Number(date.slice(0, 4)),
2989
+ Number(date.slice(4, 6)) - 1 + months,
2990
+ Number(date.slice(6, 8)) + days
2991
+ );
2992
+ return new Date(utc).toISOString().slice(0, 10).replaceAll("-", "");
2993
+ }
2994
+ function addDays(date, days) {
2995
+ return shiftDate(date, days);
2996
+ }
2997
+ function endOfMonth(date) {
2998
+ return shiftDate(date, -Number(date.slice(6, 8)), 1);
2999
+ }
3000
+ function minDate(a, b) {
3001
+ return a < b ? a : b;
3002
+ }
3003
+ function maxDate(a, b) {
3004
+ return a > b ? a : b;
3005
+ }
2962
3006
  function assertIssueObject(value, field) {
2963
3007
  if (value === null || typeof value !== "object" || Array.isArray(value)) {
2964
3008
  invalid3(field, "an object");
@@ -3536,6 +3580,7 @@ function createVouchersService(wsfe, context, wsmtxca) {
3536
3580
  };
3537
3581
  return {
3538
3582
  lookup: async (voucher, options) => lookupVoucher(select(options), voucher, options ?? {}),
3583
+ lastAuthorized: async (sequence, options) => lastAuthorizedNumber(select(options), sequence, options ?? {}),
3539
3584
  recover: async (key, options) => recoverOperation(
3540
3585
  select,
3541
3586
  key,
@@ -3603,7 +3648,18 @@ function previewInvoice(input, options) {
3603
3648
  11
3604
3649
  );
3605
3650
  }
3606
- return toPreview(prepareInvoice(input, options), options);
3651
+ return toPreview(
3652
+ withinDateWindow(prepareInvoice(input, options), options),
3653
+ options
3654
+ );
3655
+ }
3656
+ function withinDateWindow(prepared, options) {
3657
+ assertVoucherDateWindow(
3658
+ prepared.data,
3659
+ options.service ?? "wsfe",
3660
+ buenosAiresDate(/* @__PURE__ */ new Date())
3661
+ );
3662
+ return prepared;
3607
3663
  }
3608
3664
  function prepareInvoice(input, options) {
3609
3665
  const prepared = deriveWsfeInvoice(input);
@@ -3671,7 +3727,12 @@ async function issueInvoice(wsfe, input, inputOptions, context, select) {
3671
3727
  async function runOperation(wsfe, operation, input, prepare, options, context, replayAmounts, select) {
3672
3728
  const issuer = issuerTaxId(options, context);
3673
3729
  if (options.idempotencyKey === void 0 || !context?.store) {
3674
- return runAuthorization(wsfe, await prepare(), options, issuer);
3730
+ return runAuthorization(
3731
+ wsfe,
3732
+ withinDateWindow(await prepare(), options),
3733
+ options,
3734
+ issuer
3735
+ );
3675
3736
  }
3676
3737
  const store = context.store;
3677
3738
  const { environment, taxId } = context;
@@ -3689,7 +3750,7 @@ async function runOperation(wsfe, operation, input, prepare, options, context, r
3689
3750
  if (existing !== null) {
3690
3751
  return await replay(existing);
3691
3752
  }
3692
- const prepared = await prepare();
3753
+ const prepared = withinDateWindow(await prepare(), options);
3693
3754
  const sequence = {
3694
3755
  coordinates: {
3695
3756
  salesPoint: prepared.data.salesPoint,
@@ -4393,7 +4454,10 @@ async function previewNote(wsfe, input, inputOptions, context, kind) {
4393
4454
  ["representedTaxId", "service", "forceRefresh", "abortSignal"],
4394
4455
  "options"
4395
4456
  );
4396
- const prepared = await prepareNote(wsfe, input, options, context, kind);
4457
+ const prepared = withinDateWindow(
4458
+ await prepareNote(wsfe, input, options, context, kind),
4459
+ options
4460
+ );
4397
4461
  return {
4398
4462
  ...toPreview(prepared, options),
4399
4463
  ...prepared.originals === void 0 ? {} : { originals: prepared.originals }
@@ -4472,11 +4536,21 @@ function validateFceAssociations(note, targets, service) {
4472
4536
  );
4473
4537
  }
4474
4538
  }
4475
- var LOOKUP_BOUNDS = [
4539
+ var SEQUENCE_BOUNDS = [
4476
4540
  ["salesPoint", 99999],
4477
- ["voucherType", 999],
4478
- ["number", 99999999]
4541
+ ["voucherType", 999]
4479
4542
  ];
4543
+ var LOOKUP_BOUNDS = [...SEQUENCE_BOUNDS, ["number", 99999999]];
4544
+ function assertBounds(value, bounds, field, method) {
4545
+ for (const [key, max] of bounds) {
4546
+ if (!Number.isSafeInteger(value[key]) || value[key] < 1 || value[key] > max) {
4547
+ throw new ArcaInputError(
4548
+ `${method} requires ${field}.${key} to be an integer from 1 through ${max}.`,
4549
+ { code: "ARCA_INPUT_INVALID_VALUE", field: `${field}.${key}` }
4550
+ );
4551
+ }
4552
+ }
4553
+ }
4480
4554
  async function lookupVoucher(wsfe, voucher, inputOptions) {
4481
4555
  const options = cloneOptions(inputOptions);
4482
4556
  assertIssueKeys(
@@ -4492,15 +4566,7 @@ async function lookupVoucher(wsfe, voucher, inputOptions) {
4492
4566
  "voucher",
4493
4567
  "lookup()"
4494
4568
  );
4495
- for (const [field, max] of LOOKUP_BOUNDS) {
4496
- const value = voucher[field];
4497
- if (!Number.isSafeInteger(value) || value < 1 || value > max) {
4498
- throw new ArcaInputError(
4499
- `lookup() requires voucher.${field} to be an integer from 1 through ${max}.`,
4500
- { code: "ARCA_INPUT_INVALID_VALUE", field: `voucher.${field}` }
4501
- );
4502
- }
4503
- }
4569
+ assertBounds(voucher, LOOKUP_BOUNDS, "voucher", "lookup()");
4504
4570
  const found = await wsfe.lookupVoucher({
4505
4571
  representedTaxId: options.representedTaxId,
4506
4572
  forceRefresh: options.forceRefresh,
@@ -4520,6 +4586,31 @@ async function lookupVoucher(wsfe, voucher, inputOptions) {
4520
4586
  }
4521
4587
  return toVoucherSummary(found.voucher);
4522
4588
  }
4589
+ async function lastAuthorizedNumber(wsfe, sequence, inputOptions) {
4590
+ const options = cloneOptions(inputOptions);
4591
+ assertIssueKeys(
4592
+ options,
4593
+ ["representedTaxId", "service", "forceRefresh", "abortSignal"],
4594
+ "options",
4595
+ "lastAuthorized()"
4596
+ );
4597
+ assertIssueObject(sequence, "sequence");
4598
+ assertIssueKeys(
4599
+ sequence,
4600
+ ["salesPoint", "voucherType"],
4601
+ "sequence",
4602
+ "lastAuthorized()"
4603
+ );
4604
+ assertBounds(sequence, SEQUENCE_BOUNDS, "sequence", "lastAuthorized()");
4605
+ const next = await wsfe.getNextVoucherNumber({
4606
+ representedTaxId: options.representedTaxId,
4607
+ forceRefresh: options.forceRefresh,
4608
+ ...options.abortSignal === void 0 ? {} : { abortSignal: options.abortSignal },
4609
+ salesPoint: sequence.salesPoint,
4610
+ voucherType: sequence.voucherType
4611
+ });
4612
+ return next - 1;
4613
+ }
4523
4614
  async function lookupOriginal(wsfe, target, options) {
4524
4615
  const original = await wsfe.lookupVoucher({
4525
4616
  representedTaxId: options.representedTaxId,
@@ -5077,6 +5168,7 @@ function createArcaClient(config = {}) {
5077
5168
  previewCreditNote: vouchers.previewCreditNote,
5078
5169
  previewDebitNote: vouchers.previewDebitNote,
5079
5170
  lookup: vouchers.lookup,
5171
+ lastAuthorized: vouchers.lastAuthorized,
5080
5172
  wsfe,
5081
5173
  wsmtxca,
5082
5174
  padron: createPadronService({ config: normalizedConfig, auth, soap })
@@ -5366,4 +5458,4 @@ export {
5366
5458
  withLease,
5367
5459
  createFileStore
5368
5460
  };
5369
- //# sourceMappingURL=chunk-JP5IBMVI.mjs.map
5461
+ //# sourceMappingURL=chunk-U3HODOOP.mjs.map