Skip to content

Commit

Permalink
feat(cash): payment summary on printed registry
Browse files Browse the repository at this point in the history
This commit summarizes some basic statistics about the printed
registries on the bottom of the registry.  This allows a user to quickly
get some blush statistics about the contained data.
  • Loading branch information
Jonathan Niles authored and sfount committed Jan 14, 2017
1 parent 6b968c3 commit 140282b
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 26 deletions.
5 changes: 4 additions & 1 deletion client/src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1397,7 +1397,10 @@
"NUM_SERVICES" : "Number of Services",
"NUM_PROJECTS" : "Number of Projects",
"NUM_PAYMENTS" : "Number of Payments",
"NUM_INVOICES" : "Number of Invoices"
"NUM_INVOICES" : "Number of Invoices",
"NUM_CAUTIONS" : "Number of PrePayments",
"NUM_CASHBOXES" : "Number of Cashboxes",
"NUM_CLIENTS" : "Number of Unique Clients"
}
},
"TRANSACTION_TYPE" : {
Expand Down
54 changes: 43 additions & 11 deletions server/controllers/finance/reports/cash/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const BadRequest = require('../../../../lib/errors/BadRequest');
const ReportManager = require('../../../../lib/ReportManager');

const pdf = require('../../../../lib/renderers/pdf');
const db = require('../../../../lib/db');

const CashPayments = require('../../cash');
const Debtors = require('../../debtors');
Expand Down Expand Up @@ -141,7 +142,6 @@ function report(req, res, next) {

let report;
let lang = req.query.lang;
let enterprise = req.session.enterprise;

// set up the report with report manager
try {
Expand All @@ -156,23 +156,55 @@ function report(req, res, next) {
return next(e);
}

// aggregates basic statistics about the selection
const aggregateSql = `
SELECT MIN(cash.date) AS minDate, MAX(cash.date) AS maxDate,
COUNT(DISTINCT(cash.user_id)) AS numUsers,
COUNT(DISTINCT(cash.project_id)) AS numProjects,
COUNT(DISTINCT(DATE(cash.date))) AS numDays,
COUNT(DISTINCT(cash.cashbox_id)) AS numCashboxes,
COUNT(DISTINCT(cash.debtor_uuid)) AS numDebtors,
SUM(IF(cash.is_caution, 0, 1)) AS numPayments,
SUM(cash.is_caution) AS numCautions
FROM cash
WHERE cash.uuid IN (?);
`;

// aggregates the cost by currency id.
const costSql = `
SELECT SUM(cash.amount) AS amount, cash.currency_id, currency.symbol
FROM cash JOIN currency ON cash.currency_id = currency.id
WHERE cash.uuid IN (?)
GROUP BY currency_id;
`;

const data = {};
let uuids;

CashPayments.listPayment(options)
.then(rows => {

// sum the currencies in each
const aggregates = rows.reduce(function (totals, row) {

// make sure a total exists
totals[row.currency_id] = totals[row.currency_id] || 0;
data.rows = rows;
data.hasFilter = hasFilter;
data.csv = rows;
data.display = display;

// add on to the total the amount in the row
totals[row.currency_id] += row.amount;
// map the uuids for aggregate sql consumption
uuids = rows.map(row => db.bid(row.uuid));

return totals;
}, {});
return db.one(aggregateSql, [uuids]);
})
.then(aggregates => {
data.aggregates = aggregates;

const data = { rows, display, hasFilter, enterprise, aggregates, csv: rows };
// conditional switches
data.hasMultipleProjects = aggregates.numProjects > 1;
data.hasMultipleCashboxes = aggregates.numCashboxes > 1;

return db.exec(costSql, [uuids]);
})
.then(amounts => {
data.amounts = amounts;
return report.render(data);
})
.then(result => {
Expand Down
74 changes: 60 additions & 14 deletions server/controllers/finance/reports/cash/report.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
{{translate 'TREE.CASH_PAYMENT_REGISTRY'}}
</h2>

<br>

<!-- filters -->
{{#if hasFilter}}
<br />
<div>
<i class="fa fa-filter"></i>
{{#if display.reference}}{{translate 'TABLE.COLUMNS.REFERENCE'}}: {{look display 'reference'}} /{{/if}}
Expand All @@ -26,10 +25,9 @@
{{#if display.user_id}}{{translate 'TABLE.COLUMNS.USER'}}: {{look display 'user_id'}}/{{/if}}
{{#if display.is_caution}}{{translate 'FORM.LABELS.CAUTION'}}: <i class="fa fa-check"></i> {{/if}}
</div>
<br />
{{/if}}

<br>

<!-- list of data -->
<table class="table table-condensed table-bordered table-striped">
<thead>
Expand All @@ -39,7 +37,7 @@
<th>{{translate 'TABLE.COLUMNS.PATIENT'}}</th>
<th>{{translate 'TABLE.COLUMNS.AMOUNT'}}</th>
<th>{{translate 'TABLE.COLUMNS.CASHBOX'}}</th>
<th>{{translate 'TABLE.COLUMNS.USER'}}</th>
<th>{{translate 'TABLE.COLUMNS.CREATED_BY'}}</th>
</tr>
</thead>
<tbody>
Expand All @@ -52,20 +50,68 @@
<td>{{cashbox_label}}</td>
<td>{{display_name}}</td>
</tr>
{{else}}
{{> emptyTable columns=6}}
{{/each}}
</tbody>
</table>
</div>
</div>

<div class="row">
<div class="col-xs-offset-10 col-xs-2">
<dl class="dl-horizontal">
{{#each aggregates }}
<dt>{{translate 'FORM.LABELS.TOTAL'}}</dt>
<dd>{{currency this @key}}</dd>
{{/each}}
</dl>
{{#if aggregates}}
<div class="row">
<div class="col-xs-6">

<!-- summary table -->
<table class="table table-condensed table-bordered table-striped">
<tbody>
<tr>
<th colspan="2" class="text-center">
{{ translate 'FORM.LABELS.SUMMARY' }} ({{date aggregates.minDate }} - {{date aggregates.maxDate}})
</th>
</tr>
<tr>
<th>{{ translate 'TABLE.AGGREGATES.NUM_PAYMENTS' }}</th>
<td class="text-right">{{ aggregates.numPayments }}</td>
</tr>
<tr>
<th>{{ translate 'TABLE.AGGREGATES.NUM_CAUTIONS' }}</th>
<td class="text-right">{{ aggregates.numCautions }}</td>
</tr>

{{! loop through the currency amounts and print them out }}
{{#each amounts}}
<tr>
<th>{{ translate 'TABLE.AGGREGATES.TOTAL_AMOUNT' }} ({{ symbol }})</th>
<td class="text-right">{{currency amount currency_id}}</td>
</tr>
{{/each}}

<tr>
<th>{{ translate 'TABLE.AGGREGATES.NUMBER_OF_DAYS' }}</th>
<td class="text-right">{{ aggregates.numDays }}</td>
</tr>
<tr>
<th>{{ translate 'TABLE.AGGREGATES.NUM_CLIENTS' }}</th>
<td class="text-right">{{ aggregates.numDebtors }}</td>
</tr>

{{#if hasMultipleCashboxes }}
<tr>
<th>{{ translate 'TABLE.AGGREGATES.NUM_CASHBOXES' }}</th>
<td class="text-right">{{ aggregates.numCashboxes }}</td>
</tr>
{{/if}}

{{#if hasMultipleProjects }}
<tr>
<th>{{ translate 'TABLE.AGGREGATES.NUM_PROJECTS' }}</th>
<td class="text-right">{{ aggregates.numProjects }}</td>
</tr>
{{/if}}
</tbody>
</table>
</div>
</div>
</div>
{{/if}}
</body>

0 comments on commit 140282b

Please sign in to comment.