Skip to content

Commit

Permalink
fix(reports): Acc. Statement balance and dates
Browse files Browse the repository at this point in the history
This commit demonstrates one way of tackling #1793. Dates are converted
to the YYYY-MM-DD string before being sent to the server. This ensures
that no conversion of timezones happens between the client and the
server.

This commit also resolves an issue with period totals date calculation
and displays the balance of an account at the end of the statement
report instead of showing individual debits and credits.
  • Loading branch information
sfount committed Jun 21, 2017
1 parent d9e8991 commit b019609
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 14 deletions.
1 change: 1 addition & 0 deletions client/src/i18n/en/form.json
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@
"COLS_ALL":"All columns",
"COLS_VISIBLE":"Visible columns",
"COLS_SELECTED":"Selected columns",
"PERIOD_TOTAL":"Period Total",
"ROWS_ALL":"All records",
"ROWS_VISIBLE":"Visible records",
"ROWS_SELECTED":"Selected records",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ angular.module('bhima.controllers')
.controller('account_reportController', AccountReportConfigController);

AccountReportConfigController.$inject = [
'$sce', 'NotifyService', 'BaseReportService', 'AppCache', 'reportData', '$state',
'$sce', 'NotifyService', 'BaseReportService', 'AppCache', 'reportData',
'$state', 'moment'
];

function AccountReportConfigController($sce, Notify, SavedReports, AppCache, reportData, $state) {
function AccountReportConfigController($sce, Notify, SavedReports, AppCache, reportData, $state, Moment) {
var vm = this;
var cache = new AppCache('configure_account_report');
var reportUrl = 'reports/finance/account_report';
Expand Down Expand Up @@ -49,10 +50,15 @@ function AccountReportConfigController($sce, Notify, SavedReports, AppCache, rep

parseDateInterval(vm.reportDetails);


// update cached configuration
cache.reportDetails = angular.copy(vm.reportDetails);

return SavedReports.requestPreview(reportUrl, reportData.id, angular.copy(vm.reportDetails))
var sendDetails = angular.copy(vm.reportDetails);
sendDetails.dateTo = Moment(sendDetails.dateTo).format('YYYY-MM-DD');
sendDetails.dateFrom = Moment(sendDetails.dateFrom).format('YYYY-MM-DD');

return SavedReports.requestPreview(reportUrl, reportData.id, sendDetails)
.then(function (result) {
vm.previewGenerated = true;
vm.previewResult = $sce.trustAsHtml(result);
Expand Down
6 changes: 3 additions & 3 deletions server/controllers/finance/accounts/extra.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ function getPeriodAccountBalanceUntilDate(accountId, date, fiscalYearId) {
// is selected transactions will be added on top and the current period will
// be selected
const periodCondition = `
period.number = 0
(period.number = 0
OR
period.end_date < DATE(?)
period.end_date <= DATE(?))
`;

const sql = `
Expand All @@ -96,7 +96,7 @@ function getComputedAccountBalanceUntilDate(accountId, date, periodId) {
const sql = `
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 DATE(trans_date) <= DATE(?)
AND period_id = ?;
`;

Expand Down
13 changes: 10 additions & 3 deletions server/controllers/finance/reports/reportAccounts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function document(req, res, next) {
balance : balance.balance,
credit : balance.credit,
debit : balance.debit,
isCreditBalance : balance < 0,
isCreditBalance : balance.balance < 0,
};

_.extend(bundle, { openingBalance });
Expand Down Expand Up @@ -88,8 +88,7 @@ function getAccountTransactions(accountId, dateFrom, dateTo, openingBalance) {
groups.document_reference, groups.cumsum, groups.description
FROM (
SELECT trans_id, description, trans_date, document_reference, debit, credit,
@cumsum := balance + @cumsum AS cumsum
FROM (
@cumsum := balance + @cumsum AS cumsum FROM (
SELECT trans_id, description, trans_date, document_map.text AS document_reference,
SUM(debit_equiv) as debit, SUM(credit_equiv) as credit, (SUM(debit_equiv) - SUM(credit_equiv)) AS balance
FROM general_ledger
Expand Down Expand Up @@ -120,6 +119,14 @@ function getAccountTransactions(accountId, dateFrom, dateTo, openingBalance) {
sum.credit = sum.credit || 0;
sum.debit = sum.debit || 0;
sum.balance = sum.balance || 0;
sum.isCreditBalance = sum.balance < 0;

// @TODO sum calculated using javascript and may not line up with MySQLs values
// this should be done with a mysql query
sum.customPeriodDebitSum = _.sumBy(bundle.transactions, 'debit');
sum.customPeriodCreditSum = _.sumBy(bundle.transactions, 'credit');
sum.customPeriodBalanceSum = _.sumBy(bundle.transactions, (transaction) => transaction.debit - transaction.credit);

_.extend(bundle, { sum });
return bundle;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@
<tr>
<th>{{ date openingBalance.date }}</th>
<th colspan="3">{{translate "REPORT.OPENING_BALANCE"}}</th>

<th class="text-right">
{{currency openingBalance.debit metadata.enterprise.currency_id}}
{{#unless openingBalance.isCreditBalance}} {{currency openingBalance.balance metadata.enterprise.currency_id}} {{/unless}}
</th>
<th class="text-right">
{{currency openingBalance.credit metadata.enterprise.currency_id}}
{{#if openingBalance.isCreditBalance}} {{currency openingBalance.balance metadata.enterprise.currency_id}} {{/if}}
</th>
<th class="text-right">{{currency openingBalance.balance metadata.enterprise.currency_id}}</th>

</tr>

{{#each transactions}}
Expand Down Expand Up @@ -66,13 +68,29 @@
{{else}}
{{> emptyTable columns=7}}
{{/each}}

{{#if transactions}}
<tr>
<td><strong>{{date params.dateTo}}</strong></th>
<td colspan="3"><strong>{{translate "FORM.LABELS.PERIOD_TOTAL"}}</strong></td>
<td class="text-right"><strong>{{currency sum.customPeriodDebitSum}}</strong></td>
<td class="text-right"><strong>{{currency sum.customPeriodCreditSum}}</strong></td>
<td class="text-right"><strong>{{currency sum.customPeriodBalanceSum}}</strong></td>
</tr>
{{/if}}
</tbody>
<tfoot>
<tr style="background-color: #ddd;">
<td colspan="4"><strong>{{translate "FORM.LABELS.TOTAL" }}</strong></td>
<td class="text-right"><strong>{{currency sum.debit metadata.enterprise.currency_id}}</strong></td>
<td class="text-right"><strong>{{currency sum.credit metadata.enterprise.currency_id}}</strong></td>
<td class="text-right"><strong>{{currency sum.balance metadata.enterprise.currency_id}}</strong></td>

<th class="text-right">
{{#unless sum.isCreditBalance}} {{currency sum.balance metadata.enterprise.currency_id}} {{/unless}}
</th>
<th class="text-right">
{{#if sum.isCreditBalance}} {{currency sum.balance metadata.enterprise.currency_id}} {{/if}}
</th>
<th class="text-right">{{currency sum.balance metadata.enterprise.currency_id}}</th>

</tr>
</tfoot>
</table>
Expand Down

0 comments on commit b019609

Please sign in to comment.