code refactor

This commit is contained in:
MaysWind
2023-08-14 00:14:53 +08:00
parent 7376fbe7a1
commit e29afa3155
2 changed files with 34 additions and 29 deletions
+33
View File
@@ -23,6 +23,9 @@ import {
getDay, getDay,
getDayOfWeekName getDayOfWeekName
} from '@/lib/datetime.js'; } from '@/lib/datetime.js';
import {
stringCurrencyToNumeric
} from '@/lib/currency.js';
const emptyTransactionResult = { const emptyTransactionResult = {
items: [], items: [],
@@ -324,6 +327,36 @@ export const useTransactionsStore = defineStore('transactions', {
geoLocation: null 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) { updateTransactionListInvalidState(invalidState) {
this.transactionListStateInvalid = invalidState; this.transactionListStateInvalid = invalidState;
}, },
+1 -29
View File
@@ -347,16 +347,12 @@ import {
getNameByKeyValue getNameByKeyValue
} from '@/lib/common.js'; } from '@/lib/common.js';
import { import {
getTimeDifferenceHoursAndMinutes,
getTimezoneOffsetMinutes, getTimezoneOffsetMinutes,
getBrowserTimezoneOffsetMinutes, getBrowserTimezoneOffsetMinutes,
getUtcOffsetByUtcOffsetMinutes, getUtcOffsetByUtcOffsetMinutes,
getDummyUnixTimeForLocalUsage, getDummyUnixTimeForLocalUsage,
getActualUnixTimeForStore getActualUnixTimeForStore
} from '@/lib/datetime.js'; } from '@/lib/datetime.js';
import {
stringCurrencyToNumeric
} from '@/lib/currency.js';
import { import {
getCategorizedAccounts, getCategorizedAccounts,
getAllFilteredAccountsBalance getAllFilteredAccountsBalance
@@ -638,31 +634,7 @@ export default {
return; return;
} }
if (this.transaction.type === this.allTransactionTypes.Expense || this.transaction.type === this.allTransactionTypes.Income) { this.transactionsStore.setTransactionSuitableDestinationAmount(this.transaction, oldValue, newValue);
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;
}
}
}, },
'transaction.destinationAmount': function (newValue) { 'transaction.destinationAmount': function (newValue) {
if (this.mode === 'view') { if (this.mode === 'view') {