Extract another method

This commit is contained in:
Jan Bader 2021-12-11 12:57:15 +00:00
parent 18cd29cca2
commit e2413290b4
2 changed files with 43 additions and 38 deletions

View File

@ -14,7 +14,7 @@ import (
type BudgetingData struct {
AlwaysNeededData
Categories []CategoryWithBalance
AvailableBalance postgres.Numeric
AvailableBalance float64
Date time.Time
Next time.Time
Previous time.Time
@ -75,6 +75,12 @@ func (h *Handler) budgeting(c *gin.Context) {
}
firstOfNextMonth := firstOfMonth.AddDate(0, 1, 0)
firstOfPreviousMonth := firstOfMonth.AddDate(0, -1, 0)
d := BudgetingData{
AlwaysNeededData: c.MustGet("data").(AlwaysNeededData),
Date: firstOfMonth,
Next: firstOfNextMonth,
Previous: firstOfPreviousMonth,
}
categories, err := h.Service.DB.GetCategories(c.Request.Context(), budgetUUID)
@ -84,6 +90,40 @@ func (h *Handler) budgeting(c *gin.Context) {
return
}
// skip everything in the future
categoriesWithBalance, added, assigned, err := h.calculateBalances(c, budgetUUID, firstOfNextMonth, firstOfMonth, categories, cumultativeBalances)
if err != nil {
return
}
d.Categories = categoriesWithBalance
data := c.MustGet("data").(AlwaysNeededData)
var availableBalance float64 = 0
for _, cat := range categories {
if cat.ID != data.Budget.IncomeCategoryID {
continue
}
availableBalance = -assigned - added
for _, bal := range cumultativeBalances {
if bal.CategoryID != cat.ID {
continue
}
if !bal.Date.Before(firstOfNextMonth) {
continue
}
availableBalance += bal.Transactions.GetFloat64()
}
}
d.AvailableBalance = availableBalance
c.HTML(http.StatusOK, "budgeting.html", d)
}
func (h *Handler) calculateBalances(c *gin.Context, budgetUUID uuid.UUID, firstOfNextMonth time.Time, firstOfMonth time.Time, categories []postgres.GetCategoriesRow, cumultativeBalances []postgres.GetCumultativeBalancesRow) ([]CategoryWithBalance, float64, float64, error) {
categoriesWithBalance := []CategoryWithBalance{}
var added float64 = 0
@ -98,7 +138,6 @@ func (h *Handler) budgeting(c *gin.Context) {
continue
}
// skip everything in the future
if !bal.Date.Before(firstOfNextMonth) {
continue
}
@ -122,39 +161,5 @@ func (h *Handler) budgeting(c *gin.Context) {
categoriesWithBalance = append(categoriesWithBalance, categoryWithBalance)
}
data := c.MustGet("data").(AlwaysNeededData)
var availableBalance float64 = 0
for _, cat := range categories {
if cat.ID != data.Budget.IncomeCategoryID {
continue
}
availableBalance = -assigned - added
for _, bal := range cumultativeBalances {
if bal.CategoryID != cat.ID {
continue
}
if !bal.Date.Before(firstOfNextMonth) {
continue
}
availableBalance += bal.Transactions.GetFloat64()
}
}
var availableBalanceNum postgres.Numeric
availableBalanceNum.Set(availableBalance)
d := BudgetingData{
AlwaysNeededData: c.MustGet("data").(AlwaysNeededData),
Categories: categoriesWithBalance,
AvailableBalance: availableBalanceNum,
Date: firstOfMonth,
Next: firstOfNextMonth,
Previous: firstOfPreviousMonth,
}
c.HTML(http.StatusOK, "budgeting.html", d)
return categoriesWithBalance, added, assigned, nil
}

View File

@ -20,7 +20,7 @@
<a href="{{printf "/budget/%s/%d/%d" .Budget.ID .Next.Year .Next.Month}}">Next Month</a>
</div>
<div>
<span>Available Balance: </span>{{template "amount" .AvailableBalance}}
<span>Available Balance: </span>{{template "amountf64" .AvailableBalance}}
</div>
<table class="container col-lg-12" id="content">
<tr>