Moving in another direction regarding debts

This commit is contained in:
2025-08-25 22:04:35 +01:00
parent 49a1978736
commit b5d918a873
13 changed files with 177 additions and 16 deletions
@@ -0,0 +1,3 @@
-- 0004_calculated_share.down.sql
ALTER TABLE "expensesShares" DROP COLUMN "Calculated";
@@ -0,0 +1,3 @@
-- 0004_calculated_share.up.sql
ALTER TABLE "expensesShares" ADD COLUMN "Calculated" TEXT NOT NULL DEFAULT 0;
+3
View File
@@ -29,6 +29,7 @@ CREATE TABLE "expenses" (
FOREIGN KEY(OwnerUserID) REFERENCES users(UserID) ON DELETE RESTRICT, FOREIGN KEY(OwnerUserID) REFERENCES users(UserID) ON DELETE RESTRICT,
FOREIGN KEY(TypeID) REFERENCES expensetypes(TypeID) ON DELETE RESTRICT FOREIGN KEY(TypeID) REFERENCES expensetypes(TypeID) ON DELETE RESTRICT
) )
CREATE TABLE "expensesPayments" ( CREATE TABLE "expensesPayments" (
"ExpPaymID" integer PRIMARY KEY AUTOINCREMENT, "ExpPaymID" integer PRIMARY KEY AUTOINCREMENT,
"ExpID" int NOT NULL, "ExpID" int NOT NULL,
@@ -37,6 +38,7 @@ CREATE TABLE "expensesPayments" (
FOREIGN KEY(ExpID) REFERENCES expenses(ExpID) ON DELETE RESTRICT, FOREIGN KEY(ExpID) REFERENCES expenses(ExpID) ON DELETE RESTRICT,
FOREIGN KEY(UserID) REFERENCES users(UserID) ON DELETE RESTRICT FOREIGN KEY(UserID) REFERENCES users(UserID) ON DELETE RESTRICT
) )
CREATE TABLE "expensesShares" ( CREATE TABLE "expensesShares" (
"ExpShareID" integer PRIMARY KEY AUTOINCREMENT, "ExpShareID" integer PRIMARY KEY AUTOINCREMENT,
"ExpID" int NOT NULL, "ExpID" int NOT NULL,
@@ -45,6 +47,7 @@ CREATE TABLE "expensesShares" (
FOREIGN KEY(ExpID) REFERENCES expenses(ExpID) ON DELETE RESTRICT, FOREIGN KEY(ExpID) REFERENCES expenses(ExpID) ON DELETE RESTRICT,
FOREIGN KEY(UserID) REFERENCES users(UserID) ON DELETE RESTRICT FOREIGN KEY(UserID) REFERENCES users(UserID) ON DELETE RESTRICT
) )
CREATE TABLE stores( CREATE TABLE stores(
"StoreID" integer PRIMARY KEY AUTOINCREMENT, "StoreID" integer PRIMARY KEY AUTOINCREMENT,
"StoreName" text NOT NULL "StoreName" text NOT NULL
+10 -2
View File
@@ -118,8 +118,16 @@ func resolveDebts(debtors UserTabs, creditors UserTabs) mod.Debts {
debts := mod.Debts{} debts := mod.Debts{}
for key, debt := range keyedDebts { for key, debt := range keyedDebts {
debt := mod.Debt{Debtor: key.Debtor, Creditor: key.Creditor, Sum: debt} newDebt := mod.Debt{
debts = append(debts, debt) Debtor: key.Debtor,
Creditor: key.Creditor,
Sum: debt.Truncate(2), // Removes fractional cents from the debt
}
// Only appends the debt if its not zero
if !newDebt.Sum.IsZero() {
debts = append(debts, newDebt)
}
} }
return debts return debts
+15
View File
@@ -139,6 +139,21 @@ var (
{ExpPaymID: 7, User: user4, PayedAmount: decimal.NewFromInt(8)}, {ExpPaymID: 7, User: user4, PayedAmount: decimal.NewFromInt(8)},
}, },
} }
expense4 = mod.Expense{
ExpID: 7,
Value: decimal.NewFromFloat(0.50),
Shares: []mod.ExpenseShare{
{ExpShareID: 0, User: user1, Share: decimal.NewFromFloat(0.34)},
{ExpShareID: 1, User: user2, Share: decimal.NewFromFloat(0.33)},
{ExpShareID: 2, User: user3, Share: decimal.NewFromFloat(0.33)},
},
Payments: []mod.ExpensePayment{
{ExpPaymID: 0, User: user1, PayedAmount: decimal.NewFromFloat(0.18)},
{ExpPaymID: 1, User: user1, PayedAmount: decimal.NewFromFloat(0.16)},
{ExpPaymID: 2, User: user2, PayedAmount: decimal.NewFromFloat(0.16)},
},
}
) )
func TestSharesAndPaymentsMapping(t *testing.T) { func TestSharesAndPaymentsMapping(t *testing.T) {
+6
View File
@@ -46,6 +46,12 @@ func ParseFormShares(userIDs []string, shares []string, sharesIDs []string,
return shareList, nil return shareList, nil
} }
// 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 normalizeShares(total decimal.Decimal, shares []mod.ExpenseShare) error {
return nil
}
func (s *Service) DeleteShare(id int64) error { func (s *Service) DeleteShare(id int64) error {
tx, err := s.DB.Begin() tx, err := s.DB.Begin()
if err != nil { if err != nil {
+101
View File
@@ -0,0 +1,101 @@
package expenses
// Do the test like this
// func TestSplitByPercent(t *testing.T) {
// tests := []struct {
// name string
// total string
// shares []Share
// ownerIdx int
// ownerAbsorb bool
// want []int64 // cents
// }{
// {
// name: "Even split, no remainder",
// total: "120.00",
// shares: []Share{
// {ID: "P1", Percent: d("50")},
// {ID: "P2", Percent: d("50")},
// },
// ownerIdx: 0,
// ownerAbsorb: false,
// want: []int64{6000, 6000},
// },
// {
// name: "Three way split with remainder (largest remainder mode)",
// total: "111.11",
// shares: []Share{
// {ID: "P1", Percent: d("34")},
// {ID: "P2", Percent: d("33")},
// {ID: "P3", Percent: d("33")},
// },
// ownerIdx: 0,
// ownerAbsorb: false,
// // One possible: P1=37.78, P2=36.67, P3=36.66
// want: []int64{3778, 3667, 3666},
// },
// {
// name: "Three way split with remainder (owner absorbs mode)",
// total: "111.11",
// shares: []Share{
// {ID: "P1", Percent: d("34")},
// {ID: "P2", Percent: d("33")},
// {ID: "P3", Percent: d("33")},
// },
// ownerIdx: 0,
// ownerAbsorb: true,
// // Owner takes both extra cents
// want: []int64{3779, 3666, 3666},
// },
// {
// name: "Unequal split with rounding",
// total: "10.00",
// shares: []Share{
// {ID: "P1", Percent: d("70")},
// {ID: "P2", Percent: d("30")},
// },
// ownerIdx: 0,
// ownerAbsorb: false,
// want: []int64{700, 300}, // perfect
// },
// {
// name: "Edge case: all to one person",
// total: "99.99",
// shares: []Share{
// {ID: "P1", Percent: d("100")},
// },
// ownerIdx: 0,
// ownerAbsorb: true,
// want: []int64{9999},
// },
// {
// name: "Stuff",
// total: "111.11",
// shares: []Share{
// {ID: "P1", Percent: d("0.34")},
// {ID: "P2", Percent: d("0.33")},
// {ID: "P3", Percent: d("0.33")},
// },
// ownerIdx: 0,
// ownerAbsorb: true,
// want: []int64{3779, 3666, 3666},
// },
// }
//
// for _, tt := range tests {
// t.Run(tt.name, func(t *testing.T) {
// got, err := SplitByPercent(d(tt.total), tt.shares, tt.ownerIdx, tt.ownerAbsorb)
// if err != nil {
// t.Fatalf("unexpected error: %v", err)
// }
// if len(got) != len(tt.want) {
// t.Fatalf("length mismatch: got %d, want %d", len(got), len(tt.want))
// }
// for i := range got {
// if got[i].Cents != tt.want[i] {
// t.Errorf("person %s: got %d, want %d", tt.shares[i].ID, got[i].Cents, tt.want[i])
// }
// }
// })
// }
// }
+13 -1
View File
@@ -172,6 +172,8 @@ func ExpenseTotalPayed(exp *mod.Expense) decimal.Decimal {
} }
func ExpenseIsEvenlyShared(exp *mod.Expense) bool { func ExpenseIsEvenlyShared(exp *mod.Expense) bool {
log.Printf("is exp %d even?", exp.ExpID)
shares := mapShares(exp) shares := mapShares(exp)
payments := mapPayments(exp) payments := mapPayments(exp)
@@ -184,11 +186,14 @@ func ExpenseIsEvenlyShared(exp *mod.Expense) bool {
return false return false
} }
if !val.Equal(userShare) { if !val.Truncate(2).Equal(userShare.Truncate(2)) {
log.Printf("No: %s != %s", val.Truncate(2), userShare.Truncate(2))
return false return false
} }
} }
log.Printf("Yes")
return true return true
} }
@@ -198,6 +203,13 @@ func analyzeExpense(e *mod.Expense) {
// Figure out if its evenly shared by the people associated to it // Figure out if its evenly shared by the people associated to it
e.SharesEven = ExpenseIsEvenlyShared(e) e.SharesEven = ExpenseIsEvenlyShared(e)
// Create the debts
debts, err := CalculateDebts(e)
if err != nil {
// do nothing apparently
}
e.Debts = debts
} }
func (s *Service) NewExpense(exp mod.Expense) error { func (s *Service) NewExpense(exp mod.Expense) error {
+9
View File
@@ -1,6 +1,7 @@
package expenses package expenses
import ( import (
"fmt"
"testing" "testing"
"github.com/shopspring/decimal" "github.com/shopspring/decimal"
@@ -30,4 +31,12 @@ func TestExpenseIsEvenlyShared(t *testing.T) {
if expected != result { if expected != result {
t.Errorf("Expected %v, got %v\n", expected, result) t.Errorf("Expected %v, got %v\n", expected, result)
} }
// expense4 is evenly shared but with the caveat of fractional cents
expected = true
result = ExpenseIsEvenlyShared(&expense4)
if expected != result {
fmt.Printf("Expense: %+v\n", expense4)
t.Errorf("Expected %v, got %v\n", expected, result)
}
} }
-6
View File
@@ -38,9 +38,3 @@ func (ds Debts) Equal(t Debts) bool {
return true return true
} }
func (d *Debt) Insert() {
}
// func (d *Debt) Update() {
// }
+4 -2
View File
@@ -9,13 +9,15 @@ import (
"fmt" "fmt"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
"github.com/shopspring/decimal" dec "github.com/shopspring/decimal"
) )
type ExpenseShare struct { type ExpenseShare struct {
ExpShareID int64 ExpShareID int64
User User User User
Share decimal.Decimal Share dec.Decimal
Calculated dec.Decimal // If certain shares equal fractional cent counts
// Its more useful to already have that computed
} }
func (e *Expense) GetShares(tx *sql.Tx) error { func (e *Expense) GetShares(tx *sql.Tx) error {
+5
View File
@@ -3,3 +3,8 @@
# Expenses # Expenses
## Expenses Types ## Expenses Types
- Salary: doesn't require entries in `shares` or `payments` - Salary: doesn't require entries in `shares` or `payments`
- If a third decimal place appears, the owner of the expense gets all the extra
cents added to their part
## Shares
- Shares sum needs to be 1.0