Created
October 31, 2012 11:51
-
-
Save ruiwen/3986644 to your computer and use it in GitHub Desktop.
Balance calculation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function recordTransaction(txn) { | |
// NON FUNCTIONING CODE | |
// Let 'friend' be the User's friend, the 'other' party in this Activity/Transaction | |
// Record the origin | |
var origin = friend.balance; | |
// Calculate friend balance | |
if(role == Activity.CREDITOR) { | |
friend.balance += txn.amount; | |
} | |
else if(role == Activity.DEBTOR) { | |
friend.balance -= txn.amount; | |
} | |
friend.balance = parseFloat(friend.balance.toFixed(2)); | |
// Calculate sum totals | |
// Algorithm as follows: | |
// Sum total (sumFriendsOwe/sumIOwe) is the cumulative representation of debt/credit | |
// across all a User's friends. It is wrong to treat sumFriendsOwe/sumIOwe the same | |
// way as a single friend's balance, ie. one friend's credit does not cancel out | |
// another's debt. Each friend's credit/debit must be taken into account individually | |
// first, via the '.balance' field, then its effect factored into the sum totals. | |
// | |
// (1) In calculating the sum total, we first look at the current friend's balance | |
// and if the transaction is a credit or debit against him. | |
// If the balance is in credit, ie. positive, and the transaction is a credit, | |
// then we simply add the transaction amount towards the sum total, sumFriendsOwe. | |
// The same applies in the opposite direction, ie. a debit against a negative balance | |
// is simply added to sumIOwe. | |
// | |
// (2) The tricky bit arises when we cross zero, ie. a debt becomes credit and vice versa | |
// When crossing zero, if the transaction amount is larger than the existing balance, | |
// we need to first eliminate the balance on one side, then add the remainder to the other. | |
// | |
// Eg. | |
// Transaction type: Credit | |
// Transaction amount: 20 | |
// Balance: -10 | |
// | |
// (A) Eliminate the balance: sumIOwe -= 10 (making balance zero) | |
// (B) Add the remainder to the other side: sumFriendsOwe += (20-10) (making balance +10) | |
// | |
if(role == Activity.CREDITOR) { | |
if(origin >= 0) { // See above: (1) | |
this.sumFriendsOwe += txn.amount; | |
} | |
else if(origin < 0) { | |
// Here we take into account the case where the difference provided by | |
// txn.amount will cause a shift from credit to debt or vice versa. | |
// We first eliminate excess credit/debt, then add the remainder to the | |
// other side. | |
if(txn.amount > Math.abs(origin)) { // See above: (2) | |
this.sumFriendsOwe += txn.amount - Math.abs(origin); | |
this.sumIOwe -= Math.abs(origin); | |
} | |
else { | |
this.sumIOwe -= txn.amount; | |
} | |
} | |
} | |
else if(role == Activity.DEBTOR) { | |
if(origin <= 0) { // See above: (1) | |
this.sumIOwe += txn.amount; | |
} | |
else if(origin > 0) { | |
// Here we take into account the case where the difference provided by | |
// txn.amount will cause a shift from credit to debt or vice versa. | |
// We first eliminate excess credit/debt, then add the remainder to the | |
// other side. | |
if(txn.amount > origin) { // See above: (2) | |
this.sumFriendsOwe -= origin; | |
this.sumIOwe += txn.amount - Math.abs(origin); | |
} | |
else { | |
this.sumFriendsOwe -= txn.amount; | |
} | |
} | |
} | |
// Re-jigger the balances | |
friend.balance = Math.round(friend.balance * 100) / 100; | |
this.sumIOwe = Math.round(this.sumIOwe * 100) / 100; | |
this.sumFriendsOwe = Math.round(this.sumFriendsOwe * 100) / 100; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function voidTransaction() { | |
// NON FUNCTIONING CODE | |
// Let 'friend' be the User's friend, the 'other' party in this Activity/Transaction | |
// Undo the transaction | |
var origin = friend.balance; | |
if(role == Activity.CREDITOR) { | |
friend.balance -= txn.amount; | |
if(origin <= 0) { | |
this.sumIOwe += txn.amount; | |
} | |
else if(origin > 0) { | |
if(txn.amount > Math.abs(origin)) { | |
var balance = 0; | |
if(this.sumFriendsOwe >= Math.abs(origin)) { | |
this.sumFriendsOwe -= Math.abs(origin); | |
} | |
else { | |
balance = Math.abs(origin) - this.sumFriendsOwe; | |
} | |
this.sumIOwe += (txn.amount - Math.abs(origin)) + balance; | |
} | |
else { | |
this.sumFriendsOwe -= txn.amount; | |
} | |
} | |
} | |
else if(role == Activity.DEBTOR) { | |
friend.balance += txn.amount; | |
if(origin >= 0) { | |
this.sumFriendsOwe += txn.amount; | |
} | |
else if(origin < 0) { | |
if(txn.amount > Math.abs(origin)) { | |
var balance = 0; | |
if(this.sumIOwe >= Math.abs(origin)) { | |
this.sumIOwe -= Math.abs(origin); | |
} | |
else { | |
balance = Math.abs(origin) - this.sumIOwe; | |
} | |
this.sumFriendsOwe += (txn.amount - Math.abs(origin)) + balance; | |
} | |
else { | |
this.sumIOwe -= txn.amount; | |
} | |
} | |
} | |
// Re-jigger the balances | |
friend.balance = Math.round(friend.balance * 100) / 100; | |
this.sumIOwe = Math.round(this.sumIOwe * 100) / 100; | |
this.sumFriendsOwe = Math.round(this.sumFriendsOwe * 100) / 100; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment