Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/components/ReportActionItem/MoneyRequestView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ function MoneyRequestView({
tag: transactionTag,
originalCurrency: transactionOriginalCurrency,
postedDate: transactionPostedDate,
convertedAmount: transactionConvertedAmount,
} = getTransactionDetails(transaction, undefined, undefined, allowNegativeAmount, false, currentUserPersonalDetails) ?? {};
const isEmptyMerchant = transactionMerchant === '' || transactionMerchant === CONST.TRANSACTION.PARTIAL_TRANSACTION_MERCHANT;
const isDistanceRequest = isDistanceRequestTransactionUtils(transaction);
Expand Down Expand Up @@ -461,6 +462,11 @@ function MoneyRequestView({
if (isExpenseSplit) {
amountDescription += ` ${CONST.DOT_SEPARATOR} ${translate('iou.split')}`;
}
if (currency !== moneyRequestReport?.currency && !isManagedCardTransaction && transaction?.reportID !== CONST.REPORT.UNREPORTED_REPORT_ID) {
if (transactionConvertedAmount) {
amountDescription += ` ${CONST.DOT_SEPARATOR} ${translate('common.converted')} ${convertToDisplayString(transactionConvertedAmount, moneyRequestReport?.currency)}`;
}
}

if (isFromMergeTransaction) {
// Because we lack the necessary data in policy.customUnits to determine the rate in merge flow,
Expand Down
3 changes: 3 additions & 0 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ import {
getCardID,
getCardName,
getCategory,
getConvertedAmount,
getCurrency,
getDescription,
getFormattedCreated,
Expand Down Expand Up @@ -781,6 +782,7 @@ type TransactionDetails = {
postedDate: string;
transactionID: string;
distance?: number;
convertedAmount: number;
};

type OptimisticIOUReport = Pick<
Expand Down Expand Up @@ -4461,6 +4463,7 @@ function getTransactionDetails(
cardName: getCardName(transaction),
originalAmount: getOriginalAmount(transaction),
originalCurrency: getOriginalCurrency(transaction),
convertedAmount: getConvertedAmount(transaction, isFromExpenseReport, transaction?.reportID === CONST.REPORT.UNREPORTED_REPORT_ID, allowNegativeAmount, disableOppositeConversion),
postedDate: getFormattedPostedDate(transaction),
transactionID: transaction.transactionID,
...(isManualDistanceRequest && {distance: transaction.comment?.customUnit?.quantity ?? undefined}),
Expand Down
26 changes: 26 additions & 0 deletions src/libs/TransactionUtils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
};

let deprecatedAllReports: OnyxCollection<Report> = {};
Onyx.connect({

Check warning on line 129 in src/libs/TransactionUtils/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -135,7 +135,7 @@
});

let deprecatedAllTransactionViolations: OnyxCollection<TransactionViolations> = {};
Onyx.connect({

Check warning on line 138 in src/libs/TransactionUtils/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS,
waitForCollectionCallback: true,
callback: (value) => (deprecatedAllTransactionViolations = value),
Expand Down Expand Up @@ -844,6 +844,31 @@
return Math.abs(amount);
}

function getConvertedAmount(
transaction: OnyxInputOrEntry<Transaction>,
isFromExpenseReport = false,
isFromTrackedExpense = false,
allowNegative = false,
disableOppositeConversion = false,
): number {
// IOU requests cannot have negative values, but they can be stored as negative values, let's return absolute value
if (!isFromExpenseReport && !isFromTrackedExpense && !allowNegative) {
return Math.abs(transaction?.convertedAmount ?? 0);
}

if (disableOppositeConversion) {
return transaction?.convertedAmount ?? 0;
}

// Expense report case:
// The amounts are stored using an opposite sign and negative values can be set,
// we need to return an opposite sign than is saved in the transaction object
const convertedAmount = transaction?.convertedAmount ?? 0;

// To avoid -0 being shown, lets only change the sign if the value is other than 0.
return convertedAmount ? -convertedAmount : 0;
}

Comment on lines +847 to +871
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add tests for this util

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ZhenjaHorbach Updated.

/**
* Return the original amount for display/sorting purposes.
* For expense reports, returns the negated value of (originalAmount || amount || modifiedAmount).
Expand Down Expand Up @@ -2572,6 +2597,7 @@
shouldReuseInitialTransaction,
getOriginalAmountForDisplay,
getOriginalCurrencyForDisplay,
getConvertedAmount,
shouldShowExpenseBreakdown,
};

Expand Down
2 changes: 2 additions & 0 deletions tests/unit/ReportSecondaryActionUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2631,6 +2631,7 @@ describe('getSecondaryTransactionThreadActions', () => {
originalCurrency: 'USD',
postedDate: '2025-01-01',
cardID: 1,
convertedAmount: -100,
});

jest.spyOn(ReportUtils, 'isMoneyRequestReportEligibleForMerge').mockReturnValue(true);
Expand Down Expand Up @@ -2683,6 +2684,7 @@ describe('getSecondaryTransactionThreadActions', () => {
originalCurrency: 'USD',
postedDate: '2025-01-01',
cardID: 1,
convertedAmount: 100,
});

jest.spyOn(ReportUtils, 'isMoneyRequestReportEligibleForMerge').mockReturnValue(true);
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/TransactionUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1537,4 +1537,27 @@ describe('TransactionUtils', () => {
expect(TransactionUtils.shouldShowExpenseBreakdown(transactions)).toBe(true);
});
});

describe('getConvertedAmount', () => {
it('should return the absolute amount if transaction is not from expense report, tracked expense and allowNegative is false', () => {
const transaction = generateTransaction({
convertedAmount: -100,
});
expect(TransactionUtils.getConvertedAmount(transaction)).toBe(100);
});

it('should return the opposite sign amount if the transaction is from the expense report and disableOppositeConversion is false', () => {
const transaction = generateTransaction({
convertedAmount: -100,
});
expect(TransactionUtils.getConvertedAmount(transaction, true, false, false, false)).toBe(100);
});

it('should return the current converted amount if the transaction is from the expense report and disableOppositeConversion is true', () => {
const transaction = generateTransaction({
convertedAmount: -100,
});
expect(TransactionUtils.getConvertedAmount(transaction, true, false, false, true)).toBe(-100);
});
});
});
Loading