mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-19 06:10:31 +00:00
feat: export reports csv and xlsx (#286)
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { CustomerBalanceSummaryExportInjectable } from './CustomerBalanceSummaryExportInjectable';
|
||||
import { CustomerBalanceSummaryTableInjectable } from './CustomerBalanceSummaryTableInjectable';
|
||||
import { ICustomerBalanceSummaryQuery } from '@/interfaces';
|
||||
import { CustomerBalanceSummaryService } from './CustomerBalanceSummaryService';
|
||||
|
||||
@Service()
|
||||
export class CustomerBalanceSummaryApplication {
|
||||
@Inject()
|
||||
private customerBalanceSummaryTable: CustomerBalanceSummaryTableInjectable;
|
||||
|
||||
@Inject()
|
||||
private customerBalanceSummaryExport: CustomerBalanceSummaryExportInjectable;
|
||||
|
||||
@Inject()
|
||||
private customerBalanceSummarySheet: CustomerBalanceSummaryService;
|
||||
|
||||
/**
|
||||
* Retrieves the customer balance sheet in json format.
|
||||
* @param {number} tenantId
|
||||
* @param {ICustomerBalanceSummaryQuery} query
|
||||
* @returns {Promise<ICustomerBalanceSummarySheet>}
|
||||
*/
|
||||
public sheet(tenantId: number, query: ICustomerBalanceSummaryQuery) {
|
||||
return this.customerBalanceSummarySheet.customerBalanceSummary(
|
||||
tenantId,
|
||||
query
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the customer balance sheet in json format.
|
||||
* @param {number} tenantId
|
||||
* @param {ICustomerBalanceSummaryQuery} query
|
||||
* @returns {Promise<ICustomerBalanceSummaryTable>}
|
||||
*/
|
||||
public table(tenantId: number, query: ICustomerBalanceSummaryQuery) {
|
||||
return this.customerBalanceSummaryTable.table(tenantId, query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the customer balance sheet in XLSX format.
|
||||
* @param {number} tenantId
|
||||
* @param {ICustomerBalanceSummaryQuery} query
|
||||
* @returns {Promise<Buffer>}
|
||||
*/
|
||||
public xlsx(tenantId: number, query: ICustomerBalanceSummaryQuery) {
|
||||
return this.customerBalanceSummaryExport.xlsx(tenantId, query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the customer balance sheet in CSV format.
|
||||
* @param {number} tenantId
|
||||
* @param {ICustomerBalanceSummaryQuery} query
|
||||
* @returns {Promise<Buffer>}
|
||||
*/
|
||||
public csv(tenantId: number, query: ICustomerBalanceSummaryQuery) {
|
||||
return this.customerBalanceSummaryExport.csv(tenantId, query);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { ICustomerBalanceSummaryQuery } from '@/interfaces';
|
||||
import { CustomerBalanceSummaryTableInjectable } from './CustomerBalanceSummaryTableInjectable';
|
||||
import { TableSheet } from '@/lib/Xlsx/TableSheet';
|
||||
|
||||
@Service()
|
||||
export class CustomerBalanceSummaryExportInjectable {
|
||||
@Inject()
|
||||
private customerBalanceSummaryTable: CustomerBalanceSummaryTableInjectable;
|
||||
|
||||
/**
|
||||
* Retrieves the cashflow sheet in XLSX format.
|
||||
* @param {number} tenantId
|
||||
* @param {ICustomerBalanceSummaryQuery} query
|
||||
* @returns {Promise<Buffer>}
|
||||
*/
|
||||
public async xlsx(tenantId: number, query: ICustomerBalanceSummaryQuery) {
|
||||
const table = await this.customerBalanceSummaryTable.table(tenantId, query);
|
||||
|
||||
const tableSheet = new TableSheet(table.table);
|
||||
const tableCsv = tableSheet.convertToXLSX();
|
||||
|
||||
return tableSheet.convertToBuffer(tableCsv, 'xlsx');
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the cashflow sheet in CSV format.
|
||||
* @param {number} tenantId
|
||||
* @param {ICustomerBalanceSummaryQuery} query
|
||||
* @returns {Promise<Buffer>}
|
||||
*/
|
||||
public async csv(
|
||||
tenantId: number,
|
||||
query: ICustomerBalanceSummaryQuery
|
||||
): Promise<string> {
|
||||
const table = await this.customerBalanceSummaryTable.table(tenantId, query);
|
||||
|
||||
const tableSheet = new TableSheet(table.table);
|
||||
const tableCsv = tableSheet.convertToCSV();
|
||||
|
||||
return tableCsv;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
import { Inject } from 'typedi';
|
||||
import moment from 'moment';
|
||||
import { isEmpty, map } from 'lodash';
|
||||
import TenancyService from '@/services/Tenancy/TenancyService';
|
||||
import * as R from 'ramda';
|
||||
import {
|
||||
ICustomerBalanceSummaryService,
|
||||
@@ -11,28 +9,21 @@ import {
|
||||
ILedgerEntry,
|
||||
} from '@/interfaces';
|
||||
import { CustomerBalanceSummaryReport } from './CustomerBalanceSummary';
|
||||
|
||||
import Ledger from '@/services/Accounting/Ledger';
|
||||
import CustomerBalanceSummaryRepository from './CustomerBalanceSummaryRepository';
|
||||
import { Tenant } from '@/system/models';
|
||||
|
||||
export default class CustomerBalanceSummaryService
|
||||
export class CustomerBalanceSummaryService
|
||||
implements ICustomerBalanceSummaryService
|
||||
{
|
||||
@Inject()
|
||||
tenancy: TenancyService;
|
||||
|
||||
@Inject('logger')
|
||||
logger: any;
|
||||
|
||||
@Inject()
|
||||
reportRepository: CustomerBalanceSummaryRepository;
|
||||
private reportRepository: CustomerBalanceSummaryRepository;
|
||||
|
||||
/**
|
||||
* Defaults balance sheet filter query.
|
||||
* @return {ICustomerBalanceSummaryQuery}
|
||||
*/
|
||||
get defaultQuery(): ICustomerBalanceSummaryQuery {
|
||||
private get defaultQuery(): ICustomerBalanceSummaryQuery {
|
||||
return {
|
||||
asDate: moment().format('YYYY-MM-DD'),
|
||||
numberFormat: {
|
||||
@@ -43,13 +34,12 @@ export default class CustomerBalanceSummaryService
|
||||
negativeFormat: 'mines',
|
||||
},
|
||||
percentageColumn: false,
|
||||
|
||||
|
||||
noneZero: false,
|
||||
noneTransactions: true,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieve the customers ledger entries mapped from accounts transactions.
|
||||
* @param {number} tenantId
|
||||
@@ -75,7 +65,7 @@ export default class CustomerBalanceSummaryService
|
||||
* @param {ICustomerBalanceSummaryQuery} query
|
||||
* @return {Promise<ICustomerBalanceSummaryStatement>}
|
||||
*/
|
||||
async customerBalanceSummary(
|
||||
public async customerBalanceSummary(
|
||||
tenantId: number,
|
||||
query: ICustomerBalanceSummaryQuery
|
||||
): Promise<ICustomerBalanceSummaryStatement> {
|
||||
@@ -86,13 +76,6 @@ export default class CustomerBalanceSummaryService
|
||||
// Merges the default query and request query.
|
||||
const filter = { ...this.defaultQuery, ...query };
|
||||
|
||||
this.logger.info(
|
||||
'[customer_balance_summary] trying to calculate the report.',
|
||||
{
|
||||
filter,
|
||||
tenantId,
|
||||
}
|
||||
);
|
||||
// Retrieve the customers list ordered by the display name.
|
||||
const customers = await this.reportRepository.getCustomers(
|
||||
tenantId,
|
||||
@@ -111,7 +94,7 @@ export default class CustomerBalanceSummaryService
|
||||
ledger,
|
||||
customers,
|
||||
filter,
|
||||
tenant.metadata.baseCurrency,
|
||||
tenant.metadata.baseCurrency
|
||||
);
|
||||
|
||||
return {
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { CustomerBalanceSummaryService } from './CustomerBalanceSummaryService';
|
||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
||||
import {
|
||||
ICustomerBalanceSummaryQuery,
|
||||
ICustomerBalanceSummaryTable,
|
||||
} from '@/interfaces';
|
||||
import { CustomerBalanceSummaryTable } from './CustomerBalanceSummaryTableRows';
|
||||
|
||||
@Service()
|
||||
export class CustomerBalanceSummaryTableInjectable {
|
||||
@Inject()
|
||||
private customerBalanceSummaryService: CustomerBalanceSummaryService;
|
||||
|
||||
@Inject()
|
||||
private tenancy: HasTenancyService;
|
||||
|
||||
/**
|
||||
* Retrieves the customer balance sheet in table format.
|
||||
* @param {number} tenantId
|
||||
* @param {ICustomerBalanceSummaryQuery} filter
|
||||
* @returns {Promise<ICustomerBalanceSummaryTable>}
|
||||
*/
|
||||
public async table(
|
||||
tenantId: number,
|
||||
filter: ICustomerBalanceSummaryQuery
|
||||
): Promise<ICustomerBalanceSummaryTable> {
|
||||
const i18n = this.tenancy.i18n(tenantId);
|
||||
const { data, query } =
|
||||
await this.customerBalanceSummaryService.customerBalanceSummary(
|
||||
tenantId,
|
||||
filter
|
||||
);
|
||||
const tableRows = new CustomerBalanceSummaryTable(data, filter, i18n);
|
||||
|
||||
return {
|
||||
table: {
|
||||
columns: tableRows.tableColumns(),
|
||||
rows: tableRows.tableRows(),
|
||||
},
|
||||
query,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,7 @@ enum TABLE_ROWS_TYPES {
|
||||
TOTAL = 'TOTAL',
|
||||
}
|
||||
|
||||
export default class CustomerBalanceSummaryTable {
|
||||
export class CustomerBalanceSummaryTable {
|
||||
report: ICustomerBalanceSummaryData;
|
||||
query: ICustomerBalanceSummaryQuery;
|
||||
i18n: any;
|
||||
|
||||
Reference in New Issue
Block a user