mirror of
https://github.com/aclindsa/moneygo.git
synced 2025-06-13 21:48:39 -04:00
Move balance calculations into DBs
This commit is contained in:
@ -6,7 +6,6 @@ import (
|
||||
"github.com/aclindsa/moneygo/internal/models"
|
||||
"github.com/aclindsa/moneygo/internal/store"
|
||||
"github.com/yuin/gopher-lua"
|
||||
"math/big"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@ -147,14 +146,6 @@ func luaAccount__index(L *lua.LState) int {
|
||||
return 1
|
||||
}
|
||||
|
||||
func balanceFromSplits(splits *[]*models.Split) *big.Rat {
|
||||
var balance big.Rat
|
||||
for _, s := range *splits {
|
||||
balance.Add(&balance, &s.Amount.Rat)
|
||||
}
|
||||
return &balance
|
||||
}
|
||||
|
||||
func luaAccountBalance(L *lua.LState) int {
|
||||
a := luaCheckAccount(L, 1)
|
||||
|
||||
@ -176,23 +167,22 @@ func luaAccountBalance(L *lua.LState) int {
|
||||
panic("SecurityId not in lua security_map")
|
||||
}
|
||||
date := luaWeakCheckTime(L, 2)
|
||||
var splits *[]*models.Split
|
||||
var balance *models.Amount
|
||||
if date != nil {
|
||||
end := luaWeakCheckTime(L, 3)
|
||||
if end != nil {
|
||||
splits, err = tx.GetAccountSplitsDateRange(user, a.AccountId, date, end)
|
||||
balance, err = tx.GetAccountBalanceDateRange(user, a.AccountId, date, end)
|
||||
} else {
|
||||
splits, err = tx.GetAccountSplitsDate(user, a.AccountId, date)
|
||||
balance, err = tx.GetAccountBalanceDate(user, a.AccountId, date)
|
||||
}
|
||||
} else {
|
||||
splits, err = tx.GetAccountSplits(user, a.AccountId)
|
||||
balance, err = tx.GetAccountBalance(user, a.AccountId)
|
||||
}
|
||||
if err != nil {
|
||||
panic("Failed to fetch splits for account:" + err.Error())
|
||||
panic("Failed to fetch balance for account:" + err.Error())
|
||||
}
|
||||
rat := balanceFromSplits(splits)
|
||||
b := &Balance{
|
||||
Amount: models.Amount{*rat},
|
||||
Amount: *balance,
|
||||
Security: security,
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user