Skip to content
Merged
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
30 changes: 25 additions & 5 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5497,28 +5497,48 @@ function getMoneyRequestSpendBreakdown(report: OnyxInputOrEntry<Report>, searchR
};
}

/**
* Whether the report's total is loaded and has no pending optimistic update, so its spend breakdown can be trusted.
*/
function isReportTotalLoadedAndSettled(report: OnyxInputOrEntry<Report>): boolean {
const isTotalLoaded = report?.total !== undefined || report?.reimbursableTotal !== undefined;
return isTotalLoaded && !isReportTotalPending(report);
}

/**
* Whether an expense report has nothing to reimburse and every expense on it has a settled value, e.g. its reimbursable expenses cancel out.
* Such a report can only be marked as paid. Fails closed while the report total is not loaded or is pending, or while any expense is still
* scanning, failed to scan or is a pending card charge, because its real reimbursable spend is not known yet.
*/
function hasSettledZeroReimbursableSpend(spendBreakdown: SpendBreakdown, report: OnyxInputOrEntry<Report>, transactionsParam?: Transaction[]): boolean {
if (!isExpenseReport(report) || spendBreakdown.reimbursableSpend !== 0) {
return false;
}
const isTotalLoaded = report?.total !== undefined || report?.reimbursableTotal !== undefined;
if (!isTotalLoaded || isReportTotalPending(report)) {
if (!isExpenseReport(report) || spendBreakdown.reimbursableSpend !== 0 || !isReportTotalLoadedAndSettled(report)) {
return false;
}
const expenses = resolveReportTransactions(report?.reportID, transactionsParam).filter((transaction) => !isTransactionPendingDelete(transaction));
return expenses.length > 0 && !expenses.some((transaction) => isExpenseValueUnsettled(transaction, report ?? undefined, isScanningTransaction));
}

/**
* Whether an expense report's loaded, non-pending totals show non-reimbursable spend and no reimbursable spend.
* Used when the report's expenses haven't been fetched yet, so the totals are the only signal available.
*/
function hasOnlyNonReimbursableTotal(report: OnyxInputOrEntry<Report>): boolean {
if (!isExpenseReport(report) || !isReportTotalLoadedAndSettled(report)) {
return false;
}
const {reimbursableSpend, nonReimbursableSpend} = getMoneyRequestSpendBreakdown(report);
return reimbursableSpend === 0 && nonReimbursableSpend !== 0;
}

/**
* Whether paying the report is optional because nothing is owed on it: every expense is non-reimbursable, or its reimbursable spend is settled at $0.
* These reports can still be marked as paid from the report, but aren't surfaced as something to pay (LHN badge, Search row action, Pay to-do).
*/
function isPayOptional(report: OnyxInputOrEntry<Report>, transactionsParam?: Transaction[]): boolean {
const transactions = resolveReportTransactions(report?.reportID, transactionsParam).filter((transaction) => !isTransactionPendingDelete(transaction));
if (transactions.length === 0) {
return hasOnlyNonReimbursableTotal(report);
Comment on lines +5539 to +5540

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 Keep unsettled reimbursements in the pay queue

When an approved report combines non-reimbursable spend with an unloaded reimbursable SmartScan expense whose amount is not known yet, the report totals still look like reimbursableTotal: 0 with a nonzero non-reimbursable total. This empty-transaction branch therefore marks the report pay-optional and removes it from the Pay to-do, LHN badge, and Search Pay action; once its transactions load, the existing unsettled-expense check reverses that decision, so a payer may not discover the reimbursement unless they independently open or fetch the report. Avoid trusting this totals-only classification until the absence of scanning or otherwise unsettled reimbursable expenses can be established.

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 please review this suggestion

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.

The suggestion identifies a real classification gap, but the P1 impact is not yet demonstrated.

Recommendation: Validate approval and scan-completion recovery for the mixed case. If reachable, preserve fail-closed behavior until complete transactions or an authoritative report-level signal rules out unsettled reimbursements. Add regression coverage for unloaded → loaded → scan-completed transitions; the new tests do not cover them.

No code changes made or runtime tests run for this review.


view run

}
return hasOnlyNonReimbursableTransactions(report?.reportID, transactionsParam) || hasSettledZeroReimbursableSpend(getMoneyRequestSpendBreakdown(report), report, transactionsParam);
}

Expand Down
33 changes: 33 additions & 0 deletions tests/unit/ReportUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30296,6 +30296,39 @@ describe('isPayOptional', () => {
// Then it is not, since the report can't be paid at all
expect(isPayOptional(expenseReport, [])).toBe(false);
});

it('returns true for a non-reimbursable-only report whose expenses have not loaded yet', () => {
// Given an approved report whose totals show only non-reimbursable spend, but whose expenses haven't been fetched yet
const report: Report = {...expenseReport, total: -5000, nonReimbursableTotal: -5000, reimbursableTotal: 0};

// When checking whether paying it is optional before its expenses load
// Then it is, so it stays out of Ready to pay instead of disappearing once the report is opened
expect(isPayOptional(report, [])).toBe(true);
});

it('returns false for a report with reimbursable spend whose expenses have not loaded yet', () => {
// Given an approved report whose totals show reimbursable spend, but whose expenses haven't been fetched yet
const report: Report = {...expenseReport, total: -5000, nonReimbursableTotal: -2000, reimbursableTotal: -3000};

// When checking whether paying it is optional before its expenses load
// Then it is not, since money is owed on it
expect(isPayOptional(report, [])).toBe(false);
});

it('returns false for a non-reimbursable-only report whose expenses have not loaded yet and whose total is pending', () => {
// Given a report whose totals show only non-reimbursable spend, but an optimistic update to its total is still pending
const report: Report = {
...expenseReport,
total: -5000,
nonReimbursableTotal: -5000,
reimbursableTotal: 0,
pendingFields: {total: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE},
};

// When checking whether paying it is optional before its expenses load
// Then it is not, since the pending total can't be trusted yet
expect(isPayOptional(report, [])).toBe(false);
});
});

describe('hasOutstandingChildRequest', () => {
Expand Down
29 changes: 29 additions & 0 deletions tests/unit/TodosUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,35 @@ describe('TodosUtils', () => {
expect(result.reportsToPay.map((report) => report.reportID)).toEqual(['pay_export_failed']);
});

it('excludes a non-reimbursable-only report from the pay bucket before its transactions load', () => {
// Given an approved report whose totals show only non-reimbursable spend, and whose transactions haven't been fetched yet
const nonReimbursableReport = createMockReport('pay_non_reimbursable_unloaded', {
stateNum: CONST.REPORT.STATE_NUM.APPROVED,
statusNum: CONST.REPORT.STATUS_NUM.APPROVED,
ownerAccountID: OTHER_USER_ACCOUNT_ID,
managerID: CURRENT_USER_ACCOUNT_ID,
total: -100,
nonReimbursableTotal: -100,
reimbursableTotal: 0,
});
const policy = createMockPolicy(POLICY_ID, {
role: CONST.POLICY.ROLE.ADMIN,
ownerAccountID: CURRENT_USER_ACCOUNT_ID,
reimbursementChoice: CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_YES,
});

// When building the to-do buckets
const result = createTodosReportsAndTransactions({
...baseParams,
allReports: toReportsCollection([nonReimbursableReport]),
allTransactions: undefined,
allPolicies: toPoliciesCollection([policy]),
});

// Then it is kept out of the pay bucket, so it doesn't drop out of Ready to pay only once the report is opened
expect(result.reportsToPay).toEqual([]);
});

it('excludes a report whose expenses are all pending card transactions', () => {
const pendingOverride: Partial<Transaction> = {status: CONST.TRANSACTION.STATUS.PENDING, bank: CONST.EXPENSIFY_CARD.BANK};
const submitReport = createMockReport('pending_submit', {
Expand Down
Loading