Skip to content

Commit

Permalink
refactor(reports): Account balance, sum deb/creds
Browse files Browse the repository at this point in the history
This commit adds individual summing of debits and credits to the account
summing utility.

It also tweaks the account statement report to limit descriptions onto a
single line and uses the standard balance calculation instead of a
custom aggregation query.
  • Loading branch information
sfount committed Jun 21, 2017
1 parent 8d2492a commit d9e8991
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 30 deletions.
27 changes: 18 additions & 9 deletions server/controllers/finance/accounts/extra.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,14 @@ function getPeriodAccountBalanceUntilDate(accountId, date, fiscalYearId) {
`;

const sql = `
SELECT SUM(debit - credit) AS balance
SELECT IFNULL(SUM(debit), 0) as debit, IFNULL(SUM(credit), 0) as credit, IFNULL(SUM(debit - credit), 0) AS balance
FROM period_total JOIN period ON period.id = period_total.period_id
WHERE period_total.account_id = ?
AND ${periodCondition}
AND period.fiscal_year_id = ?;
`;

return db.one(sql, [accountId, date, fiscalYearId])
.then(data => data.balance);
return db.one(sql, [accountId, date, fiscalYearId]);
}

/**
Expand All @@ -95,14 +94,13 @@ function getPeriodAccountBalanceUntilDate(accountId, date, fiscalYearId) {
*/
function getComputedAccountBalanceUntilDate(accountId, date, periodId) {
const sql = `
SELECT SUM(debit_equiv - credit_equiv) AS balance FROM general_ledger
SELECT IFNULL(SUM(debit), 0) as debit, IFNULL(SUM(credit), 0) as credit, IFNULL(SUM(debit_equiv - credit_equiv), 0) AS balance FROM general_ledger
WHERE account_id = ?
AND trans_date <= DATE(?)
AND period_id = ?;
`;

return db.one(sql, [accountId, date, periodId])
.then(data => data.balance);
return db.one(sql, [accountId, date, periodId]);
}


Expand All @@ -120,6 +118,8 @@ function getComputedAccountBalanceUntilDate(accountId, date, periodId) {
*/
function getOpeningBalanceForDate(accountId, date) {
let balance = 0;
let credit = 0;
let debit = 0;

return getFiscalYearForDate(date)

Expand All @@ -129,8 +129,11 @@ function getOpeningBalanceForDate(accountId, date) {
)

// 2. fetch the current dates period
.then((previousPeriodClosingBalance) => {
balance += previousPeriodClosingBalance;
.then((previousPeriodClosing) => {
balance += previousPeriodClosing.balance;
credit += previousPeriodClosing.credit;
debit += previousPeriodClosing.debit;

return getPeriodForDate(date);
})

Expand All @@ -139,7 +142,13 @@ function getOpeningBalanceForDate(accountId, date) {
.then(periodId =>
getComputedAccountBalanceUntilDate(accountId, date, periodId)
)
.then(runningPeriodBalance => (balance + runningPeriodBalance).toFixed(4));
.then(runningPeriod => {
return {
balance : (balance + runningPeriod.balance).toFixed(4),
credit : (credit + runningPeriod.credit).toFixed(4),
debit : (debit + runningPeriod.debit).toFixed(4)
}
});
}

exports.getOpeningBalanceForDate = getOpeningBalanceForDate;
26 changes: 10 additions & 16 deletions server/controllers/finance/reports/reportAccounts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@ function document(req, res, next) {
.then((balance) => {
const openingBalance = {
date : dateFrom,
amount : balance,
balance : balance.balance,
credit : balance.credit,
debit : balance.debit,
isCreditBalance : balance < 0,
};

_.extend(bundle, { openingBalance });
return getAccountTransactions(params.account_id, params.dateFrom, params.dateTo, balance);
return getAccountTransactions(params.account_id, params.dateFrom, params.dateTo, balance.balance);
})
.then((result) => {
_.extend(bundle, {
Expand All @@ -73,7 +75,6 @@ function document(req, res, next) {
* This feature select all transactions for a specific account
*/
function getAccountTransactions(accountId, dateFrom, dateTo, openingBalance) {

const params = [accountId];
let dateCondition = '';

Expand All @@ -100,17 +101,6 @@ function getAccountTransactions(accountId, dateFrom, dateTo, openingBalance) {
) AS groups
`;

const sqlAggrega = `
SELECT SUM(t.debit) AS debit, SUM(t.credit) AS credit, SUM(t.debit - t.credit) AS balance
FROM (
SELECT SUM(debit_equiv) as debit, SUM(credit_equiv) AS credit
FROM general_ledger
WHERE account_id = ? ${dateCondition}
GROUP BY record_uuid
ORDER BY trans_date ASC
) AS t
`;

const bundle = {};

return Accounts.lookupAccount(accountId)
Expand All @@ -120,12 +110,16 @@ function getAccountTransactions(accountId, dateFrom, dateTo, openingBalance) {
})
.then((transactions) => {
_.extend(bundle, { transactions });
return db.one(sqlAggrega, params);

// get the balance at the final date
return AccountsExtra.getOpeningBalanceForDate(accountId, dateTo);
})
.then((sum) => {
// if the sum come back as zero (because there were no lines), set the default sum to the
// opening balance
sum.balance = sum.balance || openingBalance;
sum.credit = sum.credit || 0;
sum.debit = sum.debit || 0;
sum.balance = sum.balance || 0;
_.extend(bundle, { sum });
return bundle;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@
<th>{{ date openingBalance.date }}</th>
<th colspan="3">{{translate "REPORT.OPENING_BALANCE"}}</th>
<th class="text-right">
{{#unless openingBalance.isCreditBalance}} {{currency openingBalance.amount metadata.enterprise.currency_id}} {{/unless}}
{{currency openingBalance.debit metadata.enterprise.currency_id}}
</th>
<th class="text-right">
{{#if openingBalance.isCreditBalance}} {{currency openingBalance.amount metadata.enterprise.currency_id}} {{/if}}
{{currency openingBalance.credit metadata.enterprise.currency_id}}
</th>
<th class="text-right">{{currency openingBalance.amount metadata.enterprise.currency_id}}</th>
<th class="text-right">{{currency openingBalance.balance metadata.enterprise.currency_id}}</th>
</tr>

{{#each transactions}}
<tr>
<td>{{date this.trans_date}}</td>
<td>{{this.trans_id}}</td>
<td>{{this.document_reference}}</td>
<td>{{this.description}}</td>
<td style="max-width : 200px; white-space : nowrap; overflow : hidden; text-overflow : ellipsis;">{{this.description}}</td>
<td class="text-right">
{{#if this.debit}}
{{currency this.debit ../metadata.enterprise.currency_id}}
Expand Down Expand Up @@ -79,7 +79,7 @@
<table class="table table-condensed">
<tr>
<th colspan="5" class="text-right">
<strong>{{translate "FORM.LABELS.BALANCE" }} {{currency sum.balance metadata.enterprise.currency_id}}</strong>
<strong>{{translate "FORM.LABELS.BALANCE" }} ({{date params.dateTo }}) {{currency sum.balance metadata.enterprise.currency_id}}</strong>
</th>
</tr>
</table>
Expand Down

0 comments on commit d9e8991

Please sign in to comment.