diff --git a/src/libs/actions/IOU/index.ts b/src/libs/actions/IOU/index.ts index ca5cac8fc354..0d56bf558e08 100644 --- a/src/libs/actions/IOU/index.ts +++ b/src/libs/actions/IOU/index.ts @@ -3241,8 +3241,17 @@ function getMoneyRequestInformation(moneyRequestInformation: MoneyRequestInforma } if (isSplitExpense && existingTransaction) { - const {convertedAmount: omittedConvertedAmount, ...existingTransactionWithoutConvertedAmount} = existingTransaction; + const {convertedAmount: originalConvertedAmount, ...existingTransactionWithoutConvertedAmount} = existingTransaction; optimisticTransaction = fastMerge(existingTransactionWithoutConvertedAmount, optimisticTransaction, false); + + // Calculate proportional convertedAmount for the split based on the original conversion rate + // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing -- modifiedAmount can be empty string + const originalAmount = Number(existingTransaction.modifiedAmount) || existingTransaction.amount; + // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing -- modifiedAmount can be empty string + const splitAmount = Number(optimisticTransaction.modifiedAmount) || optimisticTransaction.amount; + if (originalConvertedAmount && originalAmount && splitAmount) { + optimisticTransaction.convertedAmount = Math.round((originalConvertedAmount * splitAmount) / originalAmount); + } } // STEP 4: Build optimistic reportActions. We need: diff --git a/tests/actions/IOUTest.ts b/tests/actions/IOUTest.ts index 01b1bbd6ba1f..c4186682b97c 100644 --- a/tests/actions/IOUTest.ts +++ b/tests/actions/IOUTest.ts @@ -3620,6 +3620,157 @@ describe('actions/IOU', () => { // Then the description should be the same since it was not changed expect(splitTransaction?.comment?.comment).toBe('

test

'); }); + + it('should calculate proportional convertedAmount for split transactions with foreign currency', async () => { + jest.setTimeout(10 * 1000); + + // Given: An expense report with AED currency and a USD transaction with convertedAmount + const originalAmount = -1000; + const originalConvertedAmount = -3673; + const reportID = rand64(); + const originalTransactionID = rand64(); + + const expenseReport: Report = { + reportID, + type: CONST.REPORT.TYPE.EXPENSE, + currency: 'AED', + ownerAccountID: RORY_ACCOUNT_ID, + total: originalAmount, + }; + + const originalTransaction = { + transactionID: originalTransactionID, + amount: originalAmount, + modifiedAmount: '', // Empty string - the edge case that was causing the bug + currency: 'USD', + modifiedCurrency: '', + convertedAmount: originalConvertedAmount, + created: DateUtils.getDBTime(), + merchant: 'Test Merchant', + reportID, + comment: {}, + } as unknown as Transaction; + + const transactionThread: Report = { + reportID: rand64(), + type: CONST.REPORT.TYPE.CHAT, + parentReportID: reportID, + parentReportActionID: rand64(), + }; + + const iouAction: ReportAction = { + ...buildOptimisticIOUReportAction({ + type: CONST.IOU.REPORT_ACTION_TYPE.CREATE, + amount: Math.abs(originalAmount), + currency: 'USD', + comment: '', + participants: [], + transactionID: originalTransactionID, + iouReportID: reportID, + }), + childReportID: transactionThread.reportID, + }; + + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${expenseReport.reportID}`, expenseReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${transactionThread.reportID}`, transactionThread); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseReport.reportID}`, { + [iouAction.reportActionID]: iouAction, + }); + await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${originalTransactionID}`, originalTransaction); + + const splitExpenses: SplitExpense[] = [ + { + transactionID: rand64(), + amount: -500, + created: DateUtils.getDBTime(), + merchant: 'Test Merchant', + }, + { + transactionID: rand64(), + amount: -500, + created: DateUtils.getDBTime(), + merchant: 'Test Merchant', + }, + ]; + + let allTransactions: OnyxCollection; + let allReports: OnyxCollection; + let allReportNameValuePairs: OnyxCollection; + await getOnyxData({ + key: ONYXKEYS.COLLECTION.TRANSACTION, + waitForCollectionCallback: true, + callback: (value) => { + allTransactions = value; + }, + }); + await getOnyxData({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (value) => { + allReports = value; + }, + }); + await getOnyxData({ + key: ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS, + waitForCollectionCallback: true, + callback: (value) => { + allReportNameValuePairs = value; + }, + }); + + // When splitting the expense + updateSplitTransactionsFromSplitExpensesFlow({ + allTransactionsList: allTransactions, + allReportsList: allReports, + allReportNameValuePairsList: allReportNameValuePairs, + transactionData: { + reportID, + originalTransactionID, + splitExpenses, + splitExpensesTotal: -1000, + }, + searchContext: { + currentSearchHash: -1, + }, + policyCategories: undefined, + policy: undefined, + policyRecentlyUsedCategories: [], + iouReport: expenseReport, + firstIOU: iouAction, + isASAPSubmitBetaEnabled: false, + currentUserPersonalDetails, + transactionViolations: {}, + policyRecentlyUsedCurrencies: [], + quickAction: undefined, + iouReportNextStep: undefined, + }); + + await waitForBatchedUpdates(); + + // Then each split transaction should have proportional convertedAmount + // Formula: Math.round((originalConvertedAmount * splitAmount) / originalAmount) + const expectedProportionalConvertedAmount = -1836; + + const splitTransactions = await new Promise((resolve) => { + const connection = Onyx.connect({ + key: ONYXKEYS.COLLECTION.TRANSACTION, + waitForCollectionCallback: true, + callback: (transactions) => { + Onyx.disconnect(connection); + const splits = Object.values(transactions ?? {}).filter( + (t) => t?.transactionID !== originalTransactionID && t?.comment?.originalTransactionID === originalTransactionID, + ); + resolve(splits as Transaction[]); + }, + }); + }); + + expect(splitTransactions.length).toBe(2); + + for (const splitTransaction of splitTransactions) { + expect(splitTransaction.convertedAmount).toBe(expectedProportionalConvertedAmount); + } + }); }); describe('startSplitBill', () => {