public
Created

Balance calculation

  • Download Gist
recordTransaction.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
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;
 
 
}
voidTransaction.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
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;
 
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.