Fixed a small problem with the current charts

This commit is contained in:
2025-09-25 22:53:19 +01:00
parent 4cdbdaf5d8
commit df761d3022
6 changed files with 74 additions and 34 deletions
+4
View File
@@ -1,3 +1,7 @@
# v0.0.94-alpha
- Small fix to how the built in charts got their data
# v0.0.93-alpha # v0.0.93-alpha
Breaking changes: Breaking changes:
- In the configuration file, the following fields have be to be renamed: - In the configuration file, the following fields have be to be renamed:
+12 -7
View File
@@ -6,7 +6,7 @@ import (
mod "github.com/ESilva15/expenses/expenses/models" mod "github.com/ESilva15/expenses/expenses/models"
"github.com/ESilva15/expenses/expenses/repo" "github.com/ESilva15/expenses/expenses/repo"
"github.com/shopspring/decimal" dec "github.com/shopspring/decimal"
) )
// GetAllExpenses returns the list of Expenses in which the 'user' participates // GetAllExpenses returns the list of Expenses in which the 'user' participates
@@ -71,8 +71,8 @@ func (a *ExpApp) DeleteExpense(id int32) error {
return err return err
} }
func mapShares(e *mod.Expense) map[mod.User]decimal.Decimal { func mapShares(e *mod.Expense) map[mod.User]dec.Decimal {
shares := make(map[mod.User]decimal.Decimal) shares := make(map[mod.User]dec.Decimal)
for _, share := range e.Shares { for _, share := range e.Shares {
shares[share.User] = share.Calculated shares[share.User] = share.Calculated
} }
@@ -80,8 +80,8 @@ func mapShares(e *mod.Expense) map[mod.User]decimal.Decimal {
return shares return shares
} }
func mapPayments(e *mod.Expense) map[mod.User]decimal.Decimal { func mapPayments(e *mod.Expense) map[mod.User]dec.Decimal {
payments := make(map[mod.User]decimal.Decimal) payments := make(map[mod.User]dec.Decimal)
for _, p := range e.Payments { for _, p := range e.Payments {
payments[p.User] = payments[p.User].Add(p.PayedAmount) payments[p.User] = payments[p.User].Add(p.PayedAmount)
} }
@@ -90,8 +90,8 @@ func mapPayments(e *mod.Expense) map[mod.User]decimal.Decimal {
} }
// TODO - move to the models // TODO - move to the models
func ExpenseTotalPayed(exp *mod.Expense) decimal.Decimal { func ExpenseTotalPayed(exp *mod.Expense) dec.Decimal {
total := decimal.NewFromFloat(0.0) total := dec.NewFromFloat(0.0)
for _, p := range exp.Payments { for _, p := range exp.Payments {
total = total.Add(p.PayedAmount) total = total.Add(p.PayedAmount)
} }
@@ -146,3 +146,8 @@ func (a *ExpApp) UpdateExpense(exp mod.Expense) error {
return a.ExpRepo.Update(ctx, exp) return a.ExpRepo.Update(ctx, exp)
} }
// PaidByUser returns the amount a given user paid on an expense
func (a *ExpApp) PaidByUser(e mod.Expense, uID int32) dec.Decimal {
return e.PaidByUser(uID)
}
+37 -18
View File
@@ -2,34 +2,39 @@ package models
import ( import (
"encoding/json" "encoding/json"
"github.com/shopspring/decimal"
"time" "time"
dec "github.com/shopspring/decimal"
) )
// Expense is the model for the expense concept in our application.
type Expense struct { type Expense struct {
ExpID int32 `json:"ExpID"` ExpID int32 `json:"ExpID"`
Description string `json:"Description"` Description string `json:"Description"`
Value decimal.Decimal `json:"Value"` Value dec.Decimal `json:"Value"`
Store Store `json:"Store"` Store Store `json:"Store"`
Type Type `json:"Type"` Type Type `json:"Type"`
Category Category `json:"Category"` Category Category `json:"Category"`
Owner User `json:"Owner"` Owner User `json:"Owner"`
Date time.Time `json:"Date"` Date time.Time `json:"Date"`
Payments []Payment `json:"Payments"` Payments []Payment `json:"Payments"`
Shares []Share `json:"Shares"` Shares []Share `json:"Shares"`
Debts Debts `json:"Debts"` Debts Debts `json:"Debts"`
PaidOff bool `json:"PaidOff"` PaidOff bool `json:"PaidOff"`
SharesEven bool `json:"SharesEven"` SharesEven bool `json:"SharesEven"`
QRString string `json:"qr"` QRString string `json:"qr"`
CreationDate time.Time `json:"CreationDate"` CreationDate time.Time `json:"CreationDate"`
} }
// Expenses defines a list of Expense.
type Expenses []Expense type Expenses []Expense
// NewExpense returns an empty Expense.
func NewExpense() Expense { func NewExpense() Expense {
return Expense{ return Expense{
ExpID: -1, ExpID: -1,
Description: "", Description: "",
Value: decimal.NewFromFloat(0.0), Value: dec.NewFromFloat(0.0),
Store: NewStore(), Store: NewStore(),
Category: NewCategory(), Category: NewCategory(),
Owner: NewUser(), Owner: NewUser(),
@@ -42,9 +47,23 @@ func NewExpense() Expense {
} }
} }
// ExpenseFromJSON takes []byte and returns an *Expense // ExpenseFromJSON takes []byte and returns an *Expense.
func ExpenseFromJSON(data []byte) (*Expense, error) { func ExpenseFromJSON(data []byte) (*Expense, error) {
var expense Expense var expense Expense
err := json.Unmarshal(data, &expense) err := json.Unmarshal(data, &expense)
return &expense, err return &expense, err
} }
// PaidByUser returns the total paid by the user of a given ID.
func (e *Expense) PaidByUser(uID int32) dec.Decimal {
total := dec.NewFromFloat(0.0)
// TODO - Could we optimize this by storing the payments mapped to users in the list?
for _, payment := range e.Payments {
if payment.User.UserID == uID {
total = total.Add(payment.PayedAmount)
}
}
return total
}
+4
View File
@@ -1,12 +1,16 @@
package models package models
// User defines the model for the a User in the application
type User struct { type User struct {
UserID int32 `json:"UserID"` UserID int32 `json:"UserID"`
UserName string `json:"UserName"` UserName string `json:"UserName"`
Password string `json:"Password"` Password string `json:"Password"`
} }
// Users defines a list of User
type Users []User type Users []User
// NewUser returns a new empty user
func NewUser() User { func NewUser() User {
return User{ return User{
UserID: -1, UserID: -1,
+16 -8
View File
@@ -18,18 +18,23 @@ type MonthlyTotal struct {
Total dec.Decimal `json:"total"` Total dec.Decimal `json:"total"`
} }
func getMonthlyBalance(exp mod.Expenses) ([]MonthlyTotal, error) { func getMonthlyBalance(expenses mod.Expenses, uID int32) ([]MonthlyTotal, error) {
monthlyBalance := make([]MonthlyTotal, val.MonthsPerYear) monthlyBalance := make([]MonthlyTotal, val.MonthsPerYear)
for k := range val.MonthsPerYear { for k := range val.MonthsPerYear {
monthlyBalance[k].Month = k monthlyBalance[k].Month = k
} }
for _, e := range exp { for _, e := range expenses {
month := e.Date.Month() - 1 month := e.Date.Month() - 1
t := e.Type.TypeName t := e.Type.TypeName
// Get the current expenses value and make it negative if its an expense // Get the current expenses value and make it negative if its an expense
value := e.Value // It's not the value, its what we paid on it (our payments)
// I don't like this like this - takes too long, we should load it from DB
// at the start, and cache it too
_ = exp.App.LoadExpensePayments(&e)
value := exp.App.PaidByUser(e, uID)
// value := e.Value
if t == "Despesa" { if t == "Despesa" {
value = value.Neg() value = value.Neg()
} }
@@ -41,16 +46,18 @@ func getMonthlyBalance(exp mod.Expenses) ([]MonthlyTotal, error) {
return monthlyBalance, nil return monthlyBalance, nil
} }
func getMonthlyTotal(exp mod.Expenses) ([]MonthlyTotal, error) { func getMonthlyTotal(expenses mod.Expenses, uID int32) ([]MonthlyTotal, error) {
mTotal := make([]MonthlyTotal, val.MonthsPerYear) mTotal := make([]MonthlyTotal, val.MonthsPerYear)
for k := range mTotal { for k := range mTotal {
mTotal[k].Month = k mTotal[k].Month = k
} }
// Step 1: net total for each month // Step 1: net total for each month
for _, e := range exp { for _, e := range expenses {
month := int(e.Date.Month()) - 1 // Jan = 0 month := int(e.Date.Month()) - 1 // Jan = 0
value := e.Value // Same as on the other one here
_ = exp.App.LoadExpensePayments(&e)
value := exp.App.PaidByUser(e, uID)
if e.Type.TypeName == "Despesa" { if e.Type.TypeName == "Despesa" {
value = value.Neg() value = value.Neg()
} }
@@ -70,6 +77,7 @@ func getAnalysisDataWG(c *gin.Context) (map[string]any, error) {
if err != nil { if err != nil {
return map[string]any{}, err return map[string]any{}, err
} }
user := *ctx.Value("user").(*mod.User)
filter, err := expenseFilterFromQuery(c) filter, err := expenseFilterFromQuery(c)
if err != nil { if err != nil {
@@ -92,11 +100,11 @@ func getAnalysisDataWG(c *gin.Context) (map[string]any, error) {
wg.Add(2) wg.Add(2)
go func() { go func() {
defer wg.Done() defer wg.Done()
mBalance, bErr = getMonthlyBalance(queriedExpenses) mBalance, bErr = getMonthlyBalance(queriedExpenses, user.UserID)
}() }()
go func() { go func() {
defer wg.Done() defer wg.Done()
mTotal, tErr = getMonthlyTotal(queriedExpenses) mTotal, tErr = getMonthlyTotal(queriedExpenses, user.UserID)
}() }()
wg.Wait() wg.Wait()
+1 -1
View File
@@ -5,4 +5,4 @@ REQUIRED_VARS := registry repo
$(foreach v,$(REQUIRED_VARS),\ $(foreach v,$(REQUIRED_VARS),\
$(if $(value $(v)),,$(error Variable '$(v)' is required but empty))) $(if $(value $(v)),,$(error Variable '$(v)' is required but empty)))
tag = v0.0.93-alpha tag = v0.0.94-alpha