Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
4f3b823
Fix: negate every total column when converting an IOU report to an ex…
MelvinBot Sep 7, 2026
995db0b
Fix: apply oxfmt formatting to satisfy the Oxfmt check
MelvinBot Sep 7, 2026
74b52f0
Address review feedback: lock in absent-column behavior, cover Policy…
MelvinBot Sep 10, 2026
7d735b6
Merge remote-tracking branch 'origin/main' into claude-negateIouTotal…
MelvinBot Sep 10, 2026
929a44b
Fix: pass the new `rules` argument to convertIOUReportToExpenseReport…
MelvinBot Sep 11, 2026
85107d5
Merge remote-tracking branch 'origin/main' into claude-negateIouTotal…
MelvinBot Sep 11, 2026
06516eb
Merge remote-tracking branch 'origin/main' into claude-negateIouTotal…
MelvinBot Sep 13, 2026
489a5ee
Merge remote-tracking branch 'origin/main' into claude-negateIouTotal…
MelvinBot Sep 17, 2026
f7714a6
Recompute unheldTotal on the paths that already rewrite the other tot…
MelvinBot Sep 20, 2026
4ad589a
Merge remote-tracking branch 'origin/main' into claude-negateIouTotal…
MelvinBot Sep 20, 2026
558021a
Pass isVendorMatchingBetaEnabled in the new unheldTotal tests
MelvinBot Sep 20, 2026
25415f0
Merge remote-tracking branch 'origin/main' into claude-negateIouTotal…
MelvinBot Sep 21, 2026
b42bb68
Only update unheldTotal when moving expenses if the report already ha…
MelvinBot Sep 29, 2026
1c54275
Merge remote-tracking branch 'origin/main' into claude-negateIouTotal…
MelvinBot Sep 29, 2026
703480e
Pass reportTransactions and delegateAccountID in the unheldTotal poli…
MelvinBot Sep 29, 2026
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
22 changes: 22 additions & 0 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4812,6 +4812,27 @@ function getMoneyRequestSpendBreakdown(report: OnyxInputOrEntry<Report>, searchR
};
}

/**
* Builds the total columns used when an IOU report is converted to an expense report.
*
* IOU reports store their totals positive while expense reports store them negative, so every total column has to
* flip together with `total`. `getMoneyRequestSpendBreakdown` reads `reimbursableTotal` in preference to `total`,
* so negating `total` alone leaves the stale positive siblings behind and renders the Total as negative.
* Absent columns are not added so they keep being derived from `total`.
*/
function getNegatedReportTotals(
report: OnyxEntry<Report>,
): Pick<Report, 'total' | 'reimbursableTotal' | 'nonReimbursableTotal' | 'unheldTotal' | 'unheldReimbursableTotal' | 'unheldNonReimbursableTotal'> {
return {
total: -(report?.total ?? 0),
...(report?.reimbursableTotal != null && {reimbursableTotal: -report.reimbursableTotal}),
...(report?.nonReimbursableTotal != null && {nonReimbursableTotal: -report.nonReimbursableTotal}),
...(report?.unheldTotal != null && {unheldTotal: -report.unheldTotal}),
...(report?.unheldReimbursableTotal != null && {unheldReimbursableTotal: -report.unheldReimbursableTotal}),
...(report?.unheldNonReimbursableTotal != null && {unheldNonReimbursableTotal: -report.unheldNonReimbursableTotal}),
};
}

function getBillableAndTaxTotal(report: OnyxEntry<Report>, transactions: Array<OnyxEntry<Transaction>>) {
if (!isExpenseReport(report)) {
return {
Expand Down Expand Up @@ -14592,6 +14613,7 @@ export {
getMissingPaymentMethodForQueuedPayment,
getLastVisibleMessage,
getMoneyRequestSpendBreakdown,
getNegatedReportTotals,
getNonHeldAndFullAmount,
getReimbursableTotal,
getUnheldReimbursableTotal,
Expand Down
5 changes: 3 additions & 2 deletions src/libs/actions/Policy/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4731,7 +4731,8 @@ function createWorkspaceFromIOUPayment({
// Next we need to convert the IOU report to Expense report.
// We need to change:
// - report type
// - change the sign of the report total
// - change the sign of every total column (`total` plus the reimbursable/non-reimbursable and unheld siblings),
// because the Total on screen is read from `reimbursableTotal` in preference to `total`
// - update its policyID and policyName
// - update the chatReportID to point to the new expense chat
// - recompute reportName so the header and the policy expense chat preview don't show the stale "IOU" name
Expand All @@ -4742,7 +4743,7 @@ function createWorkspaceFromIOUPayment({
policyID,
policyName: workspaceName,
type: CONST.REPORT.TYPE.EXPENSE,
total: -(iouReport?.total ?? 0),
...ReportUtils.getNegatedReportTotals(iouReport),
fieldList: newWorkspace.fieldList,
};

Expand Down
14 changes: 13 additions & 1 deletion src/libs/actions/Report/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ import {
getChildReportNotificationPreference,
getDefaultNotificationPreferenceForReport,
getLastVisibleMessage,
getNegatedReportTotals,
getNextApproverAccountID,
getOptimisticDataForAncestors,
getOriginalReportID,
Expand Down Expand Up @@ -7652,7 +7653,7 @@ function convertIOUReportToExpenseReport(
policyName: policy.name,
parentReportID: optimisticPolicyExpenseChatReportID,
type: CONST.REPORT.TYPE.EXPENSE,
total: -(iouReport?.total ?? 0),
...getNegatedReportTotals(iouReport),
};

const nextApproverAccountID = getNextApproverAccountID(iouReport, rules, true);
Expand Down Expand Up @@ -8311,6 +8312,7 @@ function buildOptimisticChangePolicyData({
// Only include transactions that match the destination currency (their amounts can be used directly)
if (sourceCurrency && destinationCurrency && sourceCurrency !== destinationCurrency) {
let newTotal = 0;
let newUnheldTotal = 0;
let newNonReimbursableTotal = 0;
let newUnheldNonReimbursableTotal = 0;
let newReimbursableTotal = 0;
Expand All @@ -8323,6 +8325,12 @@ function buildOptimisticChangePolicyData({
if (transactionCurrency === destinationCurrency) {
const transactionAmount = getAmount(transaction, true);
newTotal -= transactionAmount;
// `unheldTotal` is the signed sum of the transactions that are not on hold, so it has to be
// recomputed here too. `getNonHeldAndFullAmount` prefers it over the derived sum, so leaving the
// old-currency value behind would show the hold and Pay amounts in the source currency.
if (!isOnHold(transaction)) {
newUnheldTotal -= transactionAmount;
}
if (!transaction.reimbursable) {
newNonReimbursableTotal -= transactionAmount;
} else {
Expand All @@ -8343,6 +8351,7 @@ function buildOptimisticChangePolicyData({
value: {
currency: destinationCurrency,
total: newTotal,
unheldTotal: newUnheldTotal,
nonReimbursableTotal: newNonReimbursableTotal,
unheldNonReimbursableTotal: newUnheldNonReimbursableTotal,
reimbursableTotal: newReimbursableTotal,
Expand All @@ -8368,6 +8377,7 @@ function buildOptimisticChangePolicyData({
value: {
currency: report.currency,
total: report.total,
unheldTotal: report.unheldTotal,
nonReimbursableTotal: report.nonReimbursableTotal,
unheldNonReimbursableTotal: report.unheldNonReimbursableTotal,
reimbursableTotal: report.reimbursableTotal,
Expand Down Expand Up @@ -8801,6 +8811,7 @@ function mergeReports({
failureData: moveFailureData = [],
transactionIDToReportActionAndThreadData = {},
updatedReportTotals,
updatedReportUnheldTotals,
updatedReportTransactionCounts,
updatedReportNonReimbursableTotals,
updatedReportUnheldNonReimbursableTotals,
Expand Down Expand Up @@ -8914,6 +8925,7 @@ function mergeReports({
optimisticSnapshotData[`${ONYXKEYS.COLLECTION.REPORT}${destinationReportID}`] = {
...destinationReport,
total: updatedReportTotals?.[destinationReportID] ?? destinationReport.total,
unheldTotal: updatedReportUnheldTotals?.[destinationReportID] ?? destinationReport.unheldTotal,
transactionCount: updatedReportTransactionCounts?.[destinationReportID] ?? destinationReport.transactionCount,
reimbursableTotal: updatedReportReimbursableTotals?.[destinationReportID] ?? destinationReport.reimbursableTotal,
unheldReimbursableTotal: updatedReportUnheldReimbursableTotals?.[destinationReportID] ?? destinationReport.unheldReimbursableTotal,
Expand Down
38 changes: 38 additions & 0 deletions src/libs/actions/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,7 @@ function getChangeTransactionsReportOnyxData({

const transactionIDToReportActionAndThreadData: Record<string, TransactionThreadInfo> = {};
const updatedReportTotals: Record<string, number> = {};
const updatedReportUnheldTotals: Record<string, number> = {};
const updatedReportTransactionCounts: Record<string, number> = {};
const updatedReportNonReimbursableTotals: Record<string, number> = {};
const updatedReportUnheldNonReimbursableTotals: Record<string, number> = {};
Expand Down Expand Up @@ -1085,6 +1086,7 @@ function getChangeTransactionsReportOnyxData({
};
const clearAccumulatedReportTotals = (reportIDToUpdate: string) => {
delete updatedReportTotals[reportIDToUpdate];
delete updatedReportUnheldTotals[reportIDToUpdate];
delete updatedReportNonReimbursableTotals[reportIDToUpdate];
delete updatedReportUnheldNonReimbursableTotals[reportIDToUpdate];
delete updatedReportReimbursableTotals[reportIDToUpdate];
Expand Down Expand Up @@ -1453,6 +1455,7 @@ function getChangeTransactionsReportOnyxData({
if (willBeEmpty) {
clearStaleReportState(oldReportID);
updatedReportTotals[oldReportID] = 0;
updatedReportUnheldTotals[oldReportID] = 0;
updatedReportNonReimbursableTotals[oldReportID] = 0;
updatedReportUnheldNonReimbursableTotals[oldReportID] = 0;
updatedReportStateNums[oldReportID] = CONST.REPORT.STATE_NUM.OPEN;
Expand All @@ -1463,6 +1466,14 @@ function getChangeTransactionsReportOnyxData({
const currentTotal = updatedReportTotals[oldReportID] ?? oldReportTotal;
updatedReportTotals[oldReportID] = currentTotal + sourceTransactionAmount;

// `unheldTotal` only tracks transactions that are not on hold, so a held expense leaving the report
// does not change it. `getNonHeldAndFullAmount` reads it in preference to the derived sum, so it is
// only updated when already set; writing it for a report that lacks it would replace the derived sum.

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.

❌ CONSISTENCY-15 (docs)

Comments should read as plain, natural sentences. This comment uses a semicolon to join two independent clauses (...only updated when already set; writing it for a report that lacks it would replace the derived sum.), which the style guide asks to split into two separate sentences.

Split the clauses into two sentences, for example:

// `unheldTotal` only tracks transactions that are not on hold, so a held expense leaving the report
// does not change it. `getNonHeldAndFullAmount` reads it in preference to the derived sum, so it is
// only updated when already set. Writing it for a report that lacks it would replace the derived sum.

Reviewed at: 703480e | Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.

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.

meh

const currentUnheldTotal = updatedReportUnheldTotals[oldReportID] ?? oldReport?.unheldTotal;
if (typeof currentUnheldTotal === 'number') {
updatedReportUnheldTotals[oldReportID] = currentUnheldTotal + (!isOnHold(transaction) ? sourceTransactionAmount : 0);
}

const currentNonReimbursableTotal = updatedReportNonReimbursableTotals[oldReportID] ?? oldReport?.nonReimbursableTotal ?? 0;
updatedReportNonReimbursableTotals[oldReportID] = currentNonReimbursableTotal + (transaction?.reimbursable ? 0 : sourceTransactionAmount);

Expand Down Expand Up @@ -1492,6 +1503,11 @@ function getChangeTransactionsReportOnyxData({
const currentTotal = updatedReportTotals[targetReportID] ?? targetReport?.total ?? 0;
updatedReportTotals[targetReportID] = currentTotal - targetTransactionAmount;

const currentUnheldTotal = updatedReportUnheldTotals[targetReportID] ?? targetReport?.unheldTotal;
if (typeof currentUnheldTotal === 'number') {
updatedReportUnheldTotals[targetReportID] = currentUnheldTotal - (!isOnHold(transaction) ? targetTransactionAmount : 0);
}

const currentNonReimbursableTotal = updatedReportNonReimbursableTotals[targetReportID] ?? targetReport?.nonReimbursableTotal ?? 0;
updatedReportNonReimbursableTotals[targetReportID] = currentNonReimbursableTotal - (transactionReimbursable ? 0 : targetTransactionAmount);

Expand All @@ -1510,6 +1526,11 @@ function getChangeTransactionsReportOnyxData({
const currentTotal = updatedReportTotals[targetReportID] ?? targetReport?.total ?? 0;
updatedReportTotals[targetReportID] = currentTotal + convertedAmount;

const currentUnheldTotal = updatedReportUnheldTotals[targetReportID] ?? targetReport?.unheldTotal;
if (typeof currentUnheldTotal === 'number') {
updatedReportUnheldTotals[targetReportID] = currentUnheldTotal + (!isOnHold(transaction) ? convertedAmount : 0);
}

const currentNonReimbursableTotal = updatedReportNonReimbursableTotals[targetReportID] ?? targetReport?.nonReimbursableTotal ?? 0;
updatedReportNonReimbursableTotals[targetReportID] = currentNonReimbursableTotal + (transactionReimbursable ? 0 : convertedAmount);

Expand Down Expand Up @@ -1828,6 +1849,22 @@ function getChangeTransactionsReportOnyxData({
value: {total: reports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportIDToUpdate}`]?.total},
});
}
for (const [reportIDToUpdate, unheldTotal] of Object.entries(updatedReportUnheldTotals)) {
if (skippedReportIDsSet.has(reportIDToUpdate)) {
continue;
}
optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${reportIDToUpdate}`,
value: {unheldTotal},
});

failureData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${reportIDToUpdate}`,
value: {unheldTotal: reports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportIDToUpdate}`]?.unheldTotal},
});
}
for (const [reportIDToUpdate, transactionCount] of Object.entries(updatedReportTransactionCounts)) {
if (skippedReportIDsSet.has(reportIDToUpdate)) {
continue;
Expand Down Expand Up @@ -2070,6 +2107,7 @@ function getChangeTransactionsReportOnyxData({
transactionIDToReportActionAndThreadData,
transactionIDToUpdatedCustomUnitRateID,
updatedReportTotals,
updatedReportUnheldTotals,
updatedReportTransactionCounts,
updatedReportNonReimbursableTotals,
updatedReportUnheldNonReimbursableTotals,
Expand Down
30 changes: 30 additions & 0 deletions tests/actions/PolicyTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7769,6 +7769,11 @@ describe('actions/Policy', () => {
policyID: 'oldPolicyID',
currency: CONST.CURRENCY.USD,
total: 5000,
reimbursableTotal: 5000,
nonReimbursableTotal: 0,
unheldTotal: 5000,
unheldReimbursableTotal: 5000,
unheldNonReimbursableTotal: 0,
};

const transaction: Transaction = {
Expand Down Expand Up @@ -7815,6 +7820,31 @@ describe('actions/Policy', () => {

expect(optimisticTransaction?.amount).toBe(-5000);
expect(optimisticTransaction?.convertedAmount).toBe(-6000);

// The optimistic report merge has to flip every total column too, not just `total`, because the Total on
// screen is read from `reimbursableTotal` in preference to `total`.
const optimisticReport: OnyxEntry<Report> = await new Promise((resolve) => {
const connection = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`,
callback: (value) => {
Onyx.disconnect(connection);
resolve(value);
},
});
});

expect(optimisticReport?.type).toBe(CONST.REPORT.TYPE.EXPENSE);
expect(optimisticReport?.total).toBe(-5000);
expect(optimisticReport?.reimbursableTotal).toBe(-5000);
expect(optimisticReport?.unheldTotal).toBe(-5000);
expect(optimisticReport?.unheldReimbursableTotal).toBe(-5000);

// `toBe` is `Object.is`, so the already-zero columns have to be matched as -0.
expect(optimisticReport?.nonReimbursableTotal).toBe(-0);
expect(optimisticReport?.unheldNonReimbursableTotal).toBe(-0);

// And the Total rendered for the converted report is positive rather than -$50.00
expect(ReportUtils.getMoneyRequestSpendBreakdown(optimisticReport).totalDisplaySpend).toBe(5000);
});
});
});
Loading
Loading