From bf11e6778da40d3c5c1cbe9223c56a3a9d7cbaca Mon Sep 17 00:00:00 2001 From: Jordan Walsh Date: Wed, 15 Mar 2017 16:08:42 +1100 Subject: [PATCH] Added support for 9 reports. '1': 'BalanceSheet', '2': 'TrialBalance', '3': 'ProfitAndLoss', '4': 'BankStatement', '5': 'BudgetSummary', '6': 'ExecutiveSummary', '7': 'BankSummary', '8': 'AgedReceivablesByContact', '9': 'AgedPayablesByContact' The missing ones are the 1099 report (US Only) and the GST/BAS report (AU/NZ). These will be added in a subsequent feature release. --- lib/entities/accounting/report.js | 13 +++-- sample_app/index.js | 87 +++++++++++++++++++++++++---- sample_app/views/reports.handlebars | 55 ++++++++++++++---- test/accountingtests.js | 2 +- 4 files changed, 128 insertions(+), 29 deletions(-) diff --git a/lib/entities/accounting/report.js b/lib/entities/accounting/report.js index 9c8ae180..2b1063b0 100644 --- a/lib/entities/accounting/report.js +++ b/lib/entities/accounting/report.js @@ -6,10 +6,10 @@ var ReportSchema = new Entity.SchemaObject({ ReportID: { type: String, toObject: 'always' }, ReportName: { type: String, toObject: 'always' }, ReportType: { type: String, toObject: 'always' }, - ReportTitles: [ReportTitleSchema], + ReportTitles: { type: Array, arrayType: ReportTitleSchema, toObject: 'always' }, ReportDate: { type: String, toObject: 'always' }, UpdatedDateUTC: { type: String, toObject: 'always' }, - Rows: [ReportRowSchema] + Rows: { type: Array, arrayType: ReportRowSchema, toObject: 'always' } }); var ReportTitleSchema = new Entity.SchemaObject({ @@ -19,12 +19,12 @@ var ReportTitleSchema = new Entity.SchemaObject({ var ReportRowSchema = new Entity.SchemaObject({ RowType: { type: String, toObject: 'always' }, Title: { type: String, toObject: 'always' }, - Cells: [ReportCellSchema] + Cells: { type: Array, arrayType: ReportCellSchema, toObject: 'always' } }); var ReportCellSchema = new Entity.SchemaObject({ Value: { type: String, toObject: 'always' }, - Attributes: [ReportAttributeSchema] + Attributes: { type: Array, arrayType: ReportAttributeSchema, toObject: 'always' } }); var ReportAttributeSchema = new Entity.SchemaObject({ @@ -43,7 +43,10 @@ var Report = Entity.extend(ReportSchema, { Object.assign(self, _.omit(obj, 'Rows', 'Cells', 'ReportTitles')); if (obj.ReportTitles) { - this.extractArray(obj.ReportTitles.ReportTitle, this.ReportTitles); + var reportTitles = this.application.asArray(obj.ReportTitles.ReportTitle); + _.each(reportTitles, function(reportTitle) { + self.ReportTitles.push(reportTitle); + }) } if (hasMoreRows(obj)) { diff --git a/sample_app/index.js b/sample_app/index.js index e3777fc4..6b55feb6 100644 --- a/sample_app/index.js +++ b/sample_app/index.js @@ -79,6 +79,17 @@ var exphbs = exphbs.create({ default: return options.inverse(this); } + }, + debug: function(optionalValue) { + console.log("Current Context"); + console.log("===================="); + console.log(this); + + if (optionalValue) { + console.log("Value"); + console.log("===================="); + console.log(optionalValue); + } } } }); @@ -462,18 +473,70 @@ app.get('/reports', function(req, res) { data.active[selectedReport.toLowerCase()] = true; - xeroClient.core.reports.generateReport({ - id: selectedReport - }) - .then(function(report) { - console.log(report); - data.report = report; - res.render('reports', data); - }) - .catch(function(err) { - handleErr(err, req, res, 'reports'); - }) - + /** + * We may need some dependent data: + * + * BankStatement - requires a BankAccountId + * AgedReceivablesByContact - requires a ContactId + * AgedPayablesByContact - requires a ContactId + * + */ + + if (selectedReport == 'BankStatement') { + xeroClient.core.accounts.getAccounts({ where: 'Type=="BANK"' }) + .then(function(accounts) { + xeroClient.core.reports.generateReport({ + id: selectedReport, + params: { + bankAccountID: accounts[0].AccountID + } + }) + .then(function(report) { + data.report = report.toObject(); + data.colspan = data.report.Rows[0].Cells.length; + res.render('reports', data); + }) + .catch(function(err) { + handleErr(err, req, res, 'reports'); + }); + }) + .catch(function(err) { + handleErr(err, req, res, 'reports'); + }); + } else if (selectedReport == 'AgedReceivablesByContact' || selectedReport == 'AgedPayablesByContact') { + xeroClient.core.contacts.getContacts() + .then(function(contacts) { + xeroClient.core.reports.generateReport({ + id: selectedReport, + params: { + contactID: contacts[0].ContactID + } + }) + .then(function(report) { + data.report = report.toObject(); + data.colspan = data.report.Rows[0].Cells.length; + res.render('reports', data); + }) + .catch(function(err) { + handleErr(err, req, res, 'reports'); + }); + }) + .catch(function(err) { + handleErr(err, req, res, 'reports'); + }); + } else { + xeroClient.core.reports.generateReport({ + id: selectedReport + }) + .then(function(report) { + data.report = report.toObject(); + data.colspan = data.report.Rows[0].Cells.length; + res.render('reports', data); + }) + .catch(function(err) { + handleErr(err, req, res, 'reports'); + }); + } } else { res.render('index', { diff --git a/sample_app/views/reports.handlebars b/sample_app/views/reports.handlebars index 0f11ff67..ae800ee3 100644 --- a/sample_app/views/reports.handlebars +++ b/sample_app/views/reports.handlebars @@ -1,21 +1,54 @@

{{report.ReportName}}

{{#each report.ReportTitles as |reportTitle|}} +
{{reportTitle}}
+
{{/each}} -
+

Report Date: {{report.ReportDate}}

{{#if report.Rows}} - - {{#each report.Rows}} - - {{#if this.Cells}} - {{#each this.Cells}} - +
{{this.Value}}
+ {{#each report.Rows as |row|}} + + {{#ifCond row.RowType '==' 'Header'}} + + {{#each row.Cells as |cell|}} + {{/each}} - {{else}} - + + {{/ifCond}} + + {{#ifCond row.RowType '!=' 'Header'}} + + + + + + {{#if row.Rows}} + {{#each row.Rows as |thisRow|}} + + {{#if this.Cells}} + {{#each this.Cells as |thisCell|}} + {{#if thisCell.Value}} + + {{else}} + {{#if thisCell.length}} + {{#each thisCell}} + + {{/each}} + {{else}} + + {{/if}} + {{/if}} + {{/each}} + {{/if}} + + {{/each}} + {{/if}} + + {{/ifCond}} + + {{/each}}
{{cell.Value}} - {{/if}} -
{{row.Title}}
    {{thisCell.Value}}{{this.Value}} 
{{/if}} diff --git a/test/accountingtests.js b/test/accountingtests.js index b02933a8..e6e0e049 100644 --- a/test/accountingtests.js +++ b/test/accountingtests.js @@ -411,7 +411,7 @@ describe('reporting tests', function() { }) -describe.skip('regression tests', function() { +describe('regression tests', function() { var InvoiceID = ""; var PaymentID = "";