Implement cleaning to set all historic negative balances to zero
This commit is contained in:
@ -3,7 +3,9 @@ package http
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.javil.eu/jacob1123/budgeteer/postgres"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"github.com/pressly/goose/v3"
|
||||
@ -56,3 +58,50 @@ func (h *Handler) clearBudget(c *gin.Context) {
|
||||
|
||||
fmt.Printf("Deleted %d transactions\n", rows)
|
||||
}
|
||||
|
||||
func (h *Handler) cleanNegativeBudget(c *gin.Context) {
|
||||
budgetID := c.Param("budgetid")
|
||||
budgetUUID, err := uuid.Parse(budgetID)
|
||||
if err != nil {
|
||||
c.Redirect(http.StatusTemporaryRedirect, "/login")
|
||||
return
|
||||
}
|
||||
|
||||
min_date, err := h.Service.DB.GetFirstActivity(c.Request.Context(), budgetUUID)
|
||||
date := getFirstOfMonthTime(min_date)
|
||||
for {
|
||||
nextDate := date.AddDate(0, 1, 0)
|
||||
params := postgres.GetCategoriesWithBalanceParams{
|
||||
BudgetID: budgetUUID,
|
||||
ToDate: nextDate,
|
||||
FromDate: date,
|
||||
}
|
||||
categories, err := h.Service.DB.GetCategoriesWithBalance(c.Request.Context(), params)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, category := range categories {
|
||||
available := category.Available.GetFloat64()
|
||||
if available >= 0 {
|
||||
continue
|
||||
}
|
||||
var negativeAvailable postgres.Numeric
|
||||
negativeAvailable.Set(-available)
|
||||
createAssignment := postgres.CreateAssignmentParams{
|
||||
Date: nextDate.AddDate(0, 0, -1),
|
||||
Amount: negativeAvailable,
|
||||
CategoryID: category.ID,
|
||||
}
|
||||
h.Service.DB.CreateAssignment(c.Request.Context(), createAssignment)
|
||||
}
|
||||
|
||||
if nextDate.Before(time.Now()) {
|
||||
date = nextDate
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -19,6 +19,17 @@ type BudgetingData struct {
|
||||
Previous time.Time
|
||||
}
|
||||
|
||||
func getFirstOfMonth(year, month int, location *time.Location) time.Time {
|
||||
return time.Date(year, time.Month(month), 1, 0, 0, 0, 0, location)
|
||||
}
|
||||
|
||||
func getFirstOfMonthTime(date time.Time) time.Time {
|
||||
var monthM time.Month
|
||||
year, monthM, _ := date.Date()
|
||||
month := int(monthM)
|
||||
return getFirstOfMonth(year, month, date.Location())
|
||||
}
|
||||
|
||||
func (h *Handler) budgeting(c *gin.Context) {
|
||||
budgetID := c.Param("budgetid")
|
||||
budgetUUID, err := uuid.Parse(budgetID)
|
||||
@ -27,7 +38,7 @@ func (h *Handler) budgeting(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
var firstOfMonth time.Time
|
||||
var year, month int
|
||||
yearString := c.Param("year")
|
||||
monthString := c.Param("month")
|
||||
@ -44,13 +55,11 @@ func (h *Handler) budgeting(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
firstOfMonth = getFirstOfMonth(year, month, time.Now().Location())
|
||||
} else {
|
||||
var monthM time.Month
|
||||
year, monthM, _ = now.Date()
|
||||
month = int(monthM)
|
||||
firstOfMonth = getFirstOfMonthTime(time.Now())
|
||||
}
|
||||
|
||||
firstOfMonth := time.Date(year, time.Month(month), 1, 0, 0, 0, 0, now.Location())
|
||||
firstOfNextMonth := firstOfMonth.AddDate(0, 1, 0)
|
||||
firstOfPreviousMonth := firstOfMonth.AddDate(0, -1, 0)
|
||||
|
||||
|
@ -62,6 +62,7 @@ func (h *Handler) Serve() {
|
||||
withBudget.GET("/budget/:budgetid/accounts", h.accounts)
|
||||
withBudget.GET("/budget/:budgetid/account/:accountid", h.account)
|
||||
withBudget.GET("/budget/:budgetid/clear", h.clearBudget)
|
||||
withBudget.GET("/budget/:budgetid/clean-negative", h.cleanNegativeBudget)
|
||||
|
||||
api := router.Group("/api/v1")
|
||||
|
||||
|
Reference in New Issue
Block a user