Skip to content

Commit

Permalink
Merge branch 'master' into add_refresh_token_functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanwalsh23 committed Mar 14, 2017
2 parents 53a47c9 + 003da15 commit 0409959
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 9 deletions.
27 changes: 24 additions & 3 deletions lib/entities/taxrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ var TaxRateSchema = new Entity.SchemaObject({
DisplayTaxRate: { type: Number, toObject: 'always' },
EffectiveRate: { type: Number, toObject: 'always' },
Status: { type: String, toObject: 'always' },
TaxComponents: { type: Array, arrayType: TaxComponentSchema, toObject: 'always' }
TaxComponents: { type: Array, arrayType: TaxComponentSchema, toObject: 'always' },
ReportTaxType: { type: String, toObject: 'hasValue' }
});

var TaxComponentSchema = new Entity.SchemaObject({
Expand All @@ -38,8 +39,28 @@ var TaxRate = Entity.extend(TaxRateSchema, {
return this;
},
toXml: function() {
var taxrate = _.omit(this.toObject());
return this.application.js2xml(taxrate, 'TaxRate');
var taxRate = _.omit(this.toObject(), 'TaxComponents');
Object.assign(taxRate, { TaxComponents: [] });
_.forEach(this.TaxComponents, function(taxComponent) {
taxRate.TaxComponents.push({ TaxComponent: taxComponent })
})
return this.application.js2xml(taxRate, 'TaxRate');
},
save: function(options) {
var self = this;
var xml = '<TaxRates>' + this.toXml() + '</TaxRates>';
var path = 'TaxRates',
method;
if (this.Status) {
method = 'post'
} else {
method = 'put'
}
return this.application.putOrPostEntity(method, path, xml, { entityPath: 'TaxRates.TaxRate', entityConstructor: function(data) { return self.application.core.taxrates.newTaxRate(data) } });
},
delete: function(options) {
this.Status = "DELETED";
return this.save();
}
});

Expand Down
87 changes: 81 additions & 6 deletions test/accountingtests.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,23 @@ describe('regression tests', function() {
});
});

// There appears to be no way to archive a bank account via the API
// after('archive the test account', function() {
// testAccount.Status = 'ARCHIVED';
// return testAccount.save();
// });
// There appears to be no way to archive a bank account via the API so deleting instead
after('delete the test accounts', function() {

bankAccounts.forEach(function(account) {

currentApp.core.accounts.deleteAccount(account.id)
.then(function(response) {
expect(response.Status).to.equal("OK");
done();
})
.catch(function(err) {
console.log(util.inspect(err, null, null));
done(wrapError(err));
});
});

});

describe('organisations', function() {
it('get', function(done) {
Expand All @@ -292,6 +303,9 @@ describe('regression tests', function() {
})

describe('taxrates', function() {

var createdTaxRate;

it('gets tax rates', function(done) {
currentApp.core.taxrates.getTaxRates()
.then(function(taxRates) {
Expand Down Expand Up @@ -325,7 +339,68 @@ describe('regression tests', function() {
console.log(err);
done(wrapError(err));
})
})
});

it('creates a new tax rate', function(done) {
var taxrate = {
Name: '20% GST on Expenses',
TaxComponents: [{
Name: 'GST',
Rate: 20.1234,
IsCompound: false
}],
ReportTaxType: 'INPUT'
}

var taxRate = currentApp.core.taxrates.newTaxRate(taxrate);

taxRate.save()
.then(function(response) {
expect(response.entities).to.have.length.greaterThan(0);
createdTaxRate = response.entities[0];

expect(createdTaxRate.Name).to.equal(taxrate.Name);
expect(createdTaxRate.TaxType).to.match(/TAX[0-9]{3}/);
expect(createdTaxRate.CanApplyToAssets).to.be.oneOf([true, false]);
expect(createdTaxRate.CanApplyToEquity).to.be.oneOf([true, false]);
expect(createdTaxRate.CanApplyToExpenses).to.be.oneOf([true, false]);
expect(createdTaxRate.CanApplyToLiabilities).to.be.oneOf([true, false]);
expect(createdTaxRate.CanApplyToRevenue).to.be.oneOf([true, false]);
expect(createdTaxRate.DisplayTaxRate).to.equal(taxrate.TaxComponents[0].Rate);
expect(createdTaxRate.EffectiveRate).to.equal(taxrate.TaxComponents[0].Rate);
expect(createdTaxRate.Status).to.equal('ACTIVE');
expect(createdTaxRate.ReportTaxType).to.equal(taxrate.ReportTaxType);

createdTaxRate.TaxComponents.forEach(function(taxComponent) {
expect(taxComponent.Name).to.equal(taxrate.TaxComponents[0].Name);

//This is hacked toString() because of: https://github.com/jordanwalsh23/xero-node/issues/13
expect(taxComponent.Rate).to.equal(taxrate.TaxComponents[0].Rate.toString());
expect(taxComponent.IsCompound).to.equal(taxrate.TaxComponents[0].IsCompound.toString());
});
done();
})
.catch(function(err) {
console.log(err);
done(wrapError(err));
})
});

it('updates the taxrate to DELETED', function(done) {

createdTaxRate.delete()
.then(function(response) {
expect(response.entities).to.have.lengthOf(1);
expect(response.entities[0].Status).to.equal("DELETED");
done();
})
.catch(function(err) {
console.log(err);
done(wrapError(err));
})

});

});

describe('accounts', function() {
Expand Down

0 comments on commit 0409959

Please sign in to comment.