Skip to content

Commit

Permalink
feat(account statement): grid beginning balance
Browse files Browse the repository at this point in the history
This commit adds the beginning balance to the Account Statement grid for
the period shown on the grid.  To do so, I had to add a new function to
the accounts service to search for the first date in a generic period
selection.

Closes #1762.
  • Loading branch information
jniles committed Dec 17, 2017
1 parent 7d74317 commit 01c9311
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 36 deletions.
5 changes: 4 additions & 1 deletion client/src/i18n/en/form.json
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@
"VIEW_BEGINNING_BALANCE": "View Beginning Balance",
"VIEW_CLOSING_BALANCE": "View Closing Balance"
},
"LABELS": {
"LABELS" : {
"ABBREVIATION": "Abbreviation",
"ACCOUNT": "Account",
"ACCOUNT_EXTRACT": "Account Details",
Expand Down Expand Up @@ -200,6 +200,8 @@
"MONTHLY_CONSUMPTION": "Monthly Consumption",
"BALANCE": "Balance",
"BALANCE_INTERMEDIATE" : "Intermediate Balance",
"BEGINNING_VIEW_BALANCE" : "Beginning View Balance",
"ENDING_VIEW_BALANCE" : "Ending View Balance",
"BALANCE_FINAL" : "Final Balance",
"BALANCE_SECTION": "Balance Sheet Section",
"BANK": "Bank",
Expand Down Expand Up @@ -256,6 +258,7 @@
"DATE_REGISTRATION": "Registration Date",
"DATE_TO": "End",
"DAYS": "Days",
"DIFFERENCE" : "Difference",
"DEACTIVATE": "Deactivate",
"DEBIT": "Debit",
"DEBTOR_GROUP": "Debtor Group",
Expand Down
3 changes: 3 additions & 0 deletions client/src/i18n/fr/form.json
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@
"BALANCE_SECTION": "Section du Bilan",
"BANK": "Banque",
"BANK_ACCOUNT": "Compte Bancaire",
"BEGINNING_VIEW_BALANCE" : "Solde Début du Grid",
"ENDING_VIEW_BALANCE" : "Solde Finale du Grid",
"BASIC_SALARY": "Salaire de base",
"BILLING_DATE": "Date Facturation",
"INVOICING_FEES": "Les frais de facturation appliquée",
Expand Down Expand Up @@ -259,6 +261,7 @@
"DATE_TO": "Fin",
"DAYS": "Jours",
"DEACTIVATE": "Désactiver",
"DIFFERENCE" : "Différence",
"DEBIT": "Débit",
"DEBTOR_GROUP": "Groupe Débiteur",
"DEBTOR_GROUP_FORM": "Formulaire d'enregistrement de groupe débiteur",
Expand Down
34 changes: 27 additions & 7 deletions client/src/modules/account_statement/account_statement.ctrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ AccountStatementController.$inject = [
'GridSortingService', 'GridFilteringService', 'GridColumnService',
'SessionService', 'bhConstants', 'uiGridConstants', 'AccountStatementService',
'ModalService', 'LanguageService', 'GridExportService',
'TransactionService', 'GridStateService', '$state',
'TransactionService', 'GridStateService', '$state', 'AccountService',
];

/**
Expand All @@ -19,7 +19,7 @@ AccountStatementController.$inject = [
function AccountStatementController(
GeneralLedger, Notify, Journal, Sorting, Filtering, Columns, Session,
bhConstants, uiGridConstants, AccountStatement, Modal, Languages,
GridExport, Transactions, GridState, $state
GridExport, Transactions, GridState, $state, Accounts
) {
// global variables
var vm = this;
Expand Down Expand Up @@ -292,35 +292,55 @@ function AccountStatementController(

vm.gridOptions.gridFooterTemplate = null;
vm.gridOptions.showGridFooter = false;
vm.balances = {};
vm.balances = {
opening : 0,
debits : 0,
credits : 0,
difference : 0,
};

// load the opening balance for the range
Accounts.getOpeningBalanceForPeriod(options.account_id, options)
.then(function (balances) {
vm.balances.opening = balances.balance;
vm.balances.ending = vm.balances.opening + vm.balances.difference;
})
.catch(Notify.handleError);

GeneralLedger.read(null, options)
.then(function (data) {
var balances;

vm.gridOptions.data = data;

// compute the difference between the debits and credits
// TODO(@jniles) - is there a way to get this from ui grid's aggregation?
vm.balances = data.reduce(computeTransactionBalances, {
balances = data.reduce(computeTransactionBalances, {
debits : 0,
credits : 0,
balance : 0,
difference : 0,
});

vm.balances.debits = balances.debits;
vm.balances.credits = balances.credits;
vm.balances.difference = balances.difference;
vm.balances.ending = vm.balances.opening + balances.difference;

vm.gridOptions.showGridFooter = true;
vm.gridOptions.gridFooterTemplate = '/modules/account_statement/grid.footer.html';

// @TODO investigate why footer totals aren't updated automatically on data change
vm.gridApi.core.notifyDataChange(uiGridConstants.dataChange.ALL);
})
.catch(handleError)
.catch(Notify.handleError)
.finally(toggleLoadingIndicator);
}

// computes the balances used in the grid footer
function computeTransactionBalances(aggregates, row) {
aggregates.debits += row.debit_equiv;
aggregates.credits += row.credit_equiv;
aggregates.balance += (row.debit_equiv - row.credit_equiv);
aggregates.difference += (row.debit_equiv - row.credit_equiv);
return aggregates;
}

Expand Down
5 changes: 4 additions & 1 deletion client/src/modules/account_statement/grid.footer.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<div class="ui-grid-cell-contents">
<span>({{grid.rows.length}} <span translate>FORM.INFO.ROWS</span>)</span>
<span style="padding-left:0.5em;">
<strong translate>FORM.LABELS.BEGINNING_VIEW_BALANCE</strong>: {{ grid.appScope.balances.opening | currency:grid.appScope.enterprise.currency_id }}
</span>
<span style="padding-left:0.5em;">
<strong translate>FORM.LABELS.DEBIT</strong>: {{ grid.appScope.balances.debits | currency:grid.appScope.enterprise.currency_id }}
</span>
<span style="padding-left:0.5em;">
<strong translate>FORM.LABELS.CREDIT</strong>: {{ grid.appScope.balances.credits | currency:grid.appScope.enterprise.currency_id }}
</span>
<span style="padding-left:0.5em;">
<strong translate>FORM.LABELS.BALANCE</strong>: {{ grid.appScope.balances.balance | currency:grid.appScope.enterprise.currency_id }}
<strong translate>FORM.LABELS.DIFFERENCE</strong>: {{ grid.appScope.balances.difference | currency:grid.appScope.enterprise.currency_id }}
</span>
</div>
30 changes: 21 additions & 9 deletions client/src/modules/accounts/accounts.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,43 @@ angular.module('bhima.services')
.service('AccountService', AccountService);

AccountService.$inject = [
'PrototypeApiService', '$http', 'util', 'bhConstants',
'PrototypeApiService', 'bhConstants',
];

/**
* Account Service
*
* A service wrapper for the /accounts HTTP endpoint.
*/
function AccountService(Api, $http, util, bhConstants) {
function AccountService(Api, bhConstants) {
var baseUrl = '/accounts/';
var service = new Api(baseUrl);

service.read = read;
service.label = label;

service.getBalance = getBalance;
service.getOpeningBalanceForPeriod = getOpeningBalanceForPeriod;
service.getChildren = getChildren;
service.filterTitleAccounts = filterTitleAccounts;

service.flatten = flatten;
service.order = order;

/**
* @method getOpeningBalance
*
*
* @description
* This method exists to get the opening balance for parameters like those
* used to load a date range.
*/
function getOpeningBalanceForPeriod(id, options) {
var url = service.url.concat(id, '/openingBalance');
return service.$http.get(url, { params : options })
.then(service.util.unwrapHttpResponse);
}

/**
* The read() method loads data from the api endpoint. If an id is provided,
* the $http promise is resolved with a single JSON object, otherwise an array
Expand All @@ -35,9 +50,7 @@ function AccountService(Api, $http, util, bhConstants) {
* an array of JSONs.
*/
function read(id, options) {
var url = baseUrl.concat(id || '');
return $http.get(url, { params : options })
.then(util.unwrapHttpResponse)
return Api.read.call(this, id, options)
.then(handleAccounts);
}

Expand All @@ -61,8 +74,8 @@ function AccountService(Api, $http, util, bhConstants) {

function getBalance(accountId, opt) {
var url = baseUrl.concat(accountId, '/balance');
return $http.get(url, opt)
.then(util.unwrapHttpResponse);
return service.$http.get(url, opt)
.then(service.util.unwrapHttpResponse);
}

function filterTitleAccounts(accounts) {
Expand Down Expand Up @@ -117,7 +130,7 @@ function AccountService(Api, $http, util, bhConstants) {
*/
function flatten(_tree, _depth) {
var tree = _tree || [];
var depth = isNaN(_depth) ? -1 : _depth;
var depth = Number.isNaN(_depth) ? -1 : _depth;
depth += 1;

function handleTreeLevel(array, node) {
Expand All @@ -140,7 +153,6 @@ function AccountService(Api, $http, util, bhConstants) {
* @returns {Array} - the properly ordered list of account objects
*/
function order(accounts) {

// NOTE
// we assume the root node is 0
var ROOT_NODE = 0;
Expand Down
2 changes: 0 additions & 2 deletions client/src/modules/general-ledger/general-ledger.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,5 @@ function GeneralLedgerService(Api, $httpParamSerializer, Languages) {
return $httpParamSerializer(options);
}


return service;
}

1 change: 1 addition & 0 deletions server/config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ exports.configure = function configure(app) {
app.get('/accounts', accounts.list);
app.get('/accounts/:id', accounts.detail);
app.get('/accounts/:id/balance', accounts.getBalance);
app.get('/accounts/:id/openingBalance', accounts.getOpeningBalanceForPeriod);
app.post('/accounts', accounts.create);
app.put('/accounts/:id', accounts.update);
app.delete('/accounts/:id', accounts.remove);
Expand Down
14 changes: 7 additions & 7 deletions server/controllers/finance/accounts/extra.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ function getPeriodAccountBalanceUntilDate(accountId, date, fiscalYearId) {
function getComputedAccountBalanceUntilDate(accountId, date, periodId, includeMaxDate) {
const dateOperator = includeMaxDate ? '<=' : '<';
const sql = `
SELECT
SELECT
IFNULL(SUM(debit), 0) as debit, IFNULL(SUM(credit), 0) as credit,
IFNULL(SUM(debit_equiv - credit_equiv), 0) AS balance
FROM
IFNULL(SUM(debit_equiv - credit_equiv), 0) AS balance
FROM
general_ledger
WHERE
WHERE
account_id = ?
AND DATE(trans_date) ${dateOperator} DATE(?)
AND period_id = ?;
Expand Down Expand Up @@ -129,11 +129,11 @@ function getOpeningBalanceForDate(accountId, date, includeMaxDate = true) {

return getFiscalYearForDate(date)

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

// 2. fetch the current dates period
// 2. Fetch the current dates period
.then((previousPeriodClosing) => {
balance += previousPeriodClosing.balance;
credit += previousPeriodClosing.credit;
Expand All @@ -142,7 +142,7 @@ function getOpeningBalanceForDate(accountId, date, includeMaxDate = true) {
return getPeriodForDate(date);
})

// 3. calculate the sum of all general ledger transaction against this account
// 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, includeMaxDate))
Expand Down
Loading

0 comments on commit 01c9311

Please sign in to comment.