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
18 changes: 8 additions & 10 deletions src/libs/SearchUIUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> = Partial<Record<SearchColumnType, keyof T | null>>;
type ColumnVisibility = Partial<Record<SearchColumnType, boolean>>;
Expand Down Expand Up @@ -5620,7 +5621,9 @@ function collectViolationNamesForTransaction(violationNames: Set<string>, 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);
Expand All @@ -5633,11 +5636,6 @@ function formatTransactionViolationNames(violationNames: Set<string>, 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(', ');
Expand Down Expand Up @@ -5674,8 +5672,8 @@ function collectApprovedViolationNamesForTransaction(violationNames: Set<string>
* 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) {
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/libs/Violations/ViolationsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TViolation extends {name: string}>(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);

Expand Down
56 changes: 56 additions & 0 deletions tests/unit/Search/SearchUIUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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, {
Expand Down
Loading