From 4925aad96329510aa5eb791f4d18dd5487d8eceb Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Fri, 25 Jul 2025 22:58:49 +0100 Subject: [PATCH] Reworked the overview page and some qol stuff, going to merge now --- src/Makefile | 8 ++-- src/assets/htmx/overview.html | 3 +- src/assets/htmx/overviewResults.html | 46 ++++++++++++------- src/expenses/expenseDebts.go | 2 +- src/main.go | 5 +++ src/pages/overview.go | 67 +++++++++++----------------- 6 files changed, 68 insertions(+), 63 deletions(-) diff --git a/src/Makefile b/src/Makefile index 3321ab2..fb6f756 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,8 +1,8 @@ build_release_alpine: - rm -rf build && mkdir build - GIN_MODE=RELEASE CGO_ENABLED=1 GOOS=linux CC=musl-gcc \ - go build -o ./build/expensesweb + rm -rf build && mkdir -p build/data + CGO_ENABLED=1 GOOS=linux CC=musl-gcc \ + go build -ldflags "-X main.ginMode=release" -o ./build/expensesweb cp -r ./assets ./build/ cp ./config.yaml ./build/ - cp ./data/data.db ./build/ + cp ./data/data.db ./build/data/ find build -name "*.ttf" ! -name DejaVuSansMono.ttf -delete diff --git a/src/assets/htmx/overview.html b/src/assets/htmx/overview.html index 9a3af6f..50fc6c3 100644 --- a/src/assets/htmx/overview.html +++ b/src/assets/htmx/overview.html @@ -14,12 +14,13 @@

+
diff --git a/src/assets/htmx/overviewResults.html b/src/assets/htmx/overviewResults.html index 5ebdd5e..44e805f 100644 --- a/src/assets/htmx/overviewResults.html +++ b/src/assets/htmx/overviewResults.html @@ -1,32 +1,44 @@ +{{ range .expenses }} - {{ range .expenses }} - - - + + + - {{ end }}
ID Desc Value
{{ .ExpID }}{{ .Description }}{{ formatPrice .Value }}{{ .Exp.ExpID }}{{ .Exp.Description }}{{ formatPrice .Exp.Value }}
- -
- - - - {{ range $key, $value := .summary }} + + + + {{ range .Debtors }} - - + + + + {{ end }} +
UserIDOwes
UserSum
- {{ $key }} - - {{ $value }} - {{ .Debtor.UserName }}{{ formatPrice .Sum }}
+
+{{ end }} +
+ +

User Debt Summary:

+ + + + + + + {{ range $user, $value := .summary }} + + + + {{ end }}
IDNAMEOWES
{{ $user.UserID }}{{ $user.UserName }}{{ $value }}
diff --git a/src/expenses/expenseDebts.go b/src/expenses/expenseDebts.go index 37b249e..36141b6 100644 --- a/src/expenses/expenseDebts.go +++ b/src/expenses/expenseDebts.go @@ -15,7 +15,7 @@ func sortBySum(a, b Debt) int { return 0 } -func (e *Expense) calculateDebts() ([]Debt, error) { +func (e *Expense) CalculateDebts() ([]Debt, error) { dc := NewDebtCalculator(e) dc.mapShares() dc.mapPayments() diff --git a/src/main.go b/src/main.go index 9283744..b851281 100644 --- a/src/main.go +++ b/src/main.go @@ -11,7 +11,12 @@ import ( "github.com/gin-gonic/gin" ) +// This can be set in the compilation command - in the Makefile +var ginMode = "debug" + func main() { + gin.SetMode(ginMode) + config.SetConfig("./config.yaml") cfg := config.GetInstance() diff --git a/src/pages/overview.go b/src/pages/overview.go index 8ad70a1..ae5372f 100644 --- a/src/pages/overview.go +++ b/src/pages/overview.go @@ -14,6 +14,16 @@ import ( "github.com/gin-gonic/gin" ) +type ExpenseDebtOverview struct { + Exp *expenses.Expense + Debtors []expenses.Debt +} + +type UserDebtSummary struct { + Debtor expenses.User + Total float32 +} + func overviewContent() template.HTML { cfg := config.GetInstance() @@ -80,65 +90,42 @@ func getResults(c *gin.Context) { } } - // Now we are going to try and get how much each user paid and how much - // it owes - summary := make(map[int]float32) - expensesWithDebts := []expenses.Expense{} + userDebtSummary := make(map[expenses.User]float32) + expensesWithDebts := []ExpenseDebtOverview{} for _, exp := range queriedExpenses { if len(exp.Shares) <= 1 { continue } - log.Printf("ID: %d, Value: %.2f", exp.ExpID, exp.Value) - if len(exp.Shares) > 1 { - userShares := userShares(&exp) - userPayments := userPayments(&exp) - - for userID, payment := range userPayments { - owed := (userShares[userID] * exp.Value) - userPayments[userID] - log.Printf(" [%d] %.2f - %.2f", userID, payment, owed) - if owed > 0 { - summary[userID] += owed - } - } + expenseDebts, err := exp.CalculateDebts() + if err != nil { + // whatevs yo + continue + } + if len(expenseDebts) <= 0 { + continue } - expensesWithDebts = append(expensesWithDebts, exp) + expensesWithDebts = append(expensesWithDebts, ExpenseDebtOverview{ + Exp: &exp, Debtors: expenseDebts, + }) + + for _, debt := range expenseDebts { + userDebtSummary[debt.Debtor] += debt.Sum + } } content := templating.HtmlTemplate( fp.Join(cfg.AssetsDir, "htmx/overviewResults.html"), map[string]any{ "expenses": expensesWithDebts, - "summary": summary, + "summary": userDebtSummary, }, ) - log.Println("Summary:") - log.Println(summary) c.String(http.StatusOK, string(content)) } -func userPayments(e *expenses.Expense) map[int]float32 { - userPayments := make(map[int]float32) - - for _, payment := range e.Payments { - userPayments[payment.User.UserID] += payment.PayedAmount - } - - return userPayments -} - -func userShares(e *expenses.Expense) map[int]float32 { - userShares := make(map[int]float32) - - for _, share := range e.Shares { - userShares[share.User.UserID] = share.Share - } - - return userShares -} - func RouteOverview(router *gin.Engine) { router.GET("/overview", overviewPage) router.POST("/overview", overviewPartialPage)