Reworked the overview page and some qol stuff, going to merge now
This commit is contained in:
+4
-4
@@ -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
|
||||
|
||||
@@ -14,12 +14,13 @@
|
||||
</p>
|
||||
|
||||
<button
|
||||
type="button"
|
||||
type="submit"
|
||||
hx-post="/overview/ranged"
|
||||
hx-target="#query-results"
|
||||
hx-swap="innerHTML"
|
||||
id="expenses">QUERY</button>
|
||||
</form>
|
||||
<br>
|
||||
|
||||
<div id="query-results">
|
||||
</div>
|
||||
|
||||
@@ -1,32 +1,44 @@
|
||||
{{ range .expenses }}
|
||||
<table>
|
||||
<tr>
|
||||
<th>ID</th>
|
||||
<th>Desc</th>
|
||||
<th>Value</th>
|
||||
</tr>
|
||||
{{ range .expenses }}
|
||||
<tr>
|
||||
<td><a href="/expenses/{{ .ExpID }}">{{ .ExpID }}</td>
|
||||
<td spellcheck="false" contenteditable>{{ .Description }}</td>
|
||||
<td spellcheck="false" contenteditable>{{ formatPrice .Value }}</td>
|
||||
<td><a href="/expenses/{{ .Exp.ExpID }}">{{ .Exp.ExpID }}</td>
|
||||
<td spellcheck="false" contenteditable>{{ .Exp.Description }}</td>
|
||||
<td spellcheck="false" contenteditable>{{ formatPrice .Exp.Value }}</td>
|
||||
</tr>
|
||||
{{ end }}
|
||||
</table>
|
||||
|
||||
<br>
|
||||
<table>
|
||||
<tr>
|
||||
<th>UserID</th>
|
||||
<th>Owes</th>
|
||||
</tr>
|
||||
{{ range $key, $value := .summary }}
|
||||
<th>User</th>
|
||||
<th>Sum</th>
|
||||
</tr>
|
||||
{{ range .Debtors }}
|
||||
<tr>
|
||||
<td>
|
||||
{{ $key }}
|
||||
</td>
|
||||
<td>
|
||||
{{ $value }}
|
||||
</td>
|
||||
<td>{{ .Debtor.UserName }}</td>
|
||||
<td>{{ formatPrice .Sum }}</td>
|
||||
</tr>
|
||||
{{ end }}
|
||||
</table>
|
||||
<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>
|
||||
{{ end }}
|
||||
</table>
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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()
|
||||
|
||||
+27
-40
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user