feat(server): table sheet pdf

This commit is contained in:
Ahmed Bouhuolia
2024-02-21 14:55:06 +02:00
parent f6f6bc31b6
commit d16ec7cda9
3 changed files with 34 additions and 6 deletions

View File

@@ -20,10 +20,11 @@ export class BalanceSheetMetaInjectable {
const commonMeta = await this.financialSheetMeta.meta(tenantId);
const formattedAsDate = moment(query.toDate).format('YYYY/MM/DD');
const formattedDateRange = `As ${formattedAsDate}`;
const sheetName = 'Balance Sheet Statement';
return {
...commonMeta,
sheetName: 'Balance Sheet',
sheetName,
formattedAsDate,
formattedDateRange,
};

View File

@@ -4,6 +4,7 @@ import { ITableColumn, ITableData, ITableRow } from '@/interfaces';
import { ChromiumlyTenancy } from '@/services/ChromiumlyTenancy/ChromiumlyTenancy';
import { TemplateInjectable } from '@/services/TemplateInjectable/TemplateInjectable';
import { FinancialTableStructure } from './FinancialTableStructure';
import { tableClassNames } from './utils';
@Service()
export class TableSheetPdf {
@@ -25,21 +26,30 @@ export class TableSheetPdf {
tenantId: number,
table: ITableData,
sheetName: string,
sheetDate: string
sheetDate: string,
customCSS?: string
): Promise<Buffer> {
// Prepare columns and rows for PDF conversion
const columns = this.tablePdfColumns(table.columns);
const rows = this.tablePdfRows(table.rows);
const landscape = columns.length > 4;
// Generate HTML content from the template
const htmlContent = await this.templateInjectable.render(
tenantId,
'modules/financial-sheet',
{ sheetName, sheetDate, table: { rows, columns } }
{
table: { rows, columns },
sheetName,
sheetDate,
customCSS,
}
);
// Convert the HTML content to PDF
return this.chromiumlyTenancy.convertHtmlContent(tenantId, htmlContent, {
margins: { top: 0, bottom: 0, left: 0, right: 0 },
landscape,
});
}
/**
@@ -63,6 +73,6 @@ export class TableSheetPdf {
const flatNestedTree = curriedFlatNestedTree(R.__, {
nestedPrefix: '<span style="padding-left: 15px;"></span>',
});
return R.compose(flatNestedTree)(rows);
return R.compose(tableClassNames, flatNestedTree)(rows);
};
}

View File

@@ -1,4 +1,5 @@
import { kebabCase } from 'lodash';
import { ITableRow } from '@/interfaces';
export const formatNumber = (balance, { noCents, divideOn1000 }): string => {
let formattedBalance: number = parseFloat(balance);
@@ -10,4 +11,20 @@ export const formatNumber = (balance, { noCents, divideOn1000 }): string => {
formattedBalance /= 1000;
}
return formattedBalance;
};
};
export const tableClassNames = (rows: ITableRow[]) => {
return rows.map((row) => {
const classNames =
row?.rowTypes?.map((rowType) => `row-type--${kebabCase(rowType)}`) || [];
if (row.id) {
classNames.push(`row-id--${kebabCase(row.id)}`);
}
return {
...row,
classNames,
};
});
};