Skip to content

Commit

Permalink
feat(reports): Warn on multiple fiscal year span
Browse files Browse the repository at this point in the history
This commit adds a verification and a warning on account statements that
span multiple fiscal years and target income/expense accounts. Given the
begining balance for every fiscal year will be 0, the cumulative
transaction balance will not line up with the end balance of the
statement. It also resolves issues with dates being sent incorrectly
with the saving method.
  • Loading branch information
sfount committed Jun 21, 2017
1 parent b019609 commit 2a21e19
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .env.development
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ SESS_SECRET='XopEn BlowFISH'
SESS_RESAVE=0
SESS_UNSET='destroy'

LOG_LEVEL='warn'
# LOG_LEVEL='debug'
# LOG_LEVEL='warn'
LOG_LEVEL='debug'
UPLOAD_DIR='client/upload'

# this will toggle all MYSQL errors to be reflected to the client.
Expand Down
3 changes: 2 additions & 1 deletion client/src/i18n/en/report.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"ZERO_TO_THIRTY_DAYS":"Less Than 30 Days",
"REPORT_ACCOUNTS" : {
"TITLE":"Account Report",
"DESCRIPTION":"This report show all transaction for an individual account for a give fiscal year"
"DESCRIPTION":"This report shows all transactions for an individual account given a set time period.",
"WARN_MULTIPLE":"N.B. This report concerns an income/ expense account and spans multiple fiscal years. Income/ expense accounts are cleared to 0 at the begining of every fiscal year, the cumulative balance displayed is for the transactions within this period of time. It is recommended to use this statement within a single fiscal year."
},
"UTIL":{
"CANCEL_PREVIEW":"Cancel Preview",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ function AccountReportConfigController($sce, Notify, SavedReports, AppCache, rep
vm.requestSaveAs = function requestSaveAs() {
parseDateInterval(vm.reportDetails);

// @FIXME
var options = {
url : reportUrl,
report : reportData,
reportOptions : angular.copy(vm.reportDetails),
reportOptions : sanitiseDateStrings(vm.reportDetails),
};

return SavedReports.saveAsModal(options)
Expand All @@ -54,7 +55,7 @@ function AccountReportConfigController($sce, Notify, SavedReports, AppCache, rep
// update cached configuration
cache.reportDetails = angular.copy(vm.reportDetails);

var sendDetails = angular.copy(vm.reportDetails);
var sendDetails = sanitiseDateStrings(vm.reportDetails);
sendDetails.dateTo = Moment(sendDetails.dateTo).format('YYYY-MM-DD');
sendDetails.dateFrom = Moment(sendDetails.dateFrom).format('YYYY-MM-DD');

Expand All @@ -66,6 +67,13 @@ function AccountReportConfigController($sce, Notify, SavedReports, AppCache, rep
.catch(Notify.handleError);
};

function sanitiseDateStrings(options) {
var sanitisedOptions = angular.copy(options);
sanitisedOptions.dateTo = Moment(sanitisedOptions.dateTo).format('YYYY-MM-DD');
sanitisedOptions.dateFrom = Moment(sanitisedOptions.dateFrom).format('YYYY-MM-DD');
return sanitisedOptions;
}

// @TODO validation on dates - this should be done through a 'period select' component
function parseDateInterval(reportDetails) {
if (!vm.dateInterval) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ <h3 class="text-capitalize" translate>REPORT.REPORT_ACCOUNTS.TITLE</h3>
on-select-callback="ReportConfigCtrl.selectAccount(account)">
</bh-account-select>

<bh-report-source
value="ReportConfigCtrl.reportDetails.source"
on-select-callback="ReportConfigCtrl.selectSource(source)">
</bh-report-source>

<!-- included Date interval -->
<div class="checkbox">
<label>
Expand Down
2 changes: 1 addition & 1 deletion server/controllers/finance/accounts/extra.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function getPeriodAccountBalanceUntilDate(accountId, date, fiscalYearId) {
const periodCondition = `
(period.number = 0
OR
period.end_date <= DATE(?))
period.end_date < DATE(?))
`;

const sql = `
Expand Down
33 changes: 33 additions & 0 deletions server/controllers/finance/reports/reportAccounts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,27 @@ function document(req, res, next) {
transactions : result.transactions,
sum : result.sum,
params });

return getNumberOfFiscalYears(params.dateFrom, params.dateTo);
})
.then((result) => {

// check to see if this statement spans multiple fiscal years AND concerns an income/ expense account
// @TODO these constants should be system shared variables
const incomeAccountId = 1;
const expenseAccountId = 2;

const multipleFiscalYears = result.fiscalYearSpan > 1;
const incomeExpenseAccount = bundle.accountDetails.type_id === incomeAccountId || bundle.accountDetails.type_id === expenseAccountId;

console.log('account details', bundle.accountDetails);
console.log('fiscal year span', result.fiscalYearSpan);

if (multipleFiscalYears && incomeExpenseAccount) {
_.extend(bundle, {
warnMultipleFiscalYears : true
});
}
return report.render(bundle);
})
.then((result) => {
Expand All @@ -69,6 +90,18 @@ function document(req, res, next) {
.done();
}

function getNumberOfFiscalYears(dateFrom, dateTo) {

const sql = `
SELECT COUNT(id) as fiscalYearSpan from fiscal_year
WHERE
start_date <= DATE(?) AND end_date >= DATE(?)
OR
start_date <= DATE(?) AND end_date >= DATE(?)
`;

return db.one(sql, [dateFrom, dateFrom, dateTo, dateTo]);
}

/**
* @function getAccountTransactions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
</h5>
{{/if}}

{{#if warnMultipleFiscalYears}}
<section style="border : 1px solid black; padding : 5px; margin-bottom : 10px">
<p>{{translate "REPORT.REPORT_ACCOUNTS.WARN_MULTIPLE"}}</p>
</section>
{{/if}}

<section>
<table class="table table-condensed table-report table-bordered">
<thead>
Expand Down Expand Up @@ -60,9 +66,7 @@
{{/if}}
</td>
<td class="text-right">
{{#if this.cumsum}}
{{currency this.cumsum ../metadata.enterprise.currency_id}}
{{/if}}
</td>
</tr>
{{else}}
Expand Down

0 comments on commit 2a21e19

Please sign in to comment.