@devlas/dte-sii 2.5.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.
Files changed (60) hide show
  1. package/BoletaService.js +109 -0
  2. package/CAF.js +173 -0
  3. package/CafSolicitor.js +380 -0
  4. package/Certificado.js +123 -0
  5. package/ConsumoFolio.js +376 -0
  6. package/DTE.js +399 -0
  7. package/EnviadorSII.js +1304 -0
  8. package/Envio.js +196 -0
  9. package/FolioRegistry.js +553 -0
  10. package/FolioService.js +703 -0
  11. package/LICENSE +27 -0
  12. package/LibroBase.js +134 -0
  13. package/LibroCompraVenta.js +205 -0
  14. package/LibroGuia.js +225 -0
  15. package/README.md +239 -0
  16. package/Signer.js +94 -0
  17. package/SiiCertificacion.js +1189 -0
  18. package/SiiPortalAuth.js +460 -0
  19. package/SiiSession.js +499 -0
  20. package/cert/BoletaCert.js +731 -0
  21. package/cert/CertFolioHelper.js +185 -0
  22. package/cert/CertRunner.js +2658 -0
  23. package/cert/ConfigLoader.js +133 -0
  24. package/cert/IntercambioCert.js +429 -0
  25. package/cert/LibroCompras.js +359 -0
  26. package/cert/LibroGuias.js +171 -0
  27. package/cert/LibroVentas.js +153 -0
  28. package/cert/MuestrasImpresas.js +676 -0
  29. package/cert/SetBase.js +321 -0
  30. package/cert/SetBasico.js +413 -0
  31. package/cert/SetCompra.js +472 -0
  32. package/cert/SetExenta.js +490 -0
  33. package/cert/SetGuia.js +283 -0
  34. package/cert/SetParser.js +1184 -0
  35. package/cert/SetsProvider.js +499 -0
  36. package/cert/Simulacion.js +521 -0
  37. package/cert/comunaOficina.js +460 -0
  38. package/cert/index.js +124 -0
  39. package/cert/types.js +330 -0
  40. package/dte-sii.d.ts +458 -0
  41. package/index.js +428 -0
  42. package/package.json +48 -0
  43. package/utils/c14n.js +275 -0
  44. package/utils/calculo.js +396 -0
  45. package/utils/config.js +276 -0
  46. package/utils/constants.js +302 -0
  47. package/utils/emisor.js +174 -0
  48. package/utils/endpoints.js +225 -0
  49. package/utils/error.js +235 -0
  50. package/utils/index.js +339 -0
  51. package/utils/logger.js +239 -0
  52. package/utils/pfx.js +203 -0
  53. package/utils/receptor.js +218 -0
  54. package/utils/referencia.js +169 -0
  55. package/utils/resolucion.js +119 -0
  56. package/utils/rut.js +169 -0
  57. package/utils/sanitize.js +124 -0
  58. package/utils/tokenCache.js +214 -0
  59. package/utils/xml.js +358 -0
  60. package/utils.js +4 -0
package/dte-sii.d.ts ADDED
@@ -0,0 +1,458 @@
1
+ /**
2
+ * @devlas/dte-sii - Type Definitions
3
+ *
4
+ * TypeScript declarations for @devlas/dte-sii
5
+ *
6
+ * @version 2.3.0
7
+ */
8
+
9
+ // ============================================
10
+ // COMMON TYPES
11
+ // ============================================
12
+
13
+ export interface Resolucion {
14
+ fch_resol: string;
15
+ nro_resol: number;
16
+ }
17
+
18
+ export interface RutParts {
19
+ numero: string;
20
+ dv: string;
21
+ }
22
+
23
+ export interface ValidationResult {
24
+ valid: boolean;
25
+ errors?: string[];
26
+ error?: string;
27
+ }
28
+
29
+ export interface RutValidation {
30
+ valid: boolean;
31
+ rut: string | null;
32
+ error?: string;
33
+ }
34
+
35
+ // ============================================
36
+ // DTE TYPES
37
+ // ============================================
38
+
39
+ export interface Emisor {
40
+ RUTEmisor: string;
41
+ RznSoc?: string;
42
+ RznSocEmisor?: string;
43
+ GiroEmis?: string;
44
+ GiroEmisor?: string;
45
+ Acteco?: number | string;
46
+ Telefono?: string;
47
+ CorreoEmisor?: string;
48
+ DirOrigen: string;
49
+ CmnaOrigen: string;
50
+ CiudadOrigen?: string;
51
+ }
52
+
53
+ export interface Receptor {
54
+ RUTRecep: string;
55
+ RznSocRecep: string;
56
+ GiroRecep?: string;
57
+ DirRecep: string;
58
+ CmnaRecep: string;
59
+ CiudadRecep?: string;
60
+ CorreoRecep?: string;
61
+ }
62
+
63
+ export interface DetalleItem {
64
+ NroLinDet: number;
65
+ IndExe?: number;
66
+ NmbItem: string;
67
+ QtyItem?: number;
68
+ UnmdItem?: string;
69
+ PrcItem?: number | string;
70
+ DescuentoPct?: number;
71
+ DescuentoMonto?: number;
72
+ CodImpAdic?: number;
73
+ MontoItem: number;
74
+ }
75
+
76
+ export interface Referencia {
77
+ NroLinRef: number;
78
+ TpoDocRef: string | number;
79
+ FolioRef: number;
80
+ FchRef: string;
81
+ CodRef?: number;
82
+ RazonRef?: string;
83
+ }
84
+
85
+ export interface Totales {
86
+ MntNeto?: number;
87
+ MntExe?: number;
88
+ TasaIVA?: number;
89
+ IVA?: number;
90
+ ImptoReten?: ImpuestoRetencion[];
91
+ MntTotal: number;
92
+ }
93
+
94
+ export interface ImpuestoRetencion {
95
+ TipoImp: number;
96
+ TasaImp: number;
97
+ MontoImp: number;
98
+ }
99
+
100
+ export interface DscRcgGlobal {
101
+ NroLinDR: number;
102
+ TpoMov: 'D' | 'R';
103
+ GlosaDR: string;
104
+ TpoValor: '%' | '$';
105
+ ValorDR: number;
106
+ }
107
+
108
+ // ============================================
109
+ // CONFIG TYPES
110
+ // ============================================
111
+
112
+ export interface EmisorConfig {
113
+ rut: string;
114
+ razonSocial: string;
115
+ giro: string;
116
+ direccion: string;
117
+ comuna: string;
118
+ ciudad?: string;
119
+ telefono?: string;
120
+ correo?: string;
121
+ acteco?: number | string;
122
+ }
123
+
124
+ export interface ReceptorConfig {
125
+ rut: string;
126
+ razonSocial: string;
127
+ giro?: string;
128
+ direccion: string;
129
+ comuna: string;
130
+ ciudad?: string;
131
+ correo?: string;
132
+ }
133
+
134
+ export interface ItemSimple {
135
+ nombre: string;
136
+ cantidad?: number;
137
+ precio?: number;
138
+ unidad?: string;
139
+ exento?: boolean;
140
+ descuentoPct?: number;
141
+ }
142
+
143
+ // ============================================
144
+ // CALCULO OPTIONS
145
+ // ============================================
146
+
147
+ export interface TotalesOptions {
148
+ tasaIva?: number;
149
+ descuentoGlobalPct?: number;
150
+ preciosNetos?: boolean;
151
+ soloExento?: boolean;
152
+ conRetencion?: boolean;
153
+ tipoImpRetencion?: number;
154
+ }
155
+
156
+ export interface TotalesDesdeDetalleOptions {
157
+ tasaIva?: number;
158
+ preciosNetos?: boolean;
159
+ conRetencion?: boolean;
160
+ sinValores?: boolean;
161
+ }
162
+
163
+ export interface BuildDetalleOptions {
164
+ allowIndExe?: boolean;
165
+ codImpAdic?: number | null;
166
+ forcePriced?: boolean;
167
+ includeUnidad?: boolean;
168
+ sanitize?: (text: string) => string;
169
+ }
170
+
171
+ export interface TotalesResult {
172
+ totales: Totales;
173
+ descuentoGlobalMonto: number;
174
+ }
175
+
176
+ export interface MontoItemResult {
177
+ base: number;
178
+ descuentoMonto: number;
179
+ montoItem: number;
180
+ }
181
+
182
+ // ============================================
183
+ // ERROR TYPES
184
+ // ============================================
185
+
186
+ export interface ErrorDetails {
187
+ [key: string]: any;
188
+ }
189
+
190
+ export const ERROR_CODES: {
191
+ CONFIG_INVALID: 'CONFIG_INVALID';
192
+ CONFIG_MISSING: 'CONFIG_MISSING';
193
+ CERT_NOT_FOUND: 'CERT_NOT_FOUND';
194
+ CERT_INVALID: 'CERT_INVALID';
195
+ CERT_EXPIRED: 'CERT_EXPIRED';
196
+ CERT_PASSWORD_WRONG: 'CERT_PASSWORD_WRONG';
197
+ CAF_NOT_FOUND: 'CAF_NOT_FOUND';
198
+ CAF_INVALID: 'CAF_INVALID';
199
+ CAF_EXPIRED: 'CAF_EXPIRED';
200
+ CAF_NO_FOLIOS: 'CAF_NO_FOLIOS';
201
+ FOLIO_OUT_OF_RANGE: 'FOLIO_OUT_OF_RANGE';
202
+ DTE_INVALID: 'DTE_INVALID';
203
+ DTE_MISSING_FIELDS: 'DTE_MISSING_FIELDS';
204
+ DTE_VALIDATION_FAILED: 'DTE_VALIDATION_FAILED';
205
+ SIGN_FAILED: 'SIGN_FAILED';
206
+ SIGN_VERIFY_FAILED: 'SIGN_VERIFY_FAILED';
207
+ SII_CONNECTION_FAILED: 'SII_CONNECTION_FAILED';
208
+ SII_AUTH_FAILED: 'SII_AUTH_FAILED';
209
+ SII_REJECTED: 'SII_REJECTED';
210
+ SII_TIMEOUT: 'SII_TIMEOUT';
211
+ SII_INVALID_RESPONSE: 'SII_INVALID_RESPONSE';
212
+ XML_PARSE_FAILED: 'XML_PARSE_FAILED';
213
+ XML_BUILD_FAILED: 'XML_BUILD_FAILED';
214
+ UNKNOWN: 'UNKNOWN';
215
+ };
216
+
217
+ export type ErrorCode = typeof ERROR_CODES[keyof typeof ERROR_CODES];
218
+
219
+ export class DteSiiError extends Error {
220
+ code: ErrorCode;
221
+ details: ErrorDetails;
222
+ timestamp: string;
223
+
224
+ constructor(message: string, code?: ErrorCode, details?: ErrorDetails);
225
+ toJSON(): object;
226
+ toString(): string;
227
+ isSiiError(): boolean;
228
+ isRetryable(): boolean;
229
+ }
230
+
231
+ // ============================================
232
+ // REFERENCE TYPES
233
+ // ============================================
234
+
235
+ export interface DocReferenciaParams {
236
+ tipoDte: number;
237
+ folio: number;
238
+ fecha: string;
239
+ codRef: number;
240
+ razonRef: string;
241
+ nroLinRef?: number;
242
+ }
243
+
244
+ export interface AnulacionReferenciaParams {
245
+ tipoDte: number;
246
+ folio: number;
247
+ fecha: string;
248
+ nroLinRef?: number;
249
+ }
250
+
251
+ export interface CorreccionReferenciaParams {
252
+ tipoDte: number;
253
+ folio: number;
254
+ fecha: string;
255
+ razonRef: string;
256
+ nroLinRef?: number;
257
+ }
258
+
259
+ export const CODIGOS_REFERENCIA: {
260
+ ANULA: 1;
261
+ CORRIGE_TEXTO: 2;
262
+ CORRIGE_MONTOS: 3;
263
+ };
264
+
265
+ // ============================================
266
+ // METADATA TYPES
267
+ // ============================================
268
+
269
+ export interface EnvioMetadata {
270
+ rutEmisor: string | null;
271
+ rutEnvia: string | null;
272
+ setId: string | null;
273
+ items: EnvioMetadataItem[];
274
+ parseError?: string;
275
+ }
276
+
277
+ export interface EnvioMetadataItem {
278
+ tipoDTE: string | null;
279
+ folio: string | null;
280
+ fchEmis: string | null;
281
+ }
282
+
283
+ export interface SaveEnvioArtifactsParams {
284
+ xml: string;
285
+ responseText?: string;
286
+ responseOk?: boolean;
287
+ responseStatus?: number;
288
+ trackId?: string;
289
+ ambiente?: string;
290
+ tipoEnvio?: string;
291
+ error?: string;
292
+ baseDir?: string;
293
+ }
294
+
295
+ // ============================================
296
+ // FUNCTION DECLARATIONS
297
+ // ============================================
298
+
299
+ // Sanitización
300
+ export function sanitizeSiiText(text: string): string;
301
+ export function truncateText(text: string, maxLen: number, preserveWords?: boolean): string;
302
+ export function sanitizeGiroRecep(giro: string): string;
303
+ export function sanitizeRazonSocial(razonSocial: string): string;
304
+ export function sanitizeNombreItem(nombre: string): string;
305
+ export function sanitizeDescripcionItem(descripcion: string): string;
306
+ export function safeSegment(value: any, fallback?: string): string;
307
+
308
+ // RUT
309
+ export function formatRut(rut: string): string;
310
+ export function cleanRut(rut: string): string;
311
+ export function splitRut(rut: string): RutParts;
312
+ export function formatRutWithDots(rut: string): string;
313
+ export function formatRutSii(rut: string): string;
314
+ export function calcularDV(rutSinDV: string | number): string;
315
+ export function validarRut(rut: string): boolean;
316
+ export function validateAndFormatRut(rut: string): RutValidation;
317
+
318
+ // XML
319
+ export function formatBase64InXml(xml: string): string;
320
+ export function expandSelfClosingTags(xml: string): string;
321
+ export function normalizeArray<T>(value: T | T[] | null | undefined): T[];
322
+ export function extractEnvioMetadata(xml: string): EnvioMetadata;
323
+ export function saveEnvioArtifacts(params: SaveEnvioArtifactsParams): void;
324
+
325
+ // Resolución SII
326
+ export function normalizeFechaResolucion(value: string): string;
327
+ export function createResolucion(fecha: string, numero?: number): Resolucion;
328
+ export function createResolucionCertificacion(fecha: string): Resolucion;
329
+ export function createResolucionProduccion(fecha: string, numero: number): Resolucion;
330
+ export function validarResolucion(resolucion: Resolucion, requireNumero?: boolean): ValidationResult;
331
+
332
+ // Cálculo
333
+ export const TASA_IVA_DEFAULT: number;
334
+ export function formatDecimal(value: number, decimals?: number): string;
335
+ export function calcularMontoItem(cantidad: number, precio: number, descuentoPct?: number): MontoItemResult;
336
+ export function calcularTotalesDesdeItems(items: ItemSimple[], options?: TotalesOptions | number): TotalesResult;
337
+ export function calcularTotalesDesdeDetalle(detalle: DetalleItem[], options?: TotalesDesdeDetalleOptions): Totales;
338
+ export function buildDetalle(items: ItemSimple[], options?: BuildDetalleOptions): DetalleItem[];
339
+ export function buildDetalleGuia(items: ItemSimple[], options?: { sanitize?: (v: string) => string }): DetalleItem[];
340
+ export function buildDescuentoGlobal(descuentoPct: number, glosa?: string): DscRcgGlobal[] | null;
341
+
342
+ // Referencia
343
+ export function buildSetReferencia(casoId: string, fecha: string, nroLinRef?: number): Referencia;
344
+ export function buildDocReferencia(params: DocReferenciaParams): Referencia;
345
+ export function buildAnulacionReferencia(params: AnulacionReferenciaParams): Referencia;
346
+ export function buildCorreccionTextoReferencia(params: CorreccionReferenciaParams): Referencia;
347
+ export function buildCorreccionMontosReferencia(params: CorreccionReferenciaParams): Referencia;
348
+ export function buildReferenciasNcNd(casoId: string, fechaEmision: string, docRef: Omit<Referencia, 'NroLinRef'>): Referencia[];
349
+
350
+ // Emisor
351
+ export function buildEmisor(config: EmisorConfig): Emisor;
352
+ export function buildEmisorBoleta(config: EmisorConfig): Emisor;
353
+ export function normalizeEmisor(emisor: Partial<Emisor>, esBoleta?: boolean): Emisor;
354
+ export function validarEmisor(emisor: Partial<Emisor>): ValidationResult;
355
+
356
+ // Receptor
357
+ export const RUT_CONSUMIDOR_FINAL: string;
358
+ export const RECEPTOR_CONSUMIDOR_FINAL: Receptor;
359
+ export function buildReceptor(config: ReceptorConfig): Receptor;
360
+ export function buildReceptorBoleta(config?: Partial<ReceptorConfig>): Receptor;
361
+ export function buildReceptorConsumidorFinal(overrides?: Partial<Receptor>): Receptor;
362
+ export function normalizeReceptor(receptor: Partial<Receptor> | null, esBoleta?: boolean): Receptor;
363
+ export function validarReceptor(receptor: Partial<Receptor>, options?: { requireGiro?: boolean; allowConsumidorFinal?: boolean }): ValidationResult;
364
+ export function esConsumidorFinal(receptor: Partial<Receptor>): boolean;
365
+
366
+ // Errores
367
+ export function configError(message: string, details?: ErrorDetails): DteSiiError;
368
+ export function certError(message: string, code?: ErrorCode, details?: ErrorDetails): DteSiiError;
369
+ export function cafError(message: string, code?: ErrorCode, details?: ErrorDetails): DteSiiError;
370
+ export function dteError(message: string, details?: ErrorDetails): DteSiiError;
371
+ export function siiError(message: string, code?: ErrorCode, details?: ErrorDetails): DteSiiError;
372
+ export function xmlError(message: string, details?: ErrorDetails): DteSiiError;
373
+ export function wrapError(error: Error, code?: ErrorCode, details?: ErrorDetails): DteSiiError;
374
+
375
+ // ============================================
376
+ // UTILS NAMESPACE
377
+ // ============================================
378
+
379
+ export const utils: {
380
+ // Sanitización
381
+ sanitizeSiiText: typeof sanitizeSiiText;
382
+ truncateText: typeof truncateText;
383
+ sanitizeGiroRecep: typeof sanitizeGiroRecep;
384
+ sanitizeRazonSocial: typeof sanitizeRazonSocial;
385
+ sanitizeNombreItem: typeof sanitizeNombreItem;
386
+ sanitizeDescripcionItem: typeof sanitizeDescripcionItem;
387
+ safeSegment: typeof safeSegment;
388
+
389
+ // RUT
390
+ formatRut: typeof formatRut;
391
+ cleanRut: typeof cleanRut;
392
+ splitRut: typeof splitRut;
393
+ formatRutWithDots: typeof formatRutWithDots;
394
+ formatRutSii: typeof formatRutSii;
395
+ calcularDV: typeof calcularDV;
396
+ validarRut: typeof validarRut;
397
+ validateAndFormatRut: typeof validateAndFormatRut;
398
+
399
+ // XML
400
+ formatBase64InXml: typeof formatBase64InXml;
401
+ expandSelfClosingTags: typeof expandSelfClosingTags;
402
+ normalizeArray: typeof normalizeArray;
403
+ extractEnvioMetadata: typeof extractEnvioMetadata;
404
+ saveEnvioArtifacts: typeof saveEnvioArtifacts;
405
+
406
+ // Resolución
407
+ normalizeFechaResolucion: typeof normalizeFechaResolucion;
408
+ createResolucion: typeof createResolucion;
409
+ createResolucionCertificacion: typeof createResolucionCertificacion;
410
+ createResolucionProduccion: typeof createResolucionProduccion;
411
+ validarResolucion: typeof validarResolucion;
412
+
413
+ // Cálculo
414
+ TASA_IVA_DEFAULT: typeof TASA_IVA_DEFAULT;
415
+ formatDecimal: typeof formatDecimal;
416
+ calcularMontoItem: typeof calcularMontoItem;
417
+ calcularTotalesDesdeItems: typeof calcularTotalesDesdeItems;
418
+ calcularTotalesDesdeDetalle: typeof calcularTotalesDesdeDetalle;
419
+ buildDetalle: typeof buildDetalle;
420
+ buildDetalleGuia: typeof buildDetalleGuia;
421
+ buildDescuentoGlobal: typeof buildDescuentoGlobal;
422
+
423
+ // Referencia
424
+ buildSetReferencia: typeof buildSetReferencia;
425
+ buildDocReferencia: typeof buildDocReferencia;
426
+ buildAnulacionReferencia: typeof buildAnulacionReferencia;
427
+ buildCorreccionTextoReferencia: typeof buildCorreccionTextoReferencia;
428
+ buildCorreccionMontosReferencia: typeof buildCorreccionMontosReferencia;
429
+ buildReferenciasNcNd: typeof buildReferenciasNcNd;
430
+ CODIGOS_REFERENCIA: typeof CODIGOS_REFERENCIA;
431
+
432
+ // Emisor
433
+ buildEmisor: typeof buildEmisor;
434
+ buildEmisorBoleta: typeof buildEmisorBoleta;
435
+ normalizeEmisor: typeof normalizeEmisor;
436
+ validarEmisor: typeof validarEmisor;
437
+
438
+ // Receptor
439
+ RUT_CONSUMIDOR_FINAL: typeof RUT_CONSUMIDOR_FINAL;
440
+ RECEPTOR_CONSUMIDOR_FINAL: typeof RECEPTOR_CONSUMIDOR_FINAL;
441
+ buildReceptor: typeof buildReceptor;
442
+ buildReceptorBoleta: typeof buildReceptorBoleta;
443
+ buildReceptorConsumidorFinal: typeof buildReceptorConsumidorFinal;
444
+ normalizeReceptor: typeof normalizeReceptor;
445
+ validarReceptor: typeof validarReceptor;
446
+ esConsumidorFinal: typeof esConsumidorFinal;
447
+
448
+ // Errores
449
+ DteSiiError: typeof DteSiiError;
450
+ ERROR_CODES: typeof ERROR_CODES;
451
+ configError: typeof configError;
452
+ certError: typeof certError;
453
+ cafError: typeof cafError;
454
+ dteError: typeof dteError;
455
+ siiError: typeof siiError;
456
+ xmlError: typeof xmlError;
457
+ wrapError: typeof wrapError;
458
+ };