Changed the code to use the currency_affinity value

This commit is contained in:
2025-08-31 19:13:04 +01:00
parent 8319b33c00
commit a67c103ccd
4 changed files with 21 additions and 4 deletions
+4 -1
View File
@@ -1,6 +1,7 @@
package expenses
import (
"expenses/config"
mod "expenses/expenses/models"
"sort"
@@ -101,6 +102,8 @@ func resolveDebt(creditor UserTab, debtors UserTabs) mod.Debts {
}
func resolveDebts(debtors UserTabs, creditors UserTabs) mod.Debts {
cfg := config.GetInstance()
keyedDebts := make(map[CompoundKey]dec.Decimal)
for _, creditor := range creditors {
@@ -121,7 +124,7 @@ func resolveDebts(debtors UserTabs, creditors UserTabs) mod.Debts {
newDebt := mod.Debt{
Debtor: key.Debtor,
Creditor: key.Creditor,
Sum: debt.Truncate(2), // Removes fractional cents from the debt
Sum: debt.Truncate(cfg.CurrencyAffinity), // Removes fractional cents from the debt
}
// Only appends the debt if its not zero
+5
View File
@@ -1,6 +1,7 @@
package expenses
import (
"expenses/config"
mod "expenses/expenses/models"
"fmt"
"testing"
@@ -40,6 +41,10 @@ func TestFilterExpenseParticipants(t *testing.T) {
},
}
// Need to set the config before running the tests
config.SetConfiguration(&config.Configuration{
CurrencyAffinity: 2,
})
for _, tt := range testCases {
fmt.Printf("Running test: %s\n", tt.name)
debtors, creditors := filterExpenseParticipants(tt.exp)
+7 -2
View File
@@ -1,7 +1,9 @@
package expenses
import (
"expenses/config"
mod "expenses/expenses/models"
dec "github.com/shopspring/decimal"
)
@@ -48,6 +50,9 @@ func ParseFormShares(userIDs []string, shares []string, sharesIDs []string,
// NormalizeShares will take the total of an expense and the proposed shares
// and calculate how much each user actually has to pay - avoids fracd cents
func (a *ExpensesApp) NormalizeShares(e *mod.Expense) error {
cfg := config.GetInstance()
affi := cfg.CurrencyAffinity
excess := dec.NewFromFloat(0.0)
ownerShIdx := -1
for k := range e.Shares {
@@ -55,9 +60,9 @@ func (a *ExpensesApp) NormalizeShares(e *mod.Expense) error {
ownerShIdx = k
}
owed := e.Value.Mul(e.Shares[k].Share)
excess = excess.Add(owed.Sub(owed.Truncate(2)))
excess = excess.Add(owed.Sub(owed.Truncate(affi)))
e.Shares[k].Calculated = owed.Truncate(2)
e.Shares[k].Calculated = owed.Truncate(affi)
}
// It fails here for some reason
+5 -1
View File
@@ -1,6 +1,7 @@
package expenses
import (
"expenses/config"
mod "expenses/expenses/models"
"log"
"time"
@@ -145,6 +146,9 @@ func ExpenseTotalPayed(exp *mod.Expense) decimal.Decimal {
}
func ExpenseIsEvenlyShared(exp *mod.Expense) bool {
cfg := config.GetInstance()
affi := cfg.CurrencyAffinity
shares := mapShares(exp)
payments := mapPayments(exp)
@@ -156,7 +160,7 @@ func ExpenseIsEvenlyShared(exp *mod.Expense) bool {
return false
}
if !val.Truncate(2).Equal(share.Truncate(2)) {
if !val.Truncate(affi).Equal(share.Truncate(affi)) {
return false
}
}