Skip to content

Commit

Permalink
updated items to support retrieving and saving
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Walsh committed Feb 22, 2017
1 parent c4a9b24 commit b5b82ae
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 60 deletions.
3 changes: 3 additions & 0 deletions lib/entities/invoice.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand All @@ -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, {
Expand Down
58 changes: 31 additions & 27 deletions lib/entities/item.js
Original file line number Diff line number Diff line change
@@ -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 = '<Items>' + this.toXml() + '</Items>';
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;
49 changes: 17 additions & 32 deletions lib/entity_helpers/invoices.js
Original file line number Diff line number Diff line change
@@ -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;
48 changes: 47 additions & 1 deletion test/accountingtests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit b5b82ae

Please sign in to comment.