diff --git a/src/components/MoneyReportHeader.tsx b/src/components/MoneyReportHeader.tsx index 7905bbcab4d0b..78ee5cb27970f 100644 --- a/src/components/MoneyReportHeader.tsx +++ b/src/components/MoneyReportHeader.tsx @@ -23,7 +23,6 @@ import useLocalize from '@hooks/useLocalize'; import useMobileSelectionMode from '@hooks/useMobileSelectionMode'; import useNetwork from '@hooks/useNetwork'; import useOnyx from '@hooks/useOnyx'; -import useParticipantsInvoiceReport from '@hooks/useParticipantsInvoiceReport'; import usePaymentAnimations from '@hooks/usePaymentAnimations'; import usePaymentOptions from '@hooks/usePaymentOptions'; import usePermissions from '@hooks/usePermissions'; @@ -673,7 +672,6 @@ function MoneyReportHeader({ const isReportSubmitter = isCurrentUserSubmitter(chatIOUReport); const isChatReportDM = isDM(chatReport); - const existingB2BInvoiceReport = useParticipantsInvoiceReport(activePolicyID, CONST.REPORT.INVOICE_RECEIVER_TYPE.BUSINESS, chatReport?.policyID); const isSelectionModePaymentRef = useRef(false); const confirmPayment = useCallback( ({paymentType: type, payAsBusiness, methodID, paymentMethod}: PaymentActionParams) => { @@ -706,7 +704,6 @@ function MoneyReportHeader({ currentUserAccountIDParam: accountID, currentUserEmailParam: email ?? '', payAsBusiness, - existingB2BInvoiceReport, methodID, paymentMethod, activePolicy, @@ -768,7 +765,6 @@ function MoneyReportHeader({ introSelected, accountID, email, - existingB2BInvoiceReport, activePolicy, policy, currentSearchQueryJSON, diff --git a/src/components/ReportActionItem/MoneyRequestReportPreview/MoneyRequestReportPreviewContent.tsx b/src/components/ReportActionItem/MoneyRequestReportPreview/MoneyRequestReportPreviewContent.tsx index a1536c8270504..c31a25beb1fd6 100644 --- a/src/components/ReportActionItem/MoneyRequestReportPreview/MoneyRequestReportPreviewContent.tsx +++ b/src/components/ReportActionItem/MoneyRequestReportPreview/MoneyRequestReportPreviewContent.tsx @@ -33,7 +33,6 @@ import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset'; import useLocalize from '@hooks/useLocalize'; import useNetwork from '@hooks/useNetwork'; import useOnyx from '@hooks/useOnyx'; -import useParticipantsInvoiceReport from '@hooks/useParticipantsInvoiceReport'; import usePaymentAnimations from '@hooks/usePaymentAnimations'; import usePermissions from '@hooks/usePermissions'; import usePolicy from '@hooks/usePolicy'; @@ -254,8 +253,6 @@ function MoneyRequestReportPreviewContent({ const hasReceipts = transactionsWithReceipts.length > 0; const isScanning = hasReceipts && areAllRequestsBeingSmartScanned; - const existingB2BInvoiceReport = useParticipantsInvoiceReport(activePolicyID, CONST.REPORT.INVOICE_RECEIVER_TYPE.BUSINESS, chatReport?.policyID); - const {isDelegateAccessRestricted} = useDelegateNoAccessState(); const {showDelegateNoAccessModal} = useDelegateNoAccessActions(); const [reportActions] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReportID}`); @@ -290,7 +287,6 @@ function MoneyRequestReportPreviewContent({ currentUserAccountIDParam: currentUserAccountID, currentUserEmailParam: currentUserEmail, payAsBusiness, - existingB2BInvoiceReport, methodID, paymentMethod, activePolicy, @@ -326,7 +322,6 @@ function MoneyRequestReportPreviewContent({ introSelected, currentUserAccountID, currentUserEmail, - existingB2BInvoiceReport, activePolicy, policy, betas, diff --git a/src/libs/actions/IOU/index.ts b/src/libs/actions/IOU/index.ts index fdc5fe67e3d32..2bcc695cc8e75 100644 --- a/src/libs/actions/IOU/index.ts +++ b/src/libs/actions/IOU/index.ts @@ -152,6 +152,7 @@ import { hasNonReimbursableTransactions as hasNonReimbursableTransactionsReportUtils, hasOutstandingChildRequest, hasViolations as hasViolationsReportUtils, + isArchivedNonExpenseReport, isArchivedReport, isClosedReport as isClosedReportUtil, isDeprecatedGroupDM, @@ -326,7 +327,6 @@ type PayInvoiceArgs = { currentUserAccountIDParam: number; currentUserEmailParam: string; payAsBusiness?: boolean; - existingB2BInvoiceReport?: OnyxEntry; methodID?: number; paymentMethod?: PaymentMethod; activePolicy?: OnyxTypes.Policy; @@ -1026,6 +1026,25 @@ function getAllReports(): OnyxCollection { return allReports; } +function getExistingB2BInvoiceReport(receiverID: string | number | undefined, policyID?: string): OnyxEntry { + return Object.values(allReports ?? {}).find((report) => { + if (!report || !isInvoiceRoom(report)) { + return false; + } + const rnvp = allReportNameValuePairs?.[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${report.reportID}`]; + if (isArchivedNonExpenseReport(report, isArchivedReport(rnvp))) { + return false; + } + const isSameReceiver = + report.invoiceReceiver && + report.invoiceReceiver.type === CONST.REPORT.INVOICE_RECEIVER_TYPE.BUSINESS && + 'policyID' in report.invoiceReceiver && + report.invoiceReceiver.policyID === receiverID; + + return report.policyID === policyID && isSameReceiver; + }); +} + function getAllReportActionsFromIOU(): OnyxCollection { return allReportActions; } @@ -9296,7 +9315,6 @@ function getPayMoneyRequestParams({ introSelected, paymentPolicyID, lastUsedPaymentMethod, - existingB2BInvoiceReport, activePolicy, iouReportCurrentNextStepDeprecated, betas, @@ -9312,7 +9330,6 @@ function getPayMoneyRequestParams({ bankAccountID?: number; paymentPolicyID?: string | undefined; lastUsedPaymentMethod?: OnyxTypes.LastPaymentMethodType; - existingB2BInvoiceReport?: OnyxEntry; activePolicy?: OnyxEntry; currentUserAccountIDParam: number; currentUserEmailParam?: string; @@ -9379,8 +9396,12 @@ function getPayMoneyRequestParams({ onyxData.failureData?.push(...(policyFailureData ?? []), {onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.NVP_ACTIVE_POLICY_ID, value: activePolicy?.id ?? null}); } - if (isIndividualInvoiceRoom(chatReport) && payAsBusiness && existingB2BInvoiceReport) { - chatReport = existingB2BInvoiceReport; + if (isIndividualInvoiceRoom(chatReport) && payAsBusiness) { + const existingB2B = getExistingB2BInvoiceReport(activePolicy?.id, chatReport.policyID); + + if (existingB2B) { + chatReport = existingB2B; + } } let total = (iouReport?.total ?? 0) - (iouReport?.nonReimbursableTotal ?? 0); @@ -11348,7 +11369,6 @@ function payInvoice({ currentUserAccountIDParam, currentUserEmailParam, payAsBusiness = false, - existingB2BInvoiceReport, methodID, paymentMethod, activePolicy, @@ -11380,7 +11400,6 @@ function payInvoice({ full: true, payAsBusiness, bankAccountID: methodID, - existingB2BInvoiceReport, activePolicy, currentUserAccountIDParam, currentUserEmailParam,