feat: balance sheet report.

feat: trial balance sheet.
feat: general ledger report.
feat: journal report.
feat: profit/loss report.
This commit is contained in:
a.bouhuolia
2020-12-30 20:39:17 +02:00
parent de9f6d9521
commit 7ae73ed6cd
62 changed files with 2403 additions and 1850 deletions

View File

@@ -9,10 +9,7 @@ import {
IJournalPoster,
IAccountType,
} from 'interfaces';
import {
dateRangeCollection,
flatToNestedArray,
} from 'utils';
import { dateRangeCollection, flatToNestedArray } from 'utils';
import BalanceSheetStructure from 'data/BalanceSheetStructure';
import FinancialSheet from '../FinancialSheet';
@@ -37,7 +34,7 @@ export default class BalanceSheetStatement extends FinancialSheet {
query: IBalanceSheetQuery,
accounts: IAccount & { type: IAccountType }[],
journalFinancial: IJournalPoster,
baseCurrency: string,
baseCurrency: string
) {
super();
@@ -48,9 +45,8 @@ export default class BalanceSheetStatement extends FinancialSheet {
this.journalFinancial = journalFinancial;
this.baseCurrency = baseCurrency;
this.comparatorDateType = query.displayColumnsType === 'total'
? 'day'
: query.displayColumnsBy;
this.comparatorDateType =
query.displayColumnsType === 'total' ? 'day' : query.displayColumnsBy;
this.initDateRangeCollection();
}
@@ -73,20 +69,24 @@ export default class BalanceSheetStatement extends FinancialSheet {
* @param {IBalanceSheetSection[]} sections -
* @return {IBalanceSheetAccountTotal}
*/
private getSectionTotal(sections: IBalanceSheetSection[]): IBalanceSheetAccountTotal {
private getSectionTotal(
sections: IBalanceSheetSection[]
): IBalanceSheetAccountTotal {
const amount = sumBy(sections, 'total.amount');
const formattedAmount = this.formatNumber(amount);
const currencyCode = this.baseCurrency;
return { amount, formattedAmount, currencyCode };
};
}
/**
* Retrieve accounts total periods.
* @param {IBalanceSheetAccount[]} sections -
* @param {IBalanceSheetAccount[]} sections -
* @return {IBalanceSheetAccountTotal[]}
*/
private getSectionTotalPeriods(sections: IBalanceSheetAccount[]): IBalanceSheetAccountTotal[] {
private getSectionTotalPeriods(
sections: Array<IBalanceSheetAccount|IBalanceSheetSection>
): IBalanceSheetAccountTotal[] {
return this.dateRangeSet.map((date, index) => {
const amount = sumBy(sections, `totalPeriods[${index}].amount`);
const formattedAmount = this.formatNumber(amount);
@@ -98,10 +98,12 @@ export default class BalanceSheetStatement extends FinancialSheet {
/**
* Gets the date range set from start to end date.
* @param {IAccount} account
* @param {IAccount} account
* @return {IBalanceSheetAccountTotal[]}
*/
private getAccountTotalPeriods (account: IAccount): IBalanceSheetAccountTotal[] {
private getAccountTotalPeriods(
account: IAccount
): IBalanceSheetAccountTotal[] {
return this.dateRangeSet.map((date) => {
const amount = this.journalFinancial.getAccountBalance(
account.id,
@@ -114,29 +116,30 @@ export default class BalanceSheetStatement extends FinancialSheet {
return { amount, formattedAmount, currencyCode, date };
});
}
/**
* Retrieve account total and total periods with account meta.
* @param {IAccount} account -
* @param {IAccount} account -
* @param {IBalanceSheetQuery} query -
* @return {IBalanceSheetAccount}
*/
private balanceSheetAccountMapper(account: IAccount): IBalanceSheetAccount {
// Calculates the closing balance of the given account in the specific date point.
const amount = this.journalFinancial.getAccountBalance(
account.id, this.query.toDate,
account.id,
this.query.toDate
);
const formattedAmount = this.formatNumber(amount);
// Retrieve all entries that associated to the given account.
const entries = this.journalFinancial.getAccountEntries(account.id)
const entries = this.journalFinancial.getAccountEntries(account.id);
return {
...pick(account, ['id', 'index', 'name', 'code', 'parentAccountId']),
type: 'account',
hasTransactions: entries.length > 0,
// Total date periods.
...this.query.displayColumnsType === 'date_periods' && ({
...(this.query.displayColumnsType === 'date_periods' && {
totalPeriods: this.getAccountTotalPeriods(account),
}),
total: {
@@ -145,7 +148,7 @@ export default class BalanceSheetStatement extends FinancialSheet {
currencyCode: this.baseCurrency,
},
};
};
}
/**
* Strcuture accounts related mapper.
@@ -155,10 +158,10 @@ export default class BalanceSheetStatement extends FinancialSheet {
*/
private structureRelatedAccountsMapper(
sectionAccountsTypes: string[],
accounts: IAccount & { type: IAccountType }[],
accounts: IAccount & { type: IAccountType }[]
): {
children: IBalanceSheetAccount[],
total: IBalanceSheetAccountTotal,
children: IBalanceSheetAccount[];
total: IBalanceSheetAccountTotal;
} {
const filteredAccounts = accounts
// Filter accounts that associated to the section accounts types.
@@ -169,7 +172,7 @@ export default class BalanceSheetStatement extends FinancialSheet {
// Filter accounts that have no transaction when `noneTransactions` is on.
.filter(
(section: IBalanceSheetAccount) =>
!(!section.hasTransactions && this.query.noneTransactions),
!(!section.hasTransactions && this.query.noneTransactions)
)
// Filter accounts that have zero total amount when `noneZero` is on.
.filter(
@@ -181,10 +184,10 @@ export default class BalanceSheetStatement extends FinancialSheet {
const totalAmount = sumBy(filteredAccounts, 'total.amount');
return {
children: flatToNestedArray(
filteredAccounts,
{ id: 'id', parentId: 'parentAccountId' }
),
children: flatToNestedArray(filteredAccounts, {
id: 'id',
parentId: 'parentAccountId',
}),
total: {
amount: totalAmount,
formattedAmount: this.formatNumber(totalAmount),
@@ -196,8 +199,32 @@ export default class BalanceSheetStatement extends FinancialSheet {
}
: {}),
};
};
}
/**
* Mappes the structure sections.
* @param {IBalanceSheetStructureSection} structure
* @param {IAccount} accounts
*/
private structureSectionMapper(
structure: IBalanceSheetStructureSection,
accounts: IAccount[]
) {
const children = this.balanceSheetStructureWalker(
structure.children,
accounts
);
return {
children,
total: this.getSectionTotal(children),
...(this.query.displayColumnsType === 'date_periods'
? {
totalPeriods: this.getSectionTotalPeriods(children),
}
: {}),
};
}
/**
* Balance sheet structure mapper.
* @param {IBalanceSheetStructureSection} structure -
@@ -205,29 +232,18 @@ export default class BalanceSheetStatement extends FinancialSheet {
*/
private balanceSheetStructureMapper(
structure: IBalanceSheetStructureSection,
accounts: IAccount & { type: IAccountType }[],
accounts: IAccount & { type: IAccountType }[]
): IBalanceSheetSection {
const result = {
name: structure.name,
sectionType: structure.sectionType,
type: structure.type,
...(structure.type === 'accounts_section'
? {
...this.structureRelatedAccountsMapper(
...(structure.type === 'accounts_section')
? this.structureRelatedAccountsMapper(
structure._accountsTypesRelated,
accounts,
),
}
: (() => {
const children = this.balanceSheetStructureWalker(
structure.children,
accounts,
);
return {
children,
total: this.getSectionTotal(children),
};
})()),
accounts
)
: this.structureSectionMapper(structure, accounts),
};
return result;
}
@@ -239,21 +255,24 @@ export default class BalanceSheetStatement extends FinancialSheet {
*/
private balanceSheetStructureWalker(
reportStructure: IBalanceSheetStructureSection[],
balanceSheetAccounts: IAccount & { type: IAccountType }[],
balanceSheetAccounts: IAccount & { type: IAccountType }[]
): IBalanceSheetSection[] {
return reportStructure
.map((structure: IBalanceSheetStructureSection) =>
this.balanceSheetStructureMapper(structure, balanceSheetAccounts)
)
// Filter the structure sections that have no children.
.filter((structure: IBalanceSheetSection) =>
structure.children.length > 0 || structure._forceShow
);
return (
reportStructure
.map((structure: IBalanceSheetStructureSection) =>
this.balanceSheetStructureMapper(structure, balanceSheetAccounts)
)
// Filter the structure sections that have no children.
.filter(
(structure: IBalanceSheetSection) =>
structure.children.length > 0 || structure._forceShow
)
);
}
/**
* Retrieve date range columns of the given query.
* @param {IBalanceSheetQuery} query
* @param {IBalanceSheetQuery} query
* @return {string[]}
*/
private dateRangeColumns(): string[] {
@@ -278,7 +297,7 @@ export default class BalanceSheetStatement extends FinancialSheet {
public reportData(): IBalanceSheetSection[] {
return this.balanceSheetStructureWalker(
BalanceSheetStructure,
this.accounts,
)
this.accounts
);
}
}
}

View File

@@ -72,7 +72,8 @@ export default class BalanceSheetStatementService
// Retrieve all journal transactions based on the given query.
const transactions = await transactionsRepository.journal({
fromDate: query.toDate,
fromDate: query.fromDate,
toDate: query.toDate,
});
// Transform transactions to journal collection.
const transactionsJournal = Journal.fromTransactions(

View File

@@ -81,8 +81,6 @@ export default class JournalSheet extends FinancialSheet {
* @return {IJournalReport}
*/
reportData(): IJournalReport {
return {
entries: this.entriesWalker(this.journal.entries),
};
return this.entriesWalker(this.journal.entries);
}
}

View File

@@ -1,12 +1,12 @@
import { Service, Inject } from "typedi";
import { Service, Inject } from 'typedi';
import { IJournalReportQuery } from 'interfaces';
import moment from 'moment';
import JournalSheet from "./JournalSheet";
import TenancyService from "services/Tenancy/TenancyService";
import Journal from "services/Accounting/JournalPoster";
import JournalSheet from './JournalSheet';
import TenancyService from 'services/Tenancy/TenancyService';
import Journal from 'services/Accounting/JournalPoster';
@Service()
export default class JournalSheetService {
export default class JournalSheetService {
@Inject()
tenancy: TenancyService;
@@ -33,13 +33,10 @@ export default class JournalSheetService {
/**
* Journal sheet.
* @param {number} tenantId
* @param {IJournalSheetFilterQuery} query
* @param {number} tenantId
* @param {IJournalSheetFilterQuery} query
*/
async journalSheet(
tenantId: number,
query: IJournalReportQuery,
) {
async journalSheet(tenantId: number, query: IJournalReportQuery) {
const {
accountRepository,
transactionsRepository,
@@ -49,11 +46,17 @@ export default class JournalSheetService {
...this.defaultQuery,
...query,
};
this.logger.info('[journal] trying to calculate the report.', { tenantId, filter });
this.logger.info('[journal] trying to calculate the report.', {
tenantId,
filter,
});
// Settings 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 accountsGraph = await accountRepository.getDependencyGraph();
@@ -66,10 +69,12 @@ export default class JournalSheetService {
fromAmount: filter.fromRange,
toAmount: filter.toRange,
});
// Transform the transactions array to journal collection.
const transactionsJournal = Journal.fromTransactions(transactions, tenantId, accountsGraph);
const transactionsJournal = Journal.fromTransactions(
transactions,
tenantId,
accountsGraph
);
// Journal report instance.
const journalSheetInstance = new JournalSheet(
tenantId,
@@ -80,6 +85,9 @@ export default class JournalSheetService {
// Retrieve journal report columns.
const journalSheetData = journalSheetInstance.reportData();
return journalSheetData;
return {
data: journalSheetData,
query: filter,
};
}
}
}

View File

@@ -1,6 +1,6 @@
import { flatten, pick, sumBy } from 'lodash';
import { IProfitLossSheetQuery } from "interfaces/ProfitLossSheet";
import FinancialSheet from "../FinancialSheet";
import { IProfitLossSheetQuery } from 'interfaces/ProfitLossSheet';
import FinancialSheet from '../FinancialSheet';
import {
IAccount,
IAccountType,
@@ -34,7 +34,7 @@ export default class ProfitLossSheet extends FinancialSheet {
query: IProfitLossSheetQuery,
accounts: IAccount & { type: IAccountType }[],
journal: IJournalPoster,
baseCurrency: string,
baseCurrency: string
) {
super();
@@ -44,9 +44,8 @@ export default class ProfitLossSheet extends FinancialSheet {
this.accounts = accounts;
this.journal = journal;
this.baseCurrency = baseCurrency;
this.comparatorDateType = query.displayColumnsType === 'total'
? 'day'
: query.displayColumnsBy;
this.comparatorDateType =
query.displayColumnsType === 'total' ? 'day' : query.displayColumnsBy;
this.initDateRangeCollection();
}
@@ -56,7 +55,7 @@ export default class ProfitLossSheet extends FinancialSheet {
* @return {IAccount & { type: IAccountType }[]}
*/
get incomeAccounts() {
return this.accounts.filter(a => a.type.key === 'income');
return this.accounts.filter((a) => a.type.key === 'income');
}
/**
@@ -64,7 +63,7 @@ export default class ProfitLossSheet extends FinancialSheet {
* @return {IAccount & { type: IAccountType }[]}
*/
get expensesAccounts() {
return this.accounts.filter(a => a.type.key === 'expense');
return this.accounts.filter((a) => a.type.key === 'expense');
}
/**
@@ -72,7 +71,7 @@ export default class ProfitLossSheet extends FinancialSheet {
* @return {IAccount & { type: IAccountType }[]}}
*/
get otherExpensesAccounts() {
return this.accounts.filter(a => a.type.key === 'other_expense');
return this.accounts.filter((a) => a.type.key === 'other_expense');
}
/**
@@ -80,7 +79,7 @@ export default class ProfitLossSheet extends FinancialSheet {
* @return {IAccount & { type: IAccountType }[]}
*/
get costOfSalesAccounts() {
return this.accounts.filter(a => a.type.key === 'cost_of_goods_sold');
return this.accounts.filter((a) => a.type.key === 'cost_of_goods_sold');
}
/**
@@ -105,7 +104,7 @@ export default class ProfitLossSheet extends FinancialSheet {
const amount = this.journal.getAccountBalance(
account.id,
this.query.toDate,
this.comparatorDateType,
this.comparatorDateType
);
const formattedAmount = this.formatNumber(amount);
const currencyCode = this.baseCurrency;
@@ -123,13 +122,13 @@ export default class ProfitLossSheet extends FinancialSheet {
const amount = this.journal.getAccountBalance(
account.id,
date,
this.comparatorDateType,
this.comparatorDateType
);
const formattedAmount = this.formatNumber(amount);
const currencyCode = this.baseCurrency;
return { date, amount, formattedAmount, currencyCode };
})
});
}
/**
@@ -153,26 +152,30 @@ export default class ProfitLossSheet extends FinancialSheet {
}
/**
*
*
* @param {IAccount[]} accounts -
* @return {IProfitLossSheetAccount[]}
*/
private accountsWalker(accounts: IAccount & { type: IAccountType }[]): IProfitLossSheetAccount[] {
private accountsWalker(
accounts: IAccount & { type: IAccountType }[]
): IProfitLossSheetAccount[] {
const flattenAccounts = accounts
.map(this.accountMapper.bind(this))
// Filter accounts that have no transaction when `noneTransactions` is on.
.filter((account: IProfitLossSheetAccount) =>
!(!account.hasTransactions && this.query.noneTransactions),
.filter(
(account: IProfitLossSheetAccount) =>
!(!account.hasTransactions && this.query.noneTransactions)
)
// Filter accounts that have zero total amount when `noneZero` is on.
.filter((account: IProfitLossSheetAccount) =>
!(account.total.amount === 0 && this.query.noneZero)
.filter(
(account: IProfitLossSheetAccount) =>
!(account.total.amount === 0 && this.query.noneZero)
);
return flatToNestedArray(
flattenAccounts,
{ id: 'id', parentId: 'parentAccountId' },
);
return flatToNestedArray(flattenAccounts, {
id: 'id',
parentId: 'parentAccountId',
});
}
/**
@@ -180,7 +183,9 @@ export default class ProfitLossSheet extends FinancialSheet {
* @param {IAccount[]} accounts -
* @return {IProfitLossSheetTotal}
*/
private gatTotalSection(accounts: IProfitLossSheetAccount[]): IProfitLossSheetTotal {
private gatTotalSection(
accounts: IProfitLossSheetAccount[]
): IProfitLossSheetTotal {
const amount = sumBy(accounts, 'total.amount');
const formattedAmount = this.formatNumber(amount);
const currencyCode = this.baseCurrency;
@@ -190,10 +195,12 @@ export default class ProfitLossSheet extends FinancialSheet {
/**
* Retrieve the report total section in periods display type.
* @param {IAccount} accounts -
* @param {IAccount} accounts -
* @return {IProfitLossSheetTotal[]}
*/
private getTotalPeriodsSection(accounts: IProfitLossSheetAccount[]): IProfitLossSheetTotal[] {
private getTotalPeriodsSection(
accounts: IProfitLossSheetAccount[]
): IProfitLossSheetTotal[] {
return this.dateRangeSet.map((date, index) => {
const amount = sumBy(accounts, `totalPeriods[${index}].amount`);
const formattedAmount = this.formatNumber(amount);
@@ -213,7 +220,7 @@ export default class ProfitLossSheet extends FinancialSheet {
...(this.query.displayColumnsType === 'date_periods' && {
totalPeriods: this.getTotalPeriodsSection(accounts),
}),
}
};
}
/**
@@ -266,10 +273,13 @@ export default class ProfitLossSheet extends FinancialSheet {
private getSummarySectionDatePeriods(
positiveSections: IProfitLossSheetTotalSection[],
minesSections: IProfitLossSheetTotalSection[],
minesSections: IProfitLossSheetTotalSection[]
) {
return this.dateRangeSet.map((date, index: number) => {
const totalPositive = sumBy(positiveSections, `totalPeriods[${index}].amount`);
const totalPositive = sumBy(
positiveSections,
`totalPeriods[${index}].amount`
);
const totalMines = sumBy(minesSections, `totalPeriods[${index}].amount`);
const amount = totalPositive - totalMines;
@@ -278,11 +288,11 @@ export default class ProfitLossSheet extends FinancialSheet {
return { date, amount, formattedAmount, currencyCode };
});
};
}
private getSummarySectionTotal(
positiveSections: IProfitLossSheetTotalSection[],
minesSections: IProfitLossSheetTotalSection[],
minesSections: IProfitLossSheetTotalSection[]
) {
const totalPositiveSections = sumBy(positiveSections, 'total.amount');
const totalMinesSections = sumBy(minesSections, 'total.amount');
@@ -291,36 +301,37 @@ export default class ProfitLossSheet extends FinancialSheet {
const formattedAmount = this.formatNumber(amount);
const currencyCode = this.baseCurrency;
return { amount, formattedAmount, currencyCode };
return { amount, formattedAmount, currencyCode };
}
/**
* Retrieve the summary section
* @param
* @param
*/
private getSummarySection(
sections: IProfitLossSheetTotalSection|IProfitLossSheetTotalSection[],
subtractSections: IProfitLossSheetTotalSection|IProfitLossSheetTotalSection[]
sections: IProfitLossSheetTotalSection | IProfitLossSheetTotalSection[],
subtractSections:
| IProfitLossSheetTotalSection
| IProfitLossSheetTotalSection[]
): IProfitLossSheetTotalSection {
const positiveSections = Array.isArray(sections) ? sections : [sections];
const minesSections = Array.isArray(subtractSections) ? subtractSections : [subtractSections];
const minesSections = Array.isArray(subtractSections)
? subtractSections
: [subtractSections];
return {
total: this.getSummarySectionTotal(positiveSections, minesSections),
...(this.query.displayColumnsType === 'date_periods' && {
totalPeriods: [
...this.getSummarySectionDatePeriods(
positiveSections,
minesSections,
),
...this.getSummarySectionDatePeriods(positiveSections, minesSections),
],
}),
}
};
}
/**
* Retrieve date range columns of the given query.
* @param {IBalanceSheetQuery} query
* @param {IBalanceSheetQuery} query
* @return {string[]}
*/
private dateRangeColumns(): string[] {
@@ -341,7 +352,10 @@ export default class ProfitLossSheet extends FinancialSheet {
const grossProfit = this.getSummarySection(income, costOfSales);
// - Operating profit = Gross profit - Expenses.
const operatingProfit = this.getSummarySection(grossProfit, [expenses, costOfSales]);
const operatingProfit = this.getSummarySection(grossProfit, [
expenses,
costOfSales,
]);
// - Net income = Operating profit - Other expenses.
const netIncome = this.getSummarySection(operatingProfit, otherExpenses);
@@ -366,4 +380,4 @@ export default class ProfitLossSheet extends FinancialSheet {
? this.dateRangeColumns()
: ['total'];
}
}
}

View File

@@ -13,6 +13,7 @@ export default class TrialBalanceSheet extends FinancialSheet{
query: ITrialBalanceSheetQuery;
accounts: IAccount & { type: IAccountType }[];
journalFinancial: any;
baseCurrency: string;
/**
* Constructor method.
@@ -25,7 +26,8 @@ export default class TrialBalanceSheet extends FinancialSheet{
tenantId: number,
query: ITrialBalanceSheetQuery,
accounts: IAccount & { type: IAccountType }[],
journalFinancial: any
journalFinancial: any,
baseCurrency: string,
) {
super();
@@ -35,6 +37,7 @@ export default class TrialBalanceSheet extends FinancialSheet{
this.accounts = accounts;
this.journalFinancial = journalFinancial;
this.numberFormat = this.query.numberFormat;
this.baseCurrency = baseCurrency;
}
/**
@@ -58,6 +61,7 @@ export default class TrialBalanceSheet extends FinancialSheet{
credit: trial.credit,
debit: trial.debit,
balance: trial.balance,
currencyCode: this.baseCurrency,
formattedCredit: this.formatNumber(trial.credit),
formattedDebit: this.formatNumber(trial.debit),

View File

@@ -55,6 +55,10 @@ export default class TrialBalanceSheetService {
transactionsRepository,
} = this.tenancy.repositories(tenantId);
// Settings tenant service.
const settings = this.tenancy.settings(tenantId);
const baseCurrency = settings.get({ group: 'organization', key: 'base_currency' });
this.logger.info('[trial_balance_sheet] trying to calcualte the report.', { tenantId, filter });
// Retrieve all accounts on the storage.
@@ -76,6 +80,7 @@ export default class TrialBalanceSheetService {
filter,
accounts,
transactionsJournal,
baseCurrency
);
// Trial balance sheet data.
const trialBalanceSheetData = trialBalanceInstance.reportData();