facturas 0.6.0 → 0.7.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.
@@ -0,0 +1,702 @@
1
+ import {
2
+ classifyArcaAuthenticationError,
3
+ classifyArcaAuthenticationIssues,
4
+ createArcaAuthenticationErrorFromEvidence,
5
+ createArcaAuthenticationEvidence,
6
+ executeWithAuthenticationRecovery
7
+ } from "./chunk-IOKZX6CA.mjs";
8
+ import {
9
+ ArcaInputError,
10
+ ArcaInvalidSoapResponseError,
11
+ ArcaServiceError,
12
+ ArcaSoapFaultError,
13
+ ArcaTransportError
14
+ } from "./chunk-MBWOFO67.mjs";
15
+
16
+ // src/services/wsmtxca.ts
17
+ function createWsmtxcaService(options) {
18
+ async function executeWsmtxcaAuthenticatedOperation(operation, input, body = {}, retries) {
19
+ const auth = await options.auth.login("wsmtxca", {
20
+ representedTaxId: input.representedTaxId,
21
+ forceRefresh: input.forceRefresh
22
+ });
23
+ const response = await options.soap.execute({
24
+ service: "wsmtxca",
25
+ operation,
26
+ ...retries === void 0 ? {} : { retries },
27
+ bodyElementName: `${operation}Request`,
28
+ bodyElementNamespaceMode: "prefix",
29
+ body: {
30
+ ...body,
31
+ authRequest: createWsmtxcaAuth(
32
+ input.representedTaxId ?? options.config.taxId,
33
+ auth.token,
34
+ auth.sign
35
+ )
36
+ }
37
+ });
38
+ return unwrapWsmtxcaOperationResponse(response.result, operation);
39
+ }
40
+ async function executeWsmtxcaAuthorization({
41
+ representedTaxId,
42
+ data,
43
+ forceRefresh
44
+ }) {
45
+ if (Object.hasOwn(data, "authRequest")) {
46
+ throw new ArcaInputError(
47
+ 'WSMTXCA authorization data cannot include the reserved top-level field "authRequest".',
48
+ {
49
+ code: "ARCA_INPUT_RESERVED_FIELD",
50
+ field: "data.authRequest",
51
+ expected: "omitted because facturas manages authentication fields"
52
+ }
53
+ );
54
+ }
55
+ try {
56
+ const raw = await executeWsmtxcaAuthenticatedOperation(
57
+ "autorizarComprobante",
58
+ { representedTaxId, forceRefresh },
59
+ data,
60
+ 0
61
+ );
62
+ return { outcome: classifyWsmtxcaAuthorization(raw) };
63
+ } catch (error) {
64
+ return {
65
+ outcome: createWsmtxcaIndeterminateOutcome(error),
66
+ error
67
+ };
68
+ }
69
+ }
70
+ async function authorizeVoucherOutcome(input) {
71
+ return (await executeWsmtxcaAuthorization(input)).outcome;
72
+ }
73
+ function authorizeVoucher(input) {
74
+ return executeWithAuthenticationRecovery({
75
+ service: "wsmtxca",
76
+ operation: "autorizarComprobante",
77
+ forceRefresh: input.forceRefresh,
78
+ execute: (forceRefresh) => authorizeVoucherOnce({ ...input, forceRefresh })
79
+ });
80
+ }
81
+ async function authorizeVoucherOnce(input) {
82
+ const execution = await executeWsmtxcaAuthorization(input);
83
+ if (execution.error) {
84
+ throw execution.error;
85
+ }
86
+ if (execution.outcome.kind !== "authorized") {
87
+ throw createWsmtxcaOutcomeError(execution.outcome);
88
+ }
89
+ const { outcome } = execution;
90
+ return {
91
+ cae: outcome.cae,
92
+ ...outcome.caeExpiry === void 0 ? {} : { caeExpiry: outcome.caeExpiry },
93
+ voucherNumber: outcome.voucherNumber,
94
+ messages: formatWsmtxcaIssues([
95
+ ...outcome.errors,
96
+ ...outcome.observations
97
+ ]),
98
+ raw: outcome.raw ?? {}
99
+ };
100
+ }
101
+ function getLastAuthorizedVoucher({
102
+ representedTaxId,
103
+ voucherType,
104
+ salesPoint,
105
+ forceRefresh
106
+ }) {
107
+ return executeWithAuthenticationRecovery({
108
+ service: "wsmtxca",
109
+ operation: "consultarUltimoComprobanteAutorizado",
110
+ forceRefresh,
111
+ execute: (attemptForceRefresh) => getLastAuthorizedVoucherOnce({
112
+ representedTaxId,
113
+ voucherType,
114
+ salesPoint,
115
+ forceRefresh: attemptForceRefresh
116
+ })
117
+ });
118
+ }
119
+ async function getLastAuthorizedVoucherOnce({
120
+ representedTaxId,
121
+ voucherType,
122
+ salesPoint,
123
+ forceRefresh
124
+ }) {
125
+ const operation = "consultarUltimoComprobanteAutorizado";
126
+ const raw = await executeWsmtxcaAuthenticatedOperation(
127
+ operation,
128
+ { representedTaxId, forceRefresh },
129
+ {
130
+ consultaUltimoComprobanteAutorizadoRequest: {
131
+ codigoTipoComprobante: voucherType,
132
+ numeroPuntoVenta: salesPoint
133
+ }
134
+ }
135
+ );
136
+ const errors = extractWsmtxcaIssues(raw, operation, "error");
137
+ if (errors.length > 0 && errors.every((issue) => issue.code === "1502")) {
138
+ return { voucherNumber: 0, raw };
139
+ }
140
+ if (errors.length > 0) {
141
+ throw createWsmtxcaServiceError(operation, errors);
142
+ }
143
+ return {
144
+ voucherNumber: parseWsmtxcaVoucherNumber(
145
+ raw.numeroComprobante ?? raw.cbteNro ?? raw.nroComprobante,
146
+ "WSMTXCA did not return the last authorized voucher number",
147
+ true
148
+ ),
149
+ raw
150
+ };
151
+ }
152
+ function getSalesPoints({
153
+ representedTaxId,
154
+ forceRefresh
155
+ }) {
156
+ return executeWithAuthenticationRecovery({
157
+ service: "wsmtxca",
158
+ operation: "consultarPuntosVenta",
159
+ forceRefresh,
160
+ execute: (attemptForceRefresh) => getSalesPointsOnce({
161
+ representedTaxId,
162
+ forceRefresh: attemptForceRefresh
163
+ })
164
+ });
165
+ }
166
+ async function getSalesPointsOnce({
167
+ representedTaxId,
168
+ forceRefresh
169
+ }) {
170
+ const operation = "consultarPuntosVenta";
171
+ const raw = await executeWsmtxcaAuthenticatedOperation(operation, {
172
+ representedTaxId,
173
+ forceRefresh
174
+ });
175
+ throwForWsmtxcaOperationErrors(operation, raw);
176
+ const rawSalesPoints = toRecord(raw.arrayPuntosVenta)?.puntoVenta;
177
+ const entries = Array.isArray(rawSalesPoints) ? rawSalesPoints : rawSalesPoints ? [rawSalesPoints] : [];
178
+ const salesPoints = entries.flatMap((entry) => {
179
+ const record = toRecord(entry);
180
+ const number = parseOptionalPositiveInteger(record?.numeroPuntoVenta);
181
+ if (number === void 0) {
182
+ return [];
183
+ }
184
+ const deletedAt = normalizeWsmtxcaResponseDate(record?.fechaBaja);
185
+ return [
186
+ {
187
+ number,
188
+ blocked: String(record?.bloqueado ?? "N").toUpperCase() === "S",
189
+ ...deletedAt === void 0 ? {} : { deletedAt }
190
+ }
191
+ ];
192
+ });
193
+ return { salesPoints, raw };
194
+ }
195
+ function lookupVoucher({
196
+ representedTaxId,
197
+ voucherType,
198
+ salesPoint,
199
+ voucherNumber,
200
+ forceRefresh
201
+ }) {
202
+ return executeWithAuthenticationRecovery({
203
+ service: "wsmtxca",
204
+ operation: "consultarComprobante",
205
+ forceRefresh,
206
+ execute: (attemptForceRefresh) => lookupVoucherOnce({
207
+ representedTaxId,
208
+ voucherType,
209
+ salesPoint,
210
+ voucherNumber,
211
+ forceRefresh: attemptForceRefresh
212
+ })
213
+ });
214
+ }
215
+ async function lookupVoucherOnce({
216
+ representedTaxId,
217
+ voucherType,
218
+ salesPoint,
219
+ voucherNumber,
220
+ forceRefresh
221
+ }) {
222
+ const operation = "consultarComprobante";
223
+ const raw = await executeWsmtxcaAuthenticatedOperation(
224
+ operation,
225
+ { representedTaxId, forceRefresh },
226
+ {
227
+ consultaComprobanteRequest: {
228
+ codigoTipoComprobante: voucherType,
229
+ numeroPuntoVenta: salesPoint,
230
+ numeroComprobante: voucherNumber
231
+ }
232
+ }
233
+ );
234
+ const errors = extractWsmtxcaIssues(raw, operation, "error");
235
+ const observations = extractWsmtxcaIssues(raw, operation, "observation");
236
+ if (errors.length > 0 && errors.every((issue) => issue.code === "1503")) {
237
+ return {
238
+ kind: "not_found",
239
+ service: "wsmtxca",
240
+ operation,
241
+ errors,
242
+ observations,
243
+ raw
244
+ };
245
+ }
246
+ if (errors.length > 0) {
247
+ throw createWsmtxcaServiceError(operation, errors);
248
+ }
249
+ const voucher = extractWsmtxcaVoucherPayload(raw);
250
+ if (voucher === raw && !toRecord(raw.comprobante)) {
251
+ throw new ArcaServiceError(
252
+ "WSMTXCA did not return the voucher issue date",
253
+ {
254
+ service: "wsmtxca",
255
+ operation,
256
+ issues: observations
257
+ }
258
+ );
259
+ }
260
+ return {
261
+ kind: "found",
262
+ service: "wsmtxca",
263
+ operation,
264
+ voucher: mapWsmtxcaVoucherInfo(voucher),
265
+ observations,
266
+ raw
267
+ };
268
+ }
269
+ async function getVoucher(input) {
270
+ const lookup = await lookupVoucher(input);
271
+ if (lookup.kind === "not_found") {
272
+ throw createWsmtxcaServiceError(lookup.operation, lookup.errors);
273
+ }
274
+ const invoiceDate = lookup.voucher.invoiceDate;
275
+ if (!invoiceDate) {
276
+ throw new ArcaServiceError(
277
+ formatWsmtxcaIssues(lookup.observations)[0] ?? "WSMTXCA did not return the voucher issue date",
278
+ {
279
+ service: "wsmtxca",
280
+ operation: lookup.operation,
281
+ issues: lookup.observations
282
+ }
283
+ );
284
+ }
285
+ return {
286
+ invoiceDate,
287
+ voucher: lookup.voucher.raw,
288
+ messages: formatWsmtxcaIssues(lookup.observations),
289
+ raw: lookup.raw
290
+ };
291
+ }
292
+ return {
293
+ authorizeVoucherOutcome,
294
+ authorizeVoucher,
295
+ getLastAuthorizedVoucher,
296
+ getSalesPoints,
297
+ lookupVoucher,
298
+ getVoucher
299
+ };
300
+ }
301
+ function createWsmtxcaAuth(representedTaxId, token, sign) {
302
+ return {
303
+ token,
304
+ sign,
305
+ cuitRepresentada: Number.parseInt(String(representedTaxId), 10)
306
+ };
307
+ }
308
+ function toRecord(value) {
309
+ return value && typeof value === "object" && !Array.isArray(value) ? value : void 0;
310
+ }
311
+ function unwrapWsmtxcaOperationResponse(response, operation) {
312
+ const responseRecord = toRecord(response) ?? {};
313
+ if (operation === "autorizarComprobante") {
314
+ return toRecord(responseRecord.autorizarComprobanteResponse) ?? toRecord(responseRecord.autorizarComprobanteResult) ?? toRecord(responseRecord.comprobanteCAEResponse) ?? toRecord(responseRecord.comprobanteCAEReponse) ?? responseRecord;
315
+ }
316
+ if (operation === "consultarComprobante") {
317
+ return toRecord(responseRecord.consultarComprobanteResponse) ?? toRecord(responseRecord.consultaComprobanteResponse) ?? toRecord(responseRecord.consultarComprobanteResult) ?? responseRecord;
318
+ }
319
+ if (operation === "consultarPuntosVenta") {
320
+ return toRecord(responseRecord.consultarPuntosVentaResponse) ?? toRecord(responseRecord.consultarPuntosVentaResult) ?? responseRecord;
321
+ }
322
+ return toRecord(responseRecord.consultarUltimoComprobanteAutorizadoResponse) ?? toRecord(responseRecord.consultaUltimoComprobanteAutorizadoResponse) ?? toRecord(responseRecord.consultarUltimoComprobanteAutorizadoResult) ?? responseRecord;
323
+ }
324
+ function extractWsmtxcaAuthorizationPayload(raw) {
325
+ return toRecord(raw.comprobanteResponse) ?? toRecord(raw.comprobanteCAEResponse) ?? toRecord(raw.comprobanteCAEReponse) ?? raw;
326
+ }
327
+ function extractWsmtxcaVoucherPayload(raw) {
328
+ return toRecord(raw.comprobanteResponse) ?? toRecord(raw.comprobante) ?? toRecord(raw.cmp) ?? raw;
329
+ }
330
+ function classifyWsmtxcaAuthorization(raw) {
331
+ const operation = "autorizarComprobante";
332
+ const payload = extractWsmtxcaAuthorizationPayload(raw);
333
+ const result = normalizeWsmtxcaResult(raw.resultado ?? payload.resultado);
334
+ const cae = normalizeWsmtxcaString(
335
+ payload.CAE ?? payload.codigoAutorizacion ?? raw.codigoAutorizacion
336
+ );
337
+ const caeExpiry = normalizeWsmtxcaResponseDate(
338
+ payload.fechaVencimientoCAE ?? payload.fechaVencimiento ?? raw.fechaVencimiento
339
+ );
340
+ const voucherNumber = parseOptionalPositiveInteger(
341
+ payload.numeroComprobante ?? raw.numeroComprobante
342
+ );
343
+ const errors = extractWsmtxcaIssues(raw, operation, "error");
344
+ const observations = extractWsmtxcaIssues(raw, operation, "observation");
345
+ const base = {
346
+ service: "wsmtxca",
347
+ operation,
348
+ results: createWsmtxcaResults(result),
349
+ errors,
350
+ observations,
351
+ raw
352
+ };
353
+ const authenticationOutcome = createWsmtxcaAuthenticationOutcome({
354
+ base,
355
+ result,
356
+ cae,
357
+ voucherNumber
358
+ });
359
+ if (authenticationOutcome) {
360
+ return authenticationOutcome;
361
+ }
362
+ if ((result === "A" || result === "O") && cae && voucherNumber !== void 0 && errors.length === 0) {
363
+ return {
364
+ ...base,
365
+ kind: "authorized",
366
+ result,
367
+ resultLevel: "operation",
368
+ cae,
369
+ ...caeExpiry === void 0 ? {} : { caeExpiry },
370
+ voucherNumber
371
+ };
372
+ }
373
+ if (result === "R" && !cae && errors.length > 0) {
374
+ return {
375
+ ...base,
376
+ kind: "rejected",
377
+ result: "R",
378
+ resultLevel: "operation"
379
+ };
380
+ }
381
+ const outcome = {
382
+ ...base,
383
+ kind: "indeterminate",
384
+ reason: result === "R" && Boolean(cae) || (result === "A" || result === "O") && Boolean(errors.length) ? "contradictory_response" : "incomplete_response",
385
+ ...result === void 0 ? {} : { result },
386
+ ...result === void 0 ? {} : { resultLevel: "operation" }
387
+ };
388
+ assignWsmtxcaValue(outcome, "cae", cae);
389
+ assignWsmtxcaValue(outcome, "caeExpiry", caeExpiry);
390
+ assignWsmtxcaValue(outcome, "voucherNumber", voucherNumber);
391
+ return outcome;
392
+ }
393
+ function createWsmtxcaAuthenticationOutcome({
394
+ base,
395
+ result,
396
+ cae,
397
+ voucherNumber
398
+ }) {
399
+ const authenticationError = classifyArcaAuthenticationIssues(base.errors, {
400
+ service: base.service,
401
+ operation: base.operation
402
+ });
403
+ if (!authenticationError || result === "A" || result === "O" || cae || voucherNumber !== void 0) {
404
+ return void 0;
405
+ }
406
+ return {
407
+ ...base,
408
+ kind: "indeterminate",
409
+ reason: "authentication_rejected",
410
+ authentication: createArcaAuthenticationEvidence(authenticationError),
411
+ ...result === void 0 ? {} : { result },
412
+ ...result === void 0 ? {} : { resultLevel: "operation" }
413
+ };
414
+ }
415
+ function createWsmtxcaIndeterminateOutcome(error) {
416
+ const authenticationError = classifyArcaAuthenticationError(error, {
417
+ service: "wsmtxca",
418
+ operation: "autorizarComprobante"
419
+ });
420
+ return {
421
+ kind: "indeterminate",
422
+ service: "wsmtxca",
423
+ operation: "autorizarComprobante",
424
+ results: {},
425
+ reason: authenticationError ? "authentication_rejected" : getWsmtxcaIndeterminateReason(error),
426
+ ...authenticationError ? {
427
+ authentication: createArcaAuthenticationEvidence(authenticationError)
428
+ } : {},
429
+ errors: [],
430
+ observations: []
431
+ };
432
+ }
433
+ function getWsmtxcaIndeterminateReason(error) {
434
+ if (error instanceof ArcaTransportError) {
435
+ return "transport_error";
436
+ }
437
+ if (error instanceof ArcaSoapFaultError) {
438
+ return "soap_fault";
439
+ }
440
+ if (error instanceof ArcaInvalidSoapResponseError) {
441
+ return "invalid_response";
442
+ }
443
+ return "unexpected_error";
444
+ }
445
+ function createWsmtxcaOutcomeError(outcome) {
446
+ if (outcome.kind === "indeterminate" && outcome.authentication) {
447
+ return createArcaAuthenticationErrorFromEvidence(outcome.authentication, {
448
+ service: "wsmtxca",
449
+ operation: outcome.operation
450
+ });
451
+ }
452
+ const issues = [...outcome.errors, ...outcome.observations];
453
+ const messages = formatWsmtxcaIssues(issues);
454
+ const firstIssue = issues[0];
455
+ return new ArcaServiceError(
456
+ messages.join(" | ") || (outcome.kind === "rejected" ? "WSMTXCA rejected the voucher authorization" : "WSMTXCA did not return conclusive voucher authorization data"),
457
+ {
458
+ service: "wsmtxca",
459
+ operation: outcome.operation,
460
+ ...firstIssue?.code === void 0 ? {} : { serviceCode: firstIssue.code },
461
+ ...outcome.result === void 0 ? {} : { result: outcome.result },
462
+ ...outcome.resultLevel === void 0 ? {} : { resultLevel: outcome.resultLevel },
463
+ results: outcome.results,
464
+ ...outcome.kind === "indeterminate" && outcome.cae ? { cae: outcome.cae } : {},
465
+ issues
466
+ }
467
+ );
468
+ }
469
+ function createWsmtxcaResults(operationResult) {
470
+ const results = {};
471
+ assignWsmtxcaValue(results, "operation", operationResult);
472
+ return results;
473
+ }
474
+ function createWsmtxcaServiceError(operation, issues) {
475
+ const authenticationError = classifyArcaAuthenticationIssues(issues, {
476
+ service: "wsmtxca",
477
+ operation
478
+ });
479
+ if (authenticationError) {
480
+ return authenticationError;
481
+ }
482
+ const firstIssue = issues[0];
483
+ return new ArcaServiceError(
484
+ formatWsmtxcaIssues(issues).join(" | ") || "WSMTXCA returned a service error",
485
+ {
486
+ service: "wsmtxca",
487
+ operation,
488
+ ...firstIssue?.code === void 0 ? {} : { serviceCode: firstIssue.code },
489
+ issues
490
+ }
491
+ );
492
+ }
493
+ function throwForWsmtxcaOperationErrors(operation, raw) {
494
+ const errors = extractWsmtxcaIssues(raw, operation, "error");
495
+ if (errors.length > 0) {
496
+ throw createWsmtxcaServiceError(operation, errors);
497
+ }
498
+ }
499
+ function extractWsmtxcaIssues(raw, operation, source) {
500
+ const container = toRecord(
501
+ source === "error" ? raw.arrayErrores : raw.arrayObservaciones
502
+ );
503
+ return normalizeWsmtxcaIssueEntries(container?.codigoDescripcion).map(
504
+ (entry) => ({
505
+ service: "wsmtxca",
506
+ operation,
507
+ source,
508
+ category: source === "observation" ? "observation" : operation === "autorizarComprobante" ? "business" : "unknown",
509
+ ...entry.code === void 0 ? {} : { code: entry.code },
510
+ message: entry.message,
511
+ ...operation === "autorizarComprobante" ? { resultLevel: "operation" } : {}
512
+ })
513
+ );
514
+ }
515
+ function normalizeWsmtxcaIssueEntries(value) {
516
+ const entries = Array.isArray(value) ? value : value ? [value] : [];
517
+ return entries.map((entry) => {
518
+ const record = toRecord(entry) ?? {};
519
+ const code = record.codigo;
520
+ const description = record.descripcion;
521
+ return {
522
+ ...code === void 0 || code === null ? {} : { code: String(code) },
523
+ message: description === void 0 || description === null ? "Unknown WSMTXCA issue" : String(description)
524
+ };
525
+ });
526
+ }
527
+ function formatWsmtxcaIssues(issues) {
528
+ return issues.map((issue) => {
529
+ const prefix = issue.source === "error" ? "Error" : "Obs";
530
+ return `${prefix}${issue.code ? ` ${issue.code}` : ""}: ${issue.message}`;
531
+ });
532
+ }
533
+ function mapWsmtxcaVoucherInfo(raw) {
534
+ const voucher = { raw };
535
+ const invoiceDate = normalizeWsmtxcaResponseDate(
536
+ raw.fechaEmision ?? raw.fecha ?? raw.CbteFch
537
+ );
538
+ const cae = normalizeWsmtxcaString(raw.codigoAutorizacion ?? raw.CAE);
539
+ const caeExpiry = normalizeWsmtxcaResponseDate(
540
+ raw.fechaVencimiento ?? raw.fechaVencimientoCAE
541
+ );
542
+ const vatAmount = sumWsmtxcaVatAmounts(raw.arraySubtotalesIVA);
543
+ assignWsmtxcaValue(
544
+ voucher,
545
+ "voucherNumber",
546
+ parseOptionalPositiveInteger(raw.numeroComprobante)
547
+ );
548
+ assignWsmtxcaValue(voucher, "invoiceDate", invoiceDate);
549
+ assignWsmtxcaValue(
550
+ voucher,
551
+ "salesPoint",
552
+ parseOptionalPositiveInteger(raw.numeroPuntoVenta)
553
+ );
554
+ assignWsmtxcaValue(
555
+ voucher,
556
+ "voucherType",
557
+ parseOptionalPositiveInteger(raw.codigoTipoComprobante)
558
+ );
559
+ assignWsmtxcaValue(
560
+ voucher,
561
+ "concept",
562
+ parseOptionalNumber(raw.codigoConcepto)
563
+ );
564
+ assignWsmtxcaValue(
565
+ voucher,
566
+ "documentType",
567
+ parseOptionalNumber(raw.codigoTipoDocumento)
568
+ );
569
+ assignWsmtxcaValue(
570
+ voucher,
571
+ "documentNumber",
572
+ normalizeWsmtxcaString(raw.numeroDocumento)
573
+ );
574
+ assignWsmtxcaValue(
575
+ voucher,
576
+ "receiverVatConditionId",
577
+ parseOptionalNumber(raw.condicionIVAReceptor)
578
+ );
579
+ assignWsmtxcaValue(
580
+ voucher,
581
+ "totalAmount",
582
+ parseOptionalNumber(raw.importeTotal)
583
+ );
584
+ assignWsmtxcaValue(
585
+ voucher,
586
+ "subtotalAmount",
587
+ parseOptionalNumber(raw.importeSubtotal)
588
+ );
589
+ assignWsmtxcaValue(
590
+ voucher,
591
+ "taxableAmount",
592
+ parseOptionalNumber(raw.importeGravado)
593
+ );
594
+ assignWsmtxcaValue(
595
+ voucher,
596
+ "nonTaxableAmount",
597
+ parseOptionalNumber(raw.importeNoGravado)
598
+ );
599
+ assignWsmtxcaValue(
600
+ voucher,
601
+ "exemptAmount",
602
+ parseOptionalNumber(raw.importeExento)
603
+ );
604
+ assignWsmtxcaValue(
605
+ voucher,
606
+ "taxAmount",
607
+ parseOptionalNumber(raw.importeOtrosTributos)
608
+ );
609
+ assignWsmtxcaValue(voucher, "vatAmount", vatAmount);
610
+ assignWsmtxcaValue(
611
+ voucher,
612
+ "currencyId",
613
+ normalizeWsmtxcaString(raw.codigoMoneda)
614
+ );
615
+ assignWsmtxcaValue(
616
+ voucher,
617
+ "exchangeRate",
618
+ parseOptionalNumber(raw.cotizacionMoneda)
619
+ );
620
+ assignWsmtxcaValue(voucher, "cae", cae);
621
+ assignWsmtxcaValue(voucher, "caeExpiry", caeExpiry);
622
+ return voucher;
623
+ }
624
+ function sumWsmtxcaVatAmounts(value) {
625
+ const subtotals = toRecord(value)?.subtotalIVA;
626
+ const entries = Array.isArray(subtotals) ? subtotals : subtotals ? [subtotals] : [];
627
+ const amounts = entries.map((entry) => parseOptionalNumber(toRecord(entry)?.importe)).filter((amount) => amount !== void 0);
628
+ return amounts.length > 0 ? amounts.reduce((total, amount) => total + amount, 0) : void 0;
629
+ }
630
+ function parseWsmtxcaVoucherNumber(value, message, allowZero = false) {
631
+ const parsed = Number.parseInt(String(value ?? ""), 10);
632
+ if (!Number.isFinite(parsed) || (allowZero ? parsed < 0 : parsed <= 0)) {
633
+ throw new ArcaServiceError(message, {
634
+ service: "wsmtxca"
635
+ });
636
+ }
637
+ return parsed;
638
+ }
639
+ function parseOptionalPositiveInteger(value) {
640
+ const parsed = Number.parseInt(String(value ?? ""), 10);
641
+ return Number.isFinite(parsed) && parsed > 0 ? parsed : void 0;
642
+ }
643
+ function parseOptionalNumber(value) {
644
+ if (value === void 0 || value === null || value === "") {
645
+ return void 0;
646
+ }
647
+ const parsed = Number(value);
648
+ return Number.isFinite(parsed) ? parsed : void 0;
649
+ }
650
+ function normalizeWsmtxcaResult(value) {
651
+ if (typeof value !== "string") {
652
+ return void 0;
653
+ }
654
+ const normalized = value.trim().toUpperCase();
655
+ return normalized || void 0;
656
+ }
657
+ function normalizeWsmtxcaString(value) {
658
+ if (value === void 0 || value === null) {
659
+ return void 0;
660
+ }
661
+ const normalized = String(value).trim();
662
+ return normalized || void 0;
663
+ }
664
+ function assignWsmtxcaValue(target, key, value) {
665
+ if (value !== void 0) {
666
+ target[key] = value;
667
+ }
668
+ }
669
+ function normalizeWsmtxcaResponseDate(value) {
670
+ if (typeof value === "number" && Number.isInteger(value)) {
671
+ return formatCompactDateToIso(value);
672
+ }
673
+ if (typeof value !== "string") {
674
+ return void 0;
675
+ }
676
+ const trimmed = value.trim();
677
+ if (!trimmed) {
678
+ return void 0;
679
+ }
680
+ if (/^\d{8}$/.test(trimmed)) {
681
+ return formatCompactDateToIso(Number.parseInt(trimmed, 10));
682
+ }
683
+ if (/^\d{4}-\d{2}-\d{2}/.test(trimmed)) {
684
+ return trimmed.slice(0, 10);
685
+ }
686
+ return void 0;
687
+ }
688
+ function formatCompactDateToIso(dateValue) {
689
+ if (!dateValue) {
690
+ return void 0;
691
+ }
692
+ const raw = String(dateValue);
693
+ if (raw.length !== 8) {
694
+ return void 0;
695
+ }
696
+ return `${raw.slice(0, 4)}-${raw.slice(4, 6)}-${raw.slice(6, 8)}`;
697
+ }
698
+
699
+ export {
700
+ createWsmtxcaService
701
+ };
702
+ //# sourceMappingURL=chunk-PKE4Z4GE.mjs.map