mirror of
https://github.com/FJurmanovic/wallet-go-api.git
synced 2026-02-06 06:08:16 +00:00
count wallet header by transaction type
This commit is contained in:
@@ -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,12 +66,25 @@ 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) {
|
||||||
currentBalance += trans.Amount
|
if trans.TransactionType.Type == "expense" {
|
||||||
|
currentBalance -= trans.Amount
|
||||||
|
} else {
|
||||||
|
currentBalance += trans.Amount
|
||||||
|
}
|
||||||
} else if trans.TransactionDate.Before(firstOfMonthAfterNext) && trans.TransactionDate.After(firstOfNextMonth) {
|
} else if trans.TransactionDate.Before(firstOfMonthAfterNext) && trans.TransactionDate.After(firstOfNextMonth) {
|
||||||
nextMonth += trans.Amount
|
if trans.TransactionType.Type == "expense" {
|
||||||
|
nextMonth -= trans.Amount
|
||||||
|
} else {
|
||||||
|
nextMonth += trans.Amount
|
||||||
|
}
|
||||||
} else if trans.TransactionDate.Before(firstOfMonth) {
|
} else if trans.TransactionDate.Before(firstOfMonth) {
|
||||||
lastMonthBalance += trans.Amount
|
if trans.TransactionType.Type == "expense" {
|
||||||
currentBalance += trans.Amount
|
lastMonthBalance -= trans.Amount
|
||||||
|
currentBalance -= trans.Amount
|
||||||
|
} else {
|
||||||
|
lastMonthBalance += trans.Amount
|
||||||
|
currentBalance += trans.Amount
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user