diff --git a/src/components/MoneyRequestConfirmationListFooter.tsx b/src/components/MoneyRequestConfirmationListFooter.tsx index 0a94ef9fa6c1..92a507a280aa 100644 --- a/src/components/MoneyRequestConfirmationListFooter.tsx +++ b/src/components/MoneyRequestConfirmationListFooter.tsx @@ -283,8 +283,8 @@ function MoneyRequestConfirmationListFooter({ const transactionReport = !!transaction?.reportID && Object.values(allReports ?? {}).find((report) => report?.reportID === transaction.reportID); const policyID = selectedParticipants?.at(0)?.policyID; const reportOwnerAccountID = selectedParticipants?.at(0)?.ownerAccountID; - const shouldUseTransactionReport = !!transactionReport && isReportOutstanding(transactionReport, policyID); - const firstOutstandingReport = getOutstandingReportsForUser(policyID, reportOwnerAccountID, allReports ?? {}).at(0); + const shouldUseTransactionReport = !!transactionReport && isReportOutstanding(transactionReport, policyID, undefined, false); + const firstOutstandingReport = getOutstandingReportsForUser(policyID, reportOwnerAccountID, allReports ?? {}, undefined, false).at(0); let reportName: string | undefined; if (shouldUseTransactionReport) { reportName = transactionReport.reportName; diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 0c1bf2758062..08c51b5a9c44 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -97,6 +97,7 @@ import getStateFromPath from './Navigation/helpers/getStateFromPath'; import {isFullScreenName} from './Navigation/helpers/isNavigatorName'; import {linkingConfig} from './Navigation/linkingConfig'; import Navigation, {navigationRef} from './Navigation/Navigation'; +import type {MoneyRequestNavigatorParamList, ReportsSplitNavigatorParamList} from './Navigation/types'; import {rand64} from './NumberUtils'; import Parser from './Parser'; import {getParsedMessageWithShortMentions} from './ParsingUtils'; @@ -9974,18 +9975,24 @@ function isReportOutstanding( iouReport: OnyxInputOrEntry, policyID: string | undefined, reportNameValuePairs: OnyxCollection = allReportNameValuePair, + allowSubmitted = true, ): boolean { if (!iouReport || isEmptyObject(iouReport)) { return false; } + const currentRoute = navigationRef.getCurrentRoute(); + const params = currentRoute?.params as MoneyRequestNavigatorParamList[typeof SCREENS.MONEY_REQUEST.STEP_CONFIRMATION] | ReportsSplitNavigatorParamList[typeof SCREENS.REPORT]; + const activeReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${params?.reportID}`]; + const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`]; const reportNameValuePair = reportNameValuePairs?.[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${iouReport.reportID}`]; + const shouldAllowSubmittedReport = allowSubmitted || isInstantSubmitEnabled(policy) || isProcessingReport(activeReport); return ( isExpenseReport(iouReport) && iouReport?.stateNum !== undefined && iouReport?.statusNum !== undefined && iouReport?.policyID === policyID && - iouReport?.stateNum <= CONST.REPORT.STATE_NUM.SUBMITTED && - iouReport?.statusNum <= CONST.REPORT.STATUS_NUM.SUBMITTED && + (shouldAllowSubmittedReport ? iouReport?.stateNum <= CONST.REPORT.STATE_NUM.SUBMITTED : iouReport?.stateNum < CONST.REPORT.STATE_NUM.SUBMITTED) && + (shouldAllowSubmittedReport ? iouReport?.statusNum <= CONST.REPORT.STATE_NUM.SUBMITTED : iouReport?.statusNum < CONST.REPORT.STATE_NUM.SUBMITTED) && !hasForwardedAction(iouReport.reportID) && !isArchivedReport(reportNameValuePair) ); @@ -10003,6 +10010,7 @@ function getOutstandingReportsForUser( reportOwnerAccountID: number | undefined, reports: OnyxCollection = allReports, reportNameValuePairs: OnyxCollection = allReportNameValuePair, + allowSubmitted = true, ): Array> { if (!reports) { return []; @@ -10011,7 +10019,7 @@ function getOutstandingReportsForUser( .filter( (report) => report?.pendingFields?.preview !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE && - isReportOutstanding(report, policyID, reportNameValuePairs) && + isReportOutstanding(report, policyID, reportNameValuePairs, allowSubmitted) && report?.ownerAccountID === reportOwnerAccountID, ) .sort((a, b) => localeCompare(a?.reportName?.toLowerCase() ?? '', b?.reportName?.toLowerCase() ?? '')); diff --git a/src/pages/Search/SearchTransactionsChangeReport.tsx b/src/pages/Search/SearchTransactionsChangeReport.tsx index 757510af9ace..eda8e985c3f3 100644 --- a/src/pages/Search/SearchTransactionsChangeReport.tsx +++ b/src/pages/Search/SearchTransactionsChangeReport.tsx @@ -45,6 +45,7 @@ function SearchTransactionsChangeReport() { backTo={undefined} transactionsReports={transactionsReports} selectReport={selectReport} + isEditing /> ); } diff --git a/src/pages/iou/request/step/IOURequestEditReport.tsx b/src/pages/iou/request/step/IOURequestEditReport.tsx index 9a6c879276c9..c3e3b5b15ff8 100644 --- a/src/pages/iou/request/step/IOURequestEditReport.tsx +++ b/src/pages/iou/request/step/IOURequestEditReport.tsx @@ -4,6 +4,7 @@ import type {ListItem} from '@components/SelectionList/types'; import useOnyx from '@hooks/useOnyx'; import {changeTransactionsReport} from '@libs/actions/Transaction'; import Navigation from '@libs/Navigation/Navigation'; +import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type SCREENS from '@src/SCREENS'; import IOURequestEditReportCommon from './IOURequestEditReportCommon'; @@ -18,7 +19,7 @@ type TransactionGroupListItem = ListItem & { type IOURequestEditReportProps = WithWritableReportOrNotFoundProps; function IOURequestEditReport({route}: IOURequestEditReportProps) { - const {backTo, reportID} = route.params; + const {backTo, reportID, action} = route.params; const {selectedTransactionIDs, clearSelectedTransactions} = useSearchContext(); @@ -40,6 +41,7 @@ function IOURequestEditReport({route}: IOURequestEditReportProps) { backTo={backTo} transactionsReports={transactionReport ? [transactionReport] : []} selectReport={selectReport} + isEditing={action === CONST.IOU.ACTION.EDIT} /> ); } diff --git a/src/pages/iou/request/step/IOURequestEditReportCommon.tsx b/src/pages/iou/request/step/IOURequestEditReportCommon.tsx index 6278c0f734ac..e92faf6027de 100644 --- a/src/pages/iou/request/step/IOURequestEditReportCommon.tsx +++ b/src/pages/iou/request/step/IOURequestEditReportCommon.tsx @@ -43,9 +43,10 @@ type Props = { transactionsReports: Report[]; policyID?: string; selectReport: (item: TransactionGroupListItem) => void; + isEditing?: boolean; }; -function IOURequestEditReportCommon({backTo, transactionsReports, selectReport, policyID: policyIDFromProps}: Props) { +function IOURequestEditReportCommon({backTo, transactionsReports, selectReport, policyID: policyIDFromProps, isEditing}: Props) { const {translate} = useLocalize(); const {options} = useOptionsList(); const [allReports] = useOnyx(ONYXKEYS.COLLECTION.REPORT, {selector: (reports) => mapOnyxCollectionItems(reports, reportSelector), canBeMissing: true}); @@ -66,10 +67,11 @@ function IOURequestEditReportCommon({backTo, transactionsReports, selectReport, transactionsReports.at(0)?.ownerAccountID ?? currentUserPersonalDetails.accountID, allReports ?? {}, reportNameValuePairs, + isEditing, ); return reports; }), - [allReports, currentUserPersonalDetails.accountID, transactionsReports, allPoliciesID, reportNameValuePairs, policyIDFromProps], + [allReports, currentUserPersonalDetails.accountID, transactionsReports, isEditing, allPoliciesID, reportNameValuePairs, policyIDFromProps], ); const reportOptions: TransactionGroupListItem[] = useMemo(() => { diff --git a/src/pages/iou/request/step/IOURequestStepConfirmation.tsx b/src/pages/iou/request/step/IOURequestStepConfirmation.tsx index a248ada1e09d..021cb8eb7a32 100644 --- a/src/pages/iou/request/step/IOURequestStepConfirmation.tsx +++ b/src/pages/iou/request/step/IOURequestStepConfirmation.tsx @@ -137,7 +137,7 @@ function IOURequestStepConfirmation({ */ const transactionReport = getReportOrDraftReport(transaction?.reportID); const shouldUseTransactionReport = - transactionReport && !(isProcessingReport(transactionReport) && !policyReal?.harvesting?.enabled) && isReportOutstanding(transactionReport, policyReal?.id); + transactionReport && !(isProcessingReport(transactionReport) && !policyReal?.harvesting?.enabled) && isReportOutstanding(transactionReport, policyReal?.id, undefined, false); const report = shouldUseTransactionReport ? transactionReport : (reportReal ?? reportDraft); const policy = policyReal ?? policyDraft; const isDraftPolicy = policy === policyDraft; diff --git a/src/pages/iou/request/step/IOURequestStepReport.tsx b/src/pages/iou/request/step/IOURequestStepReport.tsx index c536981d08a6..8cdac9d2f990 100644 --- a/src/pages/iou/request/step/IOURequestStepReport.tsx +++ b/src/pages/iou/request/step/IOURequestStepReport.tsx @@ -120,6 +120,7 @@ function IOURequestStepReport({route, transaction}: IOURequestStepReportProps) { transactionsReports={transactionReport ? [transactionReport] : []} selectReport={selectReport} policyID={!isEditing && !isFromGlobalCreate ? reportOrDraftReport?.policyID : undefined} + isEditing={isEditing} /> ); } diff --git a/tests/unit/ReportUtilsTest.ts b/tests/unit/ReportUtilsTest.ts index c25a9bc3cb09..6ed7e4bac85e 100644 --- a/tests/unit/ReportUtilsTest.ts +++ b/tests/unit/ReportUtilsTest.ts @@ -113,6 +113,17 @@ import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; // Be sure to include the mocked permissions library or else the beta tests won't work jest.mock('@libs/Permissions'); +jest.mock('@libs/Navigation/Navigation', () => ({ + setNavigationActionToMicrotaskQueue: jest.fn(), + navigationRef: { + getCurrentRoute: jest.fn(() => ({ + params: { + reportID: '2', + }, + })), + }, +})); + const testDate = DateUtils.getDBTime(); const currentUserEmail = 'bjorn@vikings.net'; const currentUserAccountID = 5; @@ -3695,6 +3706,34 @@ describe('ReportUtils', () => { }; expect(isReportOutstanding(report, policy.id)).toBe(true); }); + it('should return false for submitted reports if we specify it', () => { + const report: Report = { + ...createRandomReport(1), + policyID: policy.id, + type: CONST.REPORT.TYPE.EXPENSE, + stateNum: CONST.REPORT.STATE_NUM.SUBMITTED, + statusNum: CONST.REPORT.STATUS_NUM.SUBMITTED, + }; + expect(isReportOutstanding(report, policy.id, undefined, false)).toBe(false); + }); + it('should return true for submitted reports if top most report ID is processing', async () => { + const report: Report = { + ...createRandomReport(1), + policyID: policy.id, + type: CONST.REPORT.TYPE.EXPENSE, + stateNum: CONST.REPORT.STATE_NUM.SUBMITTED, + statusNum: CONST.REPORT.STATUS_NUM.SUBMITTED, + }; + const activeReport: Report = { + ...createRandomReport(2), + policyID: policy.id, + type: CONST.REPORT.TYPE.EXPENSE, + stateNum: CONST.REPORT.STATE_NUM.SUBMITTED, + statusNum: CONST.REPORT.STATUS_NUM.SUBMITTED, + }; + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${activeReport.reportID}`, activeReport); + expect(isReportOutstanding(report, policy.id)).toBe(true); + }); it('should return false for archived report', async () => { const report: Report = { ...createRandomReport(1),