Comparing version 0.12.0 to 0.12.1
33
index.js
@@ -41,3 +41,7 @@ var Q, Schema, book, entry, err, journalSchema, mongoose, transactionSchema, _; | ||
void_reason: String, | ||
_original_journal: Schema.Types.ObjectId | ||
_original_journal: Schema.Types.ObjectId, | ||
approved: { | ||
type: Boolean, | ||
"default": true | ||
} | ||
}); | ||
@@ -68,3 +72,7 @@ mongoose.model('Medici_Transaction', transactionSchema); | ||
}, | ||
void_reason: String | ||
void_reason: String, | ||
approved: { | ||
type: Boolean, | ||
"default": true | ||
} | ||
}); | ||
@@ -153,2 +161,23 @@ journalSchema.methods["void"] = function(book, reason) { | ||
}; | ||
journalSchema.pre('save', function(next) { | ||
var promises; | ||
if (this.isModified('approved') && this.approved === true) { | ||
promises = []; | ||
return mongoose.model('Medici_Transaction').find({ | ||
_journal: this._id | ||
}, function(err, transactions) { | ||
var transaction, _i, _len; | ||
for (_i = 0, _len = transactions.length; _i < _len; _i++) { | ||
transaction = transactions[_i]; | ||
transaction.approved = true; | ||
promises.push(transaction.save()); | ||
} | ||
return Q.all(promises).then(function() { | ||
return next(); | ||
}); | ||
}); | ||
} else { | ||
return next(); | ||
} | ||
}); | ||
mongoose.model('Medici_Journal', journalSchema); | ||
@@ -155,0 +184,0 @@ } |
@@ -95,2 +95,3 @@ var Book, Q, entry, mongoose, util, _; | ||
parsed.book = this.name; | ||
parsed.approved = true; | ||
return parsed; | ||
@@ -97,0 +98,0 @@ }; |
@@ -35,4 +35,10 @@ var Entry, Q, mongoose, _; | ||
this.transactionModels = []; | ||
this.journal.approved = true; | ||
} | ||
Entry.prototype.setApproved = function(bool) { | ||
this.journal.approved = bool; | ||
return this; | ||
}; | ||
Entry.prototype.credit = function(account_path, amount, extra) { | ||
@@ -132,10 +138,15 @@ var key, keys, meta, transaction, val; | ||
Entry.prototype.commit = function(success) { | ||
var deferred, err, saves, total, trans, transaction, _i, _j, _len, _len1, _ref, _ref1, | ||
var deferred, err, saves, total, trans, transaction, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2, | ||
_this = this; | ||
deferred = Q.defer(); | ||
this.transactionsSaved = 0; | ||
total = 0.0; | ||
_ref = this.transactions; | ||
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
transaction = _ref[_i]; | ||
transaction.approved = this.journal.approved; | ||
} | ||
this.transactionsSaved = 0; | ||
total = 0.0; | ||
_ref1 = this.transactions; | ||
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { | ||
transaction = _ref1[_j]; | ||
total += transaction.credit; | ||
@@ -151,5 +162,5 @@ total -= transaction.debit; | ||
saves = []; | ||
_ref1 = this.transactions; | ||
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { | ||
trans = _ref1[_j]; | ||
_ref2 = this.transactions; | ||
for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) { | ||
trans = _ref2[_k]; | ||
saves.push(this.saveTransaction(trans)); | ||
@@ -156,0 +167,0 @@ } |
{ | ||
"name": "medici", | ||
"version": "0.12.0", | ||
"version": "0.12.1", | ||
"description": "Simple double-entry accounting for Node + Mongoose", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -44,2 +44,3 @@ var medici, moment, mongoose, util; | ||
book = new medici.book('MyBook'); | ||
console.log('Getting balance...'); | ||
return book.balance({ | ||
@@ -49,2 +50,3 @@ account: 'Assets' | ||
var bal, notes; | ||
console.log('got data'); | ||
bal = data.balance; | ||
@@ -140,3 +142,3 @@ notes = data.notes; | ||
}); | ||
return it('should give you the balance by page', function(done) { | ||
it('should give you the balance by page', function(done) { | ||
var book, | ||
@@ -168,2 +170,42 @@ _this = this; | ||
}); | ||
return describe('approved/pending transactions', function() { | ||
it('should not include pending transactions in balance', function(done) { | ||
var book, | ||
_this = this; | ||
book = new medici.book('MyBook'); | ||
return book.entry('Test Entry').debit('Foo', 500).credit('Bar', 500).setApproved(false).commit().then(function(journal) { | ||
_this.pendingJournal = journal; | ||
return book.balance({ | ||
account: 'Foo' | ||
}).then(function(data) { | ||
data.balance.should.equal(0); | ||
return done(); | ||
}); | ||
}); | ||
}); | ||
it('should not include pending transactions in ledger', function(done) { | ||
var book; | ||
book = new medici.book('MyBook'); | ||
return book.ledger({ | ||
account: ['Foo'] | ||
}).then(function(response) { | ||
response.results.length.should.equal(0); | ||
return done(); | ||
}); | ||
}); | ||
return it('should set all transactions to approved when approving the journal', function(done) { | ||
var book; | ||
book = new medici.book('MyBook'); | ||
this.pendingJournal.approved = true; | ||
return this.pendingJournal.save(function() { | ||
var _this = this; | ||
return book.balance({ | ||
account: 'Bar' | ||
}).then(function(data) { | ||
data.balance.should.equal(500); | ||
return done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
52697
900