Skip to content

Commit

Permalink
feat(aged debtors): enable grouping by month
Browse files Browse the repository at this point in the history
This commit implements grouping by month for the aged debtors report.
This implements a checkbox on the client side that allows a user to
toggle the month grouping as needed.
  • Loading branch information
jniles committed Oct 31, 2017
1 parent fbf7f6b commit c354e8b
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 14 deletions.
1 change: 1 addition & 0 deletions client/src/i18n/en/report.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"TOTAL_DEBT": "Total Debt"
},
"OVER_NINETY_DAYS": "Over 90 Days",
"PREVIOUS_MONTHS": "Previous Months",
"PERIOD_START": "Start",
"PERIOD_STOP": "Stop",
"PRODUCED_BY": "Produced by",
Expand Down
1 change: 1 addition & 0 deletions client/src/i18n/fr/report.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"TOTAL_DEBT": "Total des Dettes"
},
"OVER_NINETY_DAYS": "Plus de 90 Jours",
"PREVIOUS_MONTHS": "Mois Précédents",
"PERIOD_START": "Debut",
"PERIOD_STOP": "Fin",
"PRODUCED_BY": "Produit par",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ <h3 class="text-capitalize" translate>REPORT.AGED_DEBTORS.TITLE</h3>
validation-trigger="ConfigForm.$submitted">
</bh-report-period-select>

<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 Down
34 changes: 29 additions & 5 deletions server/controllers/finance/reports/debtors/aged.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,34 @@
<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>{{translate "REPORT.ZERO_TO_THIRTY_DAYS"}}</th>
<th>{{translate "REPORT.THIRTY_TO_SIXTY_DAYS"}}</th>
<th>{{translate "REPORT.SIXTY_TO_NINETY_DAYS"}}</th>
<th>{{translate "REPORT.OVER_NINETY_DAYS"}}</th>
<th>
{{#if useMonthGrouping}}
{{month firstMonth}}
{{else}}
{{translate "REPORT.ZERO_TO_THIRTY_DAYS"}}
{{/if}}
</th>
<th>
{{#if useMonthGrouping}}
{{month secondMonth}}
{{else}}
{{translate "REPORT.THIRTY_TO_SIXTY_DAYS"}}
{{/if}}
</th>
<th>
{{#if useMonthGrouping}}
{{month thirdMonth}}
{{else}}
{{translate "REPORT.SIXTY_TO_NINETY_DAYS"}}
{{/if}}
</th>
<th>
{{#if useMonthGrouping}}
{{translate "REPORT.PREVIOUS_MONTHS"}}
{{else}}
{{translate "REPORT.OVER_NINETY_DAYS"}}
{{/if}}
</th>
<th>{{translate "TABLE.COLUMNS.TOTAL"}}</th>
</tr>
</thead>
Expand Down Expand Up @@ -75,4 +99,4 @@
</table>
</section>
</main>
</body>
</body>
37 changes: 28 additions & 9 deletions server/controllers/finance/reports/debtors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@


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

Expand Down Expand Up @@ -64,20 +65,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 debtorSql = `
SELECT BUID(dg.uuid) AS id, dg.name, a.number,
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,
${columns}
SUM(gl.debit_equiv - gl.credit_equiv) AS total
FROM debtor_group AS dg JOIN debtor AS d ON dg.uuid = d.group_uuid
LEFT JOIN ${source} AS gl ON gl.entity_uuid = d.uuid
Expand All @@ -91,10 +106,7 @@ 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.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,
${columns}
SUM(gl.debit_equiv - gl.credit_equiv) AS total
FROM debtor_group AS dg JOIN debtor AS d ON dg.uuid = d.group_uuid
LEFT JOIN ${source} AS gl ON gl.entity_uuid = d.uuid
Expand All @@ -106,10 +118,17 @@ function queryContext(queryParams) {
.then(debtors => {
data.debtors = debtors;
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
3 changes: 3 additions & 0 deletions server/lib/renderers/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
const util = require('../util');
const hbs = require('../template');
const moment = require('moment');
const translateHelperFactory = require('../helpers/translate');

const headers = {
Expand All @@ -28,6 +29,8 @@ function renderHTML(data, template, options = {}) {
const languageDependency = `moment/locale/${options.lang}`;
util.loadModuleIfExists(languageDependency);

moment.locale(options.lang);

// make sure that we have the appropriate language set. If options.lang is
// not specified, will default to English. To change this behavior, see the
// factory code.
Expand Down
14 changes: 14 additions & 0 deletions server/lib/template/helpers/dates.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ function age(dob) {
return moment().diff(dob, 'years');
}

/**
* @method month
*
* @description
* This method provides the month name for a given date.
*
* @param {Date} value - the date value to be transformed
* @returns {String} - the month name in the chosen locale.
*/
function month(value) {
return moment(value).format('MMMM');
}

exports.date = date;
exports.timestamp = timestamp;
exports.month = month;
exports.age = age;
1 change: 1 addition & 0 deletions server/lib/template/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const presentation = require('./helpers/presentation');
const hbs = exphbs.create({
helpers : {
date : dates.date,
month : dates.month,
timestamp : dates.timestamp,
age : dates.age,
multiply : math.multiply,
Expand Down

0 comments on commit c354e8b

Please sign in to comment.