diff --git a/src/libs/API/parameters/MergeDuplicatesParams.ts b/src/libs/API/parameters/MergeDuplicatesParams.ts index 7f2c3daa7eaf..8edbfa0e8cac 100644 --- a/src/libs/API/parameters/MergeDuplicatesParams.ts +++ b/src/libs/API/parameters/MergeDuplicatesParams.ts @@ -10,7 +10,7 @@ type MergeDuplicatesParams = { billable: boolean; reimbursable: boolean; tag: string; - taxCode: string; + taxCode?: string; receiptID: number; reportID: string | undefined; reportActionID?: string | undefined; diff --git a/src/libs/TransactionUtils/index.ts b/src/libs/TransactionUtils/index.ts index 6a994ee91cba..b071ea538af2 100644 --- a/src/libs/TransactionUtils/index.ts +++ b/src/libs/TransactionUtils/index.ts @@ -2559,8 +2559,8 @@ function buildNewTransactionAfterReviewingDuplicates(reviewDuplicateTransaction: modifiedMerchant: reviewDuplicateTransaction?.merchant, merchant: reviewDuplicateTransaction?.merchant, comment: {...reviewDuplicateTransaction?.comment, comment: reviewDuplicateTransaction?.description}, - // Clear stale taxName/taxValue so MoneyRequestView derives them fresh from the policy using the updated taxCode - ...(reviewDuplicateTransaction?.taxCode !== undefined && {taxName: undefined, taxValue: undefined}), + // If the taxCode changes, clear stale taxName/taxValue so MoneyRequestView derives them fresh from the policy using the updated taxCode + ...(reviewDuplicateTransaction?.taxCode !== undefined && reviewDuplicateTransaction?.taxCode !== duplicatedTransaction?.taxCode && {taxName: undefined, taxValue: undefined}), }; } @@ -2581,7 +2581,6 @@ function buildMergeDuplicatesParams( reimbursable: reviewDuplicates?.reimbursable ?? false, category: reviewDuplicates?.category ?? '', tag: reviewDuplicates?.tag ?? '', - taxCode: reviewDuplicates?.taxCode ?? '', merchant: reviewDuplicates?.merchant ?? '', comment: reviewDuplicates?.description ?? '', }; diff --git a/src/libs/actions/IOU/Duplicate.ts b/src/libs/actions/IOU/Duplicate.ts index c263b6032277..fda4aedeedbf 100644 --- a/src/libs/actions/IOU/Duplicate.ts +++ b/src/libs/actions/IOU/Duplicate.ts @@ -75,7 +75,7 @@ function getIOUActionForTransactions(transactionIDList: Array { const taxCode = reviewDuplicates?.taxCode ?? ''; - const taxRate = taxCode ? policy?.taxRates?.taxes?.[taxCode] : undefined; + const taxRate = taxCode ? duplicatedTransactionPolicy?.taxRates?.taxes?.[taxCode] : undefined; + // Preserve taxAmount and taxValue if taxCode is deleted or remains unchanged compared to duplicatedTransaction?.taxCode. + if (!taxRate || (taxCode && duplicatedTransaction?.taxCode === taxCode) || reviewDuplicates?.taxAmount === undefined) { + return; + } + return { - taxAmount: -(reviewDuplicates?.taxAmount ?? 0), - taxValue: taxRate?.value ?? '', + taxAmount: -reviewDuplicates.taxAmount, + taxValue: taxRate?.value, + taxCode, }; - }, [reviewDuplicates?.taxCode, reviewDuplicates?.taxAmount, policy?.taxRates?.taxes]); + }, [reviewDuplicates?.taxCode, reviewDuplicates?.taxAmount, duplicatedTransactionPolicy?.taxRates?.taxes, duplicatedTransaction?.taxCode]); const isReportOwner = iouReport?.ownerAccountID === currentUserPersonalDetails?.accountID; const handleMergeDuplicates = useCallback(() => { diff --git a/src/pages/TransactionDuplicate/ReviewTaxCode.tsx b/src/pages/TransactionDuplicate/ReviewTaxCode.tsx index 96dd7a8405e3..bcca68cbd62c 100644 --- a/src/pages/TransactionDuplicate/ReviewTaxCode.tsx +++ b/src/pages/TransactionDuplicate/ReviewTaxCode.tsx @@ -31,6 +31,7 @@ function ReviewTaxRate() { const [transactionThreadReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${route.params.threadReportID}`); const transactionID = getTransactionID(transactionThreadReport); const [transaction] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION}${getNonEmptyStringOnyxID(transactionID)}`); + const [duplicatedTransaction] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION}${getNonEmptyStringOnyxID(reviewDuplicates?.transactionID)}`); const [transactionViolations] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`); const allDuplicateIDs = useMemo( () => transactionViolations?.find((violation) => violation.name === CONST.VIOLATIONS.DUPLICATED_TRANSACTION)?.data?.duplicates ?? [], @@ -64,11 +65,15 @@ function ReviewTaxRate() { ); const getTaxAmount = useCallback( (taxID: string) => { + // If the tax code remains unchanged, preserve the tax amount to avoid resetting it to the default value when resolving duplicates. + if (taxID === duplicatedTransaction?.taxCode) { + return; + } const taxPercentage = getTaxValue(policy, transaction, taxID); const decimals = getCurrencyDecimals(transaction?.currency); return convertToBackendAmount(calculateTaxAmount(taxPercentage ?? '', getAmount(transaction), decimals)); }, - [policy, transaction, getCurrencyDecimals], + [policy, transaction, getCurrencyDecimals, duplicatedTransaction?.taxCode], ); const setTaxCode = useCallback(