support filtering accounts and transaction categories for overview in home page (#209)
This commit is contained in:
+20
-1
@@ -549,6 +549,25 @@ func (a *TransactionsApi) TransactionAmountsHandler(c *core.WebContext) (any, *e
|
||||
return nil, errs.ErrQueryItemsTooMuch
|
||||
}
|
||||
|
||||
excludeAccountIds := make([]int64, 0)
|
||||
excludeCategoryIds := make([]int64, 0)
|
||||
|
||||
if transactionAmountsReq.ExcludeAccountIds != "" {
|
||||
excludeAccountIds, err = utils.StringArrayToInt64Array(strings.Split(transactionAmountsReq.ExcludeAccountIds, ","))
|
||||
|
||||
if err != nil {
|
||||
return nil, errs.ErrAccountIdInvalid
|
||||
}
|
||||
}
|
||||
|
||||
if transactionAmountsReq.ExcludeCategoryIds != "" {
|
||||
excludeCategoryIds, err = utils.StringArrayToInt64Array(strings.Split(transactionAmountsReq.ExcludeCategoryIds, ","))
|
||||
|
||||
if err != nil {
|
||||
return nil, errs.ErrTransactionCategoryIdInvalid
|
||||
}
|
||||
}
|
||||
|
||||
utcOffset, err := c.GetClientTimezoneOffset()
|
||||
|
||||
if err != nil {
|
||||
@@ -571,7 +590,7 @@ func (a *TransactionsApi) TransactionAmountsHandler(c *core.WebContext) (any, *e
|
||||
for i := 0; i < len(requestItems); i++ {
|
||||
requestItem := requestItems[i]
|
||||
|
||||
incomeAmounts, expenseAmounts, err := a.transactions.GetAccountsTotalIncomeAndExpense(c, uid, requestItem.StartTime, requestItem.EndTime, utcOffset, transactionAmountsReq.UseTransactionTimezone)
|
||||
incomeAmounts, expenseAmounts, err := a.transactions.GetAccountsTotalIncomeAndExpense(c, uid, requestItem.StartTime, requestItem.EndTime, excludeAccountIds, excludeCategoryIds, utcOffset, transactionAmountsReq.UseTransactionTimezone)
|
||||
|
||||
if err != nil {
|
||||
log.Errorf(c, "[transactions.TransactionAmountsHandler] failed to get transaction amounts item for user \"uid:%d\", because %s", uid, err.Error())
|
||||
|
||||
@@ -258,6 +258,8 @@ type TransactionStatisticTrendsRequest struct {
|
||||
// TransactionAmountsRequest represents all parameters of transaction amounts request
|
||||
type TransactionAmountsRequest struct {
|
||||
Query string `form:"query"`
|
||||
ExcludeAccountIds string `form:"exclude_account_ids"`
|
||||
ExcludeCategoryIds string `form:"exclude_category_ids"`
|
||||
UseTransactionTimezone bool `form:"use_transaction_timezone"`
|
||||
}
|
||||
|
||||
|
||||
@@ -17,8 +17,10 @@ var ALL_ALLOWED_CLOUD_SYNC_APP_SETTING_KEY_TYPES = map[string]UserApplicationClo
|
||||
// Basic Settings
|
||||
"showAccountBalance": USER_APPLICATION_CLOUD_SETTING_TYPE_BOOLEAN,
|
||||
// Overview Page
|
||||
"showAmountInHomePage": USER_APPLICATION_CLOUD_SETTING_TYPE_BOOLEAN,
|
||||
"timezoneUsedForStatisticsInHomePage": USER_APPLICATION_CLOUD_SETTING_TYPE_NUMBER,
|
||||
"showAmountInHomePage": USER_APPLICATION_CLOUD_SETTING_TYPE_BOOLEAN,
|
||||
"timezoneUsedForStatisticsInHomePage": USER_APPLICATION_CLOUD_SETTING_TYPE_NUMBER,
|
||||
"overviewAccountFilterInHomePage": USER_APPLICATION_CLOUD_SETTING_TYPE_STRING_BOOLEAN_MAP,
|
||||
"overviewTransactionCategoryFilterInHomePage": USER_APPLICATION_CLOUD_SETTING_TYPE_STRING_BOOLEAN_MAP,
|
||||
// Transaction List Page
|
||||
"itemsCountInTransactionListPage": USER_APPLICATION_CLOUD_SETTING_TYPE_NUMBER,
|
||||
"showTotalAmountInTransactionListPage": USER_APPLICATION_CLOUD_SETTING_TYPE_BOOLEAN,
|
||||
|
||||
@@ -1422,7 +1422,7 @@ func (s *TransactionService) GetRelatedTransferTransaction(originalTransaction *
|
||||
}
|
||||
|
||||
// GetAccountsTotalIncomeAndExpense returns the every accounts total income and expense amount by specific date range
|
||||
func (s *TransactionService) GetAccountsTotalIncomeAndExpense(c core.Context, uid int64, startUnixTime int64, endUnixTime int64, utcOffset int16, useTransactionTimezone bool) (map[int64]int64, map[int64]int64, error) {
|
||||
func (s *TransactionService) GetAccountsTotalIncomeAndExpense(c core.Context, uid int64, startUnixTime int64, endUnixTime int64, excludeAccountIds []int64, excludeCategoryIds []int64, utcOffset int16, useTransactionTimezone bool) (map[int64]int64, map[int64]int64, error) {
|
||||
if uid <= 0 {
|
||||
return nil, nil, errs.ErrUserIdInvalid
|
||||
}
|
||||
@@ -1437,13 +1437,49 @@ func (s *TransactionService) GetAccountsTotalIncomeAndExpense(c core.Context, ui
|
||||
startTransactionTime := utils.GetMinTransactionTimeFromUnixTime(startUnixTime)
|
||||
endTransactionTime := utils.GetMaxTransactionTimeFromUnixTime(endUnixTime)
|
||||
|
||||
condition := "uid=? AND deleted=? AND (type=? OR type=?) AND transaction_time>=? AND transaction_time<=?"
|
||||
conditionParams := make([]any, 0, 4)
|
||||
condition := "uid=? AND deleted=? AND (type=? OR type=?)"
|
||||
conditionParams := make([]any, 0, 4+len(excludeAccountIds)+len(excludeCategoryIds))
|
||||
conditionParams = append(conditionParams, uid)
|
||||
conditionParams = append(conditionParams, false)
|
||||
conditionParams = append(conditionParams, models.TRANSACTION_DB_TYPE_INCOME)
|
||||
conditionParams = append(conditionParams, models.TRANSACTION_DB_TYPE_EXPENSE)
|
||||
|
||||
if len(excludeAccountIds) > 0 {
|
||||
var accountIdsCondition strings.Builder
|
||||
accountIdConditionParams := make([]any, 0, len(excludeAccountIds))
|
||||
|
||||
for i := 0; i < len(excludeAccountIds); i++ {
|
||||
if i > 0 {
|
||||
accountIdsCondition.WriteString(",")
|
||||
}
|
||||
|
||||
accountIdsCondition.WriteString("?")
|
||||
accountIdConditionParams = append(accountIdConditionParams, excludeAccountIds[i])
|
||||
}
|
||||
|
||||
condition = condition + " AND account_id NOT IN (" + accountIdsCondition.String() + ")"
|
||||
conditionParams = append(conditionParams, accountIdConditionParams...)
|
||||
}
|
||||
|
||||
if len(excludeCategoryIds) > 0 {
|
||||
var categoryIdsCondition strings.Builder
|
||||
categoryIdConditionParams := make([]any, 0, len(excludeCategoryIds))
|
||||
|
||||
for i := 0; i < len(excludeCategoryIds); i++ {
|
||||
if i > 0 {
|
||||
categoryIdsCondition.WriteString(",")
|
||||
}
|
||||
|
||||
categoryIdsCondition.WriteString("?")
|
||||
categoryIdConditionParams = append(categoryIdConditionParams, excludeCategoryIds[i])
|
||||
}
|
||||
|
||||
condition = condition + " AND category_id NOT IN (" + categoryIdsCondition.String() + ")"
|
||||
conditionParams = append(conditionParams, categoryIdConditionParams...)
|
||||
}
|
||||
|
||||
condition = condition + " AND transaction_time>=? AND transaction_time<=?"
|
||||
|
||||
minTransactionTime := startTransactionTime
|
||||
maxTransactionTime := endTransactionTime
|
||||
var allTransactions []*models.Transaction
|
||||
|
||||
Reference in New Issue
Block a user