diff --git a/src/libs/SearchUIUtils.ts b/src/libs/SearchUIUtils.ts index 1e61bc550bc5..371e4d0d9a2e 100644 --- a/src/libs/SearchUIUtils.ts +++ b/src/libs/SearchUIUtils.ts @@ -268,6 +268,7 @@ import { import getFormattedPostedDate from './TransactionUtils/getFormattedPostedDate'; import shouldShowTransactionPostedYear from './TransactionUtils/shouldShowTransactionPostedYear'; import {isInvalidMerchantValue} from './ValidationUtils'; +import {filterReceiptViolations} from './Violations/ViolationsUtils'; type ColumnSortMapping = Partial>; type ColumnVisibility = Partial>; @@ -5620,7 +5621,9 @@ function collectViolationNamesForTransaction(violationNames: Set, violat return; } - for (const violation of transactionViolations) { + // Search and Insights apply the receipt rule within each snapshot, so an expense whose earlier snapshot has only + // receipt required still shows it here. + for (const violation of filterReceiptViolations(transactionViolations)) { // Search, Insights and the CSV export skip the cash expense notice that only Classic records, so this column skips it too. if (violation.name && violation.name !== CONST.VIOLATIONS.CASH_EXPENSE_WITH_NO_RECEIPT) { violationNames.add(violation.name); @@ -5633,11 +5636,6 @@ function formatTransactionViolationNames(violationNames: Set, translate? return undefined; } - // Match ViolationMessages / submitter surfaces: only one of these receipt rules can apply. - if (violationNames.has(CONST.VIOLATIONS.ITEMIZED_RECEIPT_REQUIRED)) { - violationNames.delete(CONST.VIOLATIONS.RECEIPT_REQUIRED); - } - const names = Array.from(violationNames); if (!translate) { return names.join(', '); @@ -5674,8 +5672,8 @@ function collectApprovedViolationNamesForTransaction(violationNames: Set * dedupes by violation name, and returns a comma-separated display string. * When `translate` is provided, violation identifiers are converted to localized short labels. * - * Itemized receipt required supersedes receipt required (same rule as `filterReceiptViolations`), - * so both are never shown together for a single expense. + * Itemized receipt required supersedes receipt required within each submit action, + * so both are shown only when they come from different submit actions. */ function getSubmittedViolationsForTransaction(reportActions: OnyxTypes.ReportAction[] | undefined, transactionID: string | undefined, translate?: LocalizedTranslate): string | undefined { if (!reportActions?.length || !transactionID) { @@ -5693,8 +5691,8 @@ function getSubmittedViolationsForTransaction(reportActions: OnyxTypes.ReportAct * dedupes by violation name, and returns a comma-separated display string. * When `translate` is provided, violation identifiers are converted to localized short labels. * - * Itemized receipt required supersedes receipt required (same rule as `filterReceiptViolations`), - * so both are never shown together for a single expense. + * Itemized receipt required supersedes receipt required within each approved or forwarded action, + * so both are shown only when they come from different actions. */ function getApprovedViolationsForTransaction(reportActions: OnyxTypes.ReportAction[] | undefined, transactionID: string | undefined, translate?: LocalizedTranslate): string | undefined { if (!reportActions?.length || !transactionID) { diff --git a/src/libs/Violations/ViolationsUtils.ts b/src/libs/Violations/ViolationsUtils.ts index 24ed0cced0fb..2710fc110909 100644 --- a/src/libs/Violations/ViolationsUtils.ts +++ b/src/libs/Violations/ViolationsUtils.ts @@ -81,7 +81,7 @@ type ViolationTranslationParams = { * Filters out receiptRequired violation when itemizedReceiptRequired is also present. * Itemized receipt requirement supersedes regular receipt requirement. */ -function filterReceiptViolations(violations: TransactionViolation[]): TransactionViolation[] { +function filterReceiptViolations(violations: TViolation[]): TViolation[] { const hasItemizedReceiptViolation = violations.some((v) => v.name === CONST.VIOLATIONS.ITEMIZED_RECEIPT_REQUIRED); const hasReceiptRequiredViolation = violations.some((v) => v.name === CONST.VIOLATIONS.RECEIPT_REQUIRED); diff --git a/tests/unit/Search/SearchUIUtilsTest.ts b/tests/unit/Search/SearchUIUtilsTest.ts index 3b9ffb0f4b13..ed198aa997cc 100644 --- a/tests/unit/Search/SearchUIUtilsTest.ts +++ b/tests/unit/Search/SearchUIUtilsTest.ts @@ -16246,6 +16246,34 @@ describe('SearchUIUtils', () => { ); }); + test('keeps receiptRequired from an earlier submit action that had no itemizedReceiptRequired', () => { + // Given a report submitted with only receiptRequired, then resubmitted with both receipt violations + const firstSubmit = createSubmittedAction( + CONST.REPORT.ACTIONS.TYPE.SUBMITTED, + { + transactions: { + [transactionIDForViolations]: [{name: CONST.VIOLATIONS.RECEIPT_REQUIRED}], + }, + }, + 'submit-1', + ); + const secondSubmit = createSubmittedAction( + CONST.REPORT.ACTIONS.TYPE.SUBMITTED, + { + transactions: { + [transactionIDForViolations]: [{name: CONST.VIOLATIONS.RECEIPT_REQUIRED}, {name: CONST.VIOLATIONS.ITEMIZED_RECEIPT_REQUIRED}], + }, + }, + 'submit-2', + ); + + // When the submitted violations column is built for the expense + const violations = SearchUIUtils.getSubmittedViolationsForTransaction([firstSubmit, secondSubmit], transactionIDForViolations, translateLocal); + + // Then both are listed, because the first submission still matches violation:receiptRequired in Search and Insights + expect(violations).toBe(`${translateLocal('violations.shortName.receiptRequired')}, ${translateLocal('violations.shortName.itemizedReceiptRequired')}`); + }); + test('keeps receiptRequired when itemizedReceiptRequired is not present', () => { const submitAction = createSubmittedAction(CONST.REPORT.ACTIONS.TYPE.SUBMITTED, { transactions: { @@ -16435,6 +16463,34 @@ describe('SearchUIUtils', () => { ); }); + test('keeps receiptRequired from an earlier approval action that had no itemizedReceiptRequired', () => { + // Given a report forwarded with only receiptRequired, then approved with both receipt violations + const forwardedAction = createApprovalAction( + CONST.REPORT.ACTIONS.TYPE.FORWARDED, + { + transactions: { + [transactionIDForViolations]: [{name: CONST.VIOLATIONS.RECEIPT_REQUIRED}], + }, + }, + 'forward-1', + ); + const approvedAction = createApprovalAction( + CONST.REPORT.ACTIONS.TYPE.APPROVED, + { + transactions: { + [transactionIDForViolations]: [{name: CONST.VIOLATIONS.RECEIPT_REQUIRED}, {name: CONST.VIOLATIONS.ITEMIZED_RECEIPT_REQUIRED}], + }, + }, + 'approved-1', + ); + + // When the approved violations column is built for the expense + const violations = SearchUIUtils.getApprovedViolationsForTransaction([forwardedAction, approvedAction], transactionIDForViolations, translateLocal); + + // Then both are listed, because the forwarded snapshot still matches violation:receiptRequired in Search and Insights + expect(violations).toBe(`${translateLocal('violations.shortName.receiptRequired')}, ${translateLocal('violations.shortName.itemizedReceiptRequired')}`); + }); + test('falls back to the raw identifier when no short-name translation exists', () => { const unknownViolationName = 'unknownApprovedViolation'; const forwardedAction = createApprovalAction(CONST.REPORT.ACTIONS.TYPE.FORWARDED, {