diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 0486792df466..ff7fa1d1d352 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -385,9 +385,25 @@ function getReceiptError(receipt?: Receipt, filename?: string, isScanRequest = t : ErrorUtils.getMicroSecondOnyxErrorObject({error: CONST.IOU.RECEIPT_ERROR, source: receipt.source?.toString() ?? '', filename: filename ?? ''}); } -/** Return the object to update hasOutstandingChildRequest */ -function getOutstandingChildRequest(needsToBeManuallySubmitted: boolean, policy: OnyxEntry | EmptyObject = null): OutstandingChildRequest { - if (!needsToBeManuallySubmitted) { +function needsToBeManuallySubmitted(iouReport: OnyxTypes.Report) { + const isPolicyExpenseChat = ReportUtils.isExpenseReport(iouReport); + + if (isPolicyExpenseChat) { + const policy = ReportUtils.getPolicy(iouReport.policyID); + const isFromPaidPolicy = PolicyUtils.isPaidGroupPolicy(policy); + + // If the scheduled submit is turned off on the policy, user needs to manually submit the report which is indicated by GBR in LHN + return isFromPaidPolicy && !policy.harvesting?.enabled; + } + + return true; +} + +/** + * Return the object to update hasOutstandingChildRequest + */ +function getOutstandingChildRequest(policy: OnyxEntry | EmptyObject, iouReport: OnyxTypes.Report): OutstandingChildRequest { + if (!needsToBeManuallySubmitted(iouReport)) { return { hasOutstandingChildRequest: false, }; @@ -399,8 +415,9 @@ function getOutstandingChildRequest(needsToBeManuallySubmitted: boolean, policy: }; } - // We don't need to update hasOutstandingChildRequest in this case - return {}; + return { + hasOutstandingChildRequest: iouReport.managerID === userAccountID && iouReport.total !== 0, + }; } /** Builds the Onyx data for a money request */ @@ -423,10 +440,9 @@ function buildOnyxDataForMoneyRequest( policyTagList?: OnyxEntry, policyCategories?: OnyxEntry, optimisticNextStep?: OnyxTypes.ReportNextStep | null, - needsToBeManuallySubmitted = true, ): [OnyxUpdate[], OnyxUpdate[], OnyxUpdate[]] { const isScanRequest = TransactionUtils.isScanRequest(transaction); - const outstandingChildRequest = getOutstandingChildRequest(needsToBeManuallySubmitted, policy); + const outstandingChildRequest = getOutstandingChildRequest(policy ?? {}, iouReport); const clearedPendingFields = Object.fromEntries(Object.keys(transaction.pendingFields ?? {}).map((key) => [key, null])); const optimisticData: OnyxUpdate[] = []; @@ -819,15 +835,10 @@ function getMoneyRequestInformation( iouReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${chatReport.iouReportID}`] ?? null; } - // Check if the Scheduled Submit is enabled in case of expense report - let needsToBeManuallySubmitted = true; let isFromPaidPolicy = false; if (isPolicyExpenseChat) { isFromPaidPolicy = PolicyUtils.isPaidGroupPolicy(policy ?? null); - // If the scheduled submit is turned off on the policy, user needs to manually submit the report which is indicated by GBR in LHN - needsToBeManuallySubmitted = isFromPaidPolicy && !policy?.harvesting?.enabled; - // If the linked expense report on paid policy is not draft and not instantly submitted, we need to create a new draft expense report if (iouReport && isFromPaidPolicy && !ReportUtils.isDraftExpenseReport(iouReport) && !ReportUtils.isExpenseReportWithInstantSubmittedState(iouReport)) { iouReport = null; @@ -967,7 +978,6 @@ function getMoneyRequestInformation( policyTagList, policyCategories, optimisticNextStep, - needsToBeManuallySubmitted, ); return { @@ -1234,11 +1244,21 @@ function getUpdateMoneyRequestParams( } updatedMoneyRequestReport.cachedTotal = CurrencyUtils.convertToDisplayString(updatedMoneyRequestReport.total, transactionDetails?.currency); - optimisticData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.reportID}`, - value: updatedMoneyRequestReport, - }); + optimisticData.push( + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.reportID}`, + value: updatedMoneyRequestReport, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.parentReportID}`, + value: { + hasOutstandingChildRequest: + iouReport && needsToBeManuallySubmitted(iouReport) && updatedMoneyRequestReport.managerID === userAccountID && updatedMoneyRequestReport.total !== 0, + }, + }, + ); successData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.reportID}`, @@ -3019,6 +3039,13 @@ function deleteMoneyRequest(transactionID: string, reportAction: OnyxTypes.Repor [reportPreviewAction?.reportActionID ?? '']: updatedReportPreviewAction, }, }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${chatReport?.reportID}`, + value: { + hasOutstandingChildRequest: iouReport && needsToBeManuallySubmitted(iouReport) && updatedIOUReport?.managerID === userAccountID && updatedIOUReport.total !== 0, + }, + }, ); if (!shouldDeleteIOUReport && updatedReportPreviewAction.childMoneyRequestCount === 0) {