From a95403955d923feab3f7e6c7d07ee6127cb98249 Mon Sep 17 00:00:00 2001 From: "Bernhard Owen Josephus (via MelvinBot)" Date: Sat, 10 Oct 2026 04:55:37 +0000 Subject: [PATCH 1/3] Keep the negative tax amount sign for unreported expenses Co-authored-by: Bernhard Owen Josephus --- src/libs/ReportUtils.ts | 6 ++--- src/libs/TransactionUtils/index.ts | 12 +++++----- tests/unit/ReportUtilsTest.ts | 34 +++++++++++++++++++++++++++++ tests/unit/TransactionUtilsTest.ts | 35 ++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 9 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 1ad33c915d77..bcf9def5366c 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -5647,7 +5647,7 @@ function getTransactionDetails( created: getFormattedCreated(transaction, createdDateFormat, dateFnsLocale), amount: getTransactionAmount(transaction, isFromExpenseReport, transaction?.reportID === CONST.REPORT.UNREPORTED_REPORT_ID, allowNegativeAmount, disableOppositeConversion), attendees: getAttendees(transaction, reportOwnerAsAttendee), - taxAmount: getTaxAmount(transaction, isFromExpenseReport), + taxAmount: getTaxAmount(transaction, isFromExpenseReport, transaction?.reportID === CONST.REPORT.UNREPORTED_REPORT_ID), taxCode: getTaxCode(transaction), taxValue: transaction.taxValue, currency: getCurrency(transaction), @@ -5665,7 +5665,7 @@ function getTransactionDetails( originalAmount: getOriginalAmount(transaction), originalCurrency: getOriginalCurrency(transaction), convertedAmount: getConvertedAmount(transaction, isFromExpenseReport, transaction?.reportID === CONST.REPORT.UNREPORTED_REPORT_ID, allowNegativeAmount, disableOppositeConversion), - convertedTaxAmount: getConvertedTaxAmount(transaction, isFromExpenseReport), + convertedTaxAmount: getConvertedTaxAmount(transaction, isFromExpenseReport, transaction?.reportID === CONST.REPORT.UNREPORTED_REPORT_ID), postedDate: getFormattedPostedDate(transaction), transactionID: transaction.transactionID, ...(isDistanceRequest(transaction) && {distance: transaction.comment?.customUnit?.quantity ?? undefined}), @@ -14878,7 +14878,7 @@ function getTransactionSortValue( case CONST.SEARCH.TABLE_COLUMNS.TAX_CODE: return transaction.taxCode ?? ''; case CONST.SEARCH.TABLE_COLUMNS.TAX_AMOUNT: - return getTaxAmount(transaction, isExpenseReport(report)); + return getTaxAmount(transaction, isExpenseReport(report), transaction.reportID === CONST.REPORT.UNREPORTED_REPORT_ID); case CONST.SEARCH.TABLE_COLUMNS.TAX_RATE: return getTaxName(policy, transaction) ?? ''; case CONST.SEARCH.TABLE_COLUMNS.CARD: diff --git a/src/libs/TransactionUtils/index.ts b/src/libs/TransactionUtils/index.ts index 8346efd4a970..3459328440e6 100644 --- a/src/libs/TransactionUtils/index.ts +++ b/src/libs/TransactionUtils/index.ts @@ -535,9 +535,9 @@ function getAmount(transaction: OnyxInputOrEntry, isFromExpenseRepo /** * Convert a stored tax amount to the sign we display. */ -function normalizeTaxAmountSign(rawAmount: number | undefined, isFromExpenseReport: boolean): number { +function normalizeTaxAmountSign(rawAmount: number | undefined, isFromExpenseReport: boolean, isFromTrackedExpense = false): number { // IOU requests cannot have negative values but they can be stored as negative values, let's return absolute value - if (!isFromExpenseReport) { + if (!isFromExpenseReport && !isFromTrackedExpense) { return Math.abs(rawAmount ?? 0); } @@ -549,16 +549,16 @@ function normalizeTaxAmountSign(rawAmount: number | undefined, isFromExpenseRepo /** * Return the tax amount field from the transaction. */ -function getTaxAmount(transaction: OnyxInputOrEntry, isFromExpenseReport: boolean): number { - return normalizeTaxAmountSign(transaction?.taxAmount, isFromExpenseReport); +function getTaxAmount(transaction: OnyxInputOrEntry, isFromExpenseReport: boolean, isFromTrackedExpense = false): number { + return normalizeTaxAmountSign(transaction?.taxAmount, isFromExpenseReport, isFromTrackedExpense); } /** * Return the converted tax amount field from the transaction. * It follows the same sign convention as `getTaxAmount`. */ -function getConvertedTaxAmount(transaction: OnyxInputOrEntry, isFromExpenseReport: boolean): number { - return normalizeTaxAmountSign(transaction?.convertedTaxAmount, isFromExpenseReport); +function getConvertedTaxAmount(transaction: OnyxInputOrEntry, isFromExpenseReport: boolean, isFromTrackedExpense = false): number { + return normalizeTaxAmountSign(transaction?.convertedTaxAmount, isFromExpenseReport, isFromTrackedExpense); } /** diff --git a/tests/unit/ReportUtilsTest.ts b/tests/unit/ReportUtilsTest.ts index b0009b7421cf..8844e517e5b7 100644 --- a/tests/unit/ReportUtilsTest.ts +++ b/tests/unit/ReportUtilsTest.ts @@ -24494,6 +24494,18 @@ describe('ReportUtils', () => { expect(result).toBe(-500); }); + it('should keep the negative sign of an unreported expense tax amount for TAX_AMOUNT column', () => { + // Given an unreported expense, which is stored with the opposite sign like an expense report expense, so a -$5.00 tax is stored as 500 + const transaction = createMockTransaction({reportID: CONST.REPORT.UNREPORTED_REPORT_ID, taxAmount: 500}); + const chatReport = createMock({reportID: 'self-dm-report-id', type: CONST.REPORT.TYPE.CHAT}); + + // When we get the sort value for the tax amount column + const result = getTransactionSortValue(transaction, CONST.SEARCH.TABLE_COLUMNS.TAX_AMOUNT, chatReport, mockPolicy); + + // Then the sign is flipped back instead of returning the absolute value, matching the TOTAL_AMOUNT column + expect(result).toBe(-500); + }); + it('should return cardID for CARD column', () => { const transaction = createMockTransaction({cardID: 12345}); const result = getTransactionSortValue(transaction, CONST.SEARCH.TABLE_COLUMNS.CARD, mockReport, mockPolicy); @@ -24776,6 +24788,28 @@ describe('ReportUtils', () => { expect(details?.taxValue).toBe('10%'); }); + + it('should keep the negative sign of the tax amount for a negative unreported expense', () => { + // Given a negative unreported expense, which is stored with the opposite sign like an expense report expense, + // so a -$9.50 expense with -$0.95 tax is stored as amount 950 and taxAmount 95 + const transaction = createMock({ + transactionID: 'unreported-negative-transaction', + reportID: CONST.REPORT.UNREPORTED_REPORT_ID, + amount: 950, + currency: 'USD', + taxCode: 'id_TAX_RATE_1', + taxAmount: 95, + convertedTaxAmount: 95, + }); + + // When we get the transaction details to display + const details = getTransactionDetails(transaction); + + // Then the tax amounts keep the same negative sign as the amount instead of being shown as absolute values + expect(details?.amount).toBe(-950); + expect(details?.taxAmount).toBe(-95); + expect(details?.convertedTaxAmount).toBe(-95); + }); }); describe('getChatListItemReportName', () => { diff --git a/tests/unit/TransactionUtilsTest.ts b/tests/unit/TransactionUtilsTest.ts index 70ca5c62fb45..9c60fd3c708b 100644 --- a/tests/unit/TransactionUtilsTest.ts +++ b/tests/unit/TransactionUtilsTest.ts @@ -4135,6 +4135,30 @@ describe('TransactionUtils', () => { }); }); + describe('getTaxAmount', () => { + it('should return the absolute tax amount if the transaction is neither from an expense report nor unreported', () => { + // Given an IOU transaction whose tax is stored as a negative value + const transaction = generateTransaction({taxAmount: -100}); + + // When we read the tax amount + const taxAmount = TransactionUtils.getTaxAmount(transaction, false, false); + + // Then it is returned unsigned, because IOU requests cannot have negative values + expect(taxAmount).toBe(100); + }); + + it('should return the opposite sign if the transaction is unreported', () => { + // Given an unreported transaction, which stores its tax with the opposite sign like an expense report transaction + const transaction = generateTransaction({reportID: CONST.REPORT.UNREPORTED_REPORT_ID, taxAmount: 95}); + + // When we read the tax amount for a tracked expense that isn't on an expense report + const taxAmount = TransactionUtils.getTaxAmount(transaction, false, true); + + // Then the sign is flipped back, so a negative unreported expense keeps its negative tax + expect(taxAmount).toBe(-95); + }); + }); + describe('getConvertedTaxAmount', () => { it('should return the absolute converted tax amount if the transaction is not from an expense report', () => { // Given an IOU transaction whose converted tax is stored as a negative value @@ -4158,6 +4182,17 @@ describe('TransactionUtils', () => { expect(convertedTaxAmount).toBe(-182); }); + it('should return the opposite sign if the transaction is unreported', () => { + // Given an unreported transaction, which stores its converted tax with the opposite sign like an expense report transaction + const transaction = generateTransaction({reportID: CONST.REPORT.UNREPORTED_REPORT_ID, convertedTaxAmount: 182}); + + // When we read the converted tax amount for a tracked expense that isn't on an expense report + const convertedTaxAmount = TransactionUtils.getConvertedTaxAmount(transaction, false, true); + + // Then the sign is flipped back, so a negative unreported expense keeps its negative tax + expect(convertedTaxAmount).toBe(-182); + }); + it('should return 0 instead of -0 when there is no converted tax amount', () => { // Given an expense-report transaction with no converted tax const transaction = generateTransaction({convertedTaxAmount: 0}); From 2afbbd8566083eecbdb2e42c6132479080421bf3 Mon Sep 17 00:00:00 2001 From: "Bernhard Owen Josephus (via MelvinBot)" Date: Sat, 10 Oct 2026 05:37:50 +0000 Subject: [PATCH 2/3] Store the unreported expense check in a variable Co-authored-by: Bernhard Owen Josephus --- src/libs/ReportUtils.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index bcf9def5366c..08d20d4cb53b 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -5642,12 +5642,13 @@ function getTransactionDetails( const report = getReportOrDraftReport(transaction?.reportID, undefined, 'report' in transaction ? transaction.report : undefined); const isFromExpenseReport = isTransactionFromExpenseReport(report, policy); + const isFromTrackedExpense = transaction?.reportID === CONST.REPORT.UNREPORTED_REPORT_ID; return { created: getFormattedCreated(transaction, createdDateFormat, dateFnsLocale), - amount: getTransactionAmount(transaction, isFromExpenseReport, transaction?.reportID === CONST.REPORT.UNREPORTED_REPORT_ID, allowNegativeAmount, disableOppositeConversion), + amount: getTransactionAmount(transaction, isFromExpenseReport, isFromTrackedExpense, allowNegativeAmount, disableOppositeConversion), attendees: getAttendees(transaction, reportOwnerAsAttendee), - taxAmount: getTaxAmount(transaction, isFromExpenseReport, transaction?.reportID === CONST.REPORT.UNREPORTED_REPORT_ID), + taxAmount: getTaxAmount(transaction, isFromExpenseReport, isFromTrackedExpense), taxCode: getTaxCode(transaction), taxValue: transaction.taxValue, currency: getCurrency(transaction), @@ -5664,8 +5665,8 @@ function getTransactionDetails( cardName: getCardName(transaction), originalAmount: getOriginalAmount(transaction), originalCurrency: getOriginalCurrency(transaction), - convertedAmount: getConvertedAmount(transaction, isFromExpenseReport, transaction?.reportID === CONST.REPORT.UNREPORTED_REPORT_ID, allowNegativeAmount, disableOppositeConversion), - convertedTaxAmount: getConvertedTaxAmount(transaction, isFromExpenseReport, transaction?.reportID === CONST.REPORT.UNREPORTED_REPORT_ID), + convertedAmount: getConvertedAmount(transaction, isFromExpenseReport, isFromTrackedExpense, allowNegativeAmount, disableOppositeConversion), + convertedTaxAmount: getConvertedTaxAmount(transaction, isFromExpenseReport, isFromTrackedExpense), postedDate: getFormattedPostedDate(transaction), transactionID: transaction.transactionID, ...(isDistanceRequest(transaction) && {distance: transaction.comment?.customUnit?.quantity ?? undefined}), @@ -14841,6 +14842,8 @@ function getTransactionSortValue( policyCategories?: PolicyCategories, policyTagLists?: PolicyTagLists, ): string | number | undefined { + const isFromTrackedExpense = transaction.reportID === CONST.REPORT.UNREPORTED_REPORT_ID; + switch (key) { case CONST.SEARCH.TABLE_COLUMNS.DATE: return getTransactionCreated(transaction); @@ -14860,7 +14863,7 @@ function getTransactionSortValue( case CONST.SEARCH.SORT_BY_COLUMNS.TAG_GL_CODE: return getTagGLCode(policyTagLists, getTag(transaction)); case CONST.SEARCH.TABLE_COLUMNS.TOTAL_AMOUNT: - return getTransactionAmount(transaction, isExpenseReport(report), transaction.reportID === CONST.REPORT.UNREPORTED_REPORT_ID); + return getTransactionAmount(transaction, isExpenseReport(report), isFromTrackedExpense); case CONST.SEARCH.TABLE_COLUMNS.TOTAL: return Math.abs(transaction.convertedAmount ?? 0); case CONST.SEARCH.TABLE_COLUMNS.DESCRIPTION: @@ -14878,7 +14881,7 @@ function getTransactionSortValue( case CONST.SEARCH.TABLE_COLUMNS.TAX_CODE: return transaction.taxCode ?? ''; case CONST.SEARCH.TABLE_COLUMNS.TAX_AMOUNT: - return getTaxAmount(transaction, isExpenseReport(report), transaction.reportID === CONST.REPORT.UNREPORTED_REPORT_ID); + return getTaxAmount(transaction, isExpenseReport(report), isFromTrackedExpense); case CONST.SEARCH.TABLE_COLUMNS.TAX_RATE: return getTaxName(policy, transaction) ?? ''; case CONST.SEARCH.TABLE_COLUMNS.CARD: @@ -14892,7 +14895,7 @@ function getTransactionSortValue( if (!attendeesCount) { return 0; } - const totalAmount = getTransactionAmount(transaction, isExpenseReport(report), transaction.reportID === CONST.REPORT.UNREPORTED_REPORT_ID); + const totalAmount = getTransactionAmount(transaction, isExpenseReport(report), isFromTrackedExpense); return totalAmount / attendeesCount; } case CONST.SEARCH.TABLE_COLUMNS.SUBMITTER_USER_ID: From 0ae433f3bf2f035051108018799f59f537d3d337 Mon Sep 17 00:00:00 2001 From: "Bernhard Owen Josephus (via MelvinBot)" Date: Sat, 10 Oct 2026 07:10:45 +0000 Subject: [PATCH 3/3] Store edited tax with the opposite sign for unreported expenses Co-authored-by: Bernhard Owen Josephus --- .../TransactionUtils/getUpdatedTransaction.ts | 2 +- tests/unit/TransactionUtilsTest.ts | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/libs/TransactionUtils/getUpdatedTransaction.ts b/src/libs/TransactionUtils/getUpdatedTransaction.ts index 36c51421678b..52ff32fda585 100644 --- a/src/libs/TransactionUtils/getUpdatedTransaction.ts +++ b/src/libs/TransactionUtils/getUpdatedTransaction.ts @@ -381,7 +381,7 @@ function getUpdatedTransaction({ } if (Object.hasOwn(transactionChanges, 'taxAmount') && typeof transactionChanges.taxAmount === 'number') { - updatedTransaction.taxAmount = isFromExpenseReport ? -transactionChanges.taxAmount : transactionChanges.taxAmount; + updatedTransaction.taxAmount = isFromExpenseReport || isUnReportedExpense ? -transactionChanges.taxAmount : transactionChanges.taxAmount; } if (Object.hasOwn(transactionChanges, 'taxCode') && typeof transactionChanges.taxCode === 'string') { diff --git a/tests/unit/TransactionUtilsTest.ts b/tests/unit/TransactionUtilsTest.ts index 9c60fd3c708b..d9899eb41367 100644 --- a/tests/unit/TransactionUtilsTest.ts +++ b/tests/unit/TransactionUtilsTest.ts @@ -1576,6 +1576,25 @@ describe('TransactionUtils', () => { expect(updatedTransaction.taxAmount).toBe(-50); }); + it('should store an edited tax amount with the opposite sign for an unreported expense', () => { + // Given an unreported expense, which stores its amounts with the opposite sign like expense report expenses + const transaction = generateTransaction({reportID: CONST.REPORT.UNREPORTED_REPORT_ID, amount: -10000, taxAmount: -476}); + + // When the user edits the tax amount from the self DM, where isFromExpenseReport is false + const updatedTransaction = TransactionUtils.getUpdatedTransaction({ + transaction, + isFromExpenseReport: false, + transactionChanges: {taxAmount: 300}, + personalPolicyOutputCurrency: undefined, + getCurrencyDecimals: getCurrencyDecimalsLocal, + getCurrencySymbol: getCurrencySymbolLocal, + }); + + // Then the tax amount is stored with the opposite sign, so it reads back as the positive value the user entered + expect(updatedTransaction.taxAmount).toBe(-300); + expect(TransactionUtils.getTaxAmount(updatedTransaction, false, true)).toBe(300); + }); + it('should not update taxValue when taxCode is not in transactionChanges', () => { const transaction = generateTransaction({taxValue: '10%'});