Skip to content

Commit

Permalink
refactor: ♻️ move implocal 10 to complements
Browse files Browse the repository at this point in the history
  • Loading branch information
luffynando committed Aug 4, 2024
1 parent 15f5656 commit 639acee
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 164 deletions.
168 changes: 168 additions & 0 deletions src/templates/complements/implocal10_complement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import { type XmlNodeInterface } from '@nodecfdi/cfdi-core/types';
import { type TableCell } from 'pdfmake/interfaces.js';
import { formatCurrency, toNumber } from '#src/utils/currency';

const useImplocal10Complement = (
impLocal10: XmlNodeInterface,
primaryColor: string,
bgGrayColor: string,
tableTotales: TableCell[][],
totalesContent: TableCell[],
): void => {
const totalesSubContent: TableCell[] = [];
const totalRetencionesLocales = impLocal10.getAttribute('TotaldeRetenciones');
const totalTrasladosLocales = impLocal10.getAttribute('TotaldeTraslados');

if (toNumber(totalTrasladosLocales) > 0) {
tableTotales.push([
{ text: 'Traslados Locales', alignment: 'right' },
{ text: '$', color: primaryColor, alignment: 'center' },
{ text: formatCurrency(totalTrasladosLocales, 'code'), alignment: 'right' },
]);
}

if (toNumber(totalRetencionesLocales) > 0) {
tableTotales.push([
{ text: 'Retenciones Locales', alignment: 'right' },
{ text: '$', color: primaryColor, alignment: 'center' },
{ text: `- ${formatCurrency(totalRetencionesLocales, 'code')}`, alignment: 'right' },
]);
}

const trasladosLocales = impLocal10.searchNodes('implocal:TrasladosLocales');
const retencionesLocales = impLocal10.searchNodes('implocal:RetencionesLocales');

const retencionesLocalesTable: TableCell = {
table: {
widths: ['40%', '20%', 'auto'],
body: [
[
{
text: 'Impuestos Locales Retenidos',
style: ['tableSubtitleHeader'],
color: primaryColor,
colSpan: 3,
},
'',
'',
],
...retencionesLocales.map((retencionLocal) => {
return [
{
text: retencionLocal.getAttribute('ImpLocRetenido'),
fillColor: bgGrayColor,
},
{
text: [
{
text: 'Tasa: ',
},
{
text: `${(Number(retencionLocal.getAttribute('TasadeRetencion')) * 1).toString()} %`,
},
],
fillColor: bgGrayColor,
},
{
text: [
{ text: 'Importe: ' },
{ text: formatCurrency(retencionLocal.getAttribute('Importe')) },
],
alignment: 'right',
fillColor: bgGrayColor,
},
];
}),
],
dontBreakRows: true,
},
layout: 'tableLayout',
};
const trasladosLocalesTable: TableCell = {
table: {
widths: ['40%', '20%', 'auto'],
body: [
[
{
text: 'Impuestos Locales Trasladados',
style: ['tableSubtitleHeader'],
color: primaryColor,
colSpan: 3,
},
'',
'',
],
...trasladosLocales.map((trasladoLocal) => {
return [
{
text: trasladoLocal.getAttribute('ImpLocTrasladado'),
fillColor: bgGrayColor,
},
{
text: [
{
text: 'Tasa: ',
},
{
text: `${(Number(trasladoLocal.getAttribute('TasadeTraslado')) * 1).toString()} %`,
},
],
fillColor: bgGrayColor,
},
{
text: [
{ text: 'Importe: ' },
{ text: formatCurrency(trasladoLocal.getAttribute('Importe')) },
],
alignment: 'right',
fillColor: bgGrayColor,
},
];
}),
],
dontBreakRows: true,
},
layout: 'tableLayout',
};

if (trasladosLocales.length === 0 && retencionesLocales.length === 0) {
totalesSubContent.push('', '');
}

if (trasladosLocales.length === 0 && retencionesLocales.length > 0) {
totalesSubContent.push(
{
...retencionesLocalesTable,
colSpan: 2,
},
'',
);
}

if (trasladosLocales.length > 0 && retencionesLocales.length === 0) {
totalesSubContent.push(
{
...trasladosLocalesTable,
colSpan: 2,
},
'',
);
}

if (trasladosLocales.length > 0 && retencionesLocales.length > 0) {
totalesSubContent.push(trasladosLocalesTable, retencionesLocalesTable);
}

totalesSubContent.push({
table: {
widths: ['*', '10%', 'auto'],
body: tableTotales,
dontBreakRows: true,
},
layout: 'tableLayout',
});

totalesContent.push(totalesSubContent);
};

export default useImplocal10Complement;
166 changes: 2 additions & 164 deletions src/templates/sections/generic_cfdi_totales_content.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { type XmlNodeInterface } from '@nodecfdi/cfdi-core/types';
import { type Content, type TableCell } from 'pdfmake/interfaces.js';
import { getValueOfCatalog } from '#src/catalogs/catalogs_source';
import useImplocal10Complement from '#src/templates/complements/implocal10_complement';
import { type CatalogsData } from '#src/types';
import { formatCurrency, toNumber } from '#src/utils/currency';

Expand Down Expand Up @@ -145,169 +146,6 @@ const fillCfdiImpuestos = (
}
};

const fillComplementoImpLocal10 = (
impLocal10: XmlNodeInterface,
primaryColor: string,
bgGrayColor: string,
tableTotales: TableCell[][],
totalesContent: TableCell[],
): void => {
const totalesSubContent: TableCell[] = [];
const totalRetencionesLocales = impLocal10.getAttribute('TotaldeRetenciones');
const totalTrasladosLocales = impLocal10.getAttribute('TotaldeTraslados');

if (toNumber(totalTrasladosLocales) > 0) {
tableTotales.push([
{ text: 'Traslados Locales', alignment: 'right' },
{ text: '$', color: primaryColor, alignment: 'center' },
{ text: formatCurrency(totalTrasladosLocales, 'code'), alignment: 'right' },
]);
}

if (toNumber(totalRetencionesLocales) > 0) {
tableTotales.push([
{ text: 'Retenciones Locales', alignment: 'right' },
{ text: '$', color: primaryColor, alignment: 'center' },
{ text: `- ${formatCurrency(totalRetencionesLocales, 'code')}`, alignment: 'right' },
]);
}

const trasladosLocales = impLocal10.searchNodes('implocal:TrasladosLocales');
const retencionesLocales = impLocal10.searchNodes('implocal:RetencionesLocales');

const retencionesLocalesTable: TableCell = {
table: {
widths: ['40%', '20%', 'auto'],
body: [
[
{
text: 'Impuestos Locales Retenidos',
style: ['tableSubtitleHeader'],
color: primaryColor,
colSpan: 3,
},
'',
'',
],
...retencionesLocales.map((retencionLocal) => {
return [
{
text: retencionLocal.getAttribute('ImpLocRetenido'),
fillColor: bgGrayColor,
},
{
text: [
{
text: 'Tasa: ',
},
{
text: `${(Number(retencionLocal.getAttribute('TasadeRetencion')) * 1).toString()} %`,
},
],
fillColor: bgGrayColor,
},
{
text: [
{ text: 'Importe: ' },
{ text: formatCurrency(retencionLocal.getAttribute('Importe')) },
],
alignment: 'right',
fillColor: bgGrayColor,
},
];
}),
],
dontBreakRows: true,
},
layout: 'tableLayout',
};
const trasladosLocalesTable: TableCell = {
table: {
widths: ['40%', '20%', 'auto'],
body: [
[
{
text: 'Impuestos Locales Trasladados',
style: ['tableSubtitleHeader'],
color: primaryColor,
colSpan: 3,
},
'',
'',
],
...trasladosLocales.map((trasladoLocal) => {
return [
{
text: trasladoLocal.getAttribute('ImpLocTrasladado'),
fillColor: bgGrayColor,
},
{
text: [
{
text: 'Tasa: ',
},
{
text: `${(Number(trasladoLocal.getAttribute('TasadeTraslado')) * 1).toString()} %`,
},
],
fillColor: bgGrayColor,
},
{
text: [
{ text: 'Importe: ' },
{ text: formatCurrency(trasladoLocal.getAttribute('Importe')) },
],
alignment: 'right',
fillColor: bgGrayColor,
},
];
}),
],
dontBreakRows: true,
},
layout: 'tableLayout',
};

if (trasladosLocales.length === 0 && retencionesLocales.length === 0) {
totalesSubContent.push('', '');
}

if (trasladosLocales.length === 0 && retencionesLocales.length > 0) {
totalesSubContent.push(
{
...retencionesLocalesTable,
colSpan: 2,
},
'',
);
}

if (trasladosLocales.length > 0 && retencionesLocales.length === 0) {
totalesSubContent.push(
{
...trasladosLocalesTable,
colSpan: 2,
},
'',
);
}

if (trasladosLocales.length > 0 && retencionesLocales.length > 0) {
totalesSubContent.push(trasladosLocalesTable, retencionesLocalesTable);
}

totalesSubContent.push({
table: {
widths: ['*', '10%', 'auto'],
body: tableTotales,
dontBreakRows: true,
},
layout: 'tableLayout',
});

totalesContent.push(totalesSubContent);
};

const genericCfdiTotalesContent = (
comprobante: XmlNodeInterface,
catalogs: CatalogsData,
Expand Down Expand Up @@ -362,7 +200,7 @@ const genericCfdiTotalesContent = (
if (impLocal10) {
totalesSubContent.push('');
totalesContent.push(totalesSubContent);
fillComplementoImpLocal10(impLocal10, primaryColor, bgGrayColor, tableTotales, totalesContent);
useImplocal10Complement(impLocal10, primaryColor, bgGrayColor, tableTotales, totalesContent);
} else {
totalesSubContent.push({
table: {
Expand Down

0 comments on commit 639acee

Please sign in to comment.