Skip to content

Commit

Permalink
fix(account-statement): render landscape PDF
Browse files Browse the repository at this point in the history
Ensures that the account statement PDF report renders a full page
landscape view.  Also formats the code to use async/await.  Finally,
we've included filters in the output.
  • Loading branch information
jniles committed Feb 18, 2020
1 parent 874ecde commit de8682b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 78 deletions.
51 changes: 18 additions & 33 deletions server/controllers/finance/reports/account_statement/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const _ = require('lodash');
const db = require('../../../../lib/db');
const ReportManager = require('../../../../lib/ReportManager');
const generalLedger = require('../../generalLedger');
const shared = require('../shared');

const REPORT_TEMPLATE = './server/controllers/finance/reports/account_statement/report.handlebars';

Expand All @@ -20,8 +21,7 @@ exports.report = report;
*
* @method report
*/
function report(req, res, next) {

async function report(req, res, next) {
const options = _.extend(req.query, {
filename : 'TREE.ACCOUNT_STATEMENT',
orientation : 'landscape',
Expand All @@ -30,41 +30,26 @@ function report(req, res, next) {
suppressDefaultFormatting : false,
});

let rm;
const glb = {};
const filters = shared.formatFilters(options);

try {
rm = new ReportManager(REPORT_TEMPLATE, req.session, options);
const rm = new ReportManager(REPORT_TEMPLATE, req.session, options);
const rows = await generalLedger.findTransactions(options);

const aggregateSql = `
SELECT SUM(debit_equiv) AS debit_equiv, SUM(credit_equiv) AS credit_equiv,
SUM(debit_equiv - credit_equiv) AS balance
FROM general_ledger
WHERE uuid IN (?);
`;

const transactionUuids = rows.map(row => db.bid(row.uuid));
const aggregate = await db.one(aggregateSql, [transactionUuids]);

const result = await rm.render({ rows, aggregate, filters });
res.set(result.headers).send(result.report);
} catch (e) {
next(e);
}

generalLedger.findTransactions(options)
.then((rows) => {
glb.rows = rows;

const aggregateSql = `
SELECT SUM(debit_equiv) AS debit_equiv, SUM(credit_equiv) AS credit_equiv,
SUM(debit_equiv - credit_equiv) AS balance
FROM general_ledger
WHERE uuid IN (?);
`;
const transactionIds = rows.map(row => {
return db.bid(row.uuid);
});

return db.one(aggregateSql, [transactionIds]);
})
.then((result) => {
glb.aggregate = result;
return rm.render({
rows : glb.rows,
aggregate : glb.aggregate,
});
})
.then((result) => {
res.set(result.headers).send(result.report);
})
.catch(next)
.done();
}
Original file line number Diff line number Diff line change
@@ -1,53 +1,51 @@
{{> head title="TREE.ACCOUNT_STATEMENT"}}

<div class="container">
<body>

{{> header }}

<!-- body -->
<div class="row">
<div class="col-xs-12">

<!-- page title -->
<h2 class="text-center text-uppercase">
{{translate 'TREE.ACCOUNT_STATEMENT'}}
</h2>

<br>

<!-- list of data -->
<table class="table table-condensed table-report">
<thead>
<tr>
<th>{{translate 'TABLE.COLUMNS.TRANSACTION'}}</th>
<th>{{translate 'TABLE.COLUMNS.DATE'}}</th>
<th>{{translate 'TABLE.COLUMNS.RECORD'}}</th>
<th>{{translate 'TABLE.COLUMNS.DESCRIPTION'}}</th>
<th>{{translate 'TABLE.COLUMNS.ACCOUNT'}}</th>
<th class="text-right">{{translate 'TABLE.COLUMNS.DEBIT'}}</th>
<th class="text-right">{{translate 'TABLE.COLUMNS.CREDIT'}}</th>
<th>{{translate 'TABLE.COLUMNS.RECIPIENT'}}</th>
<th>{{translate 'TABLE.COLUMNS.REFERENCE'}}</th>
</tr>
</thead>
<tbody>
{{#each rows}}
<tr>
<td>{{trans_id}}</td>
<td>{{date date}}</td>
<td>{{hrRecord}}</td>
<td>{{description}}</td>
<td>{{account_number}}</td>
<td class="text-right">{{currency debit_equiv}}</td>
<td class="text-right">{{currency credit_equiv}}</td>
<td>{{hrEntity}}</td>
<td>{{hrReference}}</td>
</tr>
{{/each}}
</tbody>
</table>
</div>
</div>
<!-- page title -->
<h2 class="text-center text-uppercase">
{{translate 'TREE.ACCOUNT_STATEMENT'}}
</h2>

<!-- filters -->
{{> filterbar filters=filters }}

<!-- list of data -->
<table class="table table-condensed table-report">
<thead>
<tr>
<th>{{translate 'TABLE.COLUMNS.TRANSACTION'}}</th>
<th>{{translate 'TABLE.COLUMNS.DATE'}}</th>
<th>{{translate 'TABLE.COLUMNS.RECORD'}}</th>
<th>{{translate 'TABLE.COLUMNS.DESCRIPTION'}}</th>
<th>{{translate 'TABLE.COLUMNS.ACCOUNT'}}</th>
<th class="text-right">{{translate 'TABLE.COLUMNS.DEBIT'}}</th>
<th class="text-right">{{translate 'TABLE.COLUMNS.CREDIT'}}</th>
<th>{{translate 'TABLE.COLUMNS.RECIPIENT'}}</th>
<th>{{translate 'TABLE.COLUMNS.REFERENCE'}}</th>
</tr>
</thead>
<tbody>
{{#each rows}}
<tr>
<td>{{trans_id}}</td>
<td>{{date date}}</td>
<td>{{hrRecord}}</td>
<td>{{description}}</td>
<td>{{account_number}}</td>
<td class="text-right">{{currency debit_equiv}}</td>
<td class="text-right">{{currency credit_equiv}}</td>
<td>{{hrEntity}}</td>
<td>{{hrReference}}</td>
</tr>
{{else}}
{{> emptyTable columns=9}}
{{/each}}
</tbody>
</table>

<div class="row">
<div class="col-xs-4">
Expand All @@ -62,4 +60,4 @@
{{translate 'TABLE.COLUMNS.RESULT'}}: <strong>{{currency aggregate.balance}}</strong>
</div>
</div>
</div>
</body>

0 comments on commit de8682b

Please sign in to comment.