still adding the 'Calculated' column

This commit is contained in:
2025-08-26 18:28:59 +01:00
parent b5d918a873
commit b6d0d98d41
16 changed files with 228 additions and 196 deletions
+6
View File
@@ -1,3 +1,9 @@
# DOING
Adding the field `Calculated` to `Share` to avoid computing what each user owes
everytime due to the fractional cents problem
Make a script for the current migration to compute the Calculated value with
our rules
# TODO # TODO
- [ ] On the categories page, add a column of how many times a given category is - [ ] On the categories page, add a column of how many times a given category is
used used
@@ -0,0 +1 @@
print "hello, world!"
+3 -3
View File
@@ -9,13 +9,13 @@ WHERE "ExpID" = ?;
-- name: InsertShare :execresult -- name: InsertShare :execresult
INSERT INTO "expensesShares"( INSERT INTO "expensesShares"(
"ExpID", "UserID", "Share" "ExpID", "UserID", "Share", "Calculated"
) )
VALUES(?, ?, ?); VALUES(?, ?, ?, ?);
-- name: UpdateShare :execresult -- name: UpdateShare :execresult
UPDATE expensesShares UPDATE expensesShares
SET "UserID" = ?, "Share" = ? SET "UserID" = ?, "Share" = ?, "Calculated" = ?
WHERE "ExpShareID" = ?; WHERE "ExpShareID" = ?;
-- name: DeleteShare :execresult -- name: DeleteShare :execresult
+1
View File
@@ -40,6 +40,7 @@ type ExpensesShare struct {
ExpID int64 ExpID int64
UserID int64 UserID int64
Share string Share string
Calculated string
} }
type Store struct { type Store struct {
+22 -9
View File
@@ -20,7 +20,7 @@ func (q *Queries) DeleteShare(ctx context.Context, expshareid int64) (sql.Result
const getShares = `-- name: GetShares :many const getShares = `-- name: GetShares :many
SELECT SELECT
shares.ExpShareID, shares.ExpID, shares.UserID, shares.Share, users.UserID, users.UserName, users.UserPass shares.ExpShareID, shares.ExpID, shares.UserID, shares.Share, shares.Calculated, users.UserID, users.UserName, users.UserPass
FROM FROM
"expensesShares" as shares "expensesShares" as shares
JOIN JOIN
@@ -47,6 +47,7 @@ func (q *Queries) GetShares(ctx context.Context, expid int64) ([]GetSharesRow, e
&i.ExpensesShare.ExpID, &i.ExpensesShare.ExpID,
&i.ExpensesShare.UserID, &i.ExpensesShare.UserID,
&i.ExpensesShare.Share, &i.ExpensesShare.Share,
&i.ExpensesShare.Calculated,
&i.User.UserID, &i.User.UserID,
&i.User.UserName, &i.User.UserName,
&i.User.UserPass, &i.User.UserPass,
@@ -66,33 +67,45 @@ func (q *Queries) GetShares(ctx context.Context, expid int64) ([]GetSharesRow, e
const insertShare = `-- name: InsertShare :execresult const insertShare = `-- name: InsertShare :execresult
INSERT INTO "expensesShares"( INSERT INTO "expensesShares"(
"ExpID", "UserID", "Share" "ExpID", "UserID", "Share", "Calculated"
) )
VALUES(?, ?, ?) VALUES(?, ?, ?, ?)
` `
type InsertShareParams struct { type InsertShareParams struct {
ExpID int64 ExpID int64
UserID int64 UserID int64
Share string Share string
Calculated string
} }
func (q *Queries) InsertShare(ctx context.Context, arg InsertShareParams) (sql.Result, error) { func (q *Queries) InsertShare(ctx context.Context, arg InsertShareParams) (sql.Result, error) {
return q.db.ExecContext(ctx, insertShare, arg.ExpID, arg.UserID, arg.Share) return q.db.ExecContext(ctx, insertShare,
arg.ExpID,
arg.UserID,
arg.Share,
arg.Calculated,
)
} }
const updateShare = `-- name: UpdateShare :execresult const updateShare = `-- name: UpdateShare :execresult
UPDATE expensesShares UPDATE expensesShares
SET "UserID" = ?, "Share" = ? SET "UserID" = ?, "Share" = ?, "Calculated" = ?
WHERE "ExpShareID" = ? WHERE "ExpShareID" = ?
` `
type UpdateShareParams struct { type UpdateShareParams struct {
UserID int64 UserID int64
Share string Share string
Calculated string
ExpShareID int64 ExpShareID int64
} }
func (q *Queries) UpdateShare(ctx context.Context, arg UpdateShareParams) (sql.Result, error) { func (q *Queries) UpdateShare(ctx context.Context, arg UpdateShareParams) (sql.Result, error) {
return q.db.ExecContext(ctx, updateShare, arg.UserID, arg.Share, arg.ExpShareID) return q.db.ExecContext(ctx, updateShare,
arg.UserID,
arg.Share,
arg.Calculated,
arg.ExpShareID,
)
} }
+1
View File
@@ -44,6 +44,7 @@ CREATE TABLE "expensesShares" (
"ExpID" int NOT NULL, "ExpID" int NOT NULL,
"UserID" int NOT NULL, "UserID" int NOT NULL,
"Share" TEXT NOT NULL DEFAULT 0.5, "Share" TEXT NOT NULL DEFAULT 0.5,
"Calculated" TEXT NOT NULL DEFAULT 0,
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
) )
+71 -47
View File
@@ -4,7 +4,7 @@ import (
mod "expenses/expenses/models" mod "expenses/expenses/models"
"testing" "testing"
"github.com/shopspring/decimal" dec "github.com/shopspring/decimal"
) )
var ( var (
@@ -30,18 +30,18 @@ var (
// this expense is paid off // this expense is paid off
expense1 = mod.Expense{ expense1 = mod.Expense{
ExpID: 0, ExpID: 0,
Value: decimal.NewFromInt(150), Value: dec.NewFromInt(150),
Shares: []mod.ExpenseShare{ Shares: []mod.Share{
{ExpShareID: 0, User: user1, Share: decimal.NewFromFloat(0.4)}, {ExpShareID: 0, User: user1, Share: dec.NewFromFloat(0.4)},
{ExpShareID: 1, User: user2, Share: decimal.NewFromFloat(0.4)}, {ExpShareID: 1, User: user2, Share: dec.NewFromFloat(0.4)},
{ExpShareID: 2, User: user3, Share: decimal.NewFromFloat(0.2)}, {ExpShareID: 2, User: user3, Share: dec.NewFromFloat(0.2)},
}, },
Payments: []mod.ExpensePayment{ Payments: []mod.ExpensePayment{
{ExpPaymID: 0, User: user1, PayedAmount: decimal.NewFromInt(50)}, {ExpPaymID: 0, User: user1, PayedAmount: dec.NewFromInt(50)},
{ExpPaymID: 1, User: user1, PayedAmount: decimal.NewFromInt(50)}, {ExpPaymID: 1, User: user1, PayedAmount: dec.NewFromInt(50)},
{ExpPaymID: 2, User: user2, PayedAmount: decimal.NewFromInt(0)}, {ExpPaymID: 2, User: user2, PayedAmount: dec.NewFromInt(0)},
{ExpPaymID: 3, User: user3, PayedAmount: decimal.NewFromInt(30)}, {ExpPaymID: 3, User: user3, PayedAmount: dec.NewFromInt(30)},
{ExpPaymID: 4, User: user3, PayedAmount: decimal.NewFromInt(20)}, {ExpPaymID: 4, User: user3, PayedAmount: dec.NewFromInt(20)},
}, },
} }
@@ -49,22 +49,22 @@ var (
// Expense is paid off // Expense is paid off
expense2 = mod.Expense{ expense2 = mod.Expense{
ExpID: 0, ExpID: 0,
Value: decimal.NewFromInt(160), Value: dec.NewFromInt(160),
Shares: []mod.ExpenseShare{ Shares: []mod.Share{
{ExpShareID: 0, User: user1, Share: decimal.NewFromFloat(0.3)}, {ExpShareID: 0, User: user1, Share: dec.NewFromFloat(0.3)},
{ExpShareID: 1, User: user2, Share: decimal.NewFromFloat(0.3)}, {ExpShareID: 1, User: user2, Share: dec.NewFromFloat(0.3)},
{ExpShareID: 2, User: user3, Share: decimal.NewFromFloat(0.3)}, {ExpShareID: 2, User: user3, Share: dec.NewFromFloat(0.3)},
{ExpShareID: 3, User: user4, Share: decimal.NewFromFloat(0.1)}, {ExpShareID: 3, User: user4, Share: dec.NewFromFloat(0.1)},
}, },
Payments: []mod.ExpensePayment{ Payments: []mod.ExpensePayment{
{ExpPaymID: 0, User: user1, PayedAmount: decimal.NewFromInt(40)}, {ExpPaymID: 0, User: user1, PayedAmount: dec.NewFromInt(40)},
{ExpPaymID: 1, User: user1, PayedAmount: decimal.NewFromInt(40)}, {ExpPaymID: 1, User: user1, PayedAmount: dec.NewFromInt(40)},
{ExpPaymID: 2, User: user2, PayedAmount: decimal.NewFromInt(5)}, {ExpPaymID: 2, User: user2, PayedAmount: dec.NewFromInt(5)},
{ExpPaymID: 3, User: user3, PayedAmount: decimal.NewFromInt(30)}, {ExpPaymID: 3, User: user3, PayedAmount: dec.NewFromInt(30)},
{ExpPaymID: 4, User: user3, PayedAmount: decimal.NewFromInt(30)}, {ExpPaymID: 4, User: user3, PayedAmount: dec.NewFromInt(30)},
{ExpPaymID: 5, User: user3, PayedAmount: decimal.NewFromInt(10)}, {ExpPaymID: 5, User: user3, PayedAmount: dec.NewFromInt(10)},
{ExpPaymID: 6, User: user4, PayedAmount: decimal.NewFromInt(4)}, {ExpPaymID: 6, User: user4, PayedAmount: dec.NewFromInt(4)},
{ExpPaymID: 7, User: user4, PayedAmount: decimal.NewFromInt(1)}, {ExpPaymID: 7, User: user4, PayedAmount: dec.NewFromInt(1)},
}, },
} }
/* /*
@@ -121,37 +121,61 @@ var (
// Expense is: evenly shared and paid off // Expense is: evenly shared and paid off
expense3 = mod.Expense{ expense3 = mod.Expense{
ExpID: 0, ExpID: 0,
Value: decimal.NewFromInt(160), Owner: mod.User{
Shares: []mod.ExpenseShare{ UserID: user1.UserID,
{ExpShareID: 0, User: user1, Share: decimal.NewFromFloat(0.3)}, },
{ExpShareID: 1, User: user2, Share: decimal.NewFromFloat(0.3)}, Value: dec.NewFromInt(160),
{ExpShareID: 2, User: user3, Share: decimal.NewFromFloat(0.3)}, Shares: []mod.Share{
{ExpShareID: 3, User: user4, Share: decimal.NewFromFloat(0.1)}, {ExpShareID: 0, User: user1, Share: dec.NewFromFloat(0.3)},
{ExpShareID: 1, User: user2, Share: dec.NewFromFloat(0.3)},
{ExpShareID: 2, User: user3, Share: dec.NewFromFloat(0.3)},
{ExpShareID: 3, User: user4, Share: dec.NewFromFloat(0.1)},
}, },
Payments: []mod.ExpensePayment{ Payments: []mod.ExpensePayment{
{ExpPaymID: 0, User: user1, PayedAmount: decimal.NewFromInt(40)}, {ExpPaymID: 0, User: user1, PayedAmount: dec.NewFromInt(40)},
{ExpPaymID: 1, User: user1, PayedAmount: decimal.NewFromInt(8)}, {ExpPaymID: 1, User: user1, PayedAmount: dec.NewFromInt(8)},
{ExpPaymID: 2, User: user2, PayedAmount: decimal.NewFromInt(48)}, {ExpPaymID: 2, User: user2, PayedAmount: dec.NewFromInt(48)},
{ExpPaymID: 3, User: user3, PayedAmount: decimal.NewFromInt(15)}, {ExpPaymID: 3, User: user3, PayedAmount: dec.NewFromInt(15)},
{ExpPaymID: 4, User: user3, PayedAmount: decimal.NewFromInt(15)}, {ExpPaymID: 4, User: user3, PayedAmount: dec.NewFromInt(15)},
{ExpPaymID: 5, User: user3, PayedAmount: decimal.NewFromInt(18)}, {ExpPaymID: 5, User: user3, PayedAmount: dec.NewFromInt(18)},
{ExpPaymID: 6, User: user4, PayedAmount: decimal.NewFromInt(8)}, {ExpPaymID: 6, User: user4, PayedAmount: dec.NewFromInt(8)},
{ExpPaymID: 7, User: user4, PayedAmount: decimal.NewFromInt(8)}, {ExpPaymID: 7, User: user4, PayedAmount: dec.NewFromInt(8)},
}, },
} }
expense4 = mod.Expense{ expense4 = mod.Expense{
ExpID: 7, ExpID: 7,
Value: decimal.NewFromFloat(0.50), Value: dec.NewFromFloat(0.50),
Shares: []mod.ExpenseShare{ Shares: []mod.Share{
{ExpShareID: 0, User: user1, Share: decimal.NewFromFloat(0.34)}, {ExpShareID: 0, User: user1, Share: dec.NewFromFloat(0.34)},
{ExpShareID: 1, User: user2, Share: decimal.NewFromFloat(0.33)}, {ExpShareID: 1, User: user2, Share: dec.NewFromFloat(0.33)},
{ExpShareID: 2, User: user3, Share: decimal.NewFromFloat(0.33)}, {ExpShareID: 2, User: user3, Share: dec.NewFromFloat(0.33)},
}, },
Payments: []mod.ExpensePayment{ Payments: []mod.ExpensePayment{
{ExpPaymID: 0, User: user1, PayedAmount: decimal.NewFromFloat(0.18)}, {ExpPaymID: 0, User: user1, PayedAmount: dec.NewFromFloat(0.18)},
{ExpPaymID: 1, User: user1, PayedAmount: decimal.NewFromFloat(0.16)}, {ExpPaymID: 1, User: user1, PayedAmount: dec.NewFromFloat(0.16)},
{ExpPaymID: 2, User: user2, PayedAmount: decimal.NewFromFloat(0.16)}, {ExpPaymID: 2, User: user2, PayedAmount: dec.NewFromFloat(0.16)},
},
}
expense5 = mod.Expense{
ExpID: 9,
Owner: mod.User{
UserID: user1.UserID,
},
Value: dec.NewFromFloat(111.11),
Shares: []mod.Share{
{ExpShareID: 0, User: user1,
Share: dec.NewFromFloat(0.34), Calculated: dec.NewFromFloat(37.79)},
{ExpShareID: 1, User: user2,
Share: dec.NewFromFloat(0.33), Calculated: dec.NewFromFloat(36.66)},
{ExpShareID: 2, User: user3,
Share: dec.NewFromFloat(0.33), Calculated: dec.NewFromFloat(36.66)},
},
Payments: []mod.ExpensePayment{
{ExpPaymID: 0, User: user1, PayedAmount: dec.NewFromFloat(37.79)},
{ExpPaymID: 1, User: user1, PayedAmount: dec.NewFromFloat(36.66)},
{ExpPaymID: 2, User: user2, PayedAmount: dec.NewFromFloat(36.66)},
}, },
} }
) )
+23 -7
View File
@@ -2,8 +2,9 @@ package expenses
import ( import (
mod "expenses/expenses/models" mod "expenses/expenses/models"
"log"
"github.com/shopspring/decimal" dec "github.com/shopspring/decimal"
) )
// TODO // TODO
@@ -11,20 +12,20 @@ import (
// conversion that the user calls before calling this // conversion that the user calls before calling this
// Can even create a struct to then hold the data // Can even create a struct to then hold the data
func ParseFormShares(userIDs []string, shares []string, sharesIDs []string, func ParseFormShares(userIDs []string, shares []string, sharesIDs []string,
) ([]mod.ExpenseShare, error) { ) ([]mod.Share, error) {
shareList := []mod.ExpenseShare{} shareList := []mod.Share{}
for i := range userIDs { for i := range userIDs {
userID, err := ParseID(userIDs[i]) userID, err := ParseID(userIDs[i])
if err != nil { if err != nil {
return nil, err return nil, err
} }
share, err := decimal.NewFromString(shares[i]) share, err := dec.NewFromString(shares[i])
if err != nil { if err != nil {
return nil, err return nil, err
} }
newShare := mod.ExpenseShare{ newShare := mod.Share{
ExpShareID: -1, ExpShareID: -1,
User: mod.User{ User: mod.User{
UserID: userID, UserID: userID,
@@ -48,7 +49,22 @@ func ParseFormShares(userIDs []string, shares []string, sharesIDs []string,
// normalizeShares will take the total of an expense and the proposed shares // 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 // and calculate how much each user actually has to pay - avoids fracd cents
func normalizeShares(total decimal.Decimal, shares []mod.ExpenseShare) error { func normalizeShares(e *mod.Expense) error {
excess := dec.NewFromFloat(0.0)
ownerShIdx := -1
for k := range e.Shares {
if e.Shares[k].User.UserID == e.Owner.UserID {
ownerShIdx = k
}
owed := e.Value.Mul(e.Shares[k].Share)
excess = excess.Add(owed.Sub(owed.Truncate(2)))
log.Println(excess)
e.Shares[k].Calculated = owed.Truncate(2)
}
e.Shares[ownerShIdx].Calculated = e.Shares[ownerShIdx].Calculated.Add(excess)
return nil return nil
} }
@@ -59,7 +75,7 @@ func (s *Service) DeleteShare(id int64) error {
} }
defer tx.Rollback() defer tx.Rollback()
share := mod.ExpenseShare{ share := mod.Share{
ExpShareID: id, ExpShareID: id,
} }
+55 -99
View File
@@ -1,101 +1,57 @@
package expenses package expenses
// Do the test like this import (
// func TestSplitByPercent(t *testing.T) { mod "expenses/expenses/models"
// tests := []struct { dec "github.com/shopspring/decimal"
// name string "testing"
// total string )
// shares []Share
// ownerIdx int func TestNormalizeShares(t *testing.T) {
// ownerAbsorb bool testCases := []struct {
// want []int64 // cents name string
// }{ exp *mod.Expense
// { expectedShares mod.Shares
// name: "Even split, no remainder", }{
// total: "120.00", {
// shares: []Share{ name: "Split with remainder",
// {ID: "P1", Percent: d("50")}, exp: &expense5,
// {ID: "P2", Percent: d("50")}, expectedShares: mod.Shares{
// }, {ExpShareID: 0, User: user1,
// ownerIdx: 0, Share: dec.NewFromFloat(0.34), Calculated: dec.NewFromFloat(37.79)},
// ownerAbsorb: false, {ExpShareID: 1, User: user2,
// want: []int64{6000, 6000}, Share: dec.NewFromFloat(0.33), Calculated: dec.NewFromFloat(36.66)},
// }, {ExpShareID: 2, User: user3,
// { Share: dec.NewFromFloat(0.33), Calculated: dec.NewFromFloat(36.66)},
// name: "Three way split with remainder (largest remainder mode)", },
// total: "111.11", },
// shares: []Share{ {
// {ID: "P1", Percent: d("34")}, name: "Even split",
// {ID: "P2", Percent: d("33")}, exp: &expense3,
// {ID: "P3", Percent: d("33")}, expectedShares: mod.Shares{
// }, {ExpShareID: 0, User: user1,
// ownerIdx: 0, Share: dec.NewFromFloat(0.3), Calculated: dec.NewFromFloat(48.00)},
// ownerAbsorb: false, {ExpShareID: 1, User: user2,
// // One possible: P1=37.78, P2=36.67, P3=36.66 Share: dec.NewFromFloat(0.3), Calculated: dec.NewFromFloat(48.00)},
// want: []int64{3778, 3667, 3666}, {ExpShareID: 2, User: user3,
// }, Share: dec.NewFromFloat(0.3), Calculated: dec.NewFromFloat(48.00)},
// { {ExpShareID: 3, User: user4,
// name: "Three way split with remainder (owner absorbs mode)", Share: dec.NewFromFloat(0.1), Calculated: dec.NewFromFloat(16.00)},
// total: "111.11", },
// shares: []Share{ },
// {ID: "P1", Percent: d("34")}, }
// {ID: "P2", Percent: d("33")},
// {ID: "P3", Percent: d("33")}, for _, tc := range testCases {
// }, t.Run(tc.name, func(t *testing.T) {
// ownerIdx: 0, err := normalizeShares(tc.exp)
// ownerAbsorb: true, if err != nil {
// // Owner takes both extra cents t.Errorf("Failed to normalize shares: %+v", err)
// want: []int64{3779, 3666, 3666}, }
// },
// { if !tc.expectedShares.Equal(tc.exp.Shares) {
// name: "Unequal split with rounding", t.Errorf("Resulting shares: %+v\nAre not correct: %+v", tc.exp.Shares,
// total: "10.00", tc.expectedShares)
// 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])
// }
// }
// })
// }
// }
+2 -6
View File
@@ -204,12 +204,8 @@ 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 // Update the calculated fields on the shares
debts, err := CalculateDebts(e) normalizeShares(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 {
+7 -7
View File
@@ -1,7 +1,7 @@
package expenses package expenses
import ( import (
"fmt" // "fmt"
"testing" "testing"
"github.com/shopspring/decimal" "github.com/shopspring/decimal"
@@ -33,10 +33,10 @@ func TestExpenseIsEvenlyShared(t *testing.T) {
} }
// expense4 is evenly shared but with the caveat of fractional cents // expense4 is evenly shared but with the caveat of fractional cents
expected = true // expected = true
result = ExpenseIsEvenlyShared(&expense4) // result = ExpenseIsEvenlyShared(&expense4)
if expected != result { // if expected != result {
fmt.Printf("Expense: %+v\n", expense4) // fmt.Printf("Expense: %+v\n", expense4)
t.Errorf("Expected %v, got %v\n", expected, result) // t.Errorf("Expected %v, got %v\n", expected, result)
} // }
} }
+2 -2
View File
@@ -21,7 +21,7 @@ type Expense struct {
Owner User Owner User
Date int64 Date int64
Payments []ExpensePayment Payments []ExpensePayment
Shares []ExpenseShare Shares []Share
Debts Debts Debts Debts
PaidOff bool PaidOff bool
SharesEven bool SharesEven bool
@@ -38,7 +38,7 @@ func NewExpense() Expense {
Owner: NewUser(), Owner: NewUser(),
Date: 0, Date: 0,
Payments: []ExpensePayment{}, Payments: []ExpensePayment{},
Shares: []ExpenseShare{}, Shares: []Share{},
PaidOff: false, PaidOff: false,
SharesEven: false, SharesEven: false,
CreationDate: 0, CreationDate: 0,
+2 -2
View File
@@ -30,7 +30,7 @@ func mapRepoGetExpenseRow(e repo.GetExpenseRow) Expense {
UserName: e.User.UserName, UserName: e.User.UserName,
}, },
Date: e.Expense.ExpDate, Date: e.Expense.ExpDate,
Shares: []ExpenseShare{}, Shares: []Share{},
Payments: []ExpensePayment{}, Payments: []ExpensePayment{},
PaidOff: e.Expense.PaidOff, PaidOff: e.Expense.PaidOff,
SharesEven: e.Expense.SharesEven, SharesEven: e.Expense.SharesEven,
@@ -62,7 +62,7 @@ func mapRepoGetExpenseRowMulti(e repo.GetExpensesRow) Expense {
UserName: e.User.UserName, UserName: e.User.UserName,
}, },
Date: e.Expense.ExpDate, Date: e.Expense.ExpDate,
Shares: []ExpenseShare{}, Shares: []Share{},
Payments: []ExpensePayment{}, Payments: []ExpensePayment{},
PaidOff: e.Expense.PaidOff, PaidOff: e.Expense.PaidOff,
SharesEven: e.Expense.SharesEven, SharesEven: e.Expense.SharesEven,
+27 -9
View File
@@ -12,12 +12,28 @@ import (
dec "github.com/shopspring/decimal" dec "github.com/shopspring/decimal"
) )
type ExpenseShare struct { type Share struct {
ExpShareID int64 ExpShareID int64
User User User User
Share dec.Decimal Share dec.Decimal
Calculated dec.Decimal // If certain shares equal fractional cent counts Calculated dec.Decimal // Normalized user share because of fracd cents
// Its more useful to already have that computed }
type Shares []Share
func (sh Shares) Equal(other Shares) bool {
if len(sh) != len(other) {
return false
}
for k := range sh {
if sh[k].User != other[k].User ||
!sh[k].Calculated.Equal(other[k].Calculated) ||
!sh[k].Share.Equal(other[k].Share) {
return false
}
}
return true
} }
func (e *Expense) GetShares(tx *sql.Tx) error { func (e *Expense) GetShares(tx *sql.Tx) error {
@@ -33,14 +49,15 @@ func (e *Expense) GetShares(tx *sql.Tx) error {
return nil return nil
} }
func (sh *ExpenseShare) Insert(tx *sql.Tx, expID int64) error { func (sh *Share) Insert(tx *sql.Tx, expID int64) error {
ctx := context.Background() ctx := context.Background()
queries := repo.New(tx) queries := repo.New(tx)
res, err := queries.InsertShare(ctx, repo.InsertShareParams{ res, err := queries.InsertShare(ctx, repo.InsertShareParams{
ExpID: expID, ExpID: expID,
Share: sh.Share.String(), Share: sh.Share.String(),
UserID: sh.User.UserID, UserID: sh.User.UserID,
Calculated: sh.Calculated.String(),
}) })
if err != nil { if err != nil {
return err return err
@@ -56,7 +73,7 @@ func (sh *ExpenseShare) Insert(tx *sql.Tx, expID int64) error {
return nil return nil
} }
func (sh *ExpenseShare) Update(tx *sql.Tx) error { func (sh *Share) Update(tx *sql.Tx) error {
ctx := context.Background() ctx := context.Background()
queries := repo.New(tx) queries := repo.New(tx)
@@ -64,6 +81,7 @@ func (sh *ExpenseShare) Update(tx *sql.Tx) error {
ExpShareID: sh.ExpShareID, ExpShareID: sh.ExpShareID,
Share: sh.Share.String(), Share: sh.Share.String(),
UserID: sh.User.UserID, UserID: sh.User.UserID,
Calculated: sh.Calculated.String(),
}) })
if err != nil { if err != nil {
return err return err
@@ -79,7 +97,7 @@ func (sh *ExpenseShare) Update(tx *sql.Tx) error {
return nil return nil
} }
func (sh *ExpenseShare) Delete(tx *sql.Tx) error { func (sh *Share) Delete(tx *sql.Tx) error {
ctx := context.Background() ctx := context.Background()
queries := repo.New(tx) queries := repo.New(tx)
+4 -4
View File
@@ -6,10 +6,10 @@ import (
"github.com/shopspring/decimal" "github.com/shopspring/decimal"
) )
func mapRepoGetSharesRow(s repo.GetSharesRow) ExpenseShare { func mapRepoGetSharesRow(s repo.GetSharesRow) Share {
share, _ := decimal.NewFromString(s.ExpensesShare.Share) share, _ := decimal.NewFromString(s.ExpensesShare.Share)
return ExpenseShare{ return Share{
ExpShareID: s.ExpensesShare.ExpShareID, ExpShareID: s.ExpensesShare.ExpShareID,
Share: share, Share: share,
User: User{ User: User{
@@ -19,8 +19,8 @@ func mapRepoGetSharesRow(s repo.GetSharesRow) ExpenseShare {
} }
} }
func mapRepoGetSharesRows(es []repo.GetSharesRow) []ExpenseShare { func mapRepoGetSharesRows(es []repo.GetSharesRow) []Share {
shares := make([]ExpenseShare, len(es)) shares := make([]Share, len(es))
for k, exp := range es { for k, exp := range es {
shares[k] = mapRepoGetSharesRow(exp) shares[k] = mapRepoGetSharesRow(exp)
} }
+1 -1
View File
@@ -9,7 +9,7 @@ import (
"github.com/shopspring/decimal" "github.com/shopspring/decimal"
) )
func processFormShares(c *gin.Context) ([]mod.ExpenseShare, error) { func processFormShares(c *gin.Context) ([]mod.Share, error) {
userIDS := c.PostFormArray("shares-user-ids[]") userIDS := c.PostFormArray("shares-user-ids[]")
shareIDs := c.PostFormArray("shares-ids[]") shareIDs := c.PostFormArray("shares-ids[]")
shares := c.PostFormArray("shares-percent[]") shares := c.PostFormArray("shares-percent[]")