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 }}
| ID |
Desc |
Value |
- {{ range .expenses }}
- | {{ .ExpID }} |
- {{ .Description }} |
- {{ formatPrice .Value }} |
+ {{ .Exp.ExpID }} |
+ {{ .Exp.Description }} |
+ {{ formatPrice .Exp.Value }} |
- {{ end }}
-
-
- | UserID |
- Owes |
-
- {{ range $key, $value := .summary }}
+ User |
+ Sum |
+
+ {{ range .Debtors }}
- |
- {{ $key }}
- |
-
- {{ $value }}
- |
+ {{ .Debtor.UserName }} |
+ {{ formatPrice .Sum }} |
+
+ {{ end }}
+
+
+{{ end }}
+
+
+User Debt Summary:
+
+
+ | ID |
+ NAME |
+ OWES |
+
+ {{ range $user, $value := .summary }}
+
+ | {{ $user.UserID }} |
+ {{ $user.UserName }} |
+ {{ $value }} |
{{ end }}
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)