From 34a38cd9f78f7c9fe6b6b8edbbe5d8b1789ae014 Mon Sep 17 00:00:00 2001 From: Maruf Sharifi Date: Tue, 6 Jan 2026 12:57:22 +0430 Subject: [PATCH 1/2] Fix report title update when reimbursable expense changes offline --- .../ReportActionItem/MoneyRequestView.tsx | 2 +- src/libs/ReportUtils.ts | 6 ++++ src/libs/actions/IOU/index.ts | 29 +++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/components/ReportActionItem/MoneyRequestView.tsx b/src/components/ReportActionItem/MoneyRequestView.tsx index ea2f74e06358..432fa62a66f7 100644 --- a/src/components/ReportActionItem/MoneyRequestView.tsx +++ b/src/components/ReportActionItem/MoneyRequestView.tsx @@ -721,7 +721,7 @@ function MoneyRequestView({ }); // eslint-disable-next-line @typescript-eslint/no-deprecated - const reportNameToDisplay = isFromMergeTransaction ? (updatedTransaction?.reportName ?? translate('common.none')) : getReportName(parentReport) || parentReport?.reportName; + const reportNameToDisplay = isFromMergeTransaction ? (updatedTransaction?.reportName ?? translate('common.none')) : (parentReport?.reportName ?? getReportName(parentReport)); const shouldShowReport = !!parentReportID || (isFromMergeTransaction && !!reportNameToDisplay); const reportCopyValue = !canEditReport && reportNameToDisplay !== translate('common.none') ? reportNameToDisplay : undefined; const shouldShowCategoryAnalyzing = isCategoryBeingAnalyzed(updatedTransaction ?? transaction); diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 22b8ec0f97eb..eec091e4f7fb 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -6612,6 +6612,11 @@ function populateOptimisticReportFormula(formula: string, report: OptimisticExpe const createdDate = report.lastVisibleActionCreated ? new Date(report.lastVisibleActionCreated) : undefined; + const totalAmount = report.total !== undefined && !Number.isNaN(report.total) ? Math.abs(report.total) : 0; + const nonReimbursableTotal = + 'nonReimbursableTotal' in report && report.nonReimbursableTotal !== undefined && !Number.isNaN(report.nonReimbursableTotal) ? Math.abs(report.nonReimbursableTotal) : 0; + const reimbursableAmount = totalAmount - nonReimbursableTotal; + const result = formula // We don't translate because the server response is always in English .replaceAll(/\{report:type\}/gi, 'Expense Report') @@ -6619,6 +6624,7 @@ function populateOptimisticReportFormula(formula: string, report: OptimisticExpe .replaceAll(/\{report:enddate\}/gi, createdDate ? format(createdDate, CONST.DATE.FNS_FORMAT_STRING) : '') .replaceAll(/\{report:id\}/gi, getBase62ReportID(Number(report.reportID))) .replaceAll(/\{report:total\}/gi, report.total !== undefined && !Number.isNaN(report.total) ? convertToDisplayString(Math.abs(report.total), report.currency).toString() : '') + .replaceAll(/\{report:reimbursable\}/gi, report.total !== undefined && !Number.isNaN(report.total) ? convertToDisplayString(reimbursableAmount, report.currency).toString() : '') .replaceAll(/\{report:currency\}/gi, report.currency ?? '') .replaceAll(/\{report:policyname\}/gi, policy?.name ?? '') .replaceAll(/\{report:workspacename\}/gi, policy?.name ?? '') diff --git a/src/libs/actions/IOU/index.ts b/src/libs/actions/IOU/index.ts index 6cc6cfdbe687..412b3ae5d664 100644 --- a/src/libs/actions/IOU/index.ts +++ b/src/libs/actions/IOU/index.ts @@ -196,6 +196,7 @@ import { shouldEnableNegative, updateReportPreview, } from '@libs/ReportUtils'; +import {populateOptimisticReportFormula} from '@libs/ReportUtils'; import {getCurrentSearchQueryJSON} from '@libs/SearchQueryUtils'; import {getSuggestedSearches} from '@libs/SearchUIUtils'; import playSound, {SOUNDS} from '@libs/Sound'; @@ -2879,6 +2880,22 @@ function getDeleteTrackExpenseInformation( return {parameters, optimisticData, successData, failureData, shouldDeleteTransactionThread, chatReport}; } +/** + * Recalculates the report name using the policy's custom title formula. + * This is needed when report totals change (e.g., adding expenses or changing reimbursable status) + * to ensure the report title reflects the updated values like {report:reimbursable}. + */ +function recalculateOptimisticReportName(iouReport: OnyxTypes.Report, policy: OnyxEntry): string | undefined { + if (!policy?.fieldList?.[CONST.POLICY.FIELDS.FIELD_LIST_TITLE]) { + return undefined; + } + const titleFormula = policy.fieldList[CONST.POLICY.FIELDS.FIELD_LIST_TITLE]?.defaultValue ?? ''; + if (!titleFormula) { + return undefined; + } + return populateOptimisticReportFormula(titleFormula, iouReport as Parameters[1], policy); +} + /** * Gathers all the data needed to submit an expense. It attempts to find existing reports, iouReports, and receipts. If it doesn't find them, then * it creates optimistic versions of them and uses those instead @@ -3000,6 +3017,12 @@ function getMoneyRequestInformation(moneyRequestInformation: MoneyRequestInforma iouReport.nonReimbursableTotal = (iouReport.nonReimbursableTotal ?? 0) - amount; } } + + // Recalculate reportName to reflect updated totals + const updatedReportName = recalculateOptimisticReportName(iouReport, policy); + if (updatedReportName) { + iouReport.reportName = updatedReportName; + } } if (typeof iouReport.unheldTotal === 'number') { // Use newReportTotal in scenarios where the total is based on more than just the current transaction amount, and we need to override it manually @@ -4056,6 +4079,12 @@ function getUpdateMoneyRequestParams(params: GetUpdateMoneyRequestParamsType): U updatedMoneyRequestReport.unheldNonReimbursableTotal += updatedTransaction.reimbursable ? -updatedTransaction.amount : updatedTransaction.amount; } } + + // Recalculate reportName after all totals are updated + const updatedReportName = recalculateOptimisticReportName(updatedMoneyRequestReport as OnyxTypes.Report, policy); + if (updatedReportName) { + updatedMoneyRequestReport.reportName = updatedReportName; + } } else { updatedMoneyRequestReport = updateIOUOwnerAndTotal( iouReport, From 119bf4277537a91e3b33de1d7960104e88cf4118 Mon Sep 17 00:00:00 2001 From: Maruf Sharifi Date: Tue, 6 Jan 2026 14:43:03 +0430 Subject: [PATCH 2/2] fix eslint error --- src/libs/actions/IOU/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/actions/IOU/index.ts b/src/libs/actions/IOU/index.ts index 412b3ae5d664..6bb56894b22a 100644 --- a/src/libs/actions/IOU/index.ts +++ b/src/libs/actions/IOU/index.ts @@ -191,12 +191,12 @@ import { isSettled, isTestTransactionReport, isTrackExpenseReport, + populateOptimisticReportFormula, prepareOnboardingOnyxData, shouldCreateNewMoneyRequestReport as shouldCreateNewMoneyRequestReportReportUtils, shouldEnableNegative, updateReportPreview, } from '@libs/ReportUtils'; -import {populateOptimisticReportFormula} from '@libs/ReportUtils'; import {getCurrentSearchQueryJSON} from '@libs/SearchQueryUtils'; import {getSuggestedSearches} from '@libs/SearchUIUtils'; import playSound, {SOUNDS} from '@libs/Sound'; @@ -4081,7 +4081,7 @@ function getUpdateMoneyRequestParams(params: GetUpdateMoneyRequestParamsType): U } // Recalculate reportName after all totals are updated - const updatedReportName = recalculateOptimisticReportName(updatedMoneyRequestReport as OnyxTypes.Report, policy); + const updatedReportName = recalculateOptimisticReportName(updatedMoneyRequestReport, policy); if (updatedReportName) { updatedMoneyRequestReport.reportName = updatedReportName; }