Skip to content

Commit f2dca10

Browse files
authored
Merge pull request Expensify#78890 from marufsharifi/fix/offline-reimbursable-report-title
Fix report title not updating when reimbursable expense is changed offline
2 parents 852f154 + 119bf42 commit f2dca10

3 files changed

Lines changed: 36 additions & 1 deletion

File tree

src/components/ReportActionItem/MoneyRequestView.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ function MoneyRequestView({
721721
});
722722

723723
// eslint-disable-next-line @typescript-eslint/no-deprecated
724-
const reportNameToDisplay = isFromMergeTransaction ? (updatedTransaction?.reportName ?? translate('common.none')) : getReportName(parentReport) || parentReport?.reportName;
724+
const reportNameToDisplay = isFromMergeTransaction ? (updatedTransaction?.reportName ?? translate('common.none')) : (parentReport?.reportName ?? getReportName(parentReport));
725725
const shouldShowReport = !!parentReportID || (isFromMergeTransaction && !!reportNameToDisplay);
726726
const reportCopyValue = !canEditReport && reportNameToDisplay !== translate('common.none') ? reportNameToDisplay : undefined;
727727
const shouldShowCategoryAnalyzing = isCategoryBeingAnalyzed(updatedTransaction ?? transaction);

src/libs/ReportUtils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6615,13 +6615,19 @@ function populateOptimisticReportFormula(formula: string, report: OptimisticExpe
66156615

66166616
const createdDate = report.lastVisibleActionCreated ? new Date(report.lastVisibleActionCreated) : undefined;
66176617

6618+
const totalAmount = report.total !== undefined && !Number.isNaN(report.total) ? Math.abs(report.total) : 0;
6619+
const nonReimbursableTotal =
6620+
'nonReimbursableTotal' in report && report.nonReimbursableTotal !== undefined && !Number.isNaN(report.nonReimbursableTotal) ? Math.abs(report.nonReimbursableTotal) : 0;
6621+
const reimbursableAmount = totalAmount - nonReimbursableTotal;
6622+
66186623
const result = formula
66196624
// We don't translate because the server response is always in English
66206625
.replaceAll(/\{report:type\}/gi, 'Expense Report')
66216626
.replaceAll(/\{report:startdate\}/gi, createdDate ? format(createdDate, CONST.DATE.FNS_FORMAT_STRING) : '')
66226627
.replaceAll(/\{report:enddate\}/gi, createdDate ? format(createdDate, CONST.DATE.FNS_FORMAT_STRING) : '')
66236628
.replaceAll(/\{report:id\}/gi, getBase62ReportID(Number(report.reportID)))
66246629
.replaceAll(/\{report:total\}/gi, report.total !== undefined && !Number.isNaN(report.total) ? convertToDisplayString(Math.abs(report.total), report.currency).toString() : '')
6630+
.replaceAll(/\{report:reimbursable\}/gi, report.total !== undefined && !Number.isNaN(report.total) ? convertToDisplayString(reimbursableAmount, report.currency).toString() : '')
66256631
.replaceAll(/\{report:currency\}/gi, report.currency ?? '')
66266632
.replaceAll(/\{report:policyname\}/gi, policy?.name ?? '')
66276633
.replaceAll(/\{report:workspacename\}/gi, policy?.name ?? '')

src/libs/actions/IOU/index.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ import {
193193
isSettled,
194194
isTestTransactionReport,
195195
isTrackExpenseReport,
196+
populateOptimisticReportFormula,
196197
prepareOnboardingOnyxData,
197198
shouldCreateNewMoneyRequestReport as shouldCreateNewMoneyRequestReportReportUtils,
198199
shouldEnableNegative,
@@ -2890,6 +2891,22 @@ function getDeleteTrackExpenseInformation(
28902891
return {parameters, optimisticData, successData, failureData, shouldDeleteTransactionThread, chatReport};
28912892
}
28922893

2894+
/**
2895+
* Recalculates the report name using the policy's custom title formula.
2896+
* This is needed when report totals change (e.g., adding expenses or changing reimbursable status)
2897+
* to ensure the report title reflects the updated values like {report:reimbursable}.
2898+
*/
2899+
function recalculateOptimisticReportName(iouReport: OnyxTypes.Report, policy: OnyxEntry<OnyxTypes.Policy>): string | undefined {
2900+
if (!policy?.fieldList?.[CONST.POLICY.FIELDS.FIELD_LIST_TITLE]) {
2901+
return undefined;
2902+
}
2903+
const titleFormula = policy.fieldList[CONST.POLICY.FIELDS.FIELD_LIST_TITLE]?.defaultValue ?? '';
2904+
if (!titleFormula) {
2905+
return undefined;
2906+
}
2907+
return populateOptimisticReportFormula(titleFormula, iouReport as Parameters<typeof populateOptimisticReportFormula>[1], policy);
2908+
}
2909+
28932910
/**
28942911
* 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
28952912
* it creates optimistic versions of them and uses those instead
@@ -3011,6 +3028,12 @@ function getMoneyRequestInformation(moneyRequestInformation: MoneyRequestInforma
30113028
iouReport.nonReimbursableTotal = (iouReport.nonReimbursableTotal ?? 0) - amount;
30123029
}
30133030
}
3031+
3032+
// Recalculate reportName to reflect updated totals
3033+
const updatedReportName = recalculateOptimisticReportName(iouReport, policy);
3034+
if (updatedReportName) {
3035+
iouReport.reportName = updatedReportName;
3036+
}
30143037
}
30153038
if (typeof iouReport.unheldTotal === 'number') {
30163039
// Use newReportTotal in scenarios where the total is based on more than just the current transaction amount, and we need to override it manually
@@ -4075,6 +4098,12 @@ function getUpdateMoneyRequestParams(params: GetUpdateMoneyRequestParamsType): U
40754098
updatedMoneyRequestReport.unheldNonReimbursableTotal += updatedTransaction.reimbursable ? -updatedTransaction.amount : updatedTransaction.amount;
40764099
}
40774100
}
4101+
4102+
// Recalculate reportName after all totals are updated
4103+
const updatedReportName = recalculateOptimisticReportName(updatedMoneyRequestReport, policy);
4104+
if (updatedReportName) {
4105+
updatedMoneyRequestReport.reportName = updatedReportName;
4106+
}
40784107
} else {
40794108
updatedMoneyRequestReport = updateIOUOwnerAndTotal(
40804109
iouReport,

0 commit comments

Comments
 (0)