From c72386b6fe594125f0bb4ee5870e976a38633028 Mon Sep 17 00:00:00 2001 From: Jonathan Niles Date: Fri, 2 Mar 2018 10:44:32 +0100 Subject: [PATCH] feat(General Ledger): add totals on PDF printout This commit adds totals to the PDF print out, as well as ensuring that the depth is correctly computed. Due to API changes with the tree library, the depth was removed - this restores the calculation. --- .../finance/reports/generalLedger/index.js | 28 +++++++++-- .../reports/generalLedger/report.handlebars | 49 +++++++++++++------ server/lib/Tree.js | 5 +- 3 files changed, 61 insertions(+), 21 deletions(-) diff --git a/server/controllers/finance/reports/generalLedger/index.js b/server/controllers/finance/reports/generalLedger/index.js index 294c3686ca..1462983284 100644 --- a/server/controllers/finance/reports/generalLedger/index.js +++ b/server/controllers/finance/reports/generalLedger/index.js @@ -9,6 +9,7 @@ const _ = require('lodash'); const ReportManager = require('../../../../lib/ReportManager'); const GeneralLedger = require('../../generalLedger'); +const Tree = require('../../../../lib/Tree'); const REPORT_TEMPLATE = './server/controllers/finance/reports/generalLedger/report.handlebars'; @@ -41,12 +42,29 @@ function renderReport(req, res, next) { const TITLE_ACCOUNT_ID = 6; return GeneralLedger.getAccountTotalsMatrix(fiscalYearId) - .then((rows) => { - rows.forEach(row => { - row.isTitleAccount = row.type_id === TITLE_ACCOUNT_ID; - row.padLeft = row.depth * 15; + .then(rows => { + const tree = new Tree(rows); + + tree.walk((node, parentNode) => { + Tree.common.computeNodeDepth(node, parentNode); + + node.isTitleAccount = node.type_id === TITLE_ACCOUNT_ID; + node.padLeft = node.depth * 15; }); - data = { rows }; + + const keys = _.keys(rows[0]) + .filter(name => name.includes('balance')); + + const root = tree.getRootNode(); + const balances = root.children.reduce((aggregates, node) => { + keys.forEach(key => { + aggregates[key] = (aggregates[key] || 0) + node[key]; + }); + + return aggregates; + }, {}); + + data = { rows : tree.toArray(), footer : balances }; data.fiscal_year_label = options.fiscal_year_label; return report.render(data); }) diff --git a/server/controllers/finance/reports/generalLedger/report.handlebars b/server/controllers/finance/reports/generalLedger/report.handlebars index a43fa25b82..77b4528721 100644 --- a/server/controllers/finance/reports/generalLedger/report.handlebars +++ b/server/controllers/finance/reports/generalLedger/report.handlebars @@ -40,77 +40,96 @@ {{number}} {{label}} - {{currency balance ../metadata.enterprise.currency_id}} + {{debcred balance ../metadata.enterprise.currency_id}} {{#if balance0}} - {{currency balance0 ../metadata.enterprise.currency_id}} + {{debcred balance0 ../metadata.enterprise.currency_id}} {{/if}} {{#if balance1}} - {{currency balance1 ../metadata.enterprise.currency_id}} + {{debcred balance1 ../metadata.enterprise.currency_id}} {{/if}} {{#if balance2}} - {{currency balance2 ../metadata.enterprise.currency_id}} + {{debcred balance2 ../metadata.enterprise.currency_id}} {{/if}} {{#if balance3}} - {{currency balance3 ../metadata.enterprise.currency_id}} + {{debcred balance3 ../metadata.enterprise.currency_id}} {{/if}} {{#if balance4}} - {{currency balance4 ../metadata.enterprise.currency_id}} + {{debcred balance4 ../metadata.enterprise.currency_id}} {{/if}} {{#if balance5}} - {{currency balance5 ../metadata.enterprise.currency_id}} + {{debcred balance5 ../metadata.enterprise.currency_id}} {{/if}} {{#if balance6}} - {{currency balance6 ../metadata.enterprise.currency_id}} + {{debcred balance6 ../metadata.enterprise.currency_id}} {{/if}} {{#if balance7}} - {{currency balance7 ../metadata.enterprise.currency_id}} + {{debcred balance7 ../metadata.enterprise.currency_id}} {{/if}} {{#if balance8}} - {{currency balance8 ../metadata.enterprise.currency_id}} + {{debcred balance8 ../metadata.enterprise.currency_id}} {{/if}} {{#if balance9}} - {{currency balance9 ../metadata.enterprise.currency_id}} + {{debcred balance9 ../metadata.enterprise.currency_id}} {{/if}} {{#if balance10}} - {{currency balance10 ../metadata.enterprise.currency_id}} + {{debcred balance10 ../metadata.enterprise.currency_id}} {{/if}} {{#if balance11}} - {{currency balance11 ../metadata.enterprise.currency_id}} + {{debcred balance11 ../metadata.enterprise.currency_id}} {{/if}} {{#if balance12}} - {{currency balance12 ../metadata.enterprise.currency_id}} + {{debcred balance12 ../metadata.enterprise.currency_id}} {{/if}} {{else}} - {{> emptyTable columns=4}} + {{> emptyTable columns=16}} {{/each}} + + + {{translate "FORM.LABELS.TOTAL"}} + {{debcred footer.balance metadata.enterprise.currency_id}} + {{debcred footer.balance0 metadata.enterprise.currency_id}} + {{debcred footer.balance1 metadata.enterprise.currency_id}} + {{debcred footer.balance2 metadata.enterprise.currency_id}} + {{debcred footer.balance3 metadata.enterprise.currency_id}} + {{debcred footer.balance4 metadata.enterprise.currency_id}} + {{debcred footer.balance5 metadata.enterprise.currency_id}} + {{debcred footer.balance6 metadata.enterprise.currency_id}} + {{debcred footer.balance7 metadata.enterprise.currency_id}} + {{debcred footer.balance8 metadata.enterprise.currency_id}} + {{debcred footer.balance9 metadata.enterprise.currency_id}} + {{debcred footer.balance10 metadata.enterprise.currency_id}} + {{debcred footer.balance11 metadata.enterprise.currency_id}} + {{debcred footer.balance12 metadata.enterprise.currency_id}} + + diff --git a/server/lib/Tree.js b/server/lib/Tree.js index 92171e06f0..b50b800e6a 100644 --- a/server/lib/Tree.js +++ b/server/lib/Tree.js @@ -25,7 +25,6 @@ class Tree { this.buildNodeIndex(); // build a node index - debug(`#constructor() built tree with ${data.length} nodes.`); } @@ -79,6 +78,10 @@ class Tree { return array; } + getRootNode() { + return this._rootNode; + } + /** * @method isRootNode *