Comparing version 0.11.1 to 0.11.2
@@ -99,4 +99,12 @@ var Book, Q, entry, mongoose, util, _; | ||
Book.prototype.balance = function(query) { | ||
var deferred, group, match; | ||
var deferred, group, match, pagination, skip, sort; | ||
deferred = Q.defer(); | ||
if (query.perPage) { | ||
pagination = { | ||
perPage: query.perPage, | ||
page: query.page ? query.page : 1 | ||
}; | ||
delete query.perPage; | ||
delete query.page; | ||
} | ||
query = this.parseQuery(query); | ||
@@ -117,65 +125,27 @@ match = { | ||
}; | ||
this.transactionModel.aggregate(match, group, function(err, result) { | ||
var total; | ||
if (err) { | ||
return deferred.reject(err); | ||
} else { | ||
result = result.shift(); | ||
if (result == null) { | ||
return deferred.resolve(0); | ||
} | ||
total = result.credit - result.debit; | ||
return deferred.resolve(total); | ||
} | ||
}); | ||
return deferred.promise; | ||
}; | ||
Book.prototype.balanceAfterTransaction = function(_id, accounts) { | ||
var deferred, query, | ||
_this = this; | ||
deferred = Q.defer(); | ||
query = { | ||
account: accounts | ||
}; | ||
query = this.parseQuery(query); | ||
this.transactionModel.findById(_id, function(err, res) { | ||
var dateQuery, group, match; | ||
if (err) { | ||
return deferred.reject(err); | ||
} | ||
if (!res) { | ||
return deferred.reject(new Error('Transaction not found')); | ||
} | ||
dateQuery = { | ||
$or: [ | ||
{ | ||
'datetime': { | ||
$lt: res.datetime | ||
} | ||
}, { | ||
'datetime': res.datetime, | ||
'timestamp': { | ||
$lte: res.timestamp | ||
} | ||
} | ||
] | ||
if (pagination) { | ||
skip = { | ||
$skip: (pagination.page - 1) * pagination.perPage | ||
}; | ||
match = { | ||
$match: { | ||
$and: [query, dateQuery] | ||
sort = { | ||
$sort: { | ||
'datetime': -1, | ||
'timestamp': -1 | ||
} | ||
}; | ||
group = { | ||
$group: { | ||
_id: '1', | ||
credit: { | ||
$sum: '$credit' | ||
}, | ||
debit: { | ||
$sum: '$debit' | ||
this.transactionModel.aggregate(match, sort, skip, group, function(err, result) { | ||
var total; | ||
if (err) { | ||
return deferred.reject(err); | ||
} else { | ||
result = result.shift(); | ||
if (result == null) { | ||
return deferred.resolve(0); | ||
} | ||
total = result.credit - result.debit; | ||
return deferred.resolve(total); | ||
} | ||
}; | ||
return _this.transactionModel.aggregate(match, group, function(err, result) { | ||
}); | ||
} else { | ||
this.transactionModel.aggregate(match, group, function(err, result) { | ||
var total; | ||
@@ -193,3 +163,3 @@ if (err) { | ||
}); | ||
}); | ||
} | ||
return deferred.promise; | ||
@@ -196,0 +166,0 @@ }; |
{ | ||
"name": "medici", | ||
"version": "0.11.1", | ||
"version": "0.11.2", | ||
"description": "Simple double-entry accounting for Node + Mongoose", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,2 +0,2 @@ | ||
var medici, moment, mongoose; | ||
var medici, moment, mongoose, util; | ||
@@ -9,2 +9,4 @@ mongoose = require('mongoose'); | ||
util = require('util'); | ||
medici = require('../index'); | ||
@@ -123,16 +125,29 @@ | ||
}); | ||
return it('should give you the balance after a specific transaction', function(done) { | ||
return it('should give you the balance by page', function(done) { | ||
var book, | ||
_this = this; | ||
book = new medici.book('MyBook'); | ||
return book.balanceAfterTransaction(this.journal._transactions[0], 'Assets').then(function(balance) { | ||
balance.should.equal(-1200); | ||
return book.balanceAfterTransaction(_this.journal2._transactions[1], 'Income').then(function(balance) { | ||
balance.should.equal(700); | ||
return done(); | ||
return book.balance({ | ||
account: 'Assets', | ||
perPage: 1, | ||
page: 1 | ||
}).then(function(balance) { | ||
balance.should.equal(-700); | ||
return book.balance({ | ||
account: 'Assets', | ||
perPage: 1, | ||
page: 2 | ||
}).then(function(balance) { | ||
balance.should.equal(-1200); | ||
return book.balance({ | ||
account: 'Assets', | ||
perPage: 1, | ||
page: 3 | ||
}).then(function(balance) { | ||
balance.should.equal(-700); | ||
return done(); | ||
}); | ||
}); | ||
}, function(err) { | ||
return done(err); | ||
}); | ||
}); | ||
}); |
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
46803
781