still adding the 'Calculated' column
This commit is contained in:
@@ -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
|
||||
- [ ] On the categories page, add a column of how many times a given category is
|
||||
used
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
print "hello, world!"
|
||||
|
||||
@@ -9,13 +9,13 @@ WHERE "ExpID" = ?;
|
||||
|
||||
-- name: InsertShare :execresult
|
||||
INSERT INTO "expensesShares"(
|
||||
"ExpID", "UserID", "Share"
|
||||
"ExpID", "UserID", "Share", "Calculated"
|
||||
)
|
||||
VALUES(?, ?, ?);
|
||||
VALUES(?, ?, ?, ?);
|
||||
|
||||
-- name: UpdateShare :execresult
|
||||
UPDATE expensesShares
|
||||
SET "UserID" = ?, "Share" = ?
|
||||
SET "UserID" = ?, "Share" = ?, "Calculated" = ?
|
||||
WHERE "ExpShareID" = ?;
|
||||
|
||||
-- name: DeleteShare :execresult
|
||||
|
||||
@@ -40,6 +40,7 @@ type ExpensesShare struct {
|
||||
ExpID int64
|
||||
UserID int64
|
||||
Share string
|
||||
Calculated string
|
||||
}
|
||||
|
||||
type Store struct {
|
||||
|
||||
@@ -20,7 +20,7 @@ func (q *Queries) DeleteShare(ctx context.Context, expshareid int64) (sql.Result
|
||||
|
||||
const getShares = `-- name: GetShares :many
|
||||
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
|
||||
"expensesShares" as shares
|
||||
JOIN
|
||||
@@ -47,6 +47,7 @@ func (q *Queries) GetShares(ctx context.Context, expid int64) ([]GetSharesRow, e
|
||||
&i.ExpensesShare.ExpID,
|
||||
&i.ExpensesShare.UserID,
|
||||
&i.ExpensesShare.Share,
|
||||
&i.ExpensesShare.Calculated,
|
||||
&i.User.UserID,
|
||||
&i.User.UserName,
|
||||
&i.User.UserPass,
|
||||
@@ -66,33 +67,45 @@ func (q *Queries) GetShares(ctx context.Context, expid int64) ([]GetSharesRow, e
|
||||
|
||||
const insertShare = `-- name: InsertShare :execresult
|
||||
INSERT INTO "expensesShares"(
|
||||
"ExpID", "UserID", "Share"
|
||||
"ExpID", "UserID", "Share", "Calculated"
|
||||
)
|
||||
VALUES(?, ?, ?)
|
||||
VALUES(?, ?, ?, ?)
|
||||
`
|
||||
|
||||
type InsertShareParams struct {
|
||||
ExpID int64
|
||||
UserID int64
|
||||
Share string
|
||||
ExpID int64
|
||||
UserID int64
|
||||
Share string
|
||||
Calculated string
|
||||
}
|
||||
|
||||
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
|
||||
UPDATE expensesShares
|
||||
SET "UserID" = ?, "Share" = ?
|
||||
SET "UserID" = ?, "Share" = ?, "Calculated" = ?
|
||||
WHERE "ExpShareID" = ?
|
||||
`
|
||||
|
||||
type UpdateShareParams struct {
|
||||
UserID int64
|
||||
Share string
|
||||
Calculated string
|
||||
ExpShareID int64
|
||||
}
|
||||
|
||||
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,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -44,6 +44,7 @@ CREATE TABLE "expensesShares" (
|
||||
"ExpID" int NOT NULL,
|
||||
"UserID" int NOT NULL,
|
||||
"Share" TEXT NOT NULL DEFAULT 0.5,
|
||||
"Calculated" TEXT NOT NULL DEFAULT 0,
|
||||
FOREIGN KEY(ExpID) REFERENCES expenses(ExpID) ON DELETE RESTRICT,
|
||||
FOREIGN KEY(UserID) REFERENCES users(UserID) ON DELETE RESTRICT
|
||||
)
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
mod "expenses/expenses/models"
|
||||
"testing"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
dec "github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -30,18 +30,18 @@ var (
|
||||
// this expense is paid off
|
||||
expense1 = mod.Expense{
|
||||
ExpID: 0,
|
||||
Value: decimal.NewFromInt(150),
|
||||
Shares: []mod.ExpenseShare{
|
||||
{ExpShareID: 0, User: user1, Share: decimal.NewFromFloat(0.4)},
|
||||
{ExpShareID: 1, User: user2, Share: decimal.NewFromFloat(0.4)},
|
||||
{ExpShareID: 2, User: user3, Share: decimal.NewFromFloat(0.2)},
|
||||
Value: dec.NewFromInt(150),
|
||||
Shares: []mod.Share{
|
||||
{ExpShareID: 0, User: user1, Share: dec.NewFromFloat(0.4)},
|
||||
{ExpShareID: 1, User: user2, Share: dec.NewFromFloat(0.4)},
|
||||
{ExpShareID: 2, User: user3, Share: dec.NewFromFloat(0.2)},
|
||||
},
|
||||
Payments: []mod.ExpensePayment{
|
||||
{ExpPaymID: 0, User: user1, PayedAmount: decimal.NewFromInt(50)},
|
||||
{ExpPaymID: 1, User: user1, PayedAmount: decimal.NewFromInt(50)},
|
||||
{ExpPaymID: 2, User: user2, PayedAmount: decimal.NewFromInt(0)},
|
||||
{ExpPaymID: 3, User: user3, PayedAmount: decimal.NewFromInt(30)},
|
||||
{ExpPaymID: 4, User: user3, PayedAmount: decimal.NewFromInt(20)},
|
||||
{ExpPaymID: 0, User: user1, PayedAmount: dec.NewFromInt(50)},
|
||||
{ExpPaymID: 1, User: user1, PayedAmount: dec.NewFromInt(50)},
|
||||
{ExpPaymID: 2, User: user2, PayedAmount: dec.NewFromInt(0)},
|
||||
{ExpPaymID: 3, User: user3, PayedAmount: dec.NewFromInt(30)},
|
||||
{ExpPaymID: 4, User: user3, PayedAmount: dec.NewFromInt(20)},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -49,22 +49,22 @@ var (
|
||||
// Expense is paid off
|
||||
expense2 = mod.Expense{
|
||||
ExpID: 0,
|
||||
Value: decimal.NewFromInt(160),
|
||||
Shares: []mod.ExpenseShare{
|
||||
{ExpShareID: 0, User: user1, Share: decimal.NewFromFloat(0.3)},
|
||||
{ExpShareID: 1, User: user2, Share: decimal.NewFromFloat(0.3)},
|
||||
{ExpShareID: 2, User: user3, Share: decimal.NewFromFloat(0.3)},
|
||||
{ExpShareID: 3, User: user4, Share: decimal.NewFromFloat(0.1)},
|
||||
Value: dec.NewFromInt(160),
|
||||
Shares: []mod.Share{
|
||||
{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{
|
||||
{ExpPaymID: 0, User: user1, PayedAmount: decimal.NewFromInt(40)},
|
||||
{ExpPaymID: 1, User: user1, PayedAmount: decimal.NewFromInt(40)},
|
||||
{ExpPaymID: 2, User: user2, PayedAmount: decimal.NewFromInt(5)},
|
||||
{ExpPaymID: 3, User: user3, PayedAmount: decimal.NewFromInt(30)},
|
||||
{ExpPaymID: 4, User: user3, PayedAmount: decimal.NewFromInt(30)},
|
||||
{ExpPaymID: 5, User: user3, PayedAmount: decimal.NewFromInt(10)},
|
||||
{ExpPaymID: 6, User: user4, PayedAmount: decimal.NewFromInt(4)},
|
||||
{ExpPaymID: 7, User: user4, PayedAmount: decimal.NewFromInt(1)},
|
||||
{ExpPaymID: 0, User: user1, PayedAmount: dec.NewFromInt(40)},
|
||||
{ExpPaymID: 1, User: user1, PayedAmount: dec.NewFromInt(40)},
|
||||
{ExpPaymID: 2, User: user2, PayedAmount: dec.NewFromInt(5)},
|
||||
{ExpPaymID: 3, User: user3, PayedAmount: dec.NewFromInt(30)},
|
||||
{ExpPaymID: 4, User: user3, PayedAmount: dec.NewFromInt(30)},
|
||||
{ExpPaymID: 5, User: user3, PayedAmount: dec.NewFromInt(10)},
|
||||
{ExpPaymID: 6, User: user4, PayedAmount: dec.NewFromInt(4)},
|
||||
{ExpPaymID: 7, User: user4, PayedAmount: dec.NewFromInt(1)},
|
||||
},
|
||||
}
|
||||
/*
|
||||
@@ -121,37 +121,61 @@ var (
|
||||
// Expense is: evenly shared and paid off
|
||||
expense3 = mod.Expense{
|
||||
ExpID: 0,
|
||||
Value: decimal.NewFromInt(160),
|
||||
Shares: []mod.ExpenseShare{
|
||||
{ExpShareID: 0, User: user1, Share: decimal.NewFromFloat(0.3)},
|
||||
{ExpShareID: 1, User: user2, Share: decimal.NewFromFloat(0.3)},
|
||||
{ExpShareID: 2, User: user3, Share: decimal.NewFromFloat(0.3)},
|
||||
{ExpShareID: 3, User: user4, Share: decimal.NewFromFloat(0.1)},
|
||||
Owner: mod.User{
|
||||
UserID: user1.UserID,
|
||||
},
|
||||
Value: dec.NewFromInt(160),
|
||||
Shares: []mod.Share{
|
||||
{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{
|
||||
{ExpPaymID: 0, User: user1, PayedAmount: decimal.NewFromInt(40)},
|
||||
{ExpPaymID: 1, User: user1, PayedAmount: decimal.NewFromInt(8)},
|
||||
{ExpPaymID: 2, User: user2, PayedAmount: decimal.NewFromInt(48)},
|
||||
{ExpPaymID: 3, User: user3, PayedAmount: decimal.NewFromInt(15)},
|
||||
{ExpPaymID: 4, User: user3, PayedAmount: decimal.NewFromInt(15)},
|
||||
{ExpPaymID: 5, User: user3, PayedAmount: decimal.NewFromInt(18)},
|
||||
{ExpPaymID: 6, User: user4, PayedAmount: decimal.NewFromInt(8)},
|
||||
{ExpPaymID: 7, User: user4, PayedAmount: decimal.NewFromInt(8)},
|
||||
{ExpPaymID: 0, User: user1, PayedAmount: dec.NewFromInt(40)},
|
||||
{ExpPaymID: 1, User: user1, PayedAmount: dec.NewFromInt(8)},
|
||||
{ExpPaymID: 2, User: user2, PayedAmount: dec.NewFromInt(48)},
|
||||
{ExpPaymID: 3, User: user3, PayedAmount: dec.NewFromInt(15)},
|
||||
{ExpPaymID: 4, User: user3, PayedAmount: dec.NewFromInt(15)},
|
||||
{ExpPaymID: 5, User: user3, PayedAmount: dec.NewFromInt(18)},
|
||||
{ExpPaymID: 6, User: user4, PayedAmount: dec.NewFromInt(8)},
|
||||
{ExpPaymID: 7, User: user4, PayedAmount: dec.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)},
|
||||
Value: dec.NewFromFloat(0.50),
|
||||
Shares: []mod.Share{
|
||||
{ExpShareID: 0, User: user1, Share: dec.NewFromFloat(0.34)},
|
||||
{ExpShareID: 1, User: user2, Share: dec.NewFromFloat(0.33)},
|
||||
{ExpShareID: 2, User: user3, Share: dec.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)},
|
||||
{ExpPaymID: 0, User: user1, PayedAmount: dec.NewFromFloat(0.18)},
|
||||
{ExpPaymID: 1, User: user1, PayedAmount: dec.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)},
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
@@ -2,8 +2,9 @@ package expenses
|
||||
|
||||
import (
|
||||
mod "expenses/expenses/models"
|
||||
"log"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
dec "github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
// TODO
|
||||
@@ -11,20 +12,20 @@ import (
|
||||
// conversion that the user calls before calling this
|
||||
// Can even create a struct to then hold the data
|
||||
func ParseFormShares(userIDs []string, shares []string, sharesIDs []string,
|
||||
) ([]mod.ExpenseShare, error) {
|
||||
shareList := []mod.ExpenseShare{}
|
||||
) ([]mod.Share, error) {
|
||||
shareList := []mod.Share{}
|
||||
for i := range userIDs {
|
||||
userID, err := ParseID(userIDs[i])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
share, err := decimal.NewFromString(shares[i])
|
||||
share, err := dec.NewFromString(shares[i])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
newShare := mod.ExpenseShare{
|
||||
newShare := mod.Share{
|
||||
ExpShareID: -1,
|
||||
User: mod.User{
|
||||
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
|
||||
// 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
|
||||
}
|
||||
|
||||
@@ -59,7 +75,7 @@ func (s *Service) DeleteShare(id int64) error {
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
share := mod.ExpenseShare{
|
||||
share := mod.Share{
|
||||
ExpShareID: id,
|
||||
}
|
||||
|
||||
|
||||
@@ -1,101 +1,57 @@
|
||||
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])
|
||||
// }
|
||||
// }
|
||||
// })
|
||||
// }
|
||||
// }
|
||||
import (
|
||||
mod "expenses/expenses/models"
|
||||
dec "github.com/shopspring/decimal"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNormalizeShares(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
exp *mod.Expense
|
||||
expectedShares mod.Shares
|
||||
}{
|
||||
{
|
||||
name: "Split with remainder",
|
||||
exp: &expense5,
|
||||
expectedShares: mod.Shares{
|
||||
{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)},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Even split",
|
||||
exp: &expense3,
|
||||
expectedShares: mod.Shares{
|
||||
{ExpShareID: 0, User: user1,
|
||||
Share: dec.NewFromFloat(0.3), Calculated: dec.NewFromFloat(48.00)},
|
||||
{ExpShareID: 1, User: user2,
|
||||
Share: dec.NewFromFloat(0.3), Calculated: dec.NewFromFloat(48.00)},
|
||||
{ExpShareID: 2, User: user3,
|
||||
Share: dec.NewFromFloat(0.3), Calculated: dec.NewFromFloat(48.00)},
|
||||
{ExpShareID: 3, User: user4,
|
||||
Share: dec.NewFromFloat(0.1), Calculated: dec.NewFromFloat(16.00)},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := normalizeShares(tc.exp)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to normalize shares: %+v", err)
|
||||
}
|
||||
|
||||
if !tc.expectedShares.Equal(tc.exp.Shares) {
|
||||
t.Errorf("Resulting shares: %+v\nAre not correct: %+v", tc.exp.Shares,
|
||||
tc.expectedShares)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -204,12 +204,8 @@ func analyzeExpense(e *mod.Expense) {
|
||||
// Figure out if its evenly shared by the people associated to it
|
||||
e.SharesEven = ExpenseIsEvenlyShared(e)
|
||||
|
||||
// Create the debts
|
||||
debts, err := CalculateDebts(e)
|
||||
if err != nil {
|
||||
// do nothing apparently
|
||||
}
|
||||
e.Debts = debts
|
||||
// Update the calculated fields on the shares
|
||||
normalizeShares(e)
|
||||
}
|
||||
|
||||
func (s *Service) NewExpense(exp mod.Expense) error {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package expenses
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
// "fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
@@ -33,10 +33,10 @@ func TestExpenseIsEvenlyShared(t *testing.T) {
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
// expected = true
|
||||
// result = ExpenseIsEvenlyShared(&expense4)
|
||||
// if expected != result {
|
||||
// fmt.Printf("Expense: %+v\n", expense4)
|
||||
// t.Errorf("Expected %v, got %v\n", expected, result)
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ type Expense struct {
|
||||
Owner User
|
||||
Date int64
|
||||
Payments []ExpensePayment
|
||||
Shares []ExpenseShare
|
||||
Shares []Share
|
||||
Debts Debts
|
||||
PaidOff bool
|
||||
SharesEven bool
|
||||
@@ -38,7 +38,7 @@ func NewExpense() Expense {
|
||||
Owner: NewUser(),
|
||||
Date: 0,
|
||||
Payments: []ExpensePayment{},
|
||||
Shares: []ExpenseShare{},
|
||||
Shares: []Share{},
|
||||
PaidOff: false,
|
||||
SharesEven: false,
|
||||
CreationDate: 0,
|
||||
|
||||
@@ -30,7 +30,7 @@ func mapRepoGetExpenseRow(e repo.GetExpenseRow) Expense {
|
||||
UserName: e.User.UserName,
|
||||
},
|
||||
Date: e.Expense.ExpDate,
|
||||
Shares: []ExpenseShare{},
|
||||
Shares: []Share{},
|
||||
Payments: []ExpensePayment{},
|
||||
PaidOff: e.Expense.PaidOff,
|
||||
SharesEven: e.Expense.SharesEven,
|
||||
@@ -62,7 +62,7 @@ func mapRepoGetExpenseRowMulti(e repo.GetExpensesRow) Expense {
|
||||
UserName: e.User.UserName,
|
||||
},
|
||||
Date: e.Expense.ExpDate,
|
||||
Shares: []ExpenseShare{},
|
||||
Shares: []Share{},
|
||||
Payments: []ExpensePayment{},
|
||||
PaidOff: e.Expense.PaidOff,
|
||||
SharesEven: e.Expense.SharesEven,
|
||||
|
||||
@@ -12,12 +12,28 @@ import (
|
||||
dec "github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
type ExpenseShare struct {
|
||||
type Share struct {
|
||||
ExpShareID int64
|
||||
User User
|
||||
Share dec.Decimal
|
||||
Calculated dec.Decimal // If certain shares equal fractional cent counts
|
||||
// Its more useful to already have that computed
|
||||
Calculated dec.Decimal // Normalized user share because of fracd cents
|
||||
}
|
||||
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 {
|
||||
@@ -33,14 +49,15 @@ func (e *Expense) GetShares(tx *sql.Tx) error {
|
||||
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()
|
||||
|
||||
queries := repo.New(tx)
|
||||
res, err := queries.InsertShare(ctx, repo.InsertShareParams{
|
||||
ExpID: expID,
|
||||
Share: sh.Share.String(),
|
||||
UserID: sh.User.UserID,
|
||||
ExpID: expID,
|
||||
Share: sh.Share.String(),
|
||||
UserID: sh.User.UserID,
|
||||
Calculated: sh.Calculated.String(),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -56,7 +73,7 @@ func (sh *ExpenseShare) Insert(tx *sql.Tx, expID int64) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (sh *ExpenseShare) Update(tx *sql.Tx) error {
|
||||
func (sh *Share) Update(tx *sql.Tx) error {
|
||||
ctx := context.Background()
|
||||
|
||||
queries := repo.New(tx)
|
||||
@@ -64,6 +81,7 @@ func (sh *ExpenseShare) Update(tx *sql.Tx) error {
|
||||
ExpShareID: sh.ExpShareID,
|
||||
Share: sh.Share.String(),
|
||||
UserID: sh.User.UserID,
|
||||
Calculated: sh.Calculated.String(),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -79,7 +97,7 @@ func (sh *ExpenseShare) Update(tx *sql.Tx) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (sh *ExpenseShare) Delete(tx *sql.Tx) error {
|
||||
func (sh *Share) Delete(tx *sql.Tx) error {
|
||||
ctx := context.Background()
|
||||
|
||||
queries := repo.New(tx)
|
||||
|
||||
@@ -6,10 +6,10 @@ import (
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
func mapRepoGetSharesRow(s repo.GetSharesRow) ExpenseShare {
|
||||
func mapRepoGetSharesRow(s repo.GetSharesRow) Share {
|
||||
share, _ := decimal.NewFromString(s.ExpensesShare.Share)
|
||||
|
||||
return ExpenseShare{
|
||||
return Share{
|
||||
ExpShareID: s.ExpensesShare.ExpShareID,
|
||||
Share: share,
|
||||
User: User{
|
||||
@@ -19,8 +19,8 @@ func mapRepoGetSharesRow(s repo.GetSharesRow) ExpenseShare {
|
||||
}
|
||||
}
|
||||
|
||||
func mapRepoGetSharesRows(es []repo.GetSharesRow) []ExpenseShare {
|
||||
shares := make([]ExpenseShare, len(es))
|
||||
func mapRepoGetSharesRows(es []repo.GetSharesRow) []Share {
|
||||
shares := make([]Share, len(es))
|
||||
for k, exp := range es {
|
||||
shares[k] = mapRepoGetSharesRow(exp)
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
"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[]")
|
||||
shareIDs := c.PostFormArray("shares-ids[]")
|
||||
shares := c.PostFormArray("shares-percent[]")
|
||||
|
||||
Reference in New Issue
Block a user