Skip to content
Closed
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
17 changes: 10 additions & 7 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
taxAmount: getTaxAmount(transaction, isFromExpenseReport, isFromTrackedExpense),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Preserve positive tracked taxes during edits

When a positive unreported expense's amount, tax rate, or tax amount is edited, getUpdatedTransaction() still stores the positive transactionChanges.taxAmount unchanged because isFromExpenseReport is false for tracked expenses. This new tracked-expense normalization then negates that optimistic value, so the UI shows a negative tax and the update payload produced from getTransactionDetails() sends the negative amount to the API. The edit path must store tracked tax changes using the same opposite-sign convention before applying this normalization.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@MelvinBot can you explain the above in detail? The flow and the repro steps.

@MelvinBot MelvinBot Oct 10, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Codex is right. I reproduced it on this branch on web. If you type a tax amount on a positive unreported expense, the app shows it as negative. For example, entering 3.00 displays -$3.00.

Why: The PR now reads unreported tax with the opposite sign, like amount. But the edit path still saves an edited tax as typed. It doesn't apply the opposite sign:

Before this PR, Math.abs() hid the mismatch. Now the new read path in ReportUtils.ts:5651 flips the unflipped value.

Fix: At line 384, use isFromExpenseReport || isUnReportedExpense, matching line 180.

Repro steps (workspace with Taxes on and a 5% rate):

  1. In the workspace chat, create a manual expense for $100.00.
  2. Open it, select Report, then select Remove from report.
  3. Open the expense in the self DM. Tax amount shows $4.76.
  4. Select Tax amount, enter 3.00, and save.
  5. Tax amount shows -$3.00. It stays negative after you leave and reopen the expense.

Tax amount shows -3.00 after entering 3.00

Flow and more detail

Flow for a $100 unreported expense, tax set to 3.00:

  1. DynamicIOURequestStepTaxAmountPage.tsx:125 converts the input to 300 and calls updateMoneyRequestTaxAmount.
  2. UpdateMoneyRequest.ts:1960 sets isFromExpenseReport to false because the parent is the self DM.
  3. getUpdatedTransaction stores taxAmount: 300 in the optimistic transaction. An unreported expense should store -300.
  4. UpdateMoneyRequest.ts:1980-1986 builds the API params from getTransactionDetails(updatedTransaction). With this PR, getTaxAmount(..., false, true) returns -300. The UI shows -$3.00, and the request sends taxAmount: -300 (it sent 300 before this PR).

getUpdateTrackExpenseParams (UpdateMoneyRequest.ts:2600-2616) follows the same pattern with isFromExpenseReport: false.

What I tested:

  • Changing the tax rate on the unreported expense showed the correct $4.76 in my test. The rate page passes a positive tax to the same code, so I expected -$4.76. I suspect the server returns a recalculated tax that overwrites the optimistic value, but I didn't verify that.
  • On a negative unreported expense, the same gap would likely make an edited tax show as positive. I didn't test this.
  • After the fix, check the category-driven tax update at getUpdatedTransaction.ts:410-414 too.

Before the edit, the unreported expense showed tax $4.76. After changing the rate, it still showed $4.76:

Unreported expense before edit, tax 4.76 Tax rate edit keeps 4.76


view run · view recording

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@MelvinBot apply the fix

@MelvinBot MelvinBot Oct 10, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in 0ae433f. An edited tax amount on an unreported expense is now stored with the opposite sign, the same way amount is. Entering 3.00 now reads back as $3.00, not -$3.00.

TransactionUtilsTest, UpdateMoneyRequestTest and ReportUtilsTest pass (1,997 tests). ESLint, typecheck and cspell are clean. I couldn't run the repro steps on web because the test browser loaded a blank page.

Category-driven tax is not changed

I left the category path at getUpdatedTransaction.ts:413 as is. getCategoryTaxDetails calculates tax from Math.abs of the amount and stores it without a sign flip. Expense report expenses already work this way. I haven't checked whether the server response corrects that optimistic value.


view run · no recording available

taxCode: getTaxCode(transaction),
taxValue: transaction.taxValue,
currency: getCurrency(transaction),
Expand All @@ -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),
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}),
Expand Down Expand Up @@ -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);
Expand All @@ -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:
Expand All @@ -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));
return getTaxAmount(transaction, isExpenseReport(report), isFromTrackedExpense);
case CONST.SEARCH.TABLE_COLUMNS.TAX_RATE:
return getTaxName(policy, transaction) ?? '';
case CONST.SEARCH.TABLE_COLUMNS.CARD:
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/libs/TransactionUtils/getUpdatedTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
12 changes: 6 additions & 6 deletions src/libs/TransactionUtils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,9 +535,9 @@ function getAmount(transaction: OnyxInputOrEntry<Transaction>, 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);
}

Expand All @@ -549,16 +549,16 @@ function normalizeTaxAmountSign(rawAmount: number | undefined, isFromExpenseRepo
/**
* Return the tax amount field from the transaction.
*/
function getTaxAmount(transaction: OnyxInputOrEntry<Transaction>, isFromExpenseReport: boolean): number {
return normalizeTaxAmountSign(transaction?.taxAmount, isFromExpenseReport);
function getTaxAmount(transaction: OnyxInputOrEntry<Transaction>, 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<Transaction>, isFromExpenseReport: boolean): number {
return normalizeTaxAmountSign(transaction?.convertedTaxAmount, isFromExpenseReport);
function getConvertedTaxAmount(transaction: OnyxInputOrEntry<Transaction>, isFromExpenseReport: boolean, isFromTrackedExpense = false): number {
return normalizeTaxAmountSign(transaction?.convertedTaxAmount, isFromExpenseReport, isFromTrackedExpense);
}

/**
Expand Down
34 changes: 34 additions & 0 deletions tests/unit/ReportUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Report>({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);
Expand Down Expand Up @@ -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<Transaction>({
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', () => {
Expand Down
54 changes: 54 additions & 0 deletions tests/unit/TransactionUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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%'});

Expand Down Expand Up @@ -4135,6 +4154,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
Expand All @@ -4158,6 +4201,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});
Expand Down
Loading