@disruptive-learning/cfdi-to-pdf
Version:
Librería para crear un pdf basado en un XML CFDI o Retenciones
125 lines (124 loc) • 4.66 kB
JavaScript
import { getValueOfCatalog } from '#src/catalogs/catalogs_source';
import { formatCurrency } from '#src/utils/currency';
const generateImpuestosContentTable = (traslados, retenciones, catalogs, primaryColor, bgGrayColor) => {
const impuestosTableBody = [];
for (const traslado of traslados) {
const baseCells = [];
baseCells.push({ text: 'Traslado', style: ['tableSmall'] }, { text: getValueOfCatalog('cfdi40Impuestos', traslado.getAttribute('Impuesto'), catalogs) }, { text: 'Base', style: ['tableSmall'] }, {
text: formatCurrency(traslado.getAttribute('Base')),
alignment: 'right',
});
const factor = traslado.getAttribute('TipoFactor');
if (factor === 'Exento') {
baseCells.push({ text: 'Factor', style: ['tableSmall'] }, { text: factor }, '', '');
}
else {
const importe = traslado.getAttribute('Importe');
baseCells.push({
text: 'Tasa',
style: ['tableSmall'],
}, {
text: traslado.getAttribute('TasaOCuota'),
}, {
text: 'Importe',
style: ['tableSmall'],
}, {
text: formatCurrency(importe === '' ? '0' : importe),
});
}
impuestosTableBody.push(baseCells);
}
for (const retencion of retenciones) {
const baseCells = [];
baseCells.push({ text: 'Retención', color: primaryColor }, {
text: getValueOfCatalog('cfdi40Impuestos', retencion.getAttribute('Impuesto'), catalogs),
}, { text: 'Base', color: primaryColor }, {
text: formatCurrency(retencion.getAttribute('Base')),
alignment: 'right',
});
const factor = retencion.getAttribute('TipoFactor');
if (factor === 'Exento') {
baseCells.push({ text: 'Factor', color: primaryColor }, { text: factor }, '', '');
}
else {
const importe = retencion.getAttribute('Importe');
baseCells.push({
text: 'Tasa',
color: primaryColor,
}, {
text: retencion.getAttribute('TasaOCuota'),
}, {
text: 'Importe',
color: primaryColor,
}, {
text: formatCurrency(importe === '' ? '0' : importe),
});
impuestosTableBody.push(baseCells);
}
}
return {
table: {
widths: ['auto', 'auto', 'auto', 'auto', 'auto', 'auto', 'auto', 'auto'],
body: impuestosTableBody,
},
fillColor: bgGrayColor,
layout: 'conceptosLayout',
};
};
const genericCfdiConceptoImpuestosContent = (concepto, catalogs, primaryColor, bgGrayColor) => {
const impuestoConceptoTable = [];
const predial = concepto.searchNode('cfdi:CuentaPredial');
const codigoSat = concepto.getAttribute('ClaveProdServ');
const objetoImp = concepto.getAttribute('ObjetoImp');
const extraDetails = [
'',
{
text: [
{ text: 'Clave Producto/Servicio: ', color: primaryColor },
{
text: codigoSat,
},
],
},
{
text: objetoImp === ''
? ''
: [
{ text: 'Objeto Impuesto: ', color: primaryColor },
{
text: getValueOfCatalog('cfdi40ObjetosImpuestos', objetoImp, catalogs),
},
],
},
{
text: predial === undefined
? ''
: [
{ text: 'Cuenta Predial: ', color: primaryColor },
{ text: predial.getAttribute('Numero') },
],
},
];
impuestoConceptoTable.push(extraDetails);
const traslados = concepto.searchNodes('cfdi:Impuestos', 'cfdi:Traslados', 'cfdi:Traslado');
const retenciones = concepto.searchNodes('cfdi:Impuestos', 'cfdi:Retenciones', 'cfdi:Retencion');
if (traslados.length > 0 || retenciones.length > 0) {
impuestoConceptoTable.push([
{
...generateImpuestosContentTable(traslados, retenciones, catalogs, primaryColor, bgGrayColor),
colSpan: 4,
},
'',
'',
'',
]);
}
return {
table: {
widths: ['*', 'auto', 'auto', '*'],
body: impuestoConceptoTable,
},
layout: 'conceptosLayout',
};
};
export default genericCfdiConceptoImpuestosContent;