Skip to content

Commit

Permalink
added support for credit notes and allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Walsh committed Mar 22, 2017
1 parent 4075135 commit 0e3efa4
Show file tree
Hide file tree
Showing 5 changed files with 471 additions and 270 deletions.
2 changes: 1 addition & 1 deletion lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand Down
59 changes: 56 additions & 3 deletions lib/entities/accounting/creditnote.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ 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 },
Total: { type: Number },
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 },
Expand Down Expand Up @@ -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 = '<CreditNotes>' + this.toXml() + '</CreditNotes>';
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 = '<Allocations>';

_.each(allocations, function(allocation) {
xml += "<Allocation>";
xml += "<AppliedAmount>" + allocation.AppliedAmount + "</AppliedAmount>";
xml += "<Invoice>";
xml += "<InvoiceID>" + allocation.InvoiceID + "</InvoiceID>";
xml += "</Invoice>";
xml += "</Allocation>";
});

xml += "</Allocations>";

var path, method;
path = 'CreditNotes/' + this.CreditNoteID + "/Allocations";
method = 'put'
return this.application.putOrPostEntity(method, path, xml, {});
},
});


Expand Down
52 changes: 11 additions & 41 deletions lib/entities/accounting/trackingoption.js
Original file line number Diff line number Diff line change
@@ -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;
module.exports = TrackingOption;
5 changes: 4 additions & 1 deletion lib/entity_helpers/accounting/creditnotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading

0 comments on commit 0e3efa4

Please sign in to comment.