feat: export reports csv and xlsx (#286)

This commit is contained in:
Ahmed Bouhuolia
2023-11-28 19:53:13 +02:00
committed by GitHub
parent 151aff4c8e
commit d15c5890ed
125 changed files with 4674 additions and 934 deletions

View File

@@ -0,0 +1,60 @@
import { Inject, Service } from 'typedi';
import { ProfitLossSheetExportInjectable } from './ProfitLossSheetExportInjectable';
import { ProfitLossSheetTableInjectable } from './ProfitLossSheetTableInjectable';
import { IProfitLossSheetQuery, IProfitLossSheetTable } from '@/interfaces';
import ProfitLossSheetService from './ProfitLossSheetService';
@Service()
export class ProfitLossSheetApplication {
@Inject()
private profitLossTable: ProfitLossSheetTableInjectable;
@Inject()
private profitLossExport: ProfitLossSheetExportInjectable;
@Inject()
private profitLossSheet: ProfitLossSheetService;
/**
* Retreives the profit/loss sheet.
* @param {number} tenantId
* @param {IProfitLossSheetQuery} query
* @returns {}
*/
public sheet(tenantId: number, query: IProfitLossSheetQuery) {
return this.profitLossSheet.profitLossSheet(tenantId, query);
}
/**
* Retrieves the profit/loss sheet table format.
* @param {number} tenantId
* @param {IProfitLossSheetQuery} query
* @returns {Promise<IProfitLossSheetTable>}
*/
public table(
tenantId: number,
query: IProfitLossSheetQuery
): Promise<IProfitLossSheetTable> {
return this.profitLossTable.table(tenantId, query);
}
/**
* Retrieves the profit/loss sheet in csv format.
* @param {number} tenantId
* @param {IProfitLossSheetQuery} query
* @returns {Promise<string>}
*/
public csv(tenantId: number, query: IProfitLossSheetQuery): Promise<string> {
return this.profitLossExport.csv(tenantId, query);
}
/**
* Retrieves the profit/loss sheet in xlsx format.
* @param {number} tenantId
* @param {IProfitLossSheetQuery} query
* @returns {Promise<Buffer>}
*/
public xlsx(tenantId: number, query: IProfitLossSheetQuery): Promise<Buffer> {
return this.profitLossExport.xlsx(tenantId, query);
}
}

View File

@@ -0,0 +1,43 @@
import { Inject, Service } from 'typedi';
import { IProfitLossSheetQuery } from '@/interfaces';
import { TableSheet } from '@/lib/Xlsx/TableSheet';
import { ProfitLossSheetTableInjectable } from './ProfitLossSheetTableInjectable';
@Service()
export class ProfitLossSheetExportInjectable {
@Inject()
private profitLossSheetTable: ProfitLossSheetTableInjectable;
/**
* Retrieves the profit/loss sheet in XLSX format.
* @param {number} tenantId
* @param {IProfitLossSheetQuery} query
* @returns {Promise<Buffer>}
*/
public async xlsx(tenantId: number, query: IProfitLossSheetQuery) {
const table = await this.profitLossSheetTable.table(tenantId, query);
const tableSheet = new TableSheet(table.table);
const tableCsv = tableSheet.convertToXLSX();
return tableSheet.convertToBuffer(tableCsv, 'xlsx');
}
/**
* Retrieves the profit/loss sheet in CSV format.
* @param {number} tenantId
* @param {IProfitLossSheetQuery} query
* @returns {Promise<Buffer>}
*/
public async csv(
tenantId: number,
query: IProfitLossSheetQuery
): Promise<string> {
const table = await this.profitLossSheetTable.table(tenantId, query);
const tableSheet = new TableSheet(table.table);
const tableCsv = tableSheet.convertToCSV();
return tableCsv;
}
}

View File

@@ -16,39 +16,10 @@ import { ProfitLossSheetRepository } from './ProfitLossSheetRepository';
@Service()
export default class ProfitLossSheetService {
@Inject()
tenancy: TenancyService;
@Inject('logger')
logger: any;
private tenancy: TenancyService;
@Inject()
inventoryService: InventoryService;
/**
* Retrieve the trial balance sheet meta.
* @param {number} tenantId - Tenant id.
* @returns {ITrialBalanceSheetMeta}
*/
reportMetadata(tenantId: number): IProfitLossSheetMeta {
const settings = this.tenancy.settings(tenantId);
const isCostComputeRunning =
this.inventoryService.isItemsCostComputeRunning(tenantId);
const organizationName = settings.get({
group: 'organization',
key: 'name',
});
const baseCurrency = settings.get({
group: 'organization',
key: 'base_currency',
});
return {
isCostComputeRunning: parseBoolean(isCostComputeRunning, false),
organizationName,
baseCurrency,
};
}
private inventoryService: InventoryService;
/**
* Retrieve profit/loss sheet statement.
@@ -56,7 +27,7 @@ export default class ProfitLossSheetService {
* @param {IProfitLossSheetQuery} query
* @return { }
*/
profitLossSheet = async (
public profitLossSheet = async (
tenantId: number,
query: IProfitLossSheetQuery
): Promise<{
@@ -70,13 +41,6 @@ export default class ProfitLossSheetService {
// Merges the given query with default filter query.
const filter = mergeQueryWithDefaults(query);
// Get the given accounts or throw not found service error.
// if (filter.accountsIds.length > 0) {
// await this.accountsService.getAccountsOrThrowError(
// tenantId,
// filter.accountsIds
// );
// }
const tenant = await Tenant.query()
.findById(tenantId)
.withGraphFetched('metadata');
@@ -101,4 +65,30 @@ export default class ProfitLossSheetService {
meta: this.reportMetadata(tenantId),
};
};
/**
* Retrieve the trial balance sheet meta.
* @param {number} tenantId - Tenant id.
* @returns {ITrialBalanceSheetMeta}
*/
private reportMetadata(tenantId: number): IProfitLossSheetMeta {
const settings = this.tenancy.settings(tenantId);
const isCostComputeRunning =
this.inventoryService.isItemsCostComputeRunning(tenantId);
const organizationName = settings.get({
group: 'organization',
key: 'name',
});
const baseCurrency = settings.get({
group: 'organization',
key: 'base_currency',
});
return {
isCostComputeRunning: parseBoolean(isCostComputeRunning, false),
organizationName,
baseCurrency,
};
}
}

View File

@@ -0,0 +1,42 @@
import { Inject, Service } from 'typedi';
import ProfitLossSheetService from './ProfitLossSheetService';
import { ProfitLossSheetTable } from './ProfitLossSheetTable';
import { IProfitLossSheetQuery, IProfitLossSheetTable } from '@/interfaces';
import HasTenancyService from '@/services/Tenancy/TenancyService';
@Service()
export class ProfitLossSheetTableInjectable {
@Inject()
private profitLossSheet: ProfitLossSheetService;
@Inject()
private tenancy: HasTenancyService;
/**
* Retrieves the profit/loss sheet in table format.
* @param {number} tenantId
* @param {IProfitLossSheetQuery} filter
* @returns {Promise<IProfitLossSheetTable>}
*/
public async table(
tenantId: number,
filter: IProfitLossSheetQuery
): Promise<IProfitLossSheetTable> {
const i18n = this.tenancy.i18n(tenantId);
const { data, query, meta } = await this.profitLossSheet.profitLossSheet(
tenantId,
filter
);
const table = new ProfitLossSheetTable(data, query, i18n);
return {
table: {
rows: table.tableRows(),
columns: table.tableColumns(),
},
query,
meta,
};
}
}