Extract another method
This commit is contained in:
parent
18cd29cca2
commit
e2413290b4
@ -14,7 +14,7 @@ import (
|
|||||||
type BudgetingData struct {
|
type BudgetingData struct {
|
||||||
AlwaysNeededData
|
AlwaysNeededData
|
||||||
Categories []CategoryWithBalance
|
Categories []CategoryWithBalance
|
||||||
AvailableBalance postgres.Numeric
|
AvailableBalance float64
|
||||||
Date time.Time
|
Date time.Time
|
||||||
Next time.Time
|
Next time.Time
|
||||||
Previous time.Time
|
Previous time.Time
|
||||||
@ -75,6 +75,12 @@ func (h *Handler) budgeting(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
firstOfNextMonth := firstOfMonth.AddDate(0, 1, 0)
|
firstOfNextMonth := firstOfMonth.AddDate(0, 1, 0)
|
||||||
firstOfPreviousMonth := 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)
|
categories, err := h.Service.DB.GetCategories(c.Request.Context(), budgetUUID)
|
||||||
|
|
||||||
@ -84,6 +90,40 @@ func (h *Handler) budgeting(c *gin.Context) {
|
|||||||
return
|
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{}
|
categoriesWithBalance := []CategoryWithBalance{}
|
||||||
|
|
||||||
var added float64 = 0
|
var added float64 = 0
|
||||||
@ -98,7 +138,6 @@ func (h *Handler) budgeting(c *gin.Context) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// skip everything in the future
|
|
||||||
if !bal.Date.Before(firstOfNextMonth) {
|
if !bal.Date.Before(firstOfNextMonth) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -122,39 +161,5 @@ func (h *Handler) budgeting(c *gin.Context) {
|
|||||||
|
|
||||||
categoriesWithBalance = append(categoriesWithBalance, categoryWithBalance)
|
categoriesWithBalance = append(categoriesWithBalance, categoryWithBalance)
|
||||||
}
|
}
|
||||||
|
return categoriesWithBalance, added, assigned, nil
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,7 @@
|
|||||||
<a href="{{printf "/budget/%s/%d/%d" .Budget.ID .Next.Year .Next.Month}}">Next Month</a>
|
<a href="{{printf "/budget/%s/%d/%d" .Budget.ID .Next.Year .Next.Month}}">Next Month</a>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<span>Available Balance: </span>{{template "amount" .AvailableBalance}}
|
<span>Available Balance: </span>{{template "amountf64" .AvailableBalance}}
|
||||||
</div>
|
</div>
|
||||||
<table class="container col-lg-12" id="content">
|
<table class="container col-lg-12" id="content">
|
||||||
<tr>
|
<tr>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user