diff --git a/lib/application.js b/lib/application.js index c532f664..0c89e9a6 100644 --- a/lib/application.js +++ b/lib/application.js @@ -560,4 +560,4 @@ var PartnerApplication = RequireAuthorizationApplication.extend({ module.exports.PrivateApplication = PrivateApplication; module.exports.PublicApplication = PublicApplication; module.exports.PartnerApplication = PartnerApplication; -module.exports.Application = Application; +module.exports.Application = Application; \ No newline at end of file diff --git a/lib/core.js b/lib/core.js index 9942184e..d2227832 100644 --- a/lib/core.js +++ b/lib/core.js @@ -14,7 +14,8 @@ var HELPERS = { trackingCategories: { file: 'trackingcategories' }, users: { file: 'users' }, payments: { file: 'payments' }, - taxrates: { file: 'taxrates' } + taxrates: { file: 'taxrates' }, + reports: { file: 'reports' } }; function Core(application, options) { diff --git a/lib/entities/accounting/report.js b/lib/entities/accounting/report.js new file mode 100644 index 00000000..2b23517f --- /dev/null +++ b/lib/entities/accounting/report.js @@ -0,0 +1,58 @@ +var _ = require('lodash'), + Entity = require('../entity'), + logger = require('../../logger'); + +var ReportSchema = new Entity.SchemaObject({ + ReportID: { type: String, toObject: 'always' }, + ReportName: { type: String, toObject: 'always' }, + ReportType: { type: String, toObject: 'always' }, + ReportTitles: [ReportTitleSchema], + ReportDate: { type: String, toObject: 'always' }, + UpdatedDateUTC: { type: String, toObject: 'always' }, + Rows: [ReportRowSchema] +}); + +var ReportTitleSchema = new Entity.SchemaObject({ + ReportTitle: { type: String, toObject: 'always' } +}); + +var ReportRowSchema = new Entity.SchemaObject({ + RowType: { type: String, toObject: 'always' }, + Title: { type: String, toObject: 'always' }, + Cells: [ReportCellSchema] +}); + +var ReportCellSchema = new Entity.SchemaObject({ + Value: { type: String, toObject: 'always' }, + Attributes: [ReportAttributeSchema] +}); + +var ReportAttributeSchema = new Entity.SchemaObject({ + Value: { type: String, toObject: 'always' }, + Id: { type: String, toObject: 'always' } +}); + +var Report = Entity.extend(ReportSchema, { + constructor: function(application, data, options) { + logger.debug('Report::constructor'); + this.Entity.apply(this, arguments); + }, + initialize: function(data, options) {}, + fromXmlObj: function(obj) { + var self = this; + Object.assign(self, _.omit(obj, 'Rows', 'Cells')); + if (obj.Rows) { + _.each(obj.Rows.Row, function(row) { + _.each(row.Cells.Cell, function(cell) { + console.log(cell); + }); + self.Rows.push(row); + }); + } + + return this; + } +}); + +module.exports = Report; +module.exports.ItemSchema = ReportSchema; \ No newline at end of file diff --git a/lib/entity_helpers/accounting/reports.js b/lib/entity_helpers/accounting/reports.js new file mode 100644 index 00000000..9ae26c6e --- /dev/null +++ b/lib/entity_helpers/accounting/reports.js @@ -0,0 +1,25 @@ +var _ = require('lodash'), + logger = require('../../logger'), + EntityHelper = require('../entity_helper'), + Report = require('../../entities/accounting/report'), + util = require('util') + +var Reports = EntityHelper.extend({ + constructor: function(application, options) { + EntityHelper.call(this, application, Object.assign({ entityName: 'Report', entityPlural: 'Reports' }, options)); + }, + create: function(data, options) { + return new Report(this.application, data, options) + }, + generateReport: function(options) { + var self = this; + var clonedOptions = _.clone(options || {}); + clonedOptions.entityConstructor = function(data) { return self.create(data) }; + return this.getEntities(clonedOptions) + .then(function(reports) { + return _.first(reports); + }); + } +}) + +module.exports = Reports; \ No newline at end of file diff --git a/test/accountingtests.js b/test/accountingtests.js index 305f6ec3..13c340ef 100644 --- a/test/accountingtests.js +++ b/test/accountingtests.js @@ -66,7 +66,7 @@ describe('get access for public or partner application', function() { console.log("URL: " + authoriseUrl); console.log("token: " + requestToken); console.log("secret: " + requestSecret); - }); + }); }); describe('gets the request token from the url', function() { @@ -149,7 +149,7 @@ describe('get access for public or partner application', function() { describe('swaps the request token for an access token', function() { it('calls the access token method and sets the options', function() { return currentApp.getAccessToken(requestToken, requestSecret, verifier) - .then(function({token, secret}) { + .then(function({ token, secret }) { currentApp.setOptions({ accessToken: token, accessSecret: secret }); }); }); @@ -157,7 +157,30 @@ describe('get access for public or partner application', function() { }); }); -describe('regression tests', function() { +describe('reporting tests', function() { + this.timeout(10000); + it('generates a Balance Sheet', function(done) { + currentApp.core.reports.generateReport({ id: 'BalanceSheet' }) + .then(function(report) { + expect(report.ReportType).to.equal('BalanceSheet'); + expect(report.ReportName).to.equal('Balance Sheet'); + expect(report.Rows).to.have.length.greaterThan(0); + report.Rows.forEach(function(row) { + expect(row.RowType).to.be.oneOf(['Header', 'Section', 'Row', 'SummaryRow']); + expect(row.Cells).to.have.length.greaterThan(0); + }); + done(); + }) + .catch(function(err) { + console.log(err); + done(wrapError(err)); + }) + + }); + +}) + +describe.skip('regression tests', function() { var InvoiceID = ""; var PaymentID = ""; @@ -230,6 +253,7 @@ describe('regression tests', function() { done(); }) .catch(function(err) { + console.log(err); done(wrapError(err)); }) })