Implement budgeting views by calculating most values locally

This commit is contained in:
2021-12-11 12:47:41 +00:00
parent 6da1b26a2f
commit caf0126b86
6 changed files with 55 additions and 250 deletions

View File

@ -13,7 +13,7 @@ import (
type BudgetingData struct {
AlwaysNeededData
Categories []postgres.GetCategoriesWithBalanceRow
Categories []CategoryWithBalance
AvailableBalance postgres.Numeric
Date time.Time
Next time.Time
@ -31,6 +31,14 @@ func getFirstOfMonthTime(date time.Time) time.Time {
return getFirstOfMonth(year, month, date.Location())
}
type CategoryWithBalance struct {
*postgres.GetCategoriesRow
Available float64
AvailableLastMonth float64
Activity float64
Assigned float64
}
func (h *Handler) budgeting(c *gin.Context) {
budgetID := c.Param("budgetid")
budgetUUID, err := uuid.Parse(budgetID)
@ -64,17 +72,7 @@ func (h *Handler) budgeting(c *gin.Context) {
firstOfNextMonth := firstOfMonth.AddDate(0, 1, 0)
firstOfPreviousMonth := firstOfMonth.AddDate(0, -1, 0)
params := postgres.GetCategoriesWithBalanceParams{
BudgetID: budgetUUID,
FromDate: firstOfMonth,
ToDate: firstOfNextMonth,
PrevFromDate: firstOfMonth.AddDate(0, -1, 0),
}
categories, err := h.Service.DB.GetCategoriesWithBalance(c.Request.Context(), params)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("load categories: %w", err))
return
}
categories, err := h.Service.DB.GetCategories(c.Request.Context(), budgetUUID)
cumultativeBalances, err := h.Service.DB.GetCumultativeBalances(c.Request.Context(), budgetUUID)
if err != nil {
@ -82,12 +80,15 @@ func (h *Handler) budgeting(c *gin.Context) {
return
}
var available float64 = 0
categoriesWithBalance := []CategoryWithBalance{}
var added float64 = 0
var assigned float64 = 0
for i := range categories {
cat := &categories[i]
var balance float64 = 0
categoryWithBalance := CategoryWithBalance{
GetCategoriesRow: cat,
}
for _, bal := range cumultativeBalances {
if bal.CategoryID != cat.ID {
continue
@ -99,25 +100,23 @@ func (h *Handler) budgeting(c *gin.Context) {
}
assigned += bal.Assignments.GetFloat64()
balance += bal.Assignments.GetFloat64()
balance += bal.Transactions.GetFloat64()
if balance < 0 && bal.Date.Before(firstOfMonth) {
added -= balance
balance = 0
categoryWithBalance.Available += bal.Assignments.GetFloat64()
categoryWithBalance.Available += bal.Transactions.GetFloat64()
if categoryWithBalance.Available < 0 && bal.Date.Before(firstOfMonth) {
added -= categoryWithBalance.Available
categoryWithBalance.Available = 0
}
if bal.Date.Before(firstOfMonth) {
available = balance
categoryWithBalance.AvailableLastMonth = categoryWithBalance.Available
} else if bal.Date.Before(firstOfNextMonth) {
categoryWithBalance.Activity = bal.Transactions.GetFloat64()
categoryWithBalance.Assigned = bal.Assignments.GetFloat64()
}
}
num := postgres.Numeric{}
num.Set(balance)
cat.Available = num
num2 := postgres.Numeric{}
num2.Set(available)
cat.AvailableLastMonth = num2
categoriesWithBalance = append(categoriesWithBalance, categoryWithBalance)
}
data := c.MustGet("data").(AlwaysNeededData)
@ -145,12 +144,12 @@ func (h *Handler) budgeting(c *gin.Context) {
availableBalanceNum.Set(availableBalance)
d := BudgetingData{
c.MustGet("data").(AlwaysNeededData),
categories,
availableBalanceNum,
firstOfMonth,
firstOfNextMonth,
firstOfPreviousMonth,
AlwaysNeededData: c.MustGet("data").(AlwaysNeededData),
Categories: categoriesWithBalance,
AvailableBalance: availableBalanceNum,
Date: firstOfMonth,
Next: firstOfNextMonth,
Previous: firstOfPreviousMonth,
}
c.HTML(http.StatusOK, "budgeting.html", d)