Skip to content

Commit

Permalink
fix(reports): Period 0 is considered in balance
Browse files Browse the repository at this point in the history
This commit ensures that period 0 is factored into opening balance
calculations. Currently as no start or end date is provided for period 0
it is ignored in opening balance calculations. This adds a general
exception for period 0.
  • Loading branch information
sfount committed Jun 13, 2017
1 parent b014ef8 commit 4b85bcc
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions server/controllers/finance/accounts/extra.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,18 @@ function getPeriodForDate(date) {
* @returns Promise - promise wrapping the balance object
*/
function getPeriodAccountBalanceUntilDate(accountId, date, fiscalYearId) {
// always factor in period 0 which does not have a valid end date entry
const periodCondition = `
period.number = 0
OR
period.end_date <= DATE(?)
`;

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

Expand Down Expand Up @@ -101,7 +108,7 @@ function getComputedAccountBalanceUntilDate(accountId, date, periodId) {
*
* @description
* Query the database for an account's balance as of a start date. Note that the date should be escaped (via new
* Date()) prior to calling this function - this method is expected to does not do any escaping.
* Date()) prior to calling this function
*
* @param {Number} accountId - the identifier for the account
* @param {Date} date - the date that the opening balance should be computed through
Expand All @@ -111,13 +118,20 @@ function getOpeningBalanceForDate(accountId, date) {
let balance = 0;

return getFiscalYearForDate(date)

// 1. sum period totals up to the current required period
.then(fiscalYearId =>
getPeriodAccountBalanceUntilDate(accountId, date, fiscalYearId)
)

// 2. fetch the current dates period
.then((previousPeriodClosingBalance) => {
balance += previousPeriodClosingBalance;
return getPeriodForDate(date);
})

// 3. calculate the sum of all general ledger transaction against this account
// for the current period up to the current date
.then(periodId =>
getComputedAccountBalanceUntilDate(accountId, date, periodId)
)
Expand Down

0 comments on commit 4b85bcc

Please sign in to comment.