diff --git a/lib/core.js b/lib/core.js index 7082f034..b4b48782 100644 --- a/lib/core.js +++ b/lib/core.js @@ -13,7 +13,7 @@ var HELPERS = { attachments: { file: 'attachments' }, accounts: { file: 'accounts' }, invoices: { file: 'invoices' }, - invoices: { file: 'invoices' }, + trackingCategories: { file: 'trackingcategories' }, invoiceReminders: { file: 'invoicereminders' }, creditNotes: { file: 'creditnotes' }, users: { file: 'users' }, diff --git a/lib/entities/accounting/creditnote.js b/lib/entities/accounting/creditnote.js index 8fcdc443..c1936406 100644 --- a/lib/entities/accounting/creditnote.js +++ b/lib/entities/accounting/creditnote.js @@ -28,8 +28,8 @@ var CreditNoteSchema = new Entity.SchemaObject({ type: CreditNoteContactSchema, toObject: 'hasValue' }, - Date: { type: Date }, - Status: { type: String }, + Date: { type: String, toObject: 'always' }, + Status: { type: String, toObject: 'hasValue' }, LineAmountTypes: { type: String }, SubTotal: { type: Number }, TotalTax: { type: Number }, @@ -37,7 +37,7 @@ var CreditNoteSchema = new Entity.SchemaObject({ UpdatedDateUTC: { type: String }, CurrencyCode: { type: String }, FullyPaidOnDate: { type: String }, - Type: { type: String }, + Type: { type: String, toObject: 'always' }, CreditNoteID: { type: String }, CreditNoteNumber: { type: String }, CurrencyRate: { type: Number }, @@ -67,6 +67,59 @@ var CreditNote = Entity.extend(CreditNoteSchema, { return this; }, + toXml: function() { + var creditNote = _.omit(this.toObject(), 'LineItems'); + + Object.assign(creditNote, { LineItems: { LineItem: [] } }); + _.forEach(this.LineItems, function(lineItem) { + creditNote.LineItems.LineItem.push(lineItem.toObject()); + }); + + return this.application.js2xml(creditNote, 'CreditNote'); + }, + save: function(options) { + var self = this; + var xml = '' + this.toXml() + ''; + var path, method; + + options = options || {}; + + if (this.CreditNoteID) { + path = 'CreditNotes/' + this.CreditNoteID; + method = 'post' + } else { + path = 'CreditNotes'; + method = 'put' + } + + //Adding other options for saving purposes + options.entityPath = 'CreditNotes.CreditNote'; + options.entityConstructor = function(data) { + return self.application.core.creditNotes.newCreditNote(data) + }; + + return this.application.putOrPostEntity(method, path, xml, options); + }, + saveAllocations: function(allocations) { + var self = this; + var xml = ''; + + _.each(allocations, function(allocation) { + xml += ""; + xml += "" + allocation.AppliedAmount + ""; + xml += ""; + xml += "" + allocation.InvoiceID + ""; + xml += ""; + xml += ""; + }); + + xml += ""; + + var path, method; + path = 'CreditNotes/' + this.CreditNoteID + "/Allocations"; + method = 'put' + return this.application.putOrPostEntity(method, path, xml, {}); + }, }); diff --git a/lib/entities/accounting/trackingoption.js b/lib/entities/accounting/trackingoption.js index 493faaa6..96403c25 100644 --- a/lib/entities/accounting/trackingoption.js +++ b/lib/entities/accounting/trackingoption.js @@ -1,53 +1,23 @@ var _ = require('lodash'), Entity = require('../entity'), logger = require('../../logger'), - dateformat = require('dateformat'), - fs = require('fs') + TrackingOptionSchema = require('../shared').TrackingOptionSchema; -var AttachmentSchema = new Entity.SchemaObject({ - AttachmentID: { type: String, toObject: 'never' }, - FileName: { type: String, toObject: 'always' }, - Url: { type: String, toObject: 'always' }, - MimeType: { type: String, toObject: 'always' }, - ContentLength: { type: Number, toObject: 'always' } -}); - - -var Attachment = Entity.extend(AttachmentSchema, { +var TrackingOption = Entity.extend(TrackingOptionSchema, { constructor: function(application, data, options) { - logger.debug('Attachment::constructor'); + logger.debug('TrackingOption::constructor'); this.Entity.apply(this, arguments); }, initialize: function(data, options) {}, - getContent: function(ownerPath) { - return this.application.core.attachments.getContent(ownerPath, this.FileName); + changes: function(options) { + return this._super(options); }, - save: function(ownerPath, streamOrFilePath) { - var self = this; - var path = ownerPath + '/Attachments/' + this.FileName; - - var base64string = base64_encode(streamOrFilePath); - console.log(base64string); - console.log(path); - - return this.application.postEntity(path, base64string, { type: this.MimeType }) - .then(function(ret) { - console.log(ret); - return ret.response.Attachments.Attachment; - }) - .catch(function(err) { - console.log(err); - }) - - function base64_encode(file) { - // read binary data - var bitmap = fs.readFileSync(file); - // convert binary data to base64 encoded string - return new Buffer(bitmap).toString('base64'); - } + _toObject: function(options) { + return this._super(options); + }, + delete: function() { + return this.deleteEntities({ id: this.TrackingOptionID }); } }); - -module.exports = Attachment; -module.exports.AttachmentSchema = AttachmentSchema; \ No newline at end of file +module.exports = TrackingOption; \ No newline at end of file diff --git a/lib/entity_helpers/accounting/creditnotes.js b/lib/entity_helpers/accounting/creditnotes.js index b68f9f9d..058f409b 100644 --- a/lib/entity_helpers/accounting/creditnotes.js +++ b/lib/entity_helpers/accounting/creditnotes.js @@ -8,10 +8,13 @@ var CreditNotes = EntityHelper.extend({ constructor: function(application, options) { EntityHelper.call(this, application, Object.assign({ entityName: 'CreditNote', entityPlural: 'CreditNotes' }, options)); }, + newCreditNote: function(data, options) { + return new CreditNote(this.application, data, options); + }, getCreditNotes: function(options, callback) { var self = this; var clonedOptions = _.clone(options || {}); - clonedOptions.entityConstructor = function(data) { return new CreditNote(data) }; + clonedOptions.entityConstructor = function(data) { return self.newCreditNote(data) }; return this.getEntities(clonedOptions) }, getCreditNote: function(id) { diff --git a/test/accountingtests.js b/test/accountingtests.js index e74bcbce..6913184d 100644 --- a/test/accountingtests.js +++ b/test/accountingtests.js @@ -17,6 +17,11 @@ process.on('uncaughtException', function(err) { var currentApp; var eventReceiver; var organisationCountry = ''; +var expenseAccount = ''; +var revenueAccount = ''; +var salesAccount = ''; + +var someContactId = ""; var APPTYPE = metaConfig.APPTYPE; var config = metaConfig[APPTYPE.toLowerCase()]; @@ -412,7 +417,6 @@ describe('regression tests', function() { it('Generates an Aged Receivables Report', function(done) { - var someContactId = ""; currentApp.core.contacts.getContacts() .then(function(contacts) { someContactId = _.first(contacts).ContactID; @@ -444,7 +448,6 @@ describe('regression tests', function() { it('Generates an Aged Payables Report', function(done) { - var someContactId = ""; currentApp.core.contacts.getContacts() .then(function(contacts) { someContactId = _.first(contacts).ContactID; @@ -471,6 +474,10 @@ describe('regression tests', function() { }) }); + function getContactID() { + + } + function validateRows(rows) { expect(rows).to.have.length.greaterThan(0); rows.forEach(function(row) { @@ -522,6 +529,215 @@ describe('regression tests', function() { }) }); + describe('accounts', function() { + + //Accounts supporting data + var accountClasses = ["ASSET", "EQUITY", "EXPENSE", "LIABILITY", "REVENUE"]; + var accountTypes = ["BANK", "CURRENT", "CURRLIAB", "DEPRECIATN", "DIRECTCOSTS", "EQUITY", "EXPENSE", "FIXED", "INVENTORY", "LIABILITY", "NONCURRENT", "OTHERINCOME", "OVERHEADS", "PREPAYMENT", "REVENUE", "SALES", "TERMLIAB", "PAYGLIABILITY", "SUPERANNUATIONEXPENSE", "SUPERANNUATIONLIABILITY", "WAGESEXPENSE", "WAGESPAYABLELIABILITY"]; + var accountStatusCodes = ["ACTIVE", "ARCHIVED"]; + var bankAccountTypes = ["BANK", "CREDITCARD", "PAYPAL"]; + + it('GET ALL', function(done) { + currentApp.core.accounts.getAccounts() + .then(function(accounts) { + + accounts.forEach(function(account) { + + //Fields required for POST / PUT + + expect(account.Code).to.be.a('string'); + expect(account.Code).to.have.length.below(11); + + expect(account.Name).to.not.equal(""); + expect(account.Name).to.be.a('string'); + + expect(account.Type).to.not.equal(""); + expect(account.Type).to.be.oneOf(accountTypes); + + if (account.Type === "BANK") { + expect(account.BankAccountType).to.be.a('string'); + expect(account.BankAccountType).to.be.oneOf(bankAccountTypes); + + expect(account.BankAccountNumber).to.be.a('string'); + expect(account.BankAccountNumber).to.not.equal(""); + + if (account.CurrencyCode) { + expect(account.CurrencyCode).to.be.a('string'); + expect(account.CurrencyCode).to.not.equal(""); + } + } + + if (account.Type === "EXPENSE" && !account.SystemAccount && !expenseAccount) { + //Save this for later use + expenseAccount = account.Code; + } + + if (account.Type === "SALES" && !account.SystemAccount && !salesAccount) { + //Save this for later use + salesAccount = account.Code; + } + + if (account.Type === "REVENUE" && !account.SystemAccount && !revenueAccount) { + //Save this for later use + revenueAccount = account.Code; + } + + expect(account.Status).to.be.a('string'); + expect(account.Status).to.be.oneOf(accountStatusCodes); + + //Description is an optional field, when not provided it should be undefined. + if (account.Description) { + expect(account.Description).to.be.a('string'); + expect(account.Description).to.have.length.below(4001); + } else { + expect(account.Description).to.be.undefined; + } + + expect(account.TaxType).to.be.a('string'); + expect(account.TaxType).to.not.equal(""); + + expect(account.EnablePaymentsToAccount).to.be.a('boolean'); + expect(account.EnablePaymentsToAccount).to.not.be.undefined; + + expect(account.ShowInExpenseClaims).to.be.a('boolean'); + expect(account.ShowInExpenseClaims).to.not.be.undefined; + + //Fields returned in the GET + expect(account.AccountID).to.not.equal(""); + expect(account.Class).to.be.oneOf(accountClasses); + + if (account.SystemAccount) { + expect(account.SystemAccount).to.not.equal(""); + expect(account.SystemAccount).to.be.a('string'); + } + + if (account.ReportingCode) { + expect(account.ReportingCode).to.not.equal(""); + expect(account.ReportingCode).to.be.a('string'); + } + + if (account.ReportingCodeName) { + expect(account.ReportingCodeName).to.not.equal(""); + expect(account.ReportingCodeName).to.be.a('string'); + } + + expect(account.HasAttachments).to.be.a('boolean'); + expect(account.HasAttachments).to.not.be.undefined; + + expect(account.UpdatedDateUTC).to.not.equal(""); + expect(account.UpdatedDateUTC).to.be.a('string'); + }); + done(); + }) + .catch(function(err) { + console.log(util.inspect(err, null, null)); + done(wrapError(err)); + }); + }); + + //Create a new account + //Get it, Update it, then delete it + + const randomString = uuid.v4(); + + var testAccountId = ""; + var testAccountData = { + Code: randomString.replace(/-/g, '').substring(0, 10), + Name: 'Test account from Node SDK ' + randomString, + Type: 'EXPENSE' + }; + + it('CREATE ONE', function(done) { + var account = currentApp.core.accounts.newAccount(testAccountData); + + account.save() + .then(function(response) { + var thisAccount = response.entities[0]; + expect(thisAccount.Code).to.equal(testAccountData.Code); + expect(thisAccount.Name).to.equal(testAccountData.Name); + expect(thisAccount.Type).to.equal(testAccountData.Type); + expect(thisAccount.BankAccountNumber).to.equal(testAccountData.BankAccountNumber); + //expect(thisAccount.Status).to.equal(testAccountData.Status); + //expect(thisAccount.Description).to.equal(testAccountData.Description); + expect(thisAccount.BankAccountType).to.equal(testAccountData.BankAccountType); + //expect(thisAccount.CurrencyCode).to.equal(testAccountData.CurrencyCode); + //expect(thisAccount.TaxType).to.equal(testAccountData.TaxType); + //expect(thisAccount.EnablePaymentsToAccount).to.equal(testAccountData.EnablePaymentsToAccount); + //expect(thisAccount.ShowInExpenseClaims).to.equal(testAccountData.ShowInExpenseClaims); + + expect(thisAccount.AccountID).to.not.equal(""); + testAccountId = thisAccount.AccountID; + + done(); + }) + .catch(function(err) { + console.log(util.inspect(err, null, null)); + done(wrapError(err)); + }); + }); + + it('GET ONE', function(done) { + currentApp.core.accounts.getAccount(testAccountId) + .then(function(account) { + expect(account.Code).to.equal(testAccountData.Code); + expect(account.Name).to.equal(testAccountData.Name); + expect(account.Type).to.equal(testAccountData.Type); + expect(account.BankAccountNumber).to.equal(testAccountData.BankAccountNumber); + //expect(account.Status).to.equal(testAccountData.Status); + //expect(account.Description).to.equal(testAccountData.Description); + expect(account.BankAccountType).to.equal(testAccountData.BankAccountType); + //expect(account.CurrencyCode).to.equal(testAccountData.CurrencyCode); + //expect(account.TaxType).to.equal(testAccountData.TaxType); + //expect(account.EnablePaymentsToAccount).to.equal(testAccountData.EnablePaymentsToAccount); + //expect(account.ShowInExpenseClaims).to.equal(testAccountData.ShowInExpenseClaims); + + expect(account.AccountID).to.not.equal(""); + done(); + }) + .catch(function(err) { + console.log(util.inspect(err, null, null)); + done(wrapError(err)); + }); + + }); + + it('UPDATE ONE', function(done) { + currentApp.core.accounts.getAccount(testAccountId) + .then(function(account) { + testAccountData.Name = "Updated from the SDK " + uuid.v4(); + account.Name = testAccountData.Name; + + account.save() + .then(function(response) { + var thisAccount = response.entities[0]; + expect(thisAccount.Name).to.equal(testAccountData.Name); + done(); + }) + .catch(function(err) { + console.log(util.inspect(err, null, null)); + done(wrapError(err)); + }); + }) + .catch(function(err) { + console.log(util.inspect(err, null, null)); + done(wrapError(err)); + }); + }); + + it('DELETE ONE', function(done) { + currentApp.core.accounts.deleteAccount(testAccountId) + .then(function(response) { + expect(response.Status).to.equal("OK"); + done(); + }) + .catch(function(err) { + console.log(util.inspect(err, null, null)); + done(wrapError(err)); + }); + }); + + }); + describe('Credit Notes', function() { var creditNoteID = ""; it('get', function(done) { @@ -546,11 +762,11 @@ describe('regression tests', function() { expect(creditNote.LineAmountTypes).to.be.oneOf(["Exclusive", "Inclusive", "NoTax"]); expect(creditNote.SubTotal).to.be.a('Number'); - expect(creditNote.SubTotal).to.be.greaterThan(0); + expect(creditNote.SubTotal).to.be.at.least(0); expect(creditNote.TotalTax).to.be.a('Number'); - expect(creditNote.TotalTax).to.be.greaterThan(0); + expect(creditNote.TotalTax).to.be.at.least(0); expect(creditNote.Total).to.be.a('Number'); - expect(creditNote.Total).to.be.greaterThan(0); + expect(creditNote.Total).to.be.at.least(0); expect(creditNote.UpdatedDateUTC).to.not.equal(""); expect(creditNote.UpdatedDateUTC).to.not.equal(undefined); @@ -558,8 +774,9 @@ describe('regression tests', function() { expect(creditNote.CurrencyCode).to.not.equal(""); expect(creditNote.CurrencyCode).to.not.equal(undefined); - expect(creditNote.FullyPaidOnDate).to.not.equal(""); - expect(creditNote.FullyPaidOnDate).to.not.equal(undefined); + if (creditNote.FullyPaidOnDate) { + expect(creditNote.FullyPaidOnDate).to.not.equal(""); + } expect(creditNote.Type).to.be.oneOf(["ACCPAYCREDIT", "ACCRECCREDIT"]); @@ -569,11 +786,12 @@ describe('regression tests', function() { //Set the variable for the next test. creditNoteID = creditNote.CreditNoteID; - expect(creditNote.CreditNoteNumber).to.not.equal(""); - expect(creditNote.CreditNoteNumber).to.not.equal(undefined); + if (creditNote.CreditNoteNumber) { + expect(creditNote.CreditNoteNumber).to.not.equal(""); + } expect(creditNote.CurrencyRate).to.be.a('Number'); - expect(creditNote.CurrencyRate).to.be.greaterThan(0); + expect(creditNote.CurrencyRate).to.be.at.least(0); expect(creditNote.RemainingCredit).to.be.a('Number'); expect(creditNote.RemainingCredit).to.be.at.least(0); @@ -628,11 +846,11 @@ describe('regression tests', function() { expect(creditNote.LineAmountTypes).to.be.oneOf(["Exclusive", "Inclusive", "NoTax"]); expect(creditNote.SubTotal).to.be.a('Number'); - expect(creditNote.SubTotal).to.be.greaterThan(0); + expect(creditNote.SubTotal).to.be.at.least(0); expect(creditNote.TotalTax).to.be.a('Number'); - expect(creditNote.TotalTax).to.be.greaterThan(0); + expect(creditNote.TotalTax).to.be.at.least(0); expect(creditNote.Total).to.be.a('Number'); - expect(creditNote.Total).to.be.greaterThan(0); + expect(creditNote.Total).to.be.at.least(0); expect(creditNote.UpdatedDateUTC).to.not.equal(""); expect(creditNote.UpdatedDateUTC).to.not.equal(undefined); @@ -640,8 +858,9 @@ describe('regression tests', function() { expect(creditNote.CurrencyCode).to.not.equal(""); expect(creditNote.CurrencyCode).to.not.equal(undefined); - expect(creditNote.FullyPaidOnDate).to.not.equal(""); - expect(creditNote.FullyPaidOnDate).to.not.equal(undefined); + if (creditNote.FullyPaidOnDate) { + expect(creditNote.FullyPaidOnDate).to.not.equal(""); + } expect(creditNote.Type).to.be.oneOf(["ACCPAYCREDIT", "ACCRECCREDIT"]); @@ -651,11 +870,12 @@ describe('regression tests', function() { //Set the variable for the next test. creditNoteID = creditNote.CreditNoteID; - expect(creditNote.CreditNoteNumber).to.not.equal(""); - expect(creditNote.CreditNoteNumber).to.not.equal(undefined); + if (creditNote.CreditNoteNumber) { + expect(creditNote.CreditNoteNumber).to.not.equal(""); + } expect(creditNote.CurrencyRate).to.be.a('Number'); - expect(creditNote.CurrencyRate).to.be.greaterThan(0); + expect(creditNote.CurrencyRate).to.be.at.least(0); expect(creditNote.RemainingCredit).to.be.a('Number'); expect(creditNote.RemainingCredit).to.be.at.least(0); @@ -732,9 +952,6 @@ describe('regression tests', function() { }); } } - - - }); } else { console.log("Credit note has no line item records"); @@ -746,6 +963,154 @@ describe('regression tests', function() { done(wrapError(err)); }) }); + + it('creates a draft credit note', function(done) { + + var creditNoteData = { + Type: 'ACCPAYCREDIT', + Contact: { + ContactID: '' + }, + LineItems: [{ + Description: 'MacBook - White', + Quantity: 1, + UnitAmount: 1995.00, + AccountCode: salesAccount + }] + }; + + currentApp.core.contacts.getContacts() + .then(function(contacts) { + creditNoteData.Contact.ContactID = _.first(contacts).ContactID; + + var creditNote = currentApp.core.creditNotes.newCreditNote(creditNoteData); + + creditNote.save() + .then(function(creditNotes) { + expect(creditNotes.entities).to.have.length(1); + var thisNote = creditNotes.entities[0]; + + creditNoteID = thisNote.CreditNoteID; + + expect(thisNote.Type).to.equal(creditNoteData.Type); + expect(thisNote.Contact.ContactID).to.equal(creditNoteData.Contact.ContactID); + + thisNote.LineItems.forEach(function(lineItem) { + expect(lineItem.Description).to.equal(creditNoteData.LineItems[0].Description); + expect(lineItem.Quantity).to.equal(creditNoteData.LineItems[0].Quantity); + expect(lineItem.UnitAmount).to.equal(creditNoteData.LineItems[0].UnitAmount); + expect(lineItem.AccountCode.toLowerCase()).to.equal(creditNoteData.LineItems[0].AccountCode.toLowerCase()); + }); + + done(); + }) + .catch(function(err) { + console.log(util.inspect(err, null, null)); + done(wrapError(err)); + }) + }) + .catch(function(err) { + console.log(util.inspect(err, null, null)); + done(wrapError(err)); + }) + + }); + + it('approves a credit note', function(done) { + + //Get the draft credit note, update it, and save it. + currentApp.core.creditNotes.getCreditNote(creditNoteID) + .then(function(creditNote) { + + creditNote.Status = 'AUTHORISED'; + creditNote.Date = new Date().toISOString().split("T")[0]; + + creditNote.save() + .then(function(savedCreditNote) { + expect(savedCreditNote.entities).to.have.length(1); + var thisNote = savedCreditNote.entities[0]; + expect(thisNote.Status).to.equal('AUTHORISED'); + done(); + }) + .catch(function(err) { + console.log(util.inspect(err, null, null)); + done(wrapError(err)); + }); + + }) + .catch(function(err) { + console.log(util.inspect(err, null, null)); + done(wrapError(err)); + }); + }); + + it('adds an allocation to a credit note', function(done) { + + currentApp.core.invoices.getInvoices({ where: 'Type == "ACCPAY" and Status != "PAID"' }) + .then(function(invoices) { + expect(invoices).to.have.length.greaterThan(0); + + var myInvoice = invoices[0]; + var myContact = myInvoice.Contact; + var myCreditNoteAmount = myInvoice.Total / 2; //50% of total invoice + + //Create the credit note. + var creditNoteData = { + Type: 'ACCPAYCREDIT', + Contact: { + ContactID: myContact.ContactID + }, + LineItems: [{ + Description: 'Credit', + Quantity: 1, + UnitAmount: myCreditNoteAmount, + AccountCode: salesAccount + }], + Status: 'AUTHORISED', + Date: new Date().toISOString().split("T")[0] + }; + + var creditNote = currentApp.core.creditNotes.newCreditNote(creditNoteData); + + creditNote.save() + .then(function(creditNotes) { + expect(creditNotes.entities).to.have.length(1); + var thisNote = creditNotes.entities[0]; + + //Now apply the allocation to the original invoice. + var allocations = [{ + AppliedAmount: myCreditNoteAmount, + InvoiceID: myInvoice.InvoiceID + }]; + + thisNote.saveAllocations(allocations) + .then(function(res) { + //console.log(res); + done(); + }) + .catch(function(err) { + console.log(util.inspect(err, null, null)); + done(wrapError(err)); + }); + + + }) + .catch(function(err) { + console.log(util.inspect(err, null, null)); + done(wrapError(err)); + }); + + + + }) + .catch(function(err) { + console.log(util.inspect(err, null, null)); + done(wrapError(err)); + }); + + }); + + }); describe('currencies', function() { @@ -877,8 +1242,12 @@ describe('regression tests', function() { Name: 'GST', Rate: 20.1234, IsCompound: false - }], - ReportTaxType: 'INPUT' + }] + }; + + if (["AU", "NZ", "UK"].indexOf(organisationCountry) > -1) { + //we're an Org country that needs a report tax type so: + taxrate.ReportTaxType = 'INPUT'; } var taxRate = currentApp.core.taxRates.newTaxRate(taxrate); @@ -932,200 +1301,6 @@ describe('regression tests', function() { }); - describe('accounts', function() { - - //Accounts supporting data - var accountClasses = ["ASSET", "EQUITY", "EXPENSE", "LIABILITY", "REVENUE"]; - var accountTypes = ["BANK", "CURRENT", "CURRLIAB", "DEPRECIATN", "DIRECTCOSTS", "EQUITY", "EXPENSE", "FIXED", "INVENTORY", "LIABILITY", "NONCURRENT", "OTHERINCOME", "OVERHEADS", "PREPAYMENT", "REVENUE", "SALES", "TERMLIAB", "PAYGLIABILITY", "SUPERANNUATIONEXPENSE", "SUPERANNUATIONLIABILITY", "WAGESEXPENSE", "WAGESPAYABLELIABILITY"]; - var accountStatusCodes = ["ACTIVE", "ARCHIVED"]; - var bankAccountTypes = ["BANK", "CREDITCARD", "PAYPAL"]; - - it('GET ALL', function(done) { - currentApp.core.accounts.getAccounts() - .then(function(accounts) { - - accounts.forEach(function(account) { - - //Fields required for POST / PUT - - expect(account.Code).to.be.a('string'); - expect(account.Code).to.have.length.below(11); - - expect(account.Name).to.not.equal(""); - expect(account.Name).to.be.a('string'); - - expect(account.Type).to.not.equal(""); - expect(account.Type).to.be.oneOf(accountTypes); - - if (account.Type === "BANK") { - expect(account.BankAccountType).to.be.a('string'); - expect(account.BankAccountType).to.be.oneOf(bankAccountTypes); - - expect(account.BankAccountNumber).to.be.a('string'); - expect(account.BankAccountNumber).to.not.equal(""); - - if (account.CurrencyCode) { - expect(account.CurrencyCode).to.be.a('string'); - expect(account.CurrencyCode).to.not.equal(""); - } - } - - expect(account.Status).to.be.a('string'); - expect(account.Status).to.be.oneOf(accountStatusCodes); - - //Description is an optional field, when not provided it should be undefined. - if (account.Description) { - expect(account.Description).to.be.a('string'); - expect(account.Description).to.have.length.below(4001); - } else { - expect(account.Description).to.be.undefined; - } - - expect(account.TaxType).to.be.a('string'); - expect(account.TaxType).to.not.equal(""); - - expect(account.EnablePaymentsToAccount).to.be.a('boolean'); - expect(account.EnablePaymentsToAccount).to.not.be.undefined; - - expect(account.ShowInExpenseClaims).to.be.a('boolean'); - expect(account.ShowInExpenseClaims).to.not.be.undefined; - - //Fields returned in the GET - expect(account.AccountID).to.not.equal(""); - expect(account.Class).to.be.oneOf(accountClasses); - - if (account.SystemAccount) { - expect(account.SystemAccount).to.not.equal(""); - expect(account.SystemAccount).to.be.a('string'); - } - - if (account.ReportingCode) { - expect(account.ReportingCode).to.not.equal(""); - expect(account.ReportingCode).to.be.a('string'); - } - - if (account.ReportingCodeName) { - expect(account.ReportingCodeName).to.not.equal(""); - expect(account.ReportingCodeName).to.be.a('string'); - } - - expect(account.HasAttachments).to.be.a('boolean'); - expect(account.HasAttachments).to.not.be.undefined; - - expect(account.UpdatedDateUTC).to.not.equal(""); - expect(account.UpdatedDateUTC).to.be.a('string'); - }); - done(); - }) - .catch(function(err) { - console.log(util.inspect(err, null, null)); - done(wrapError(err)); - }); - }); - - //Create a new account - //Get it, Update it, then delete it - - const randomString = uuid.v4(); - - var testAccountId = ""; - var testAccountData = { - Code: randomString.replace(/-/g, '').substring(0, 10), - Name: 'Test account from Node SDK ' + randomString, - Type: 'EXPENSE' - }; - - it('CREATE ONE', function(done) { - var account = currentApp.core.accounts.newAccount(testAccountData); - - account.save() - .then(function(response) { - var thisAccount = response.entities[0]; - expect(thisAccount.Code).to.equal(testAccountData.Code); - expect(thisAccount.Name).to.equal(testAccountData.Name); - expect(thisAccount.Type).to.equal(testAccountData.Type); - expect(thisAccount.BankAccountNumber).to.equal(testAccountData.BankAccountNumber); - //expect(thisAccount.Status).to.equal(testAccountData.Status); - //expect(thisAccount.Description).to.equal(testAccountData.Description); - expect(thisAccount.BankAccountType).to.equal(testAccountData.BankAccountType); - //expect(thisAccount.CurrencyCode).to.equal(testAccountData.CurrencyCode); - //expect(thisAccount.TaxType).to.equal(testAccountData.TaxType); - //expect(thisAccount.EnablePaymentsToAccount).to.equal(testAccountData.EnablePaymentsToAccount); - //expect(thisAccount.ShowInExpenseClaims).to.equal(testAccountData.ShowInExpenseClaims); - - expect(thisAccount.AccountID).to.not.equal(""); - testAccountId = thisAccount.AccountID; - - done(); - }) - .catch(function(err) { - console.log(util.inspect(err, null, null)); - done(wrapError(err)); - }); - }); - - it('GET ONE', function(done) { - currentApp.core.accounts.getAccount(testAccountId) - .then(function(account) { - expect(account.Code).to.equal(testAccountData.Code); - expect(account.Name).to.equal(testAccountData.Name); - expect(account.Type).to.equal(testAccountData.Type); - expect(account.BankAccountNumber).to.equal(testAccountData.BankAccountNumber); - //expect(account.Status).to.equal(testAccountData.Status); - //expect(account.Description).to.equal(testAccountData.Description); - expect(account.BankAccountType).to.equal(testAccountData.BankAccountType); - //expect(account.CurrencyCode).to.equal(testAccountData.CurrencyCode); - //expect(account.TaxType).to.equal(testAccountData.TaxType); - //expect(account.EnablePaymentsToAccount).to.equal(testAccountData.EnablePaymentsToAccount); - //expect(account.ShowInExpenseClaims).to.equal(testAccountData.ShowInExpenseClaims); - - expect(account.AccountID).to.not.equal(""); - done(); - }) - .catch(function(err) { - console.log(util.inspect(err, null, null)); - done(wrapError(err)); - }); - - }); - - it('UPDATE ONE', function(done) { - currentApp.core.accounts.getAccount(testAccountId) - .then(function(account) { - testAccountData.Name = "Updated from the SDK " + uuid.v4(); - account.Name = testAccountData.Name; - - account.save() - .then(function(response) { - var thisAccount = response.entities[0]; - expect(thisAccount.Name).to.equal(testAccountData.Name); - done(); - }) - .catch(function(err) { - console.log(util.inspect(err, null, null)); - done(wrapError(err)); - }); - }) - .catch(function(err) { - console.log(util.inspect(err, null, null)); - done(wrapError(err)); - }); - }); - - it('DELETE ONE', function(done) { - currentApp.core.accounts.deleteAccount(testAccountId) - .then(function(response) { - expect(response.Status).to.equal("OK"); - done(); - }) - .catch(function(err) { - console.log(util.inspect(err, null, null)); - done(wrapError(err)); - }); - }); - - }); - describe('invoices', function() { it('create invoice', function(done) { var invoice = currentApp.core.invoices.newInvoice({ @@ -1138,7 +1313,7 @@ describe('regression tests', function() { Description: 'Services', Quantity: 2, UnitAmount: 230, - AccountCode: '400' + AccountCode: salesAccount }] }); invoice.save({ unitdp: 4 }) @@ -1198,7 +1373,7 @@ describe('regression tests', function() { Description: 'Test', Quantity: 1, UnitAmount: 200, - AccountCode: '400' + AccountCode: salesAccount }) invoice.Status = 'AUTHORISED' invoice.save() @@ -1234,7 +1409,7 @@ describe('regression tests', function() { Description: 'Services', Quantity: 2, UnitAmount: 230, - AccountCode: '400' + AccountCode: salesAccount }] })); } @@ -1383,7 +1558,7 @@ describe('regression tests', function() { LineItems: [{ Description: 'Annual Bank Account Fee', UnitAmount: 250, - AccountCode: '404' + AccountCode: expenseAccount }], BankAccount: { AccountID: bankAccounts[0].id @@ -1615,7 +1790,7 @@ describe('regression tests', function() { Description: 'Services', Quantity: 2, UnitAmount: 230, - AccountCode: '400', + AccountCode: salesAccount, Tracking: [{ TrackingCategory: { Name: trackingCategoryName, @@ -1679,11 +1854,11 @@ describe('regression tests', function() { PurchaseDescription: '2014 Merino Sweater', PurchaseDetails: { UnitPrice: 149.00, - AccountCode: '200' + AccountCode: salesAccount }, SalesDetails: { UnitPrice: 299.00, - AccountCode: '200' + AccountCode: salesAccount } };