186 lines
5.2 KiB
Go
186 lines
5.2 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"git.javil.eu/jacob1123/budgeteer/postgres"
|
|
"git.javil.eu/jacob1123/budgeteer/postgres/numeric"
|
|
"github.com/google/uuid"
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
type CategoryWithBalance struct {
|
|
*postgres.GetCategoriesRow
|
|
AvailableLastMonth numeric.Numeric
|
|
Activity numeric.Numeric
|
|
Assigned numeric.Numeric
|
|
}
|
|
|
|
func NewCategoryWithBalance(category *postgres.GetCategoriesRow) CategoryWithBalance {
|
|
return CategoryWithBalance{
|
|
GetCategoriesRow: category,
|
|
AvailableLastMonth: numeric.Zero(),
|
|
Activity: numeric.Zero(),
|
|
Assigned: numeric.Zero(),
|
|
}
|
|
}
|
|
|
|
func (h *Handler) budgetingForMonth(c echo.Context) error {
|
|
budgetID := c.Param("budgetid")
|
|
budgetUUID, err := uuid.Parse(budgetID)
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, ErrorResponse{"budgetid missing from URL"})
|
|
return
|
|
}
|
|
|
|
budget, err := h.Service.GetBudget(c.Request().Context(), budgetUUID)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, err)
|
|
return
|
|
}
|
|
|
|
month, err := getDate(c)
|
|
if err != nil {
|
|
c.Redirect(http.StatusTemporaryRedirect, "/budget/"+budget.ID.String())
|
|
return
|
|
}
|
|
|
|
data, err := h.getBudgetingViewForMonth(c.Request().Context(), budget, month)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, data)
|
|
}
|
|
|
|
func (h *Handler) getBudgetingViewForMonth(ctx context.Context, budget postgres.Budget, month Month) (BudgetingForMonthResponse, error) {
|
|
categories, err := h.Service.GetCategories(ctx, budget.ID)
|
|
if err != nil {
|
|
return BudgetingForMonthResponse{}, fmt.Errorf("error loading categories: %w", err)
|
|
}
|
|
|
|
cumultativeBalances, err := h.Service.GetCumultativeBalances(ctx, budget.ID)
|
|
if err != nil {
|
|
return BudgetingForMonthResponse{}, fmt.Errorf("error loading balances: %w", err)
|
|
}
|
|
|
|
categoriesWithBalance, moneyUsed, overspentLastMonth := h.calculateBalances(budget, month, categories, cumultativeBalances)
|
|
availableBalance := h.getAvailableBalance(budget, month, moneyUsed, cumultativeBalances)
|
|
|
|
data := BudgetingForMonthResponse{categoriesWithBalance, availableBalance, overspentLastMonth}
|
|
return data, nil
|
|
}
|
|
|
|
type BudgetingForMonthResponse struct {
|
|
Categories []CategoryWithBalance
|
|
AvailableBalance numeric.Numeric
|
|
OverspentLastMonth numeric.Numeric
|
|
}
|
|
|
|
func (*Handler) getAvailableBalance(budget postgres.Budget, month Month,
|
|
moneyUsed numeric.Numeric, cumultativeBalances []postgres.GetCumultativeBalancesRow,
|
|
) numeric.Numeric {
|
|
availableBalance := moneyUsed
|
|
|
|
for _, bal := range cumultativeBalances {
|
|
if bal.CategoryID != budget.IncomeCategoryID {
|
|
continue
|
|
}
|
|
|
|
if month.InFuture(bal.Date) {
|
|
continue
|
|
}
|
|
|
|
availableBalance.AddI(bal.Transactions)
|
|
availableBalance.AddI(bal.Assignments) // should be zero, but who knows
|
|
}
|
|
|
|
return availableBalance
|
|
}
|
|
|
|
type BudgetingResponse struct {
|
|
Accounts []postgres.GetAccountsWithBalanceRow
|
|
Budget postgres.Budget
|
|
}
|
|
|
|
func (h *Handler) budget(c echo.Context) error {
|
|
budgetID := c.Param("budgetid")
|
|
budgetUUID, err := uuid.Parse(budgetID)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, "budgetid missing from URL")
|
|
}
|
|
|
|
return h.getBudget(c, budgetUUID)
|
|
}
|
|
|
|
func (h *Handler) getBudget(c echo.Context, budgetUUID uuid.UUID) error {
|
|
budget, err := h.Service.GetBudget(c.Request().Context(), budgetUUID)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusNotFound, err)
|
|
}
|
|
|
|
accounts, err := h.Service.GetAccountsWithBalance(c.Request().Context(), budgetUUID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
data := BudgetingResponse{accounts, budget}
|
|
|
|
return c.JSON(http.StatusOK, data)
|
|
}
|
|
|
|
func (h *Handler) calculateBalances(budget postgres.Budget, month Month,
|
|
categories []postgres.GetCategoriesRow, cumultativeBalances []postgres.GetCumultativeBalancesRow,
|
|
) ([]CategoryWithBalance, numeric.Numeric, numeric.Numeric) {
|
|
categoriesWithBalance := []CategoryWithBalance{}
|
|
|
|
moneyUsed := numeric.Zero()
|
|
overspentLastMonth := numeric.Zero()
|
|
categories = append(categories, postgres.GetCategoriesRow{
|
|
Group: "Income",
|
|
Name: "No Category",
|
|
ID: uuid.UUID{},
|
|
})
|
|
for i := range categories {
|
|
cat := &categories[i]
|
|
if cat.ID == budget.IncomeCategoryID {
|
|
continue
|
|
}
|
|
|
|
categoryWithBalance := NewCategoryWithBalance(cat)
|
|
for _, bal := range cumultativeBalances {
|
|
if bal.CategoryID != cat.ID {
|
|
continue
|
|
}
|
|
|
|
// skip everything in the future
|
|
if month.InFuture(bal.Date) {
|
|
continue
|
|
}
|
|
|
|
moneyUsed.SubI(bal.Assignments)
|
|
if month.InPresent(bal.Date) {
|
|
categoryWithBalance.Activity = bal.Transactions
|
|
categoryWithBalance.Assigned = bal.Assignments
|
|
continue
|
|
}
|
|
|
|
categoryWithBalance.AvailableLastMonth.AddI(bal.Assignments)
|
|
categoryWithBalance.AvailableLastMonth.AddI(bal.Transactions)
|
|
if !categoryWithBalance.AvailableLastMonth.IsPositive() {
|
|
moneyUsed.AddI(categoryWithBalance.AvailableLastMonth)
|
|
if month.Previous().InPresent(bal.Date) {
|
|
overspentLastMonth.AddI(categoryWithBalance.AvailableLastMonth)
|
|
}
|
|
categoryWithBalance.AvailableLastMonth = numeric.Zero()
|
|
}
|
|
}
|
|
|
|
categoriesWithBalance = append(categoriesWithBalance, categoryWithBalance)
|
|
}
|
|
|
|
return categoriesWithBalance, moneyUsed, overspentLastMonth
|
|
}
|