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