@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.
- package/BoletaService.js +109 -0
- package/CAF.js +173 -0
- package/CafSolicitor.js +380 -0
- package/Certificado.js +123 -0
- package/ConsumoFolio.js +376 -0
- package/DTE.js +399 -0
- package/EnviadorSII.js +1304 -0
- package/Envio.js +196 -0
- package/FolioRegistry.js +553 -0
- package/FolioService.js +703 -0
- package/LICENSE +27 -0
- package/LibroBase.js +134 -0
- package/LibroCompraVenta.js +205 -0
- package/LibroGuia.js +225 -0
- package/README.md +239 -0
- package/Signer.js +94 -0
- package/SiiCertificacion.js +1189 -0
- package/SiiPortalAuth.js +460 -0
- package/SiiSession.js +499 -0
- package/cert/BoletaCert.js +731 -0
- package/cert/CertFolioHelper.js +185 -0
- package/cert/CertRunner.js +2658 -0
- package/cert/ConfigLoader.js +133 -0
- package/cert/IntercambioCert.js +429 -0
- package/cert/LibroCompras.js +359 -0
- package/cert/LibroGuias.js +171 -0
- package/cert/LibroVentas.js +153 -0
- package/cert/MuestrasImpresas.js +676 -0
- package/cert/SetBase.js +321 -0
- package/cert/SetBasico.js +413 -0
- package/cert/SetCompra.js +472 -0
- package/cert/SetExenta.js +490 -0
- package/cert/SetGuia.js +283 -0
- package/cert/SetParser.js +1184 -0
- package/cert/SetsProvider.js +499 -0
- package/cert/Simulacion.js +521 -0
- package/cert/comunaOficina.js +460 -0
- package/cert/index.js +124 -0
- package/cert/types.js +330 -0
- package/dte-sii.d.ts +458 -0
- package/index.js +428 -0
- package/package.json +48 -0
- package/utils/c14n.js +275 -0
- package/utils/calculo.js +396 -0
- package/utils/config.js +276 -0
- package/utils/constants.js +302 -0
- package/utils/emisor.js +174 -0
- package/utils/endpoints.js +225 -0
- package/utils/error.js +235 -0
- package/utils/index.js +339 -0
- package/utils/logger.js +239 -0
- package/utils/pfx.js +203 -0
- package/utils/receptor.js +218 -0
- package/utils/referencia.js +169 -0
- package/utils/resolucion.js +119 -0
- package/utils/rut.js +169 -0
- package/utils/sanitize.js +124 -0
- package/utils/tokenCache.js +214 -0
- package/utils/xml.js +358 -0
- package/utils.js +4 -0
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
// Copyright (c) 2026 Devlas SpA — https://devlas.cl
|
|
2
|
+
// Licencia MIT. Ver archivo LICENSE para mas detalles.
|
|
3
|
+
/**
|
|
4
|
+
* LibroCompras.js - Generador de Libro de Compras para Certificación SII
|
|
5
|
+
*
|
|
6
|
+
* Construye el libro de compras a partir de los resultados del SetCompra.
|
|
7
|
+
*
|
|
8
|
+
* Documentos incluidos:
|
|
9
|
+
* - Facturas de Compra (46) con IVA Retenido Total
|
|
10
|
+
* - Notas de Crédito (61)
|
|
11
|
+
* - Notas de Débito (56)
|
|
12
|
+
*
|
|
13
|
+
* Maneja campos especiales:
|
|
14
|
+
* - IVANoRec (IVA no recuperable)
|
|
15
|
+
* - IVAUsoComun (IVA de uso común con factor proporcionalidad)
|
|
16
|
+
* - OtrosImp (otros impuestos)
|
|
17
|
+
* - IVARetTotal (IVA retenido total)
|
|
18
|
+
*
|
|
19
|
+
* @module dte-sii/cert/LibroCompras
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
const { LibroCompraVenta } = require('../index');
|
|
23
|
+
|
|
24
|
+
// Factor de proporcionalidad para IVA de uso común (60%)
|
|
25
|
+
const FACTOR_PROPORCIONALIDAD = 0.6;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @typedef {Object} LibroComprasConfig
|
|
29
|
+
* @property {Object} emisor - Datos del emisor
|
|
30
|
+
* @property {string} periodo - Período tributario (YYYY-MM)
|
|
31
|
+
* @property {Object} certificado - Instancia de Certificado
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
class LibroCompras {
|
|
35
|
+
/**
|
|
36
|
+
* @param {LibroComprasConfig} config
|
|
37
|
+
*/
|
|
38
|
+
constructor(config) {
|
|
39
|
+
this.config = config;
|
|
40
|
+
this.emisor = config.emisor;
|
|
41
|
+
this.periodo = config.periodo;
|
|
42
|
+
this.certificado = config.certificado;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Genera el libro de compras desde los resultados del SetCompra
|
|
47
|
+
* @param {Object} setCompraResult - Resultado de SetCompra.ejecutar()
|
|
48
|
+
* @returns {Object} { libro, xml, detalle, resumen }
|
|
49
|
+
*/
|
|
50
|
+
generar(setCompraResult) {
|
|
51
|
+
const { documentos } = setCompraResult;
|
|
52
|
+
|
|
53
|
+
if (!documentos || documentos.length === 0) {
|
|
54
|
+
throw new Error('LibroCompras: No hay documentos del SetCompra para generar libro');
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const fechaBase = `${this.periodo}-15`;
|
|
58
|
+
const detalles = [];
|
|
59
|
+
|
|
60
|
+
// Procesar cada documento del set compra
|
|
61
|
+
for (const doc of documentos) {
|
|
62
|
+
const detalle = this._buildDetalle(doc, fechaBase);
|
|
63
|
+
detalles.push(detalle);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Calcular resumen automáticamente desde el detalle
|
|
67
|
+
const resumen = this._calcularResumenDesdeDetalle(detalles);
|
|
68
|
+
|
|
69
|
+
// Crear libro
|
|
70
|
+
const libro = new LibroCompraVenta(this.certificado);
|
|
71
|
+
libro.setCaratula({
|
|
72
|
+
RutEmisorLibro: this.emisor.rut,
|
|
73
|
+
RutEnvia: this.certificado.rut || this.emisor.rut,
|
|
74
|
+
PeriodoTributario: this.periodo,
|
|
75
|
+
FchResol: this.emisor.fch_resol,
|
|
76
|
+
NroResol: this.emisor.nro_resol,
|
|
77
|
+
TipoOperacion: 'COMPRA',
|
|
78
|
+
TipoLibro: 'MENSUAL',
|
|
79
|
+
TipoEnvio: 'TOTAL',
|
|
80
|
+
});
|
|
81
|
+
libro.setResumen(resumen);
|
|
82
|
+
libro.setDetalle(detalles);
|
|
83
|
+
libro.generar();
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
libro,
|
|
87
|
+
xml: libro.getXML(),
|
|
88
|
+
detalle: detalles,
|
|
89
|
+
resumen,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Genera el libro de compras desde los datos pre-procesados del SII (estructuras)
|
|
95
|
+
* @param {Object} libroComprasData - Datos del libro de compras { detalle, resumen, factorProporcionalidad }
|
|
96
|
+
* @param {string} periodo - Período tributario (YYYY-MM)
|
|
97
|
+
* @returns {Object} { libro, xml, detalle, resumen }
|
|
98
|
+
*/
|
|
99
|
+
generarDesdeEstructuras(libroComprasData, periodo) {
|
|
100
|
+
const { detalle, resumen, factorProporcionalidad } = libroComprasData;
|
|
101
|
+
|
|
102
|
+
if (!detalle || detalle.length === 0) {
|
|
103
|
+
throw new Error('LibroCompras: No hay detalle en los datos del SII');
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Ajustar fechas al período correcto y aplicar reglas SII
|
|
107
|
+
const fechaBase = `${periodo}-15`;
|
|
108
|
+
const detalleAjustado = detalle.map(doc => {
|
|
109
|
+
const docAjustado = {
|
|
110
|
+
...doc,
|
|
111
|
+
FchDoc: fechaBase,
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
// Regla SII: Cuando hay IVAUsoComun, TasaImp y MntIVA van en 0
|
|
115
|
+
if (doc.IVAUsoComun) {
|
|
116
|
+
docAjustado.TasaImp = 0;
|
|
117
|
+
docAjustado.MntIVA = 0;
|
|
118
|
+
}
|
|
119
|
+
// Regla SII: Cuando hay IVANoRec, TasaImp y MntIVA van en 0
|
|
120
|
+
else if (doc.IVANoRec) {
|
|
121
|
+
docAjustado.TasaImp = 0;
|
|
122
|
+
docAjustado.MntIVA = 0;
|
|
123
|
+
}
|
|
124
|
+
// Caso normal: Asegurar TasaImp sea número entero (19) no decimal (0.19)
|
|
125
|
+
else if (doc.TasaImp !== undefined) {
|
|
126
|
+
docAjustado.TasaImp = doc.TasaImp < 1 ? Math.round(doc.TasaImp * 100) : doc.TasaImp;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return docAjustado;
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// Recalcular resumen desde el detalle ajustado
|
|
133
|
+
const resumenRecalculado = this._calcularResumenDesdeDetalle(detalleAjustado, factorProporcionalidad);
|
|
134
|
+
|
|
135
|
+
// Crear libro
|
|
136
|
+
const libro = new LibroCompraVenta(this.certificado);
|
|
137
|
+
libro.setCaratula({
|
|
138
|
+
RutEmisorLibro: this.emisor.rut,
|
|
139
|
+
RutEnvia: this.certificado.rut || this.emisor.rut,
|
|
140
|
+
PeriodoTributario: periodo,
|
|
141
|
+
FchResol: this.emisor.fch_resol,
|
|
142
|
+
NroResol: this.emisor.nro_resol,
|
|
143
|
+
TipoOperacion: 'COMPRA',
|
|
144
|
+
TipoLibro: 'MENSUAL',
|
|
145
|
+
TipoEnvio: 'TOTAL',
|
|
146
|
+
});
|
|
147
|
+
libro.setResumen(resumenRecalculado);
|
|
148
|
+
libro.setDetalle(detalleAjustado);
|
|
149
|
+
libro.generar();
|
|
150
|
+
|
|
151
|
+
return {
|
|
152
|
+
libro,
|
|
153
|
+
xml: libro.getXML(),
|
|
154
|
+
detalle: detalleAjustado,
|
|
155
|
+
resumen: resumenRecalculado,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Construye un registro de detalle desde un documento
|
|
161
|
+
* @private
|
|
162
|
+
*/
|
|
163
|
+
_buildDetalle(doc, fechaBase) {
|
|
164
|
+
const tipoDte = doc.tipoDte;
|
|
165
|
+
const totales = doc.totales || {};
|
|
166
|
+
|
|
167
|
+
// En Factura de Compra (46), el emisor ES el receptor del documento
|
|
168
|
+
// porque nosotros RECIBIMOS la factura de compra
|
|
169
|
+
const rutDoc = this.emisor.rut;
|
|
170
|
+
const rznSoc = this.emisor.razon_social;
|
|
171
|
+
|
|
172
|
+
const detalle = {
|
|
173
|
+
TpoDoc: tipoDte,
|
|
174
|
+
NroDoc: doc.folio,
|
|
175
|
+
FchDoc: fechaBase,
|
|
176
|
+
RUTDoc: rutDoc,
|
|
177
|
+
RznSoc: rznSoc,
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
// Montos básicos
|
|
181
|
+
const mntNeto = Number(totales.MntNeto || 0);
|
|
182
|
+
const mntExe = Number(totales.MntExe || 0);
|
|
183
|
+
const mntIva = Number(totales.IVA || 0);
|
|
184
|
+
const ivaRetTotal = Number(totales.IVARetTotal || 0);
|
|
185
|
+
|
|
186
|
+
if (mntExe > 0) detalle.MntExe = mntExe;
|
|
187
|
+
if (mntNeto > 0) detalle.MntNeto = mntNeto;
|
|
188
|
+
|
|
189
|
+
// Tasa de IVA - siempre informar si hay MntNeto
|
|
190
|
+
const tasaIva = Number(totales.TasaIVA || 19);
|
|
191
|
+
|
|
192
|
+
// IVA No Recuperable
|
|
193
|
+
if (totales.IVANoRec) {
|
|
194
|
+
detalle.IVANoRec = {
|
|
195
|
+
CodIVANoRec: totales.IVANoRec.CodIVANoRec || 1,
|
|
196
|
+
MntIVANoRec: Number(totales.IVANoRec.MntIVANoRec || 0),
|
|
197
|
+
};
|
|
198
|
+
// Con IVA no recuperable, TasaImp y MntIVA van en 0
|
|
199
|
+
detalle.TasaImp = 0;
|
|
200
|
+
detalle.MntIVA = 0;
|
|
201
|
+
} else if (totales.IVAUsoComun !== undefined && totales.IVAUsoComun > 0) {
|
|
202
|
+
// IVA Uso Común
|
|
203
|
+
detalle.IVAUsoComun = Number(totales.IVAUsoComun);
|
|
204
|
+
// Con IVA uso común, TasaImp y MntIVA van en 0
|
|
205
|
+
detalle.TasaImp = 0;
|
|
206
|
+
detalle.MntIVA = 0;
|
|
207
|
+
} else if (mntNeto > 0) {
|
|
208
|
+
// Caso normal: informar TasaImp y MntIVA
|
|
209
|
+
detalle.TasaImp = tasaIva;
|
|
210
|
+
detalle.MntIVA = mntIva;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// IVA Retenido Total (para Factura de Compra tipo 46 y sus NC/ND)
|
|
214
|
+
// Se informa ADEMÁS del MntIVA, no en lugar de
|
|
215
|
+
if (ivaRetTotal > 0) {
|
|
216
|
+
detalle.IVARetTotal = ivaRetTotal;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// Otros Impuestos
|
|
220
|
+
const otrosImpMonto = totales.OtrosImp ? Number(totales.OtrosImp.MntImp || 0) : 0;
|
|
221
|
+
if (totales.OtrosImp) {
|
|
222
|
+
detalle.OtrosImp = {
|
|
223
|
+
CodImp: totales.OtrosImp.CodImp,
|
|
224
|
+
TasaImp: totales.OtrosImp.TasaImp || 0,
|
|
225
|
+
MntImp: otrosImpMonto,
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// Monto Total - RECALCULAR según fórmula del libro:
|
|
230
|
+
// MntTotal = MntNeto + MntExe + MntIVA + IVANoRec + IVAUsoComun + OtrosImp - IVARetTotal
|
|
231
|
+
const mntIvaNoRec = totales.IVANoRec ? Number(totales.IVANoRec.MntIVANoRec || 0) : 0;
|
|
232
|
+
const ivaUsoComun = Number(totales.IVAUsoComun || 0);
|
|
233
|
+
detalle.MntTotal = mntNeto + mntExe + mntIva + mntIvaNoRec + ivaUsoComun + otrosImpMonto - ivaRetTotal;
|
|
234
|
+
|
|
235
|
+
return detalle;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Calcula el RESUMEN automáticamente desde el DETALLE
|
|
240
|
+
* @private
|
|
241
|
+
* @param {Array} detalle - Array de documentos
|
|
242
|
+
* @param {number} [factorProp] - Factor de proporcionalidad (default 0.6)
|
|
243
|
+
*/
|
|
244
|
+
_calcularResumenDesdeDetalle(detalle, factorProp) {
|
|
245
|
+
const factor = factorProp || FACTOR_PROPORCIONALIDAD;
|
|
246
|
+
const resumenMap = new Map();
|
|
247
|
+
|
|
248
|
+
for (const doc of detalle) {
|
|
249
|
+
const tipo = doc.TpoDoc;
|
|
250
|
+
|
|
251
|
+
if (!resumenMap.has(tipo)) {
|
|
252
|
+
resumenMap.set(tipo, {
|
|
253
|
+
TpoDoc: tipo,
|
|
254
|
+
TotDoc: 0,
|
|
255
|
+
TotMntExe: 0,
|
|
256
|
+
TotMntNeto: 0,
|
|
257
|
+
TotMntIVA: 0,
|
|
258
|
+
TotMntTotal: 0,
|
|
259
|
+
TotOpIVAUsoComun: 0,
|
|
260
|
+
TotIVAUsoComun: 0,
|
|
261
|
+
TotCredIVAUsoComun: 0,
|
|
262
|
+
TotIVANoRec: {},
|
|
263
|
+
TotOtrosImp: {},
|
|
264
|
+
TotIVARetTotal: 0,
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const r = resumenMap.get(tipo);
|
|
269
|
+
r.TotDoc += 1;
|
|
270
|
+
r.TotMntExe += Number(doc.MntExe || 0);
|
|
271
|
+
r.TotMntNeto += Number(doc.MntNeto || 0);
|
|
272
|
+
r.TotMntIVA += Number(doc.MntIVA || 0);
|
|
273
|
+
r.TotMntTotal += Number(doc.MntTotal || 0);
|
|
274
|
+
|
|
275
|
+
// IVA Uso Común
|
|
276
|
+
if (doc.IVAUsoComun) {
|
|
277
|
+
r.TotOpIVAUsoComun += 1;
|
|
278
|
+
r.TotIVAUsoComun += Number(doc.IVAUsoComun);
|
|
279
|
+
r.TotCredIVAUsoComun += Math.round(Number(doc.IVAUsoComun) * factor);
|
|
280
|
+
r.FctProp = factor;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// IVA No Recuperable
|
|
284
|
+
if (doc.IVANoRec) {
|
|
285
|
+
const codIVA = doc.IVANoRec.CodIVANoRec;
|
|
286
|
+
if (!r.TotIVANoRec[codIVA]) {
|
|
287
|
+
r.TotIVANoRec[codIVA] = {
|
|
288
|
+
CodIVANoRec: codIVA,
|
|
289
|
+
TotOpIVANoRec: 0,
|
|
290
|
+
TotMntIVANoRec: 0,
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
r.TotIVANoRec[codIVA].TotOpIVANoRec += 1;
|
|
294
|
+
r.TotIVANoRec[codIVA].TotMntIVANoRec += Number(doc.IVANoRec.MntIVANoRec || 0);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// Otros Impuestos
|
|
298
|
+
if (doc.OtrosImp) {
|
|
299
|
+
const codImp = doc.OtrosImp.CodImp;
|
|
300
|
+
if (!r.TotOtrosImp[codImp]) {
|
|
301
|
+
r.TotOtrosImp[codImp] = {
|
|
302
|
+
CodImp: codImp,
|
|
303
|
+
TotMntImp: 0,
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
r.TotOtrosImp[codImp].TotMntImp += Number(doc.OtrosImp.MntImp || 0);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// IVA Retenido Total
|
|
310
|
+
if (doc.IVARetTotal) {
|
|
311
|
+
r.TotIVARetTotal += Number(doc.IVARetTotal);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// Convertir a array limpio
|
|
316
|
+
const resumenArray = Array.from(resumenMap.values()).map((r) => {
|
|
317
|
+
const limpio = {
|
|
318
|
+
TpoDoc: r.TpoDoc,
|
|
319
|
+
TotDoc: r.TotDoc,
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
if (r.TotMntExe > 0) limpio.TotMntExe = r.TotMntExe;
|
|
323
|
+
if (r.TotMntNeto > 0) limpio.TotMntNeto = r.TotMntNeto;
|
|
324
|
+
if (r.TotMntIVA > 0) limpio.TotMntIVA = r.TotMntIVA;
|
|
325
|
+
if (r.TotMntTotal > 0) limpio.TotMntTotal = r.TotMntTotal;
|
|
326
|
+
|
|
327
|
+
// IVA Uso Común
|
|
328
|
+
if (r.TotOpIVAUsoComun > 0) {
|
|
329
|
+
limpio.TotOpIVAUsoComun = r.TotOpIVAUsoComun;
|
|
330
|
+
limpio.TotIVAUsoComun = r.TotIVAUsoComun;
|
|
331
|
+
limpio.FctProp = r.FctProp;
|
|
332
|
+
limpio.TotCredIVAUsoComun = r.TotCredIVAUsoComun;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// IVA No Recuperable
|
|
336
|
+
const ivaNoRecArray = Object.values(r.TotIVANoRec);
|
|
337
|
+
if (ivaNoRecArray.length > 0) {
|
|
338
|
+
limpio.TotIVANoRec = ivaNoRecArray;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// Otros Impuestos
|
|
342
|
+
const otrosImpArray = Object.values(r.TotOtrosImp);
|
|
343
|
+
if (otrosImpArray.length > 0) {
|
|
344
|
+
limpio.TotOtrosImp = otrosImpArray;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// IVA Retenido Total
|
|
348
|
+
if (r.TotIVARetTotal > 0) {
|
|
349
|
+
limpio.TotIVARetTotal = r.TotIVARetTotal;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
return limpio;
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
return resumenArray;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
module.exports = LibroCompras;
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// Copyright (c) 2026 Devlas SpA — https://devlas.cl
|
|
2
|
+
// Licencia MIT. Ver archivo LICENSE para mas detalles.
|
|
3
|
+
/**
|
|
4
|
+
* LibroGuias.js - Generador de Libro de Guías para Certificación SII
|
|
5
|
+
*
|
|
6
|
+
* Construye el libro de guías a partir de los resultados del SetGuia.
|
|
7
|
+
*
|
|
8
|
+
* Documentos incluidos:
|
|
9
|
+
* - Guías de Despacho (52)
|
|
10
|
+
*
|
|
11
|
+
* El resumen (ResumenPeriodo) se genera automáticamente por LibroGuia._buildResumenPeriodo()
|
|
12
|
+
*
|
|
13
|
+
* Tipos de Operación (TpoOper):
|
|
14
|
+
* - 1: Venta (incluye datos de receptor y montos)
|
|
15
|
+
* - 2-9: Traslados varios (sin datos comerciales)
|
|
16
|
+
*
|
|
17
|
+
* Estados especiales:
|
|
18
|
+
* - Anulado=1: Folio anulado (TotFolAnulado)
|
|
19
|
+
* - Anulado=2: Guía anulada (TotGuiaAnulada)
|
|
20
|
+
*
|
|
21
|
+
* @module dte-sii/cert/LibroGuias
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
const { LibroGuia } = require('../index');
|
|
25
|
+
|
|
26
|
+
// Valor por defecto para certificación
|
|
27
|
+
const FOLIO_NOTIFICACION_DEFAULT = 3;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @typedef {Object} LibroGuiasConfig
|
|
31
|
+
* @property {Object} emisor - Datos del emisor
|
|
32
|
+
* @property {Object} receptor - Datos del receptor
|
|
33
|
+
* @property {string} periodo - Período tributario (YYYY-MM)
|
|
34
|
+
* @property {Object} certificado - Instancia de Certificado
|
|
35
|
+
* @property {number} [folioNotificacion=3] - Folio de notificación SII
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
class LibroGuias {
|
|
39
|
+
/**
|
|
40
|
+
* @param {LibroGuiasConfig} config
|
|
41
|
+
*/
|
|
42
|
+
constructor(config) {
|
|
43
|
+
this.config = config;
|
|
44
|
+
this.emisor = config.emisor;
|
|
45
|
+
this.receptor = config.receptor;
|
|
46
|
+
this.periodo = config.periodo;
|
|
47
|
+
this.certificado = config.certificado;
|
|
48
|
+
this.folioNotificacion = config.folioNotificacion || FOLIO_NOTIFICACION_DEFAULT;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Genera el libro de guías desde los resultados del SetGuia
|
|
53
|
+
* @param {Object} setGuiaResult - Resultado de SetGuia.ejecutar()
|
|
54
|
+
* @param {Object} [options] - Opciones adicionales
|
|
55
|
+
* @param {Object} [options.casosLibro] - Configuración especial por caso (anulado, operacion)
|
|
56
|
+
* @returns {Object} { libro, xml, detalle }
|
|
57
|
+
*/
|
|
58
|
+
generar(setGuiaResult, options = {}) {
|
|
59
|
+
const { documentos } = setGuiaResult;
|
|
60
|
+
|
|
61
|
+
if (!documentos || documentos.length === 0) {
|
|
62
|
+
throw new Error('LibroGuias: No hay documentos del SetGuia para generar libro');
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const casosLibro = options.casosLibro || this._getDefaultCasosLibro(documentos.length);
|
|
66
|
+
const detalles = [];
|
|
67
|
+
|
|
68
|
+
// Procesar cada documento del set guía
|
|
69
|
+
for (let i = 0; i < documentos.length; i++) {
|
|
70
|
+
const doc = documentos[i];
|
|
71
|
+
const casoConfig = casosLibro[i] || {};
|
|
72
|
+
const detalle = this._buildDetalle(doc, casoConfig);
|
|
73
|
+
detalles.push(detalle);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Crear libro (el resumen se genera automáticamente en LibroGuia._buildResumenPeriodo)
|
|
77
|
+
const libro = new LibroGuia(this.certificado);
|
|
78
|
+
libro.setCaratula({
|
|
79
|
+
RutEmisorLibro: this.emisor.rut,
|
|
80
|
+
RutEnvia: this.certificado.rut || this.emisor.rut,
|
|
81
|
+
PeriodoTributario: this.periodo,
|
|
82
|
+
FchResol: this.emisor.fch_resol,
|
|
83
|
+
NroResol: this.emisor.nro_resol,
|
|
84
|
+
TipoLibro: 'ESPECIAL',
|
|
85
|
+
TipoEnvio: 'TOTAL',
|
|
86
|
+
FolioNotificacion: this.folioNotificacion,
|
|
87
|
+
});
|
|
88
|
+
libro.setDetalle(detalles);
|
|
89
|
+
libro.generar();
|
|
90
|
+
|
|
91
|
+
return {
|
|
92
|
+
libro,
|
|
93
|
+
xml: libro.getXML(),
|
|
94
|
+
detalle: detalles,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Configuración por defecto para casos de certificación:
|
|
100
|
+
* - Caso 1 (índice 0): Normal
|
|
101
|
+
* - Caso 2 (índice 1): Operacion=1 (modificación)
|
|
102
|
+
* - Caso 3 (índice 2): Anulado=2 (guía anulada)
|
|
103
|
+
* @private
|
|
104
|
+
*/
|
|
105
|
+
_getDefaultCasosLibro(count) {
|
|
106
|
+
const casos = [];
|
|
107
|
+
for (let i = 0; i < count; i++) {
|
|
108
|
+
const caso = {};
|
|
109
|
+
if (i === 1) caso.operacion = 1; // Segundo caso: modificación
|
|
110
|
+
if (i === 2) caso.anulado = 2; // Tercer caso: anulado
|
|
111
|
+
casos.push(caso);
|
|
112
|
+
}
|
|
113
|
+
return casos;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Construye un registro de detalle desde un documento
|
|
118
|
+
* @private
|
|
119
|
+
*/
|
|
120
|
+
_buildDetalle(doc, casoConfig) {
|
|
121
|
+
const totales = doc.totales || {};
|
|
122
|
+
const fechaDoc = doc.fecha || `${this.periodo}-15`;
|
|
123
|
+
|
|
124
|
+
// Determinar tipo de operación desde el documento o caso
|
|
125
|
+
// IndTraslado=1 (venta) => TpoOper=1
|
|
126
|
+
const indTraslado = doc.indTraslado || 1;
|
|
127
|
+
const tpoOper = indTraslado === 1 ? 1 : indTraslado;
|
|
128
|
+
|
|
129
|
+
const detalle = {
|
|
130
|
+
Folio: doc.folio,
|
|
131
|
+
TpoOper: tpoOper,
|
|
132
|
+
FchDoc: fechaDoc,
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
// Anulado (1=folio anulado, 2=guía anulada)
|
|
136
|
+
if (casoConfig.anulado) {
|
|
137
|
+
detalle.Anulado = casoConfig.anulado;
|
|
138
|
+
detalle.MntTotal = 0;
|
|
139
|
+
// No incluir otros campos en guías anuladas
|
|
140
|
+
return detalle;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Operación (1=modificación de texto)
|
|
144
|
+
if (casoConfig.operacion) {
|
|
145
|
+
detalle.Operacion = casoConfig.operacion;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Para ventas (TpoOper=1), incluir datos comerciales
|
|
149
|
+
if (tpoOper === 1) {
|
|
150
|
+
if (this.receptor.rut) detalle.RUTDoc = this.receptor.rut;
|
|
151
|
+
if (this.receptor.razon_social) detalle.RznSoc = this.receptor.razon_social;
|
|
152
|
+
|
|
153
|
+
const mntNeto = Number(totales.MntNeto || 0);
|
|
154
|
+
const tasaIva = Number(totales.TasaIVA || 19);
|
|
155
|
+
const iva = Number(totales.IVA || 0);
|
|
156
|
+
|
|
157
|
+
if (mntNeto > 0) {
|
|
158
|
+
detalle.MntNeto = mntNeto;
|
|
159
|
+
detalle.TasaImp = tasaIva;
|
|
160
|
+
detalle.IVA = iva > 0 ? iva : Math.round(mntNeto * (tasaIva / 100));
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// Monto Total
|
|
165
|
+
detalle.MntTotal = Number(totales.MntTotal || 0);
|
|
166
|
+
|
|
167
|
+
return detalle;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
module.exports = LibroGuias;
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
// Copyright (c) 2026 Devlas SpA — https://devlas.cl
|
|
2
|
+
// Licencia MIT. Ver archivo LICENSE para mas detalles.
|
|
3
|
+
/**
|
|
4
|
+
* LibroVentas.js - Generador de Libro de Ventas para Certificación SII
|
|
5
|
+
*
|
|
6
|
+
* Construye el libro de ventas a partir de los resultados del SetBasico.
|
|
7
|
+
*
|
|
8
|
+
* Documentos incluidos:
|
|
9
|
+
* - Facturas (33)
|
|
10
|
+
* - Notas de Crédito (61)
|
|
11
|
+
* - Notas de Débito (56)
|
|
12
|
+
*
|
|
13
|
+
* @module dte-sii/cert/LibroVentas
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
const { LibroCompraVenta } = require('../index');
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @typedef {Object} LibroVentasConfig
|
|
20
|
+
* @property {Object} emisor - Datos del emisor
|
|
21
|
+
* @property {Object} receptor - Datos del receptor (para RUTDoc/RznSoc)
|
|
22
|
+
* @property {string} periodo - Período tributario (YYYY-MM)
|
|
23
|
+
* @property {Object} certificado - Instancia de Certificado
|
|
24
|
+
* @property {string} [signoNC='POSITIVO'] - 'POSITIVO' o 'NEGATIVO' para NC
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
class LibroVentas {
|
|
28
|
+
/**
|
|
29
|
+
* @param {LibroVentasConfig} config
|
|
30
|
+
*/
|
|
31
|
+
constructor(config) {
|
|
32
|
+
this.config = config;
|
|
33
|
+
this.emisor = config.emisor;
|
|
34
|
+
this.receptor = config.receptor;
|
|
35
|
+
this.periodo = config.periodo;
|
|
36
|
+
this.certificado = config.certificado;
|
|
37
|
+
this.signoNC = (config.signoNC || 'POSITIVO').toUpperCase();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Genera el libro de ventas desde los resultados del SetBasico
|
|
42
|
+
* @param {Object} setBasicoResult - Resultado de SetBasico.ejecutar()
|
|
43
|
+
* @returns {Object} { libro, xml, detalle, resumen }
|
|
44
|
+
*/
|
|
45
|
+
generar(setBasicoResult) {
|
|
46
|
+
const { documentos } = setBasicoResult;
|
|
47
|
+
|
|
48
|
+
if (!documentos || documentos.length === 0) {
|
|
49
|
+
throw new Error('LibroVentas: No hay documentos del SetBasico para generar libro');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const fechaBase = `${this.periodo}-15`;
|
|
53
|
+
const detalles = [];
|
|
54
|
+
const resumenMap = new Map();
|
|
55
|
+
|
|
56
|
+
// Procesar cada documento del set básico
|
|
57
|
+
for (const doc of documentos) {
|
|
58
|
+
const tipoDte = doc.tipoDte;
|
|
59
|
+
const folio = doc.folio;
|
|
60
|
+
const totales = doc.totales || {};
|
|
61
|
+
|
|
62
|
+
// Calcular signo (NC puede ser negativa según configuración)
|
|
63
|
+
const sign = this._getSignByTipoDte(tipoDte);
|
|
64
|
+
|
|
65
|
+
// Construir detalle
|
|
66
|
+
const detalle = {
|
|
67
|
+
TpoDoc: tipoDte,
|
|
68
|
+
NroDoc: folio,
|
|
69
|
+
FchDoc: fechaBase,
|
|
70
|
+
RUTDoc: this.receptor.rut,
|
|
71
|
+
RznSoc: this.receptor.razon_social,
|
|
72
|
+
MntExe: Math.round(Number(totales.MntExe || 0) * sign),
|
|
73
|
+
MntNeto: Math.round(Number(totales.MntNeto || 0) * sign),
|
|
74
|
+
MntIVA: Math.round(Number(totales.IVA || 0) * sign),
|
|
75
|
+
MntTotal: Math.round(Number(totales.MntTotal || 0) * sign),
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
// Agregar TasaImp si hay IVA
|
|
79
|
+
if (totales.TasaIVA !== undefined && totales.TasaIVA > 0) {
|
|
80
|
+
detalle.TasaImp = Number(totales.TasaIVA);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
detalles.push(detalle);
|
|
84
|
+
|
|
85
|
+
// Acumular resumen por tipo de documento
|
|
86
|
+
this._addToResumen(resumenMap, tipoDte, totales, sign);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Convertir resumen a array
|
|
90
|
+
const resumen = Array.from(resumenMap.values());
|
|
91
|
+
|
|
92
|
+
// Crear libro
|
|
93
|
+
const libro = new LibroCompraVenta(this.certificado);
|
|
94
|
+
libro.setCaratula({
|
|
95
|
+
RutEmisorLibro: this.emisor.rut,
|
|
96
|
+
RutEnvia: this.certificado.rut || this.emisor.rut,
|
|
97
|
+
PeriodoTributario: this.periodo,
|
|
98
|
+
FchResol: this.emisor.fch_resol,
|
|
99
|
+
NroResol: this.emisor.nro_resol,
|
|
100
|
+
TipoOperacion: 'VENTA',
|
|
101
|
+
TipoLibro: 'MENSUAL',
|
|
102
|
+
TipoEnvio: 'TOTAL',
|
|
103
|
+
});
|
|
104
|
+
libro.setResumen(resumen);
|
|
105
|
+
libro.setDetalle(detalles);
|
|
106
|
+
libro.generar();
|
|
107
|
+
|
|
108
|
+
return {
|
|
109
|
+
libro,
|
|
110
|
+
xml: libro.getXML(),
|
|
111
|
+
detalle: detalles,
|
|
112
|
+
resumen,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Obtiene el signo para un tipo de DTE
|
|
118
|
+
* @private
|
|
119
|
+
*/
|
|
120
|
+
_getSignByTipoDte(tipoDte) {
|
|
121
|
+
// NC (61) puede tener signo negativo según configuración
|
|
122
|
+
if (tipoDte === 61 && this.signoNC === 'NEGATIVO') {
|
|
123
|
+
return -1;
|
|
124
|
+
}
|
|
125
|
+
return 1;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Agrega totales al resumen acumulado
|
|
130
|
+
* @private
|
|
131
|
+
*/
|
|
132
|
+
_addToResumen(resumenMap, tipoDte, totales, sign) {
|
|
133
|
+
if (!resumenMap.has(tipoDte)) {
|
|
134
|
+
resumenMap.set(tipoDte, {
|
|
135
|
+
TpoDoc: tipoDte,
|
|
136
|
+
TotDoc: 0,
|
|
137
|
+
TotMntExe: 0,
|
|
138
|
+
TotMntNeto: 0,
|
|
139
|
+
TotMntIVA: 0,
|
|
140
|
+
TotMntTotal: 0,
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const r = resumenMap.get(tipoDte);
|
|
145
|
+
r.TotDoc += 1;
|
|
146
|
+
r.TotMntExe += Math.round(Number(totales.MntExe || 0) * sign);
|
|
147
|
+
r.TotMntNeto += Math.round(Number(totales.MntNeto || 0) * sign);
|
|
148
|
+
r.TotMntIVA += Math.round(Number(totales.IVA || 0) * sign);
|
|
149
|
+
r.TotMntTotal += Math.round(Number(totales.MntTotal || 0) * sign);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
module.exports = LibroVentas;
|