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
30 changes: 0 additions & 30 deletions src/libs/TransactionUtils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3333,34 +3333,6 @@ function hasSmartScanFailedWithMissingFields(transactions: Transaction[], report
);
}

/**
* Whether a scan-failed expense is one that the backend moves to its own report on payment. Auth only moves it when
* both the merchant and the amount are unset, so anything with an amount has to stay put to keep the payment total in
* sync with the server.
*/
function isScanFailedTransactionMovedOnPayment(transaction: Transaction, report: OnyxEntry<Report>): boolean {
if (!hasSmartScanFailedWithMissingFields([transaction], report)) {
return false;
}
return getMerchant(transaction) === CONST.TRANSACTION.PARTIAL_TRANSACTION_MERCHANT && getAmount(transaction, true) === 0;
}

/**
* Whether the report has scan-failed expenses to move out and at least one other expense left behind to pay.
*/
function shouldSplitScanFailedTransactions(transactions: Transaction[], report: OnyxEntry<Report>): boolean {
let hasScanFailedTransaction = false;
let hasRemainingTransaction = false;
for (const transaction of transactions) {
if (isScanFailedTransactionMovedOnPayment(transaction, report)) {
hasScanFailedTransaction = true;
} else {
hasRemainingTransaction = true;
}
}
return hasScanFailedTransaction && hasRemainingTransaction;
}

function getDistanceRequestType(transaction: OnyxEntry<Transaction>): string | undefined {
const requestType = getRequestType(transaction);
return isDistanceExpenseType(requestType) ? requestType : undefined;
Expand Down Expand Up @@ -3573,8 +3545,6 @@ export {
isDistanceTypeRequest,
recalculateUnreportedTransactionDetails,
hasSmartScanFailedWithMissingFields,
isScanFailedTransactionMovedOnPayment,
shouldSplitScanFailedTransactions,
isDeletedTransaction,
getDistanceRequestType,
isUnreportedManagedCardTransaction,
Expand Down
61 changes: 9 additions & 52 deletions src/libs/actions/IOU/Hold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
isPolicyExpenseChat as isPolicyExpenseChatReportUtil,
isProcessingReport,
} from '@libs/ReportUtils';
import {getAmount, isScanFailedTransactionMovedOnPayment} from '@libs/TransactionUtils';
import {getAmount} from '@libs/TransactionUtils';

import {notifyNewAction} from '@userActions/Report';

Expand Down Expand Up @@ -549,12 +549,7 @@ type OptimisticHoldReportExpenseActionID = {
oldReportActionID: string;
};

function getHoldReportActionsAndTransactions(
reportID: string | undefined,
iouReport?: OnyxEntry<OnyxTypes.Report>,
shouldMoveHeldTransactions = true,
shouldMoveScanFailedTransactions = false,
) {
function getHoldReportActionsAndTransactions(reportID: string | undefined) {
const allTransactions = getAllTransactions();
const iouReportActions = getAllReportActions(reportID);
const holdReportActions: Array<OnyxTypes.ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.IOU>> = [];
Expand All @@ -564,14 +559,7 @@ function getHoldReportActionsAndTransactions(
const transactionID = isMoneyRequestAction(action) ? getOriginalMessage(action)?.IOUTransactionID : undefined;
const transaction = allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`];

if (!transaction) {
continue;
}

const isHeld = shouldMoveHeldTransactions && !!transaction.comment?.hold;
const isScanFailed = shouldMoveScanFailedTransactions && isScanFailedTransactionMovedOnPayment(transaction, iouReport);

if (isHeld || isScanFailed) {
if (transaction?.comment?.hold) {
holdReportActions.push(action as OnyxTypes.ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.IOU>);
holdTransactions.push(transaction);
}
Expand Down Expand Up @@ -680,8 +668,6 @@ function getReportFromHoldRequestsOnyxData({
isApprovalFlow = false,
delegateAccountID,
getCurrencyDecimals,
shouldMoveHeldTransactions = true,
shouldMoveScanFailedTransactions = false,
}: {
chatReport: OnyxTypes.Report;
iouReport: OnyxEntry<OnyxTypes.Report>;
Expand All @@ -692,31 +678,26 @@ function getReportFromHoldRequestsOnyxData({
isApprovalFlow?: boolean;
delegateAccountID: number | undefined;
getCurrencyDecimals: CurrencyListActionsContextType['getCurrencyDecimals'];
shouldMoveHeldTransactions?: boolean;
shouldMoveScanFailedTransactions?: boolean;
}): {
optimisticHoldReportID: string;
optimisticHoldActionID: string;
optimisticCreatedReportForUnapprovedTransactionsActionID: string | undefined;
optimisticHoldReportExpenseActionIDs: OptimisticHoldReportExpenseActionID[];
optimisticReportActionCopyIDs: OptimisticReportActionCopyIDs;
optimisticData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.REPORT | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS | typeof ONYXKEYS.COLLECTION.TRANSACTION>>;
successData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.REPORT | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS>>;
successData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS>>;
failureData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.REPORT | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS | typeof ONYXKEYS.COLLECTION.TRANSACTION>>;
} {
const {holdReportActions, holdTransactions} = getHoldReportActionsAndTransactions(iouReport?.reportID, iouReport, shouldMoveHeldTransactions, shouldMoveScanFailedTransactions);
const {holdReportActions, holdTransactions} = getHoldReportActionsAndTransactions(iouReport?.reportID);
const firstHoldTransaction = holdTransactions.at(0);
const newParentReportActionID = NumberUtils.rand64();

const coefficient = isExpenseReport(iouReport) ? -1 : 1;
const isPolicyExpenseChat = isPolicyExpenseChatReportUtil(chatReport);
const holdReimbursable = getReimbursableTotal(iouReport) - getUnheldReimbursableTotal(iouReport);
const holdNonReimbursable = (iouReport?.nonReimbursableTotal ?? 0) - (iouReport?.unheldNonReimbursableTotal ?? 0);

// Scan-failed expenses only move out when they have no amount, so they carry nothing over to the new report and
// leave the totals of the report they came from untouched.
const holdAmount = shouldMoveScanFailedTransactions ? 0 : (holdReimbursable + holdNonReimbursable) * coefficient;
const holdNonReimbursableAmount = shouldMoveScanFailedTransactions ? 0 : holdNonReimbursable * coefficient;
const holdAmount = (holdReimbursable + holdNonReimbursable) * coefficient;
const holdNonReimbursableAmount = holdNonReimbursable * coefficient;

// Pass held transactions for formula computation (e.g., {report:startdate})
const reportTransactions: Record<string, OnyxTypes.Transaction> = {};
Expand Down Expand Up @@ -825,7 +806,7 @@ function getReportFromHoldRequestsOnyxData({
// Held transactions just moved out, leaving total/nonReimbursableTotal stale on this report —
// offline consumers (e.g. the Pay button) would read the wrong amount until server reconciles.
// unheldTotal stays as-is: every remaining transaction is unheld, so it already equals the new total.
const shouldUpdateOriginalReportTotals = !shouldMoveScanFailedTransactions && holdTransactions.length > 0 && iouReport?.unheldTotal !== undefined;
const shouldUpdateOriginalReportTotals = holdTransactions.length > 0 && iouReport?.unheldTotal !== undefined;

const optimisticData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.REPORT | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS | typeof ONYXKEYS.COLLECTION.TRANSACTION>> = [
{
Expand Down Expand Up @@ -903,7 +884,7 @@ function getReportFromHoldRequestsOnyxData({
bringHeldTransactionsBack[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`] = transaction;
}

const successData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.REPORT | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS>> = [
const successData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS>> = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`,
Expand Down Expand Up @@ -1011,30 +992,6 @@ function getReportFromHoldRequestsOnyxData({
});
}

// The backend creates its own report for the moved scan-failed expenses instead of reusing optimisticHoldReportID,
// so the optimistic one has to be dropped once the real report arrives, otherwise it lingers as an empty report.
if (shouldMoveScanFailedTransactions) {
successData.push(
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${optimisticExpenseReport.reportID}`,
value: null,
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticExpenseReport.reportID}`,
value: null,
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`,
value: {
[optimisticExpenseReportPreview.reportActionID]: null,
},
},
);
}

return {
optimisticData,
optimisticHoldActionID: optimisticExpenseReportPreview.reportActionID,
Expand Down
17 changes: 2 additions & 15 deletions src/libs/actions/IOU/PayMoneyRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
} from '@libs/ReportUtils';
import playSound, {SOUNDS} from '@libs/Sound';
import {shouldRestrictUserBillableActions} from '@libs/SubscriptionUtils';
import {shouldSplitScanFailedTransactions} from '@libs/TransactionUtils';

import {buildPolicyData, generatePolicyID} from '@userActions/Policy/Policy';
import type {BuildPolicyDataKeys} from '@userActions/Policy/Policy';
Expand Down Expand Up @@ -275,8 +274,6 @@ function getPayMoneyRequestParams({
total = unheldReimbursableTotal;
}

const shouldMoveScanFailedTransactions = !!full && isExpenseReport(iouReport) && shouldSplitScanFailedTransactions(reportTransactions, iouReport);

const optimisticIOUReportAction = buildOptimisticIOUReportAction({
type: CONST.IOU.REPORT_ACTION_TYPE.PAY,
amount: isExpenseReport(iouReport) ? -total : total,
Expand Down Expand Up @@ -495,18 +492,8 @@ function getPayMoneyRequestParams({
let optimisticHoldReportID;
let optimisticHoldActionID;
let optimisticHoldReportExpenseActionIDs;
if (!full || shouldMoveScanFailedTransactions) {
const holdReportOnyxData = getReportFromHoldRequestsOnyxData({
chatReport,
iouReport,
recipient,
policy: reportPolicy,
betas,
delegateAccountID,
getCurrencyDecimals,
shouldMoveHeldTransactions: !full,
shouldMoveScanFailedTransactions,
});
if (!full) {
const holdReportOnyxData = getReportFromHoldRequestsOnyxData({chatReport, iouReport, recipient, policy: reportPolicy, betas, delegateAccountID, getCurrencyDecimals});

onyxData.optimisticData?.push(...holdReportOnyxData.optimisticData);
onyxData.successData?.push(...holdReportOnyxData.successData);
Expand Down
Loading
Loading