diff --git a/pkg/api/data_managements.go b/pkg/api/data_managements.go index 7971534d..c014fdba 100644 --- a/pkg/api/data_managements.go +++ b/pkg/api/data_managements.go @@ -84,7 +84,7 @@ func (a *DataManagementsApi) ExportDataHandler(c *core.Context) ([]byte, string, var allTransactions []*models.Transaction for maxTime > 0 { - transactions, err := a.transactions.GetTransactionsByMaxTime(uid, maxTime, nil, 0, 0, pageCountForDataExport) + transactions, err := a.transactions.GetAllTransactionsByMaxTime(uid, maxTime, pageCountForDataExport) if err != nil { log.ErrorfWithRequestId(c, "[data_managements.ExportDataHandler] failed to get transactions earlier than \"%d\" for user \"uid:%d\", because %s", maxTime, uid, err.Error()) diff --git a/pkg/api/transactions.go b/pkg/api/transactions.go index 0fe4ad7f..71b0174c 100644 --- a/pkg/api/transactions.go +++ b/pkg/api/transactions.go @@ -36,7 +36,7 @@ func (a *TransactionsApi) TransactionListHandler(c *core.Context) (interface{}, } uid := c.GetCurrentUid() - transactions, err := a.transactions.GetTransactionsByMaxTime(uid, transactionListReq.MaxTime, nil, 0, 0, transactionListReq.Count+1) + transactions, err := a.transactions.GetTransactionsByMaxTime(uid, transactionListReq.MaxTime, transactionListReq.Type, transactionListReq.CategoryId, transactionListReq.AccountId, transactionListReq.Count+1) if err != nil { log.ErrorfWithRequestId(c, "[transactions.TransactionListHandler] failed to get transactions earlier than \"%d\" for user \"uid:%d\", because %s", transactionListReq.MaxTime, uid, err.Error()) @@ -96,7 +96,7 @@ func (a *TransactionsApi) TransactionMonthListHandler(c *core.Context) (interfac } uid := c.GetCurrentUid() - transactions, err := a.transactions.GetTransactionsInMonthByPage(uid, transactionListReq.Year, transactionListReq.Month, nil, 0, 0, transactionListReq.Page, transactionListReq.Count) + transactions, err := a.transactions.GetTransactionsInMonthByPage(uid, transactionListReq.Year, transactionListReq.Month, transactionListReq.Type, transactionListReq.CategoryId, transactionListReq.AccountId, transactionListReq.Page, transactionListReq.Count) if err != nil { log.ErrorfWithRequestId(c, "[transactions.TransactionMonthListHandler] failed to get transactions in month \"%d-%d\" for user \"uid:%d\", because %s", transactionListReq.Year, transactionListReq.Month, uid, err.Error()) diff --git a/pkg/models/transaction.go b/pkg/models/transaction.go index 94cc98ff..1ff5e018 100644 --- a/pkg/models/transaction.go +++ b/pkg/models/transaction.go @@ -72,16 +72,22 @@ type TransactionModifyRequest struct { // TransactionListByMaxTimeRequest represents all parameters of transaction listing by max time request type TransactionListByMaxTimeRequest struct { - MaxTime int64 `form:"max_time" binding:"min=0"` - Count int `form:"count" binding:"required,min=1,max=50"` + Type TransactionDbType `form:"type" binding:"min=0,max=4"` + CategoryId int64 `form:"category_id" binding:"min=0"` + AccountId int64 `form:"account_id" binding:"min=0"` + MaxTime int64 `form:"max_time" binding:"min=0"` + Count int `form:"count" binding:"required,min=1,max=50"` } // TransactionListInMonthByPageRequest represents all parameters of transaction listing by month request type TransactionListInMonthByPageRequest struct { - Year int `form:"year" binding:"required,min=1"` - Month int `form:"month" binding:"required,min=1"` - Page int `form:"page" binding:"required,min=1"` - Count int `form:"count" binding:"required,min=1,max=50"` + Year int `form:"year" binding:"required,min=1"` + Month int `form:"month" binding:"required,min=1"` + Type TransactionDbType `form:"type" binding:"min=0,max=4"` + CategoryId int64 `form:"category_id" binding:"min=0"` + AccountId int64 `form:"account_id" binding:"min=0"` + Page int `form:"page" binding:"required,min=1"` + Count int `form:"count" binding:"required,min=1,max=50"` } // TransactionGetRequest represents all parameters of transaction getting request diff --git a/pkg/services/transactions.go b/pkg/services/transactions.go index 40b30db1..ccdbd35e 100644 --- a/pkg/services/transactions.go +++ b/pkg/services/transactions.go @@ -31,8 +31,13 @@ var ( } ) +// GetAllTransactionsByMaxTime returns all transactions before given time +func (s *TransactionService) GetAllTransactionsByMaxTime(uid int64, maxTime int64, count int) ([]*models.Transaction, error) { + return s.GetTransactionsByMaxTime(uid, maxTime, 0, 0, 0, count) +} + // GetTransactionsByMaxTime returns transactions before given time -func (s *TransactionService) GetTransactionsByMaxTime(uid int64, maxTime int64, transactionType *models.TransactionDbType, categoryId int64, accountId int64, count int) ([]*models.Transaction, error) { +func (s *TransactionService) GetTransactionsByMaxTime(uid int64, maxTime int64, transactionType models.TransactionDbType, categoryId int64, accountId int64, count int) ([]*models.Transaction, error) { if uid <= 0 { return nil, errs.ErrUserIdInvalid } @@ -49,15 +54,26 @@ func (s *TransactionService) GetTransactionsByMaxTime(uid int64, maxTime int64, conditionParams = append(conditionParams, uid) conditionParams = append(conditionParams, false) - if transactionType != nil { + if models.TRANSACTION_DB_TYPE_MODIFY_BALANCE <= transactionType && transactionType <= models.TRANSACTION_DB_TYPE_EXPENSE { condition = condition + " AND type=?" conditionParams = append(conditionParams, transactionType) - } else if accountId == 0 { - condition = condition + " AND (type=? OR type=? OR type=? OR type=?)" - conditionParams = append(conditionParams, models.TRANSACTION_DB_TYPE_MODIFY_BALANCE) - conditionParams = append(conditionParams, models.TRANSACTION_DB_TYPE_INCOME) - conditionParams = append(conditionParams, models.TRANSACTION_DB_TYPE_EXPENSE) - conditionParams = append(conditionParams, models.TRANSACTION_DB_TYPE_TRANSFER_OUT) + } else if transactionType == models.TRANSACTION_DB_TYPE_TRANSFER_OUT || transactionType == models.TRANSACTION_DB_TYPE_TRANSFER_IN { + if accountId == 0 { + condition = condition + " AND type=?" + conditionParams = append(conditionParams, models.TRANSACTION_DB_TYPE_TRANSFER_OUT) + } else { + condition = condition + " AND (type=? OR type=?)" + conditionParams = append(conditionParams, models.TRANSACTION_DB_TYPE_TRANSFER_OUT) + conditionParams = append(conditionParams, models.TRANSACTION_DB_TYPE_TRANSFER_IN) + } + } else { + if accountId == 0 { + condition = condition + " AND (type=? OR type=? OR type=? OR type=?)" + conditionParams = append(conditionParams, models.TRANSACTION_DB_TYPE_MODIFY_BALANCE) + conditionParams = append(conditionParams, models.TRANSACTION_DB_TYPE_INCOME) + conditionParams = append(conditionParams, models.TRANSACTION_DB_TYPE_EXPENSE) + conditionParams = append(conditionParams, models.TRANSACTION_DB_TYPE_TRANSFER_OUT) + } } if categoryId > 0 { @@ -81,7 +97,7 @@ func (s *TransactionService) GetTransactionsByMaxTime(uid int64, maxTime int64, } // GetTransactionsInMonthByPage returns transactions in given year and month -func (s *TransactionService) GetTransactionsInMonthByPage(uid int64, year int, month int, transactionType *models.TransactionDbType, categoryId int64, accountId int64, page int, count int) ([]*models.Transaction, error) { +func (s *TransactionService) GetTransactionsInMonthByPage(uid int64, year int, month int, transactionType models.TransactionDbType, categoryId int64, accountId int64, page int, count int) ([]*models.Transaction, error) { if uid <= 0 { return nil, errs.ErrUserIdInvalid } @@ -114,15 +130,26 @@ func (s *TransactionService) GetTransactionsInMonthByPage(uid int64, year int, m conditionParams = append(conditionParams, startUnixTime) conditionParams = append(conditionParams, endUnixTime) - if transactionType != nil { + if models.TRANSACTION_DB_TYPE_MODIFY_BALANCE <= transactionType && transactionType <= models.TRANSACTION_DB_TYPE_EXPENSE { condition = condition + " AND type=?" conditionParams = append(conditionParams, transactionType) - } else if accountId == 0 { - condition = condition + " AND (type=? OR type=? OR type=? OR type=?)" - conditionParams = append(conditionParams, models.TRANSACTION_DB_TYPE_MODIFY_BALANCE) - conditionParams = append(conditionParams, models.TRANSACTION_DB_TYPE_INCOME) - conditionParams = append(conditionParams, models.TRANSACTION_DB_TYPE_EXPENSE) - conditionParams = append(conditionParams, models.TRANSACTION_DB_TYPE_TRANSFER_OUT) + } else if transactionType == models.TRANSACTION_DB_TYPE_TRANSFER_OUT || transactionType == models.TRANSACTION_DB_TYPE_TRANSFER_IN { + if accountId == 0 { + condition = condition + " AND type=?" + conditionParams = append(conditionParams, models.TRANSACTION_DB_TYPE_TRANSFER_OUT) + } else { + condition = condition + " AND (type=? OR type=?)" + conditionParams = append(conditionParams, models.TRANSACTION_DB_TYPE_TRANSFER_OUT) + conditionParams = append(conditionParams, models.TRANSACTION_DB_TYPE_TRANSFER_IN) + } + } else { + if accountId == 0 { + condition = condition + " AND (type=? OR type=? OR type=? OR type=?)" + conditionParams = append(conditionParams, models.TRANSACTION_DB_TYPE_MODIFY_BALANCE) + conditionParams = append(conditionParams, models.TRANSACTION_DB_TYPE_INCOME) + conditionParams = append(conditionParams, models.TRANSACTION_DB_TYPE_EXPENSE) + conditionParams = append(conditionParams, models.TRANSACTION_DB_TYPE_TRANSFER_OUT) + } } if categoryId > 0 { diff --git a/src/lib/services.js b/src/lib/services.js index d02d81e5..bf6c46e1 100644 --- a/src/lib/services.js +++ b/src/lib/services.js @@ -219,8 +219,8 @@ export default { id }); }, - getTransactions: ({ maxTime }) => { - return axios.get('v1/transactions/list.json?max_time=' + maxTime + '&count=20'); + getTransactions: ({ maxTime, type, categoryId, accountId }) => { + return axios.get(`v1/transactions/list.json?max_time=${maxTime}&type=${type}&category_id=${categoryId}&account_id=${accountId}&count=20`); }, getTransaction: ({ id }) => { return axios.get('v1/transactions/get.json?id=' + id); diff --git a/src/views/mobile/accounts/List.vue b/src/views/mobile/accounts/List.vue index 427f2700..ab80217f 100644 --- a/src/views/mobile/accounts/List.vue +++ b/src/views/mobile/accounts/List.vue @@ -129,7 +129,7 @@ :key="account.id" :id="account | accountDomId" :class="{ 'nested-list-item': true, 'has-child-list-item': account.type === $constants.account.allAccountTypes.MultiSubAccounts }" :after="accountBalance(account) | currency(account.currency)" - :link="account.type === $constants.account.allAccountTypes.SingleAccount ? '#' : null" + :link="account.type === $constants.account.allAccountTypes.SingleAccount ? '/transaction/list?accountId=' + account.id : null" swipeout @taphold.native="setSortable()" > @@ -147,7 +147,7 @@ diff --git a/src/views/mobile/transactions/List.vue b/src/views/mobile/transactions/List.vue index 43b52d9c..8d7c1147 100644 --- a/src/views/mobile/transactions/List.vue +++ b/src/views/mobile/transactions/List.vue @@ -130,7 +130,7 @@
@@ -147,6 +147,9 @@ :icon="transaction.category.icon | categoryIcon" :style="transaction.category.color | categoryIconStyle('var(--category-icon-color)')"> + +
@@ -176,7 +179,10 @@
- + @@ -215,6 +221,11 @@ export default { data() { return { transactions: [], + query: { + type: 0, + categoryId: 0, + accountId: 0 + }, allAccounts: {}, allCategories: {}, allTags: {}, @@ -245,6 +256,21 @@ export default { } }, created() { + const self = this; + const query = self.$f7route.query; + + if (query.type) { + self.query.type = query.type; + } + + if (query.categoryId) { + self.query.categoryId = query.categoryId; + } + + if (query.accountId) { + self.query.accountId = query.accountId; + } + this.reload(null); }, methods: { @@ -263,7 +289,10 @@ export default { self.$services.getAllTransactionCategories({}), self.$services.getAllTransactionTags(), self.$services.getTransactions({ - maxTime: self.maxTime + maxTime: self.maxTime, + type: self.query.type, + categoryId: self.query.categoryId, + accountId: self.query.accountId }) ]; @@ -384,7 +413,10 @@ export default { self.loadingMore = true; self.$services.getTransactions({ - maxTime: self.maxTime + maxTime: self.maxTime, + type: self.query.type, + categoryId: self.query.categoryId, + accountId: self.query.accountId }).then(response => { self.loadingMore = false; @@ -595,6 +627,12 @@ export default { totalExpense += amount; } else if (transaction.type === this.$constants.transaction.allTransactionTypes.Income) { totalIncome += amount; + } else if (transaction.type === this.$constants.transaction.allTransactionTypes.Transfer && this.query.accountId) { + if (this.query.accountId === transaction.sourceAccountId) { + totalExpense += amount; + } else if (this.query.accountId === transaction.destinationAccountId) { + totalIncome += amount; + } } }