Skip to content

Commit

Permalink
feat(Aged Creditors): implement monthly grouping
Browse files Browse the repository at this point in the history
This commit implements "Group by Month" on the Aged Creditors page.  It
transforms the date range from being a simply 30/60/90 range to being
this month, last month, and the previous month(s).  This allows a much
more intuitive grouping for users who are not used to thinking in ranges
of 30 days.

Closes #1367.
  • Loading branch information
jniles committed Oct 31, 2017
1 parent c354e8b commit d5b5a28
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 22 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 @@ -493,6 +493,7 @@
"UPDATE_GROUP_DEBTOR": "Update Debtor Group",
"UPDATE_PATIENT_GROUP": "Update Patient Group Subscription",
"USE": "Use",
"USE_MONTH_GROUPING": "Group By Months",
"USE_POS_RECEIPT": "POS Receipts",
"USE_SIMPLIFIED_CARD": "Use simple patient identity",
"USER": "User",
Expand Down
1 change: 1 addition & 0 deletions client/src/i18n/fr/form.json
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@
"UPDATE_GROUP_DEBTOR": "Mettre à jour le Groupe débiteur",
"UPDATE_PATIENT_GROUP": "Mettre à jour les groupes de patients",
"USE": "Utilisation",
"USE_MONTH_GROUPING": "Grouper par Mois",
"USERNAME": "Nom de l'utilisateur",
"USE_POS_RECEIPT": "Impression POS",
"USE_SIMPLIFIED_CARD": "Utiliser une identité de patient simple",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ <h3 class="text-capitalize" translate>REPORT.AGED_CREDITORS.TITLE</h3>
on-select-callback = "ReportConfigCtrl.onSelectPeriod(period)">
</bh-report-period-select>

<!-- toggle to grouping by months -->
<div class="checkbox">
<label>
<input type="checkbox" ng-model="ReportConfigCtrl.reportDetails.useMonthGrouping" ng-true-value="1" ng-false-value="0">
<span translate>FORM.LABELS.USE_MONTH_GROUPING</span>
</label>
</div>

<!-- included Total Zeros -->
<div class="checkbox">
<label>
Expand All @@ -44,4 +52,4 @@ <h3 class="text-capitalize" translate>REPORT.AGED_CREDITORS.TITLE</h3>
</div>
</div>
</div>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ <h3 class="text-capitalize" translate>REPORT.AGED_DEBTORS.TITLE</h3>
validation-trigger="ConfigForm.$submitted">
</bh-report-period-select>

<!-- toggle to grouping by months -->
<div class="checkbox">
<label>
<input type="checkbox" ng-model="ReportConfigCtrl.reportDetails.useMonthGrouping" ng-true-value="1" ng-false-value="0">
Expand Down
33 changes: 28 additions & 5 deletions server/controllers/finance/reports/creditors/aged.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,34 @@
<tr class="text-capitalize" style="background-color: #ddd;">
<th class="text-center">{{translate "TABLE.COLUMNS.NAME"}}</th>
<th class="text-center" style="width:15%">{{translate "FORM.LABELS.ACCOUNT_NUMBER"}}</th>
<th class="text-center">{{translate "REPORT.ZERO_TO_THIRTY_DAYS"}}</th>
<th class="text-center">{{translate "REPORT.THIRTY_TO_SIXTY_DAYS"}}</th>
<th class="text-center">{{translate "REPORT.SIXTY_TO_NINETY_DAYS"}}</th>
<th class="text-center">{{translate "REPORT.OVER_NINETY_DAYS"}}</th>
<th class="text-center">{{translate "TABLE.COLUMNS.TOTAL"}}</th>
<th class="text-center">
{{#if useMonthGrouping}}
{{month firstMonth}}
{{else}}
{{translate "REPORT.ZERO_TO_THIRTY_DAYS"}}
{{/if}}
</th>
<th class="text-center">
{{#if useMonthGrouping}}
{{month secondMonth}}
{{else}}
{{translate "REPORT.THIRTY_TO_SIXTY_DAYS"}}
{{/if}}
</th>
<th class="text-center">
{{#if useMonthGrouping}}
{{month thirdMonth}}
{{else}}
{{translate "REPORT.SIXTY_TO_NINETY_DAYS"}}
{{/if}}
</th>
<th class="text-center">
{{#if useMonthGrouping}}
{{translate "REPORT.PREVIOUS_MONTHS"}}
{{else}}
{{translate "REPORT.OVER_NINETY_DAYS"}}
{{/if}}
</th>
</tr>
</thead>
<tbody>
Expand Down
39 changes: 29 additions & 10 deletions server/controllers/finance/reports/creditors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@


const _ = require('lodash');
const moment = require('moment');
const ReportManager = require('../../../../lib/ReportManager');
const db = require('../../../../lib/db');

Expand Down Expand Up @@ -58,20 +59,34 @@ function queryContext(queryParams) {
const params = queryParams || {};
const havingNonZeroValues = ' HAVING total > 0 ';
const includeZeroes = Boolean(Number(params.zeroes));
const useMonthGrouping = Boolean(Number(params.useMonthGrouping));

// format the dates for MySQL escape
const dates = _.fill(Array(5), new Date(params.date));

const data = {};
const source = 'general_ledger';

const groupByMonthColumns = `
SUM(IF(MONTH(?) - MONTH(gl.trans_date) = 0, gl.debit_equiv - gl.credit_equiv, 0)) AS thirty,
SUM(IF(MONTH(?) - MONTH(gl.trans_date) = 1, gl.debit_equiv - gl.credit_equiv, 0)) AS sixty,
SUM(IF(MONTH(?) - MONTH(gl.trans_date) = 2, gl.debit_equiv - gl.credit_equiv, 0)) AS ninety,
SUM(IF(MONTH(?) - MONTH(gl.trans_date) > 2, gl.debit_equiv - gl.credit_equiv, 0)) AS excess,
`;

const groupByRangeColumns = `
SUM(IF(DATEDIFF(DATE(?), DATE(gl.trans_date)) BETWEEN 0 AND 29, gl.debit_equiv - gl.credit_equiv, 0)) AS thirty,
SUM(IF(DATEDIFF(DATE(?), DATE(gl.trans_date)) BETWEEN 30 AND 59, gl.debit_equiv - gl.credit_equiv, 0)) AS sixty,
SUM(IF(DATEDIFF(DATE(?), DATE(gl.trans_date)) BETWEEN 60 AND 89, gl.debit_equiv - gl.credit_equiv, 0)) AS ninety,
SUM(IF(DATEDIFF(DATE(?), DATE(gl.trans_date)) > 90, gl.debit_equiv - gl.credit_equiv, 0)) AS excess,
`;

const columns = useMonthGrouping ? groupByMonthColumns : groupByRangeColumns;

// selects into columns of 30, 60, 90, and >90
const creditorSql = `
SELECT BUID(cg.uuid) AS id, cg.name, a.number,
SUM(IF(DATEDIFF(DATE(?), DATE(gl.trans_date)) BETWEEN 0 AND 29, gl.credit_equiv - gl.debit_equiv, 0)) AS thirty,
SUM(IF(DATEDIFF(DATE(?), DATE(gl.trans_date)) BETWEEN 30 AND 59, gl.credit_equiv - gl.debit_equiv, 0)) AS sixty,
SUM(IF(DATEDIFF(DATE(?), DATE(gl.trans_date)) BETWEEN 60 AND 89, gl.credit_equiv - gl.debit_equiv, 0)) AS ninety,
SUM(IF(DATEDIFF(DATE(?), DATE(gl.trans_date)) > 90, gl.credit_equiv - gl.debit_equiv, 0)) AS excess,
${columns}
SUM(gl.credit_equiv - gl.debit_equiv) AS total
FROM creditor_group AS cg
JOIN ${source} AS gl ON gl.account_id = cg.account_id
Expand All @@ -85,12 +100,9 @@ function queryContext(queryParams) {
// aggregates the data above as totals into columns of 30, 60, 90, and >90
const aggregateSql = `
SELECT
SUM(IF(DATEDIFF(DATE(?), DATE(gl.trans_date)) BETWEEN 0 AND 29, gl.credit_equiv - gl.debit_equiv, 0)) AS thirty,
SUM(IF(DATEDIFF(DATE(?), DATE(gl.trans_date)) BETWEEN 30 AND 59, gl.credit_equiv - gl.debit_equiv, 0)) AS sixty,
SUM(IF(DATEDIFF(DATE(?), DATE(gl.trans_date)) BETWEEN 60 AND 89, gl.credit_equiv - gl.debit_equiv, 0)) AS ninety,
SUM(IF(DATEDIFF(DATE(?), DATE(gl.trans_date)) > 90, gl.credit_equiv - gl.debit_equiv, 0)) AS excess,
${columns}
SUM(gl.credit_equiv - gl.debit_equiv) AS total
FROM creditor_group AS cg
FROM creditor_group AS cg
JOIN ${source} AS gl ON gl.account_id = cg.account_id
WHERE DATE(gl.trans_date) <= DATE(?)
${includeZeroes ? '' : havingNonZeroValues}
Expand All @@ -100,10 +112,17 @@ function queryContext(queryParams) {
.then(creditors => {
data.creditors = creditors;
data.dateUntil = params.date;

// this is specific to grouping by months
data.firstMonth = params.date;
data.secondMonth = moment(params.date).subtract(1, 'month');
data.thirdMonth = moment(params.date).subtract(2, 'month');

data.useMonthGrouping = useMonthGrouping;
return db.exec(aggregateSql, dates);
})
.then(aggregates => {
data.aggregates = aggregates[0];
[data.aggregates] = aggregates;
return data;
});
}
Expand Down
12 changes: 6 additions & 6 deletions server/controllers/finance/reports/debtors/aged.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,36 @@
<thead>
<tr class="text-capitalize text-center" style="background-color: #ddd;">
<th>{{translate "TABLE.COLUMNS.NAME"}}</th>
<th style="width:15%">{{translate "FORM.LABELS.ACCOUNT_NUMBER"}}</th>
<th>
<th style="width:15%" class="text-center">{{translate "FORM.LABELS.ACCOUNT_NUMBER"}}</th>
<th class="text-center">
{{#if useMonthGrouping}}
{{month firstMonth}}
{{else}}
{{translate "REPORT.ZERO_TO_THIRTY_DAYS"}}
{{/if}}
</th>
<th>
<th class="text-center">
{{#if useMonthGrouping}}
{{month secondMonth}}
{{else}}
{{translate "REPORT.THIRTY_TO_SIXTY_DAYS"}}
{{/if}}
</th>
<th>
<th class="text-center">
{{#if useMonthGrouping}}
{{month thirdMonth}}
{{else}}
{{translate "REPORT.SIXTY_TO_NINETY_DAYS"}}
{{/if}}
</th>
<th>
<th class="text-center">
{{#if useMonthGrouping}}
{{translate "REPORT.PREVIOUS_MONTHS"}}
{{else}}
{{translate "REPORT.OVER_NINETY_DAYS"}}
{{/if}}
</th>
<th>{{translate "TABLE.COLUMNS.TOTAL"}}</th>
<th class="text-center">{{translate "TABLE.COLUMNS.TOTAL"}}</th>
</tr>
</thead>
<tbody>
Expand Down

0 comments on commit d5b5a28

Please sign in to comment.