mirror of
https://github.com/aclindsa/moneygo.git
synced 2024-12-26 15:42:27 -05:00
Transaction.Balanced: Balance each security independently
This commit is contained in:
parent
79ad47971e
commit
bcbc4df67f
@ -99,19 +99,28 @@ func (t *Transaction) Valid() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *Transaction) Balanced() bool {
|
func (t *Transaction) Balanced() bool {
|
||||||
var zero, sum big.Rat
|
var zero big.Rat
|
||||||
|
sums := make(map[int64]big.Rat)
|
||||||
|
|
||||||
if !t.Valid() {
|
if !t.Valid() {
|
||||||
return false // TODO Open question: should we report an error here instead?
|
return false // TODO Open question: should we report an error here instead?
|
||||||
}
|
}
|
||||||
for i := range t.Splits {
|
for i := range t.Splits {
|
||||||
|
account, err := GetAccount(t.Splits[i].AccountId, t.UserId)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
amount, _ := t.Splits[i].GetAmount()
|
amount, _ := t.Splits[i].GetAmount()
|
||||||
if t.Splits[i].Debit {
|
sum := sums[account.SecurityId]
|
||||||
sum.Add(&sum, amount)
|
(&sum).Add(&sum, amount)
|
||||||
} else {
|
sums[account.SecurityId] = sum
|
||||||
sum.Sub(&sum, amount)
|
}
|
||||||
|
for _, security_sum := range sums {
|
||||||
|
if security_sum.Cmp(&zero) != 0 {
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return sum.Cmp(&zero) == 0
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetTransaction(transactionid int64, userid int64) (*Transaction, error) {
|
func GetTransaction(transactionid int64, userid int64) (*Transaction, error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user