mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-15 15:37:33 +08:00
transaction overview supports multi currencies
This commit is contained in:
+66
-5
@@ -14,12 +14,14 @@ import (
|
||||
// OverviewApi represents overview api
|
||||
type OverviewApi struct {
|
||||
transactions *services.TransactionService
|
||||
accounts *services.AccountService
|
||||
}
|
||||
|
||||
// Initialize an overview api singleton instance
|
||||
var (
|
||||
Overviews = &OverviewApi{
|
||||
transactions: services.Transactions,
|
||||
accounts: services.Accounts,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -79,23 +81,82 @@ func (a *OverviewApi) TransactionOverviewHandler(c *core.Context) (interface{},
|
||||
|
||||
uid := c.GetCurrentUid()
|
||||
|
||||
accounts, err := a.accounts.GetAllAccountsByUid(uid)
|
||||
accountMap := a.accounts.GetAccountMapByList(accounts)
|
||||
|
||||
if err != nil {
|
||||
log.ErrorfWithRequestId(c, "[overviews.TransactionOverviewHandler] failed to get all accounts for user \"uid:%d\", because %s", uid, err.Error())
|
||||
return nil, errs.ErrOperationFailed
|
||||
}
|
||||
|
||||
overviewResp := make(map[string]*models.TransactionOverviewResponseItem)
|
||||
|
||||
for i := 0; i < len(requestItems); i++ {
|
||||
requestItem := requestItems[i]
|
||||
|
||||
incomeAmount, expenseAmount, err := a.transactions.GetTotalIncomeAndExpenseByDateRange(uid, requestItem.StartTime, requestItem.EndTime)
|
||||
incomeAmounts, expenseAmounts, err := a.transactions.GetAccountsTotalIncomeAndExpense(uid, requestItem.StartTime, requestItem.EndTime)
|
||||
|
||||
if err != nil {
|
||||
log.ErrorfWithRequestId(c, "[overviews.TransactionOverviewHandler] failed to get transaction overview item for user \"uid:%d\", because %s", uid, err.Error())
|
||||
return nil, errs.ErrOperationFailed
|
||||
}
|
||||
|
||||
amountsMap := make(map[string]*models.TransactionOverviewResponseItemAmount)
|
||||
|
||||
for accountId, incomeAmount := range incomeAmounts {
|
||||
account, exists := accountMap[accountId]
|
||||
|
||||
if !exists {
|
||||
log.WarnfWithRequestId(c, "[overviews.TransactionOverviewHandler] cannot find account for account \"id:%d\" of user \"uid:%d\", because %s", accountId, uid)
|
||||
continue
|
||||
}
|
||||
|
||||
totalAmounts, exists := amountsMap[account.Currency]
|
||||
|
||||
if !exists {
|
||||
totalAmounts = &models.TransactionOverviewResponseItemAmount{
|
||||
Currency: account.Currency,
|
||||
IncomeAmount: 0,
|
||||
ExpenseAmount: 0,
|
||||
}
|
||||
}
|
||||
|
||||
totalAmounts.IncomeAmount += incomeAmount
|
||||
amountsMap[account.Currency] = totalAmounts
|
||||
}
|
||||
|
||||
for accountId, expenseAmount := range expenseAmounts {
|
||||
account, exists := accountMap[accountId]
|
||||
|
||||
if !exists {
|
||||
log.WarnfWithRequestId(c, "[overviews.TransactionOverviewHandler] cannot find account for account \"id:%d\" of user \"uid:%d\", because %s", accountId, uid)
|
||||
continue
|
||||
}
|
||||
|
||||
totalAmounts, exists := amountsMap[account.Currency]
|
||||
|
||||
if !exists {
|
||||
totalAmounts = &models.TransactionOverviewResponseItemAmount{
|
||||
Currency: account.Currency,
|
||||
IncomeAmount: 0,
|
||||
ExpenseAmount: 0,
|
||||
}
|
||||
}
|
||||
|
||||
totalAmounts.ExpenseAmount += expenseAmount
|
||||
amountsMap[account.Currency] = totalAmounts
|
||||
}
|
||||
|
||||
allTotalAmounts := make([]*models.TransactionOverviewResponseItemAmount, 0)
|
||||
|
||||
for _, totalAmounts := range amountsMap {
|
||||
allTotalAmounts = append(allTotalAmounts, totalAmounts)
|
||||
}
|
||||
|
||||
overviewResp[requestItem.Name] = &models.TransactionOverviewResponseItem{
|
||||
StartTime: requestItem.StartTime,
|
||||
EndTime: requestItem.EndTime,
|
||||
IncomeAmount: incomeAmount,
|
||||
ExpenseAmount: expenseAmount,
|
||||
StartTime: requestItem.StartTime,
|
||||
EndTime: requestItem.EndTime,
|
||||
Amounts: allTotalAmounts,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+10
-4
@@ -14,8 +14,14 @@ type TransactionOverviewRequestItem struct {
|
||||
|
||||
// TransactionOverviewResponseItem represents an item of transaction overview
|
||||
type TransactionOverviewResponseItem struct {
|
||||
StartTime int64 `json:"startTime"`
|
||||
EndTime int64 `json:"endTime"`
|
||||
IncomeAmount int64 `json:"incomeAmount"`
|
||||
ExpenseAmount int64 `json:"expenseAmount"`
|
||||
StartTime int64 `json:"startTime"`
|
||||
EndTime int64 `json:"endTime"`
|
||||
Amounts []*TransactionOverviewResponseItemAmount `json:"amounts"`
|
||||
}
|
||||
|
||||
// TransactionOverviewResponseItemAmount represents amount info for an response item
|
||||
type TransactionOverviewResponseItemAmount struct {
|
||||
Currency string `json:"currency"`
|
||||
IncomeAmount int64 `json:"incomeAmount"`
|
||||
ExpenseAmount int64 `json:"expenseAmount"`
|
||||
}
|
||||
|
||||
@@ -923,36 +923,36 @@ func (s *TransactionService) GetRelatedTransferTransaction(originalTransaction *
|
||||
return relatedTransaction
|
||||
}
|
||||
|
||||
// GetTotalIncomeAndExpenseByDateRange returns the total income and expense amount by specific date range
|
||||
func (s *TransactionService) GetTotalIncomeAndExpenseByDateRange(uid int64, startUnixTime int64, endUnixTime int64) (int64, int64, error) {
|
||||
// GetAccountsTotalIncomeAndExpense returns the every accounts total income and expense amount by specific date range
|
||||
func (s *TransactionService) GetAccountsTotalIncomeAndExpense(uid int64, startUnixTime int64, endUnixTime int64) (map[int64]int64, map[int64]int64, error) {
|
||||
if uid <= 0 {
|
||||
return 0, 0, errs.ErrUserIdInvalid
|
||||
return nil, nil, errs.ErrUserIdInvalid
|
||||
}
|
||||
|
||||
startTransactionTime := utils.GetMinTransactionTimeFromUnixTime(startUnixTime)
|
||||
endTransactionTime := utils.GetMaxTransactionTimeFromUnixTime(endUnixTime)
|
||||
|
||||
var transactionTotalAmounts []*models.Transaction
|
||||
err := s.UserDataDB(uid).Select("uid, type, SUM(amount) as amount").Where("uid=? AND deleted=? AND (type=? OR type=?) AND transaction_time>=? AND transaction_time<=?", uid, false, models.TRANSACTION_DB_TYPE_INCOME, models.TRANSACTION_DB_TYPE_EXPENSE, startTransactionTime, endTransactionTime).GroupBy("type").Find(&transactionTotalAmounts)
|
||||
err := s.UserDataDB(uid).Select("uid, type, account_id, SUM(amount) as amount").Where("uid=? AND deleted=? AND (type=? OR type=?) AND transaction_time>=? AND transaction_time<=?", uid, false, models.TRANSACTION_DB_TYPE_INCOME, models.TRANSACTION_DB_TYPE_EXPENSE, startTransactionTime, endTransactionTime).GroupBy("type, account_id").Find(&transactionTotalAmounts)
|
||||
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
var incomeAmount int64
|
||||
var expenseAmount int64
|
||||
incomeAmounts := make(map[int64]int64)
|
||||
expenseAmounts := make(map[int64]int64)
|
||||
|
||||
for i := 0; i < len(transactionTotalAmounts); i++ {
|
||||
transactionTotalAmount := transactionTotalAmounts[i]
|
||||
|
||||
if transactionTotalAmount.Type == models.TRANSACTION_DB_TYPE_INCOME {
|
||||
incomeAmount = transactionTotalAmount.Amount
|
||||
incomeAmounts[transactionTotalAmount.AccountId] = transactionTotalAmount.Amount
|
||||
} else if transactionTotalAmount.Type == models.TRANSACTION_DB_TYPE_EXPENSE {
|
||||
expenseAmount = transactionTotalAmount.Amount
|
||||
expenseAmounts[transactionTotalAmount.AccountId] = transactionTotalAmount.Amount
|
||||
}
|
||||
}
|
||||
|
||||
return incomeAmount, expenseAmount, nil
|
||||
return incomeAmounts, expenseAmounts, nil
|
||||
}
|
||||
|
||||
func (s *TransactionService) isAccountIdValid(transaction *models.Transaction) error {
|
||||
|
||||
Reference in New Issue
Block a user