count wallet header by transaction type

This commit is contained in:
Fran Jurmanović
2021-06-13 15:50:46 +02:00
parent abf9490832
commit 5b53610b61

View File

@@ -43,7 +43,7 @@ func (as *WalletService) GetHeader(am *models.Auth, embed string, walletId strin
//wallets := new([]models.Wallet) //wallets := new([]models.Wallet)
transactions := new([]models.Transaction) transactions := new([]models.Transaction)
query := as.Db.Model(transactions).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id) query := as.Db.Model(transactions).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id).Relation("TransactionType")
if walletId != "" { if walletId != "" {
query.Where("? = ?", pg.Ident("wallet_id"), walletId) query.Where("? = ?", pg.Ident("wallet_id"), walletId)
@@ -66,14 +66,27 @@ func (as *WalletService) GetHeader(am *models.Auth, embed string, walletId strin
for _, trans := range *transactions { for _, trans := range *transactions {
if trans.TransactionDate.Before(firstOfNextMonth) && trans.TransactionDate.After(firstOfMonth) { if trans.TransactionDate.Before(firstOfNextMonth) && trans.TransactionDate.After(firstOfMonth) {
if trans.TransactionType.Type == "expense" {
currentBalance -= trans.Amount
} else {
currentBalance += trans.Amount currentBalance += trans.Amount
}
} else if trans.TransactionDate.Before(firstOfMonthAfterNext) && trans.TransactionDate.After(firstOfNextMonth) { } else if trans.TransactionDate.Before(firstOfMonthAfterNext) && trans.TransactionDate.After(firstOfNextMonth) {
if trans.TransactionType.Type == "expense" {
nextMonth -= trans.Amount
} else {
nextMonth += trans.Amount nextMonth += trans.Amount
}
} else if trans.TransactionDate.Before(firstOfMonth) { } else if trans.TransactionDate.Before(firstOfMonth) {
if trans.TransactionType.Type == "expense" {
lastMonthBalance -= trans.Amount
currentBalance -= trans.Amount
} else {
lastMonthBalance += trans.Amount lastMonthBalance += trans.Amount
currentBalance += trans.Amount currentBalance += trans.Amount
} }
} }
}
wm.CurrentBalance = currentBalance wm.CurrentBalance = currentBalance
wm.LastMonth = lastMonthBalance wm.LastMonth = lastMonthBalance