Refactor transfer amount calculation to handle account changes

- Refactor `setTransactionSuitableDestinationAmount` in transactions store to handle account ID changes and avoid logic duplication.
- Recalculate transfer-in amount when changing accounts, while preserving manual edits by verifying previous exchange rate relationships.
- Clean up redundant calculation logic in `TransactionEditPageBase.ts`.
This commit is contained in:
GaryOu
2026-01-09 00:05:05 +08:00
committed by mayswind
parent f7d0e2279a
commit a730ebab8f
2 changed files with 31 additions and 54 deletions
+27 -13
View File
@@ -556,34 +556,48 @@ export const useTransactionsStore = defineStore('transactions', () => {
clearUserTransactionDraft(); clearUserTransactionDraft();
} }
function setTransactionSuitableDestinationAmount(transaction: Transaction, oldValue: number, newValue: number): void { function setTransactionSuitableDestinationAmount(transaction: Transaction, oldSourceAmount: number, newSourceAmount: number, oldSourceAccountId?: string, oldDestinationAccountId?: string): void {
if (transaction.type === TransactionType.Expense || transaction.type === TransactionType.Income) { if (transaction.type === TransactionType.Expense || transaction.type === TransactionType.Income) {
transaction.destinationAmount = newValue; transaction.destinationAmount = newSourceAmount;
} else if (transaction.type === TransactionType.Transfer) { } else if (transaction.type === TransactionType.Transfer) {
const sourceAccount = accountsStore.allAccountsMap[transaction.sourceAccountId]; const sourceAccount = accountsStore.allAccountsMap[transaction.sourceAccountId];
const destinationAccount = accountsStore.allAccountsMap[transaction.destinationAccountId]; const destinationAccount = accountsStore.allAccountsMap[transaction.destinationAccountId];
if (sourceAccount && destinationAccount && sourceAccount.currency !== destinationAccount.currency) { if (!sourceAccount || !destinationAccount) {
const decimalNumberCount = getCurrencyFraction(destinationAccount.currency); return;
const exchangedOldValue = exchangeRatesStore.getExchangedAmount(oldValue, sourceAccount.currency, destinationAccount.currency); }
const exchangedNewValue = exchangeRatesStore.getExchangedAmount(newValue, sourceAccount.currency, destinationAccount.currency);
const oldSourceAccount = oldSourceAccountId ? accountsStore.allAccountsMap[oldSourceAccountId] : sourceAccount;
const oldDestinationAccount = oldDestinationAccountId ? accountsStore.allAccountsMap[oldDestinationAccountId] : destinationAccount;
let oldValueToCompare = oldSourceAmount;
let newValueToSet = newSourceAmount;
if (oldSourceAccount && oldDestinationAccount && oldSourceAccount.currency !== oldDestinationAccount.currency) {
const decimalNumberCount = getCurrencyFraction(oldDestinationAccount.currency);
const exchangedOldValue = exchangeRatesStore.getExchangedAmount(oldSourceAmount, oldSourceAccount.currency, oldDestinationAccount.currency);
if (isNumber(decimalNumberCount) && isNumber(exchangedOldValue)) { if (isNumber(decimalNumberCount) && isNumber(exchangedOldValue)) {
oldValue = Math.trunc(exchangedOldValue); oldValueToCompare = Math.trunc(exchangedOldValue);
oldValue = getAmountWithDecimalNumberCount(oldValue, decimalNumberCount); oldValueToCompare = getAmountWithDecimalNumberCount(oldValueToCompare, decimalNumberCount);
} }
}
if (sourceAccount.currency !== destinationAccount.currency) {
const decimalNumberCount = getCurrencyFraction(destinationAccount.currency);
const exchangedNewValue = exchangeRatesStore.getExchangedAmount(newSourceAmount, sourceAccount.currency, destinationAccount.currency);
if (isNumber(decimalNumberCount) && isNumber(exchangedNewValue)) { if (isNumber(decimalNumberCount) && isNumber(exchangedNewValue)) {
newValue = Math.trunc(exchangedNewValue); newValueToSet = Math.trunc(exchangedNewValue);
newValue = getAmountWithDecimalNumberCount(newValue, decimalNumberCount); newValueToSet = getAmountWithDecimalNumberCount(newValueToSet, decimalNumberCount);
} else { } else {
return; return;
} }
} }
if ((!sourceAccount || !destinationAccount || transaction.destinationAmount === oldValue || transaction.destinationAmount === 0) && if ((transaction.destinationAmount === oldValueToCompare || transaction.destinationAmount === 0) &&
(TRANSACTION_MIN_AMOUNT <= newValue && newValue <= TRANSACTION_MAX_AMOUNT)) { (TRANSACTION_MIN_AMOUNT <= newValueToSet && newValueToSet <= TRANSACTION_MAX_AMOUNT)) {
transaction.destinationAmount = newValue; transaction.destinationAmount = newValueToSet;
} }
} }
} }
@@ -16,7 +16,7 @@ import type { LocalizedTimezoneInfo } from '@/core/timezone.ts';
import { TransactionType } from '@/core/transaction.ts'; import { TransactionType } from '@/core/transaction.ts';
import { TemplateType } from '@/core/template.ts'; import { TemplateType } from '@/core/template.ts';
import { DISPLAY_HIDDEN_AMOUNT } from '@/consts/numeral.ts'; import { DISPLAY_HIDDEN_AMOUNT } from '@/consts/numeral.ts';
import { TRANSACTION_MAX_PICTURE_COUNT, TRANSACTION_MIN_AMOUNT, TRANSACTION_MAX_AMOUNT } from '@/consts/transaction.ts'; import { TRANSACTION_MAX_PICTURE_COUNT } from '@/consts/transaction.ts';
import { Account, type CategorizedAccountWithDisplayBalance } from '@/models/account.ts'; import { Account, type CategorizedAccountWithDisplayBalance } from '@/models/account.ts';
import type { TransactionCategory } from '@/models/transaction_category.ts'; import type { TransactionCategory } from '@/models/transaction_category.ts';
@@ -26,19 +26,13 @@ import { Transaction } from '@/models/transaction.ts';
import { TransactionTemplate } from '@/models/transaction_template.ts'; import { TransactionTemplate } from '@/models/transaction_template.ts';
import { import {
isArray, isArray
isNumber
} from '@/lib/common.ts'; } from '@/lib/common.ts';
import { import {
getExchangedAmountByRate, getExchangedAmountByRate
getAmountWithDecimalNumberCount
} from '@/lib/numeral.ts'; } from '@/lib/numeral.ts';
import {
getCurrencyFraction
} from '@/lib/currency.ts';
import { import {
getUtcOffsetByUtcOffsetMinutes, getUtcOffsetByUtcOffsetMinutes,
getTimezoneOffsetMinutes, getTimezoneOffsetMinutes,
@@ -437,11 +431,6 @@ export function useTransactionEditPageBase(type: TransactionEditPageType, initMo
return; return;
} }
// Only recalculate if accounts actually changed
if (newSourceAccountId === oldSourceAccountId && newDestinationAccountId === oldDestinationAccountId) {
return;
}
// Only recalculate if accounts actually changed (skip initial watch call) // Only recalculate if accounts actually changed (skip initial watch call)
if (oldSourceAccountId !== undefined && oldDestinationAccountId !== undefined) { if (oldSourceAccountId !== undefined && oldDestinationAccountId !== undefined) {
if (newSourceAccountId === oldSourceAccountId && newDestinationAccountId === oldDestinationAccountId) { if (newSourceAccountId === oldSourceAccountId && newDestinationAccountId === oldDestinationAccountId) {
@@ -449,33 +438,7 @@ export function useTransactionEditPageBase(type: TransactionEditPageType, initMo
} }
} }
const sourceAccount = allAccountsMap.value[transaction.value.sourceAccountId]; transactionsStore.setTransactionSuitableDestinationAmount(transaction.value, transaction.value.sourceAmount, transaction.value.sourceAmount, oldSourceAccountId as string, oldDestinationAccountId as string);
const destinationAccount = allAccountsMap.value[transaction.value.destinationAccountId];
if (!sourceAccount || !destinationAccount) {
return;
}
// Recalculate destination amount based on source amount and exchange rate
if (sourceAccount.currency !== destinationAccount.currency) {
const exchangedAmount = exchangeRatesStore.getExchangedAmount(transaction.value.sourceAmount, sourceAccount.currency, destinationAccount.currency);
if (isNumber(exchangedAmount)) {
const decimalNumberCount = getCurrencyFraction(destinationAccount.currency);
let newDestinationAmount = Math.trunc(exchangedAmount);
if (isNumber(decimalNumberCount)) {
newDestinationAmount = getAmountWithDecimalNumberCount(newDestinationAmount, decimalNumberCount);
}
if (TRANSACTION_MIN_AMOUNT <= newDestinationAmount && newDestinationAmount <= TRANSACTION_MAX_AMOUNT) {
transaction.value.destinationAmount = newDestinationAmount;
}
}
} else {
// Same currency, just copy the source amount
transaction.value.destinationAmount = transaction.value.sourceAmount;
}
}); });
return { return {