From b5b82aedf87bfbb156f935b9a78b06cc8a82821c Mon Sep 17 00:00:00 2001 From: Jordan Walsh Date: Wed, 22 Feb 2017 12:25:07 +1100 Subject: [PATCH] updated items to support retrieving and saving --- lib/entities/invoice.js | 3 ++ lib/entities/item.js | 58 ++++++++++++++++++---------------- lib/entity_helpers/invoices.js | 49 ++++++++++------------------ test/accountingtests.js | 48 +++++++++++++++++++++++++++- 4 files changed, 98 insertions(+), 60 deletions(-) diff --git a/lib/entities/invoice.js b/lib/entities/invoice.js index 84fb1de4..97d50e39 100644 --- a/lib/entities/invoice.js +++ b/lib/entities/invoice.js @@ -43,6 +43,7 @@ var InvoiceSchema = new Entity.SchemaObject({ SubTotal: { type: Number, toObject: 'hasValue' }, TotalTax: { type: Number, toObject: 'hasValue' }, Total: { type: Number, toObject: 'hasValue' }, + TotalDiscount: { type: String, toObject: 'hasValue' }, InvoiceID: { type: String, toObject: 'hasValue' }, HasAttachments: { type: Boolean, toObject: 'hasValue' }, Payments: { type: Array, arrayType: PaymentSchema, toObject: 'hasValue' }, @@ -52,6 +53,8 @@ var InvoiceSchema = new Entity.SchemaObject({ AmountCredited: { type: Number, toObject: 'hasValue' }, UpdatedDateUTC: { type: Date, toObject: 'hasValue' } //CreditNotes: {type: Array, arrayType: CreditNoteSchema, toObject: 'always'}, + //Prepayments: {type: Array, arrayType: PrepaymentsSchema, toObject: 'always'}, + //Overpayments: {type: Array, arrayType: OverpaymentsSchema, toObject: 'always'}, }); var Invoice = Entity.extend(InvoiceSchema, { diff --git a/lib/entities/item.js b/lib/entities/item.js index 22efbb7f..911bc9db 100644 --- a/lib/entities/item.js +++ b/lib/entities/item.js @@ -1,53 +1,57 @@ -var _ = require('lodash') - , Entity = require('./entity') - , logger = require('../logger') +var _ = require('lodash'), + Entity = require('./entity'), + logger = require('../logger') var ItemDetailSchema = new Entity.SchemaObject({ - UnitPrice: {type: Number}, - AccountCode: {type: String}, - TaxType: {type: String} + UnitPrice: { type: Number, toObject: 'hasValue' }, + AccountCode: { type: String, toObject: 'hasValue' }, + COGSAccountCode: { type: String, toObject: 'hasValue' }, + TaxType: { type: String, toObject: 'hasValue' } + }); var ItemSchema = new Entity.SchemaObject({ + Code: { type: String, toObject: 'always', validators: { maxLength: 30 } }, + InventoryAssetAccountCode: { type: String, toObject: 'hasValue' }, + Name: { type: String, toObject: 'always' }, + IsSold: { type: Boolean, toObject: 'always' }, + IsPurchased: { type: Boolean, toObject: 'always' }, + Description: { type: String, toObject: 'always' }, + PurchaseDescription: { type: String, toObject: 'always' }, ItemID: { type: String, toObject: 'never' }, - Code: {type: String, toObject: 'always'}, - Description: {type: String,toObject:'always'}, - PurchaseDetails: {type: ItemDetailSchema}, - SalesDetails: {type: ItemDetailSchema} + PurchaseDetails: { type: ItemDetailSchema, toObject: 'always' }, + SalesDetails: { type: ItemDetailSchema, toObject: 'always' }, + IsTrackedAsInventory: { type: Boolean, toObject: 'hasValue' }, + TotalCostPool: { type: String, toObject: 'hasValue' }, + QuantityOnHand: { type: String, toObject: 'hasValue' }, + UpdatedDateUTC: { type: String, toObject: 'hasValue' } }); var Item = Entity.extend(ItemSchema, { - constructor: function (application, data, options) - { + constructor: function(application, data, options) { logger.debug('Item::constructor'); this.Entity.apply(this, arguments); }, - initialize: function (data, options) - { - }, - toXml: function () - { + initialize: function(data, options) {}, + toXml: function() { var item = _.omit(this.toObject()); - return this.application.js2xml(account, 'Item'); + return this.application.js2xml(item, 'Item'); }, - save:function(options) - { + save: function(options) { + var self = this; var xml = '' + this.toXml() + ''; var path, method; - if (this.ItemID) - { + if (this.ItemID) { path = 'Items/' + this.ItemID; method = 'post' - } - else - { + } else { path = 'Items'; method = 'put' } - return this.application.putOrPostEntity(method, path, xml,{ entityPath: 'Items.Item',entityConstructor: function(data) { return self.application.core.items.newItem(data)}}); + return this.application.putOrPostEntity(method, path, xml, { entityPath: 'Items.Item', entityConstructor: function(data) { return self.application.core.items.newItem(data) } }); } }); module.exports = Item; -module.exports.ItemSchema = ItemSchema; +module.exports.ItemSchema = ItemSchema; \ No newline at end of file diff --git a/lib/entity_helpers/invoices.js b/lib/entity_helpers/invoices.js index 71c1c947..2b2a603f 100644 --- a/lib/entity_helpers/invoices.js +++ b/lib/entity_helpers/invoices.js @@ -1,47 +1,32 @@ -var _ = require('lodash') - , logger = require('../logger') - , EntityHelper = require('./entity_helper') - , Invoice = require('../entities/invoice') - , p = require('../misc/promise') - , util = require('util') +var _ = require('lodash'), + logger = require('../logger'), + EntityHelper = require('./entity_helper'), + Invoice = require('../entities/invoice'), + p = require('../misc/promise'), + util = require('util') var Invoices = EntityHelper.extend({ - constructor: function (application, options) - { - EntityHelper.call(this, application, _.extend({ entityName:'Invoice', entityPlural:'Invoices'}, options)); + constructor: function(application, options) { + EntityHelper.call(this, application, _.extend({ entityName: 'Invoice', entityPlural: 'Invoices' }, options)); }, - newInvoice: function (data, options) - { + newInvoice: function(data, options) { return new Invoice(this.application, data, options) }, - streamInvoice: function(id,format,stream) - { - return this.streamEntity({id: id, stream: stream, format: 'pdf'}); + streamInvoice: function(id, format, stream) { + return this.streamEntity({ id: id, stream: stream, format: 'pdf' }); }, - getInvoice: function (id, modifiedAfter,where, order) - { - return this.getInvoices({ id: id, modifiedAfter: modifiedAfter, where: where, order: order}) - .then(function (invoices) - { + getInvoice: function(id, modifiedAfter, where, order) { + return this.getInvoices({ id: id, modifiedAfter: modifiedAfter, where: where, order: order }) + .then(function(invoices) { return _.first(invoices); }) }, - saveInvoices: function (invoices, options) - { - return this.saveEntities(invoices, options) - }, - getInvoices: function (options) - { + getInvoices: function(options) { var self = this; var clonedOptions = _.clone(options || {}); - clonedOptions.entityConstructor = function(data) { return self.newInvoice(data)}; + clonedOptions.entityConstructor = function(data) { return self.newInvoice(data) }; return this.getEntities(clonedOptions) - }, - deleteInvoice: function(id) - { - } }) -module.exports = Invoices; - +module.exports = Invoices; \ No newline at end of file diff --git a/test/accountingtests.js b/test/accountingtests.js index 63c5f965..494efd32 100644 --- a/test/accountingtests.js +++ b/test/accountingtests.js @@ -725,7 +725,53 @@ describe('regression tests', function() { }); - describe.skip('tracking categories', function() {}) + describe.skip('tracking categories', function() {}); + + describe('items', function() { + this.timeout(10000); + + it('retrieves some items (no paging)', function(done) { + this.timeout(10000); + currentApp.core.items.getItems() + .then(function(items) { + _.each(items, function(item) { + expect(item.ItemID).to.not.equal(""); + }); + done(); + }) + .fail(function(err) { + done(wrapError(err)); + }); + }); + + it('creates an item', function(done) { + this.timeout(10000); + + var item = currentApp.core.items.newItem({ + Code: 'Item-' + Math.random(), + Name: 'Fully Tracked Item', + Description: '2014 Merino Sweater', + PurchaseDescription: '2014 Merino Sweater', + PurchaseDetails: { + UnitPrice: 149.00, + AccountCode: '200' + }, + SalesDetails: { + UnitPrice: 299.00, + AccountCode: '200' + } + }); + + item.save() + .then(function(item) { + expect(item.ItemID).to.not.equal(""); + done(); + }) + .fail(function(err) { + done(wrapError(err)); + }); + }); + }); describe.skip('contacts', function() { var sampleContactID;