Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(proposal) Server utility for finding beginning balance for an account. #1636

Closed
jniles opened this issue May 10, 2017 · 0 comments
Closed
Assignees
Labels

Comments

@jniles
Copy link
Collaborator

jniles commented May 10, 2017

To completed the work on #1635, we need to have a way to get the beginning balance for any give date.

Proposal
I propose we write a server utility that can be tested as a standalone. It will be in server/controllers/finance/accounts.js called getOpeningBalanceForDate(accountId, date). It should return a number - the balance of the account. The usage pseudocode would be like this:

const Accounts = require('../finance/accounts');

const date = new Date(2017, 03, 01);

Accounts.getOpeningBalanceForDate(accountId, date)
  .then(balance => {
   console.log(`The account opening balance is: ${balance}`);
  })
  .catch(next);

Implementation Algorithm
Internally, this function must do two things:

  1. Look up the period's opening balance via the period_total table. First, look up the fiscal year:
SELECT id FROM fiscal_year WHERE start_date >= DATE(?) AND end_date <= DATE(?);

Then, find all the period totals for this fiscal year and account.

SELECT SUM(debit - credit) AS balance FROM period_total
WHERE period_total.fiscal_year_id = ? 
  AND period_total.end_date <= DATE(?)
  AND period_total.account_id = ?;
  1. Afterwards, we must figure out the balance up until that date. Start by getting the period current period:
SELECT id FROM period WHERE start_date >= DATE(?) AND end_date <= DATE(?);

Then we can find the balance for this period up to today:

SELECT SUM(debit - credit) AS balance FROM general_ledger WHERE trans_date <= DATE(?) AND period_id = ? and account_id = ?;

Finally, we return the result of 1) + 2) as the final result!

@IMA-WorldHealth/bhima-core what do you think?

@jniles jniles self-assigned this May 11, 2017
jniles added a commit to jniles/bhima that referenced this issue May 11, 2017
This commit builds a new feature for accounts that can discern their opening balance (from the
General Ledger) given a date and account id.  The major changes have been added in an "AccountExtra"
file that should be merged into accounts/index.js as soon as the major refactoring is finished.

This PR consists of two parts: the updated account report that now
contains an opening balance and the utility to find the opening balance
give a particular date and account_id.

Closes Third-Culture-Software#1611.  Closes Third-Culture-Software#1636.
jniles added a commit to jniles/bhima that referenced this issue May 19, 2017
This commit builds a new feature for accounts that can discern their opening balance (from the
General Ledger) given a date and account id.  The major changes have been added in an "AccountExtra"
file that should be merged into accounts/index.js as soon as the major refactoring is finished.

This PR consists of two parts: the updated account report that now
contains an opening balance and the utility to find the opening balance
give a particular date and account_id.

Closes Third-Culture-Software#1611.  Closes Third-Culture-Software#1636.
jniles added a commit to jniles/bhima that referenced this issue May 22, 2017
This commit builds a new feature for accounts that can discern their opening balance (from the
General Ledger) given a date and account id.  The major changes have been added in an "AccountExtra"
file that should be merged into accounts/index.js as soon as the major refactoring is finished.

This PR consists of two parts: the updated account report that now
contains an opening balance and the utility to find the opening balance
give a particular date and account_id.

Closes Third-Culture-Software#1611.  Closes Third-Culture-Software#1636.
jniles added a commit to jniles/bhima that referenced this issue May 23, 2017
This commit builds a new feature for accounts that can discern their opening balance (from the
General Ledger) given a date and account id.  The major changes have been added in an "AccountExtra"
file that should be merged into accounts/index.js as soon as the major refactoring is finished.

This PR consists of two parts: the updated account report that now
contains an opening balance and the utility to find the opening balance
give a particular date and account_id.

Closes Third-Culture-Software#1611.  Closes Third-Culture-Software#1636.
sfount pushed a commit that referenced this issue May 23, 2017
This commit builds a new feature for accounts that can discern their opening balance (from the
General Ledger) given a date and account id.  The major changes have been added in an "AccountExtra"
file that should be merged into accounts/index.js as soon as the major refactoring is finished.

This PR consists of two parts: the updated account report that now
contains an opening balance and the utility to find the opening balance
give a particular date and account_id.

Closes #1611.  Closes #1636.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant