feat: format amount rows of financial statemnets.

This commit is contained in:
a.bouhuolia
2021-01-12 11:45:36 +02:00
parent 463c748717
commit 7680150a31
27 changed files with 392 additions and 208 deletions

View File

@@ -15,14 +15,17 @@ export default class PayableAgingSummaryService {
/**
* Default report query.
*/
get defaultQuery() {
get defaultQuery(): IAPAgingSummaryQuery {
return {
asDate: moment().format('YYYY-MM-DD'),
agingDaysBefore: 30,
agingPeriods: 3,
numberFormat: {
noCents: false,
precision: 2,
divideOn1000: false,
showZero: false,
formatMoney: 'total',
negativeFormat: 'mines'
},
vendorsIds: [],
noneZero: false,

View File

@@ -15,14 +15,17 @@ export default class ARAgingSummaryService {
/**
* Default report query.
*/
get defaultQuery() {
get defaultQuery(): IARAgingSummaryQuery {
return {
asDate: moment().format('YYYY-MM-DD'),
agingDaysBefore: 30,
agingPeriods: 3,
numberFormat: {
no_cents: false,
divide_1000: false,
divideOn1000: false,
negativeFormat: 'mines',
showZero: false,
formatMoney: 'total',
precision: 2,
},
customersIds: [],
noneZero: false,
@@ -50,6 +53,7 @@ export default class ARAgingSummaryService {
});
// Settings tenant service.
const settings = this.tenancy.settings(tenantId);
const baseCurrency = settings.get({
group: 'organization',
key: 'base_currency',

View File

@@ -68,7 +68,7 @@ export default class ARAgingSummarySheet extends AgingSummaryReport {
return {
customerName: customer.displayName,
current: this.formatTotalAmount(currentTotal),
current: this.formatAmount(currentTotal),
aging: agingPeriods,
total: this.formatTotalAmount(amount),
};

View File

@@ -7,6 +7,7 @@ import {
IARAgingSummaryCustomer,
IContact,
IARAgingSummaryQuery,
IFormatNumberSettings,
} from 'interfaces';
import AgingReport from './AgingReport';
import { Dictionary } from 'tsyringe/dist/typings/types';
@@ -30,7 +31,7 @@ export default abstract class AgingSummaryReport extends AgingReport {
protected getInitialAgingPeriodsTotal() {
return this.agingPeriods.map((agingPeriod) => ({
...agingPeriod,
...this.formatTotalAmount(0),
...this.formatAmount(0),
}));
}
@@ -70,12 +71,15 @@ export default abstract class AgingSummaryReport extends AgingReport {
const isInAgingPeriod =
agingPeriod.beforeDays <= overdueDays &&
(agingPeriod.toDays > overdueDays || !agingPeriod.toDays);
const total = isInAgingPeriod
? agingPeriod.total + dueAmount
: agingPeriod.total;
return {
...agingPeriod,
total: isInAgingPeriod
? agingPeriod.total + dueAmount
: agingPeriod.total,
total,
formattedAmount: this.formatAmount(total),
};
});
return newAgingPeriods;
@@ -86,14 +90,28 @@ export default abstract class AgingSummaryReport extends AgingReport {
* @param {number} amount
* @return {IAgingPeriodTotal}
*/
protected formatTotalAmount(amount: number): IAgingPeriodTotal {
protected formatAmount(
amount: number,
settings: IFormatNumberSettings = {}
): IAgingPeriodTotal {
return {
total: amount,
formattedTotal: this.formatNumber(amount),
formattedTotal: this.formatNumber(amount, settings),
currencyCode: this.baseCurrency,
};
}
protected formatTotalAmount(
amount: number,
settings: IFormatNumberSettings = {}
): IAgingPeriodTotal {
return this.formatAmount(amount, {
money: true,
excerptZero: false,
...settings
});
}
/**
* Calculates the total of the aging period by the given index.
* @param {number} index
@@ -142,7 +160,7 @@ export default abstract class AgingSummaryReport extends AgingReport {
/**
* Retrieve the current invoices by the given contact id.
* @param {number} contactId
* @param {number} contactId - Specific contact id.
* @return {(ISaleInvoice | IBill)[]}
*/
protected getCurrentInvoicesByContactId(
@@ -153,23 +171,23 @@ export default abstract class AgingSummaryReport extends AgingReport {
/**
* Retrieve the contact total due amount.
* @param {number} contactId
* @param {number} contactId - Specific contact id.
* @return {number}
*/
protected getContactCurrentTotal(contactId: number): number {
const currentInvoices = this.getCurrentInvoicesByContactId(contactId);
return sumBy(currentInvoices, invoice => invoice.dueAmount);
return sumBy(currentInvoices, (invoice) => invoice.dueAmount);
}
/**
* Retrieve to total sumation of the given customers sections.
* @param {IARAgingSummaryCustomer[]} contactsSections -
* @param {IARAgingSummaryCustomer[]} contactsSections -
* @return {number}
*/
protected getTotalCurrent(
customersSummary: IARAgingSummaryCustomer[]
): number {
return sumBy(customersSummary, summary => summary.current.total);
return sumBy(customersSummary, (summary) => summary.current.total);
}
/**
@@ -177,9 +195,7 @@ export default abstract class AgingSummaryReport extends AgingReport {
* @param {IAgingPeriodTotal[]} agingPeriods
* @return {number}
*/
protected getAgingPeriodsTotal(
agingPeriods: IAgingPeriodTotal[],
): number {
protected getAgingPeriodsTotal(agingPeriods: IAgingPeriodTotal[]): number {
return sumBy(agingPeriods, 'total');
}
}

View File

@@ -73,7 +73,7 @@ export default class BalanceSheetStatement extends FinancialSheet {
sections: IBalanceSheetSection[]
): IBalanceSheetAccountTotal {
const amount = sumBy(sections, 'total.amount');
const formattedAmount = this.formatNumber(amount);
const formattedAmount = this.formatTotalNumber(amount);
const currencyCode = this.baseCurrency;
return { amount, formattedAmount, currencyCode };
@@ -89,7 +89,25 @@ export default class BalanceSheetStatement extends FinancialSheet {
): IBalanceSheetAccountTotal[] {
return this.dateRangeSet.map((date, index) => {
const amount = sumBy(sections, `totalPeriods[${index}].amount`);
const formattedAmount = this.formatNumber(amount);
const formattedAmount = this.formatTotalNumber(amount);
const currencyCode = this.baseCurrency;
return { date, amount, formattedAmount, currencyCode };
});
}
/**
* Retrieve accounts total periods.
* @param {Array<IBalanceSheetAccount>} accounts -
* @return {IBalanceSheetAccountTotal[]}
*/
private getAccountsTotalPeriods(
accounts: Array<IBalanceSheetAccount>
): IBalanceSheetAccountTotal[] {
return this.dateRangeSet.map((date, index) => {
const amount = sumBy(accounts, `totalPeriods[${index}].amount`);
const formattedAmount = this.formatNumber(amount)
const currencyCode = this.baseCurrency;
return { date, amount, formattedAmount, currencyCode };
@@ -190,12 +208,12 @@ export default class BalanceSheetStatement extends FinancialSheet {
}),
total: {
amount: totalAmount,
formattedAmount: this.formatNumber(totalAmount),
formattedAmount: this.formatTotalNumber(totalAmount),
currencyCode: this.baseCurrency,
},
...(this.query.displayColumnsType === 'date_periods'
? {
totalPeriods: this.getSectionTotalPeriods(filteredAccounts),
totalPeriods: this.getAccountsTotalPeriods(filteredAccounts),
}
: {}),
};
@@ -232,7 +250,7 @@ export default class BalanceSheetStatement extends FinancialSheet {
*/
private balanceSheetStructureMapper(
structure: IBalanceSheetStructureSection,
accounts: IAccount & { type: IAccountType }[]
accounts: IAccount & { type: IAccountType }[],
): IBalanceSheetSection {
const result = {
name: structure.name,
@@ -276,14 +294,9 @@ export default class BalanceSheetStatement extends FinancialSheet {
}
)
// Mappes the balance sheet scetions only
.map(
([sheetSection, structure]: [
IBalanceSheetSection,
IBalanceSheetStructureSection
]) => {
return sheetSection;
}
)
.map(([sheetSection]: [IBalanceSheetSection]) => {
return sheetSection;
})
);
}

View File

@@ -29,8 +29,11 @@ export default class BalanceSheetStatementService
fromDate: moment().startOf('year').format('YYYY-MM-DD'),
toDate: moment().endOf('year').format('YYYY-MM-DD'),
numberFormat: {
noCents: false,
precision: 2,
divideOn1000: false,
showZero: false,
formatMoney: 'total',
negativeFormat: 'mines'
},
noneZero: false,
noneTransactions: false,

View File

@@ -187,7 +187,7 @@ export default class ProfitLossSheet extends FinancialSheet {
accounts: IProfitLossSheetAccount[]
): IProfitLossSheetTotal {
const amount = sumBy(accounts, 'total.amount');
const formattedAmount = this.formatNumber(amount);
const formattedAmount = this.formatTotalNumber(amount);
const currencyCode = this.baseCurrency;
return { amount, formattedAmount, currencyCode };
@@ -203,7 +203,7 @@ export default class ProfitLossSheet extends FinancialSheet {
): IProfitLossSheetTotal[] {
return this.dateRangeSet.map((date, index) => {
const amount = sumBy(accounts, `totalPeriods[${index}].amount`);
const formattedAmount = this.formatNumber(amount);
const formattedAmount = this.formatTotalNumber(amount);
const currencyCode = this.baseCurrency;
return { amount, formattedAmount, currencyCode };
@@ -229,7 +229,7 @@ export default class ProfitLossSheet extends FinancialSheet {
*/
private get incomeSection(): IProfitLossSheetAccountsSection {
return {
sectionTitle: 'Income accounts',
name: 'Income accounts',
entryNormal: 'credit',
...this.sectionMapper(this.incomeAccounts),
};
@@ -241,7 +241,7 @@ export default class ProfitLossSheet extends FinancialSheet {
*/
private get expensesSection(): IProfitLossSheetAccountsSection {
return {
sectionTitle: 'Expense accounts',
name: 'Expense accounts',
entryNormal: 'debit',
...this.sectionMapper(this.expensesAccounts),
};
@@ -253,7 +253,7 @@ export default class ProfitLossSheet extends FinancialSheet {
*/
private get otherExpensesSection(): IProfitLossSheetAccountsSection {
return {
sectionTitle: 'Other expenses accounts',
name: 'Other expenses accounts',
entryNormal: 'debit',
...this.sectionMapper(this.otherExpensesAccounts),
};
@@ -265,7 +265,7 @@ export default class ProfitLossSheet extends FinancialSheet {
*/
private get costOfSalesSection(): IProfitLossSheetAccountsSection {
return {
sectionTitle: 'Cost of sales',
name: 'Cost of sales',
entryNormal: 'debit',
...this.sectionMapper(this.costOfSalesAccounts),
};
@@ -283,7 +283,7 @@ export default class ProfitLossSheet extends FinancialSheet {
const totalMines = sumBy(minesSections, `totalPeriods[${index}].amount`);
const amount = totalPositive - totalMines;
const formattedAmount = this.formatNumber(amount);
const formattedAmount = this.formatTotalNumber(amount);
const currencyCode = this.baseCurrency;
return { date, amount, formattedAmount, currencyCode };
@@ -298,7 +298,7 @@ export default class ProfitLossSheet extends FinancialSheet {
const totalMinesSections = sumBy(minesSections, 'total.amount');
const amount = totalPositiveSections - totalMinesSections;
const formattedAmount = this.formatNumber(amount);
const formattedAmount = this.formatTotalNumber(amount);
const currencyCode = this.baseCurrency;
return { amount, formattedAmount, currencyCode };

View File

@@ -27,8 +27,11 @@ export default class ProfitLossSheetService {
fromDate: moment().startOf('year').format('YYYY-MM-DD'),
toDate: moment().endOf('year').format('YYYY-MM-DD'),
numberFormat: {
noCents: false,
divideOn1000: false,
negativeFormat: 'mines',
showZero: false,
formatMoney: 'total',
precision: 2,
},
basis: 'accural',
noneZero: false,
@@ -41,8 +44,8 @@ export default class ProfitLossSheetService {
/**
* Retrieve profit/loss sheet statement.
* @param {number} tenantId
* @param {IProfitLossSheetQuery} query
* @param {number} tenantId
* @param {IProfitLossSheetQuery} query
* @return { }
*/
async profitLossSheet(tenantId: number, query: IProfitLossSheetQuery) {
@@ -55,16 +58,24 @@ export default class ProfitLossSheetService {
...this.defaultQuery,
...query,
};
this.logger.info('[profit_loss_sheet] trying to calculate the report.', { tenantId, filter });
this.logger.info('[profit_loss_sheet] trying to calculate the report.', {
tenantId,
filter,
});
// Get the given accounts or throw not found service error.
if (filter.accountsIds.length > 0) {
await this.accountsService.getAccountsOrThrowError(tenantId, filter.accountsIds);
await this.accountsService.getAccountsOrThrowError(
tenantId,
filter.accountsIds
);
}
// Settings tenant service.
const settings = this.tenancy.settings(tenantId);
const baseCurrency = settings.get({ group: 'organization', key: 'base_currency' });
const baseCurrency = settings.get({
group: 'organization',
key: 'base_currency',
});
// Retrieve all accounts on the storage.
const accounts = await accountRepository.all('type');
const accountsGraph = await accountRepository.getDependencyGraph();
@@ -75,8 +86,11 @@ export default class ProfitLossSheetService {
toDate: query.toDate,
});
// Transform transactions to journal collection.
const transactionsJournal = Journal.fromTransactions(transactions, tenantId, accountsGraph);
const transactionsJournal = Journal.fromTransactions(
transactions,
tenantId,
accountsGraph
);
// Profit/Loss report instance.
const profitLossInstance = new ProfitLossSheet(
tenantId,
@@ -95,4 +109,4 @@ export default class ProfitLossSheetService {
query: filter,
};
}
}
}

View File

@@ -1,12 +1,19 @@
import { sumBy } from 'lodash';
import {
ITrialBalanceSheetQuery,
ITrialBalanceAccount,
IAccount,
ITrialBalanceTotal,
IAccountType,
} from 'interfaces';
import FinancialSheet from '../FinancialSheet';
import { flatToNestedArray } from 'utils';
const AMOUNT_TYPE = {
TOTAL: 'TOTAL',
SECTION_TOTAL: 'SECTION_TOTAL',
};
export default class TrialBalanceSheet extends FinancialSheet {
tenantId: number;
query: ITrialBalanceSheetQuery;
@@ -103,10 +110,37 @@ export default class TrialBalanceSheet extends FinancialSheet {
});
}
/**
* Retrieve trial balance total section.
* @param {ITrialBalanceAccount[]} accountsBalances
* @return {ITrialBalanceTotal}
*/
private tatalSection(
accountsBalances: ITrialBalanceAccount[]
): ITrialBalanceTotal {
const credit = sumBy(accountsBalances, 'credit');
const debit = sumBy(accountsBalances, 'debit');
const balance = sumBy(accountsBalances, 'balance');
const currencyCode = this.baseCurrency;
return {
credit,
debit,
balance,
currencyCode,
formattedCredit: this.formatTotalNumber(credit),
formattedDebit: this.formatTotalNumber(debit),
formattedBalance: this.formatTotalNumber(balance),
};
}
/**
* Retrieve trial balance sheet statement data.
*/
public reportData() {
return this.accountsWalker(this.accounts);
const accounts = this.accountsWalker(this.accounts);
const total = this.tatalSection(accounts);
return { accounts, total };
}
}

View File

@@ -1,12 +1,13 @@
import { Service, Inject } from 'typedi';
import moment from 'moment';
import TenancyService from 'services/Tenancy/TenancyService';
import { ITrialBalanceSheetQuery, ITrialBalanceStatement } from 'interfaces';
import TrialBalanceSheet from './TrialBalanceSheet';
import Journal from 'services/Accounting/JournalPoster';
import { INumberFormatQuery, ITrialBalanceSheetQuery, ITrialBalanceStatement } from 'interfaces';
import TrialBalanceSheet from './TrialBalanceSheet';
import FinancialSheet from '../FinancialSheet';
@Service()
export default class TrialBalanceSheetService {
export default class TrialBalanceSheetService extends FinancialSheet {
@Inject()
tenancy: TenancyService;
@@ -22,8 +23,11 @@ export default class TrialBalanceSheetService {
fromDate: moment().startOf('year').format('YYYY-MM-DD'),
toDate: moment().endOf('year').format('YYYY-MM-DD'),
numberFormat: {
noCents: false,
divideOn1000: false,
negativeFormat: 'mines',
showZero: false,
formatMoney: 'total',
precision: 2,
},
basis: 'accural',
noneZero: false,
@@ -42,7 +46,7 @@ export default class TrialBalanceSheetService {
*/
public async trialBalanceSheet(
tenantId: number,
query: ITrialBalanceSheetQuery
query: ITrialBalanceSheetQuery,
): Promise<ITrialBalanceStatement> {
const filter = {
...this.defaultQuery,