diff --git a/src/stores/transaction.js b/src/stores/transaction.js index caf62d43..8a030cdd 100644 --- a/src/stores/transaction.js +++ b/src/stores/transaction.js @@ -23,6 +23,9 @@ import { getDay, getDayOfWeekName } from '@/lib/datetime.js'; +import { + stringCurrencyToNumeric +} from '@/lib/currency.js'; const emptyTransactionResult = { items: [], @@ -324,6 +327,36 @@ export const useTransactionsStore = defineStore('transactions', { geoLocation: null }; }, + setTransactionSuitableDestinationAmount(transaction, oldValue, newValue) { + const accountsStore = useAccountsStore(); + const exchangeRatesStore = useExchangeRatesStore(); + + if (transaction.type === transactionConstants.allTransactionTypes.Expense || transaction.type === transactionConstants.allTransactionTypes.Income) { + transaction.destinationAmount = newValue; + } else if (transaction.type === transactionConstants.allTransactionTypes.Transfer) { + const sourceAccount = accountsStore.allAccountsMap[transaction.sourceAccountId]; + const destinationAccount = accountsStore.allAccountsMap[transaction.destinationAccountId]; + + if (sourceAccount && destinationAccount && sourceAccount.currency !== destinationAccount.currency) { + const exchangedOldValue = exchangeRatesStore.getExchangedAmount(oldValue, sourceAccount.currency, destinationAccount.currency); + const exchangedNewValue = exchangeRatesStore.getExchangedAmount(newValue, sourceAccount.currency, destinationAccount.currency); + + if (isNumber(exchangedOldValue)) { + oldValue = Math.floor(exchangedOldValue); + } + + if (isNumber(exchangedNewValue)) { + newValue = Math.floor(exchangedNewValue); + } + } + + if ((!sourceAccount || !destinationAccount || transaction.destinationAmount === oldValue) && + (stringCurrencyToNumeric(transactionConstants.minAmount) <= newValue && + newValue <= stringCurrencyToNumeric(transactionConstants.maxAmount))) { + transaction.destinationAmount = newValue; + } + } + }, updateTransactionListInvalidState(invalidState) { this.transactionListStateInvalid = invalidState; }, diff --git a/src/views/mobile/transactions/EditPage.vue b/src/views/mobile/transactions/EditPage.vue index 918609d5..ac9e21d0 100644 --- a/src/views/mobile/transactions/EditPage.vue +++ b/src/views/mobile/transactions/EditPage.vue @@ -347,16 +347,12 @@ import { getNameByKeyValue } from '@/lib/common.js'; import { - getTimeDifferenceHoursAndMinutes, getTimezoneOffsetMinutes, getBrowserTimezoneOffsetMinutes, getUtcOffsetByUtcOffsetMinutes, getDummyUnixTimeForLocalUsage, getActualUnixTimeForStore } from '@/lib/datetime.js'; -import { - stringCurrencyToNumeric -} from '@/lib/currency.js'; import { getCategorizedAccounts, getAllFilteredAccountsBalance @@ -638,31 +634,7 @@ export default { return; } - if (this.transaction.type === this.allTransactionTypes.Expense || this.transaction.type === this.allTransactionTypes.Income) { - this.transaction.destinationAmount = newValue; - } else if (this.transaction.type === this.allTransactionTypes.Transfer) { - const sourceAccount = this.allAccountsMap[this.transaction.sourceAccountId] - const destinationAccount = this.allAccountsMap[this.transaction.destinationAccountId] - - if (sourceAccount && destinationAccount && sourceAccount.currency !== destinationAccount.currency) { - const exchangedOldValue = this.exchangeRatesStore.getExchangedAmount(oldValue, sourceAccount.currency, destinationAccount.currency); - const exchangedNewValue = this.exchangeRatesStore.getExchangedAmount(newValue, sourceAccount.currency, destinationAccount.currency); - - if (isNumber(exchangedOldValue)) { - oldValue = Math.floor(exchangedOldValue); - } - - if (isNumber(exchangedNewValue)) { - newValue = Math.floor(exchangedNewValue); - } - } - - if ((!sourceAccount || !destinationAccount || this.transaction.destinationAmount === oldValue) && - (stringCurrencyToNumeric(this.allowedMinAmount) <= newValue && - newValue <= stringCurrencyToNumeric(this.allowedMaxAmount))) { - this.transaction.destinationAmount = newValue; - } - } + this.transactionsStore.setTransactionSuitableDestinationAmount(this.transaction, oldValue, newValue); }, 'transaction.destinationAmount': function (newValue) { if (this.mode === 'view') {