show exchange rate in transaction edit page when the currencies of source account and destination account are different

This commit is contained in:
MaysWind
2024-03-04 00:08:53 +08:00
parent bed2aef7f8
commit deb465377e
3 changed files with 68 additions and 2 deletions
+24
View File
@@ -126,3 +126,27 @@ export function getDisplayExchangeRateAmount(rateStr, isEnableThousandsSeparator
return appendThousandsSeparator(trimmedRateStr, isEnableThousandsSeparator);
}
}
export function getAdaptiveDisplayAmountRate(amount1, amount2, fromExchangeRate, toExchangeRate, isEnableThousandsSeparator) {
if (!amount1 || !amount2 || amount1 === amount2) {
if (!fromExchangeRate || !fromExchangeRate.rate || !toExchangeRate || !toExchangeRate.rate) {
return null;
}
amount1 = fromExchangeRate.rate;
amount2 = toExchangeRate.rate;
}
amount1 = parseFloat(amount1);
amount2 = parseFloat(amount2);
if (amount1 > amount2) {
const rateStr = (amount1 / amount2).toString();
const displayRateStr = getDisplayExchangeRateAmount(rateStr, isEnableThousandsSeparator);
return `${displayRateStr} : 1`;
} else {
const rateStr = (amount2 / amount1).toString();
const displayRateStr = getDisplayExchangeRateAmount(rateStr, isEnableThousandsSeparator);
return `1 : ${displayRateStr}`;
}
}
@@ -73,7 +73,7 @@
:disabled="loading || submitting"
:persistent-placeholder="true"
:hide="transaction.hideAmount"
:label="$t('Transfer In Amount')"
:label="transferInAmountTitle"
:placeholder="$t('Transfer In Amount')"
v-model="transaction.destinationAmount"/>
</v-col>
@@ -325,6 +325,9 @@ import {
getTimezoneOffsetMinutes,
getCurrentUnixTime
} from '@/lib/datetime.js';
import {
getAdaptiveDisplayAmountRate
} from '@/lib/currency.js';
import {
getFirstAvailableCategoryId
} from '@/lib/category.js';
@@ -417,6 +420,24 @@ export default {
return 'Account';
}
},
transferInAmountTitle() {
const sourceAccount = this.allAccountsMap[this.transaction.sourceAccountId];
const destinationAccount = this.allAccountsMap[this.transaction.destinationAccountId];
if (!sourceAccount || !destinationAccount || sourceAccount.currency === destinationAccount.currency) {
return this.$t('Transfer In Amount');
}
const fromExchangeRate = this.exchangeRatesStore.latestExchangeRateMap[sourceAccount.currency];
const toExchangeRate = this.exchangeRatesStore.latestExchangeRateMap[destinationAccount.currency];
const amountRate = getAdaptiveDisplayAmountRate(this.transaction.sourceAmount, this.transaction.destinationAmount, fromExchangeRate, toExchangeRate, this.settingsStore.appSettings.thousandsSeparator);
if (!amountRate) {
return this.$t('Transfer In Amount');
}
return this.$t('Transfer In Amount') + ` (${amountRate})`;
},
defaultCurrency() {
return this.userStore.currentUserDefaultCurrency;
},
+22 -1
View File
@@ -66,7 +66,7 @@
class="transaction-edit-amount text-color-primary"
link="#" no-chevron
:class="destinationAmountClass"
:header="$t('Transfer In Amount')"
:header="transferInAmountTitle"
:title="getDisplayAmount(transaction.destinationAmount, transaction.hideAmount)"
@click="showDestinationAmountSheet = true"
v-if="transaction.type === allTransactionTypes.Transfer"
@@ -358,6 +358,9 @@ import {
getUtcOffsetByUtcOffsetMinutes,
getActualUnixTimeForStore
} from '@/lib/datetime.js';
import {
getAdaptiveDisplayAmountRate
} from '@/lib/currency.js';
import {
getTransactionPrimaryCategoryName,
getTransactionSecondaryCategoryName,
@@ -435,6 +438,24 @@ export default {
return 'Account';
}
},
transferInAmountTitle() {
const sourceAccount = this.allAccountsMap[this.transaction.sourceAccountId];
const destinationAccount = this.allAccountsMap[this.transaction.destinationAccountId];
if (!sourceAccount || !destinationAccount || sourceAccount.currency === destinationAccount.currency) {
return this.$t('Transfer In Amount');
}
const fromExchangeRate = this.exchangeRatesStore.latestExchangeRateMap[sourceAccount.currency];
const toExchangeRate = this.exchangeRatesStore.latestExchangeRateMap[destinationAccount.currency];
const amountRate = getAdaptiveDisplayAmountRate(this.transaction.sourceAmount, this.transaction.destinationAmount, fromExchangeRate, toExchangeRate, this.settingsStore.appSettings.thousandsSeparator);
if (!amountRate) {
return this.$t('Transfer In Amount');
}
return this.$t('Transfer In Amount') + ` (${amountRate})`;
},
defaultCurrency() {
return this.userStore.currentUserDefaultCurrency;
},