Reworked the overview page and some qol stuff, going to merge now

This commit is contained in:
2025-07-25 22:58:49 +01:00
parent 9ba5106c2d
commit 4925aad963
6 changed files with 68 additions and 63 deletions
+4 -4
View File
@@ -1,8 +1,8 @@
build_release_alpine: build_release_alpine:
rm -rf build && mkdir build rm -rf build && mkdir -p build/data
GIN_MODE=RELEASE CGO_ENABLED=1 GOOS=linux CC=musl-gcc \ CGO_ENABLED=1 GOOS=linux CC=musl-gcc \
go build -o ./build/expensesweb go build -ldflags "-X main.ginMode=release" -o ./build/expensesweb
cp -r ./assets ./build/ cp -r ./assets ./build/
cp ./config.yaml ./build/ cp ./config.yaml ./build/
cp ./data/data.db ./build/ cp ./data/data.db ./build/data/
find build -name "*.ttf" ! -name DejaVuSansMono.ttf -delete find build -name "*.ttf" ! -name DejaVuSansMono.ttf -delete
+2 -1
View File
@@ -14,12 +14,13 @@
</p> </p>
<button <button
type="button" type="submit"
hx-post="/overview/ranged" hx-post="/overview/ranged"
hx-target="#query-results" hx-target="#query-results"
hx-swap="innerHTML" hx-swap="innerHTML"
id="expenses">QUERY</button> id="expenses">QUERY</button>
</form> </form>
<br>
<div id="query-results"> <div id="query-results">
</div> </div>
+29 -17
View File
@@ -1,32 +1,44 @@
{{ range .expenses }}
<table> <table>
<tr> <tr>
<th>ID</th> <th>ID</th>
<th>Desc</th> <th>Desc</th>
<th>Value</th> <th>Value</th>
</tr> </tr>
{{ range .expenses }}
<tr> <tr>
<td><a href="/expenses/{{ .ExpID }}">{{ .ExpID }}</td> <td><a href="/expenses/{{ .Exp.ExpID }}">{{ .Exp.ExpID }}</td>
<td spellcheck="false" contenteditable>{{ .Description }}</td> <td spellcheck="false" contenteditable>{{ .Exp.Description }}</td>
<td spellcheck="false" contenteditable>{{ formatPrice .Value }}</td> <td spellcheck="false" contenteditable>{{ formatPrice .Exp.Value }}</td>
</tr> </tr>
{{ end }}
</table> </table>
<br>
<table> <table>
<tr> <tr>
<th>UserID</th> <th>User</th>
<th>Owes</th> <th>Sum</th>
</tr> </tr>
{{ range $key, $value := .summary }} {{ range .Debtors }}
<tr> <tr>
<td> <td>{{ .Debtor.UserName }}</td>
{{ $key }} <td>{{ formatPrice .Sum }}</td>
</td> </tr>
<td> {{ end }}
{{ $value }} </table>
</td> <hr>
{{ end }}
<br>
<p>User Debt Summary:</p>
<table>
<tr>
<th>ID</th>
<th>NAME</th>
<th>OWES</th>
</tr>
{{ range $user, $value := .summary }}
<tr>
<td>{{ $user.UserID }}</td>
<td>{{ $user.UserName }}</td>
<td>{{ $value }}</td>
</tr> </tr>
{{ end }} {{ end }}
</table> </table>
+1 -1
View File
@@ -15,7 +15,7 @@ func sortBySum(a, b Debt) int {
return 0 return 0
} }
func (e *Expense) calculateDebts() ([]Debt, error) { func (e *Expense) CalculateDebts() ([]Debt, error) {
dc := NewDebtCalculator(e) dc := NewDebtCalculator(e)
dc.mapShares() dc.mapShares()
dc.mapPayments() dc.mapPayments()
+5
View File
@@ -11,7 +11,12 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
// This can be set in the compilation command - in the Makefile
var ginMode = "debug"
func main() { func main() {
gin.SetMode(ginMode)
config.SetConfig("./config.yaml") config.SetConfig("./config.yaml")
cfg := config.GetInstance() cfg := config.GetInstance()
+27 -40
View File
@@ -14,6 +14,16 @@ import (
"github.com/gin-gonic/gin" "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 { func overviewContent() template.HTML {
cfg := config.GetInstance() 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 userDebtSummary := make(map[expenses.User]float32)
// it owes expensesWithDebts := []ExpenseDebtOverview{}
summary := make(map[int]float32)
expensesWithDebts := []expenses.Expense{}
for _, exp := range queriedExpenses { for _, exp := range queriedExpenses {
if len(exp.Shares) <= 1 { if len(exp.Shares) <= 1 {
continue continue
} }
log.Printf("ID: %d, Value: %.2f", exp.ExpID, exp.Value) expenseDebts, err := exp.CalculateDebts()
if len(exp.Shares) > 1 { if err != nil {
userShares := userShares(&exp) // whatevs yo
userPayments := userPayments(&exp) continue
}
for userID, payment := range userPayments { if len(expenseDebts) <= 0 {
owed := (userShares[userID] * exp.Value) - userPayments[userID] continue
log.Printf(" [%d] %.2f - %.2f", userID, payment, owed)
if owed > 0 {
summary[userID] += owed
}
}
} }
expensesWithDebts = append(expensesWithDebts, exp) expensesWithDebts = append(expensesWithDebts, ExpenseDebtOverview{
Exp: &exp, Debtors: expenseDebts,
})
for _, debt := range expenseDebts {
userDebtSummary[debt.Debtor] += debt.Sum
}
} }
content := templating.HtmlTemplate( content := templating.HtmlTemplate(
fp.Join(cfg.AssetsDir, "htmx/overviewResults.html"), fp.Join(cfg.AssetsDir, "htmx/overviewResults.html"),
map[string]any{ map[string]any{
"expenses": expensesWithDebts, "expenses": expensesWithDebts,
"summary": summary, "summary": userDebtSummary,
}, },
) )
log.Println("Summary:")
log.Println(summary)
c.String(http.StatusOK, string(content)) 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) { func RouteOverview(router *gin.Engine) {
router.GET("/overview", overviewPage) router.GET("/overview", overviewPage)
router.POST("/overview", overviewPartialPage) router.POST("/overview", overviewPartialPage)