@disruptive-learning/cfdi-to-pdf
Version:
Librería para crear un pdf basado en un XML CFDI o Retenciones
191 lines (190 loc) • 7.67 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const catalogs_source_1 = require("#src/catalogs/catalogs_source");
const implocal10_complement_1 = __importDefault(require("#src/templates/complements/implocal10_complement"));
const currency_1 = require("#src/utils/currency");
const fillCfdiImpuestos = (comprobante, catalogs, primaryColor, bgGrayColor, totalesSubContent) => {
const elementImpuestos = 'cfdi:Impuestos';
const traslados = comprobante
.searchNodes(elementImpuestos, 'cfdi:Traslados', 'cfdi:Traslado')
.filter((value) => value.getAttribute('TipoFactor') !== 'Exento' &&
(0, currency_1.toNumber)(value.getAttribute('TasaOCuota')) > 0);
const retenciones = comprobante.searchNodes(elementImpuestos, 'cfdi:Retenciones', 'cfdi:Retencion');
const retencionesTable = {
table: {
widths: ['40%', 'auto'],
body: [
[
{
text: 'Impuestos Retenidos',
style: ['tableSubtitleHeader'],
color: primaryColor,
colSpan: 2,
},
'',
],
...retenciones.map((retencion) => {
return [
{
text: (0, catalogs_source_1.getValueOfCatalog)('cfdi40Impuestos', retencion.getAttribute('Impuesto'), catalogs),
fillColor: bgGrayColor,
},
{
text: [
{ text: 'Importe: ' },
{ text: (0, currency_1.formatCurrency)(retencion.getAttribute('Importe')) },
],
alignment: 'right',
fillColor: bgGrayColor,
},
];
}),
],
dontBreakRows: true,
},
layout: 'tableLayout',
};
const trasladosTable = {
table: {
widths: ['10%', '15%', '20%', '18%', 'auto'],
body: [
[
{
text: 'Impuestos Trasladados',
style: ['tableSubtitleHeader'],
colSpan: 5,
},
'',
'',
'',
'',
],
...traslados.map((traslado) => {
const isTasa = traslado.getAttribute('TipoFactor') === 'Tasa';
const tasaOCuota = traslado.getAttribute('TasaOCuota');
return [
{
text: (0, catalogs_source_1.getValueOfCatalog)('cfdi40Impuestos', traslado.getAttribute('Impuesto'), catalogs),
style: ['tableSmall'],
},
{
text: traslado.getAttribute('TipoFactor'),
alignment: 'center',
style: ['tableSmall'],
},
{
text: isTasa ? `${(Number(tasaOCuota) * 100).toString()} %` : tasaOCuota,
alignment: 'center',
style: ['tableSmall'],
},
{ text: 'Importe: ', alignment: 'left', style: ['tableSmall'] },
{
text: (0, currency_1.formatCurrency)(traslado.getAttribute('Importe')),
alignment: 'right',
style: ['tableSmall'],
},
];
}),
],
dontBreakRows: true,
},
layout: 'tableLayout',
};
if (traslados.length === 0 && retenciones.length === 0) {
totalesSubContent.push('', '');
}
if (traslados.length === 0 && retenciones.length > 0) {
totalesSubContent.push({
...retencionesTable,
colSpan: 2,
}, '');
}
if (traslados.length > 0 && retenciones.length === 0) {
totalesSubContent.push({
...trasladosTable,
colSpan: 2,
}, '');
}
if (traslados.length > 0 && retenciones.length > 0) {
totalesSubContent.push(trasladosTable, retencionesTable);
}
};
const genericCfdiTotalesContent = (comprobante, catalogs, primaryColor, bgGrayColor) => {
const elementImpuestos = 'cfdi:Impuestos';
const totalesContent = [];
const totalImpuestosTrasladados = comprobante.searchAttribute(elementImpuestos, 'TotalImpuestosTrasladados');
const totalImpuestosRetenidos = comprobante.searchAttribute(elementImpuestos, 'TotalImpuestosRetenidos');
const totalesSubContent = [];
fillCfdiImpuestos(comprobante, catalogs, primaryColor, bgGrayColor, totalesSubContent);
const tableTotales = [
[
{ text: 'Subtotal', alignment: 'right' },
{ text: '$', color: primaryColor, alignment: 'center' },
{
text: (0, currency_1.formatCurrency)(comprobante.getAttribute('SubTotal'), 'code'),
alignment: 'right',
},
],
[
{ text: 'Descuento', alignment: 'right' },
{ text: '$', color: primaryColor, alignment: 'center' },
{
text: `- ${(0, currency_1.formatCurrency)(comprobante.getAttribute('Descuento'), 'code')}`,
alignment: 'right',
},
],
[
{ text: 'Traslados', alignment: 'right' },
{ text: '$', color: primaryColor, alignment: 'center' },
{ text: (0, currency_1.formatCurrency)(totalImpuestosTrasladados, 'code'), alignment: 'right' },
],
[
{ text: 'Retenciones', alignment: 'right' },
{ text: '$', color: primaryColor, alignment: 'center' },
{ text: `- ${(0, currency_1.formatCurrency)(totalImpuestosRetenidos, 'code')}`, alignment: 'right' },
],
];
const impLocal10 = comprobante.searchNode('cfdi:Complemento', 'implocal:ImpuestosLocales');
if (impLocal10) {
totalesSubContent.push('');
totalesContent.push(totalesSubContent);
(0, implocal10_complement_1.default)(impLocal10, primaryColor, bgGrayColor, tableTotales, totalesContent);
}
else {
totalesSubContent.push({
table: {
widths: ['*', '10%', 'auto'],
body: tableTotales,
dontBreakRows: true,
},
layout: 'tableLayout',
});
totalesContent.push(totalesSubContent);
}
totalesContent.push([
'',
{
style: 'tableHeader',
text: `Total ${comprobante.getAttribute('Moneda')} ${(0, currency_1.formatCurrency)(comprobante.getAttribute('Total'))}`,
alignment: 'right',
margin: [2, 5, 2, 2],
color: primaryColor,
fontSize: 14,
fillColor: bgGrayColor,
colSpan: 2,
},
'',
]);
return {
table: {
widths: ['38%', '28%', '34%'],
body: totalesContent,
dontBreakRows: true,
},
layout: 'tableLayout',
};
};
exports.default = genericCfdiTotalesContent;