Moving in another direction regarding debts
This commit is contained in:
@@ -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;
|
||||
@@ -28,7 +28,8 @@ CREATE TABLE "expenses" (
|
||||
FOREIGN KEY(CategoryID) REFERENCES categories(CategoryID) ON DELETE RESTRICT,
|
||||
FOREIGN KEY(OwnerUserID) REFERENCES users(UserID) ON DELETE RESTRICT,
|
||||
FOREIGN KEY(TypeID) REFERENCES expensetypes(TypeID) ON DELETE RESTRICT
|
||||
)
|
||||
)
|
||||
|
||||
CREATE TABLE "expensesPayments" (
|
||||
"ExpPaymID" integer PRIMARY KEY AUTOINCREMENT,
|
||||
"ExpID" int NOT NULL,
|
||||
@@ -36,7 +37,8 @@ CREATE TABLE "expensesPayments" (
|
||||
"Payed" TEXT NOT NULL DEFAULT 0,
|
||||
FOREIGN KEY(ExpID) REFERENCES expenses(ExpID) ON DELETE RESTRICT,
|
||||
FOREIGN KEY(UserID) REFERENCES users(UserID) ON DELETE RESTRICT
|
||||
)
|
||||
)
|
||||
|
||||
CREATE TABLE "expensesShares" (
|
||||
"ExpShareID" integer PRIMARY KEY AUTOINCREMENT,
|
||||
"ExpID" int NOT NULL,
|
||||
@@ -44,7 +46,8 @@ CREATE TABLE "expensesShares" (
|
||||
"Share" TEXT NOT NULL DEFAULT 0.5,
|
||||
FOREIGN KEY(ExpID) REFERENCES expenses(ExpID) ON DELETE RESTRICT,
|
||||
FOREIGN KEY(UserID) REFERENCES users(UserID) ON DELETE RESTRICT
|
||||
)
|
||||
)
|
||||
|
||||
CREATE TABLE stores(
|
||||
"StoreID" integer PRIMARY KEY AUTOINCREMENT,
|
||||
"StoreName" text NOT NULL
|
||||
|
||||
@@ -118,8 +118,16 @@ func resolveDebts(debtors UserTabs, creditors UserTabs) mod.Debts {
|
||||
|
||||
debts := mod.Debts{}
|
||||
for key, debt := range keyedDebts {
|
||||
debt := mod.Debt{Debtor: key.Debtor, Creditor: key.Creditor, Sum: debt}
|
||||
debts = append(debts, debt)
|
||||
newDebt := mod.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
|
||||
|
||||
@@ -80,10 +80,10 @@ var (
|
||||
User3 70 | (0.3 * 160) - 70 = -22 (is owed 22)
|
||||
User4 5 | (0.1 * 160) - 5 = 11 (owes 11)
|
||||
|
||||
Debtors {u4, 11}, {u2, 43},
|
||||
Debtors {u4, 11}, {u2, 43},
|
||||
Creditors {u3, 22}, {u1, 32}
|
||||
|
||||
{u3, 22} - {u4, 11} = {u3, 11}, {u4, 00}
|
||||
{u3, 22} - {u4, 11} = {u3, 11}, {u4, 00}
|
||||
{u3, 11} - {u2, 43} = {u3, 00}, {u2, 32}
|
||||
Debts:
|
||||
{Debtor: u4, Creditor: u3, Sum: 11}
|
||||
@@ -139,6 +139,21 @@ var (
|
||||
{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) {
|
||||
|
||||
@@ -46,6 +46,12 @@ func ParseFormShares(userIDs []string, shares []string, sharesIDs []string,
|
||||
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 {
|
||||
tx, err := s.DB.Begin()
|
||||
if err != nil {
|
||||
|
||||
@@ -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])
|
||||
// }
|
||||
// }
|
||||
// })
|
||||
// }
|
||||
// }
|
||||
@@ -172,6 +172,8 @@ func ExpenseTotalPayed(exp *mod.Expense) decimal.Decimal {
|
||||
}
|
||||
|
||||
func ExpenseIsEvenlyShared(exp *mod.Expense) bool {
|
||||
log.Printf("is exp %d even?", exp.ExpID)
|
||||
|
||||
shares := mapShares(exp)
|
||||
payments := mapPayments(exp)
|
||||
|
||||
@@ -184,11 +186,14 @@ func ExpenseIsEvenlyShared(exp *mod.Expense) bool {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("Yes")
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -198,6 +203,13 @@ 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
|
||||
}
|
||||
|
||||
func (s *Service) NewExpense(exp mod.Expense) error {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package expenses
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
@@ -30,4 +31,12 @@ func TestExpenseIsEvenlyShared(t *testing.T) {
|
||||
if 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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,9 +38,3 @@ func (ds Debts) Equal(t Debts) bool {
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (d *Debt) Insert() {
|
||||
}
|
||||
|
||||
// func (d *Debt) Update() {
|
||||
// }
|
||||
|
||||
@@ -9,13 +9,15 @@ import (
|
||||
"fmt"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"github.com/shopspring/decimal"
|
||||
dec "github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
type ExpenseShare struct {
|
||||
ExpShareID int64
|
||||
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 {
|
||||
|
||||
@@ -3,3 +3,8 @@
|
||||
# Expenses
|
||||
## Expenses Types
|
||||
- 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
|
||||
|
||||
Reference in New Issue
Block a user