From 770cd384a7572d7f8db60c0b02f0400fa4fff3a1 Mon Sep 17 00:00:00 2001 From: Aaron Lindsay Date: Sat, 22 Aug 2015 09:46:11 -0400 Subject: [PATCH] Report account balances correctly for both date sorts --- static/account_register.js | 6 +++--- transactions.go | 26 ++++++++++++++++++-------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/static/account_register.js b/static/account_register.js index 261d93b..6df7939 100644 --- a/static/account_register.js +++ b/static/account_register.js @@ -355,20 +355,20 @@ const AccountRegister = React.createClass({ } var transactions = []; - var balance = new Big(data.BeginningBalance); + var balance = new Big(data.EndingBalance); for (var i = 0; i < data.Transactions.length; i++) { var t = new Transaction(); t.fromJSON(data.Transactions[i]); + t.Balance = balance.plus(0); // Make a copy of the current balance // Keep a talley of the running balance of these transactions for (var j = 0; j < data.Transactions[i].Splits.length; j++) { var split = data.Transactions[i].Splits[j]; if (this.props.selectedAccount.AccountId == split.AccountId) { - balance = balance.plus(split.Amount); + balance = balance.minus(split.Amount); } } - t.Balance = balance.plus(0); // Make a copy transactions.push(t); } var a = new Account(); diff --git a/transactions.go b/transactions.go index 718823f..b42c9dc 100644 --- a/transactions.go +++ b/transactions.go @@ -541,11 +541,26 @@ func GetAccountTransactions(user *User, accountid int64, sort string, page uint6 var transactions []Transaction var atl AccountTransactionsList - var sqlsort string + transaction, err := DB.Begin() + if err != nil { + return nil, err + } + + var sqlsort, balanceLimitOffset string + var balanceLimitOffsetArg uint64 if sort == "date-asc" { sqlsort = " ORDER BY transactions.Date ASC" + balanceLimitOffset = " LIMIT ?" + balanceLimitOffsetArg = page*limit } else if sort == "date-desc" { + numSplits, err := transaction.SelectInt("SELECT count(*) FROM splits") + if err != nil { + transaction.Rollback() + return nil, err + } sqlsort = " ORDER BY transactions.Date DESC" + balanceLimitOffset = fmt.Sprintf(" LIMIT %d OFFSET ?", numSplits) + balanceLimitOffsetArg = (page + 1)*limit } var sqloffset string @@ -553,11 +568,6 @@ func GetAccountTransactions(user *User, accountid int64, sort string, page uint6 sqloffset = fmt.Sprintf(" OFFSET %d", page*limit) } - transaction, err := DB.Begin() - if err != nil { - return nil, err - } - account, err := GetAccountTx(transaction, accountid, user.UserId) if err != nil { transaction.Rollback() @@ -611,8 +621,8 @@ func GetAccountTransactions(user *User, accountid int64, sort string, page uint6 // Sum all the splits for all transaction splits for this account that // occurred before the page we're returning var amounts []string - sql = "SELECT splits.Amount FROM splits WHERE splits.AccountId=? AND splits.TransactionId IN (SELECT DISTINCT transactions.TransactionId FROM transactions INNER JOIN splits ON transactions.TransactionId = splits.TransactionId WHERE transactions.UserId=? AND splits.AccountId=?" + sqlsort + " LIMIT ?)" - _, err = transaction.Select(&amounts, sql, accountid, user.UserId, accountid, page*limit) + sql = "SELECT splits.Amount FROM splits WHERE splits.AccountId=? AND splits.TransactionId IN (SELECT DISTINCT transactions.TransactionId FROM transactions INNER JOIN splits ON transactions.TransactionId = splits.TransactionId WHERE transactions.UserId=? AND splits.AccountId=?" + sqlsort + balanceLimitOffset + ")" + _, err = transaction.Select(&amounts, sql, accountid, user.UserId, accountid, balanceLimitOffsetArg) if err != nil { transaction.Rollback() return nil, err