Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
7751a9f
Recalculate mileage rate when editing distance expense date
Krishna2323 Jun 16, 2026
7a2befa
fix failing tests.
Krishna2323 Jun 17, 2026
dcc54a6
Address Codex review feedback for date-triggered mileage rate edits
Krishna2323 Jun 17, 2026
67d8260
Address Codex review feedback for date-triggered mileage rate edits
Krishna2323 Jun 17, 2026
e7ef8f5
Guard date-triggered mileage rate recalc for edge cases
Krishna2323 Jun 17, 2026
1bb06e9
Recalculate mileage rate on date edit for track distance expenses
Krishna2323 Jun 17, 2026
4af27af
Fix getDistanceRateTaxUpdates typing and default tax fallback
Krishna2323 Jun 17, 2026
f4ee9ec
Fix date edits reverting when mileage rate also changes
Krishna2323 Jun 18, 2026
ad76bc1
Preserve optimistic date when mileage rate recalc follows a date edit
Krishna2323 Jun 18, 2026
c835a69
Harden date-driven mileage rate recalc edge cases on expense edit.
Krishna2323 Jun 18, 2026
f3c7573
Merge branch 'main' into krishna/89836-date-aware-mileage-rate-recalc…
Krishna2323 Jun 18, 2026
7776d1d
Skip mileage rate recalc on date edit when no rate matches the new date.
Krishna2323 Jun 18, 2026
78c1401
Use a single distance rate API call when date edit also switches mile…
Krishna2323 Jun 18, 2026
53449ba
Restrict date-driven mileage rate recalc to workspace expense reports.
Krishna2323 Jun 18, 2026
437d3c0
fix test and eslint.
Krishna2323 Jun 18, 2026
27dd5a9
Address review feedback for date-driven mileage rate recalc on edit.
Krishna2323 Jun 19, 2026
217d772
fix ESLint.
Krishna2323 Jun 19, 2026
5a65853
Re-enable date-driven mileage rate recalc for self-DM track expenses.
Krishna2323 Jun 22, 2026
602ab1a
Merge branch 'Expensify:main' into krishna/89836-date-aware-mileage-r…
Krishna2323 Jun 24, 2026
4cef318
Address review feedback for date-driven mileage rate recalc on edit
Krishna2323 Jun 24, 2026
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
6 changes: 5 additions & 1 deletion src/hooks/useTransactionInlineEdit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
} from '@libs/actions/TransactionInlineEdit';
import getNonEmptyStringOnyxID from '@libs/getNonEmptyStringOnyxID';
import {getIOUActionForTransactionID} from '@libs/ReportActionsUtils';
import {isTrackExpenseReportNew} from '@libs/ReportUtils';
import {isExpenseUnreported, isPerDiemRequest} from '@libs/TransactionUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
Expand Down Expand Up @@ -128,7 +129,9 @@ function useTransactionInlineEdit({transactionID, hash, linkedReportAction}: Use
const {hasSelectedTransactions} = useSearchSelectionContext();

const isPerDiem = isPerDiemRequest(transaction);
const {shouldSelectPolicy} = usePolicyForMovingExpenses(isPerDiem);
const {shouldSelectPolicy, policyForMovingExpenses} = usePolicyForMovingExpenses(isPerDiem);

const isTrackExpense = isTrackExpenseReportNew(transactionThreadReport, effectiveParentReport, parentReportAction);

const {isOffline} = useNetwork();

Expand Down Expand Up @@ -157,6 +160,7 @@ function useTransactionInlineEdit({transactionID, hash, linkedReportAction}: Use
parentReportAction,
transactionThreadReport,
policy: completePolicy ?? policy,
policyForTrackExpense: isTrackExpense ? policyForMovingExpenses : undefined,
policyCategories,
policyTags,
policyRecentlyUsedCategories,
Expand Down
21 changes: 21 additions & 0 deletions src/libs/TransactionUtils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import type {
} from '@src/types/onyx';
import type {Attendee, DistanceExpenseType, Participant, SplitExpense} from '@src/types/onyx/IOU';
import type {Errors, PendingAction} from '@src/types/onyx/OnyxCommon';
import type {Unit} from '@src/types/onyx/Policy';
import type {OnyxData} from '@src/types/onyx/Request';
import type {
Comment,
Expand Down Expand Up @@ -2174,6 +2175,25 @@ function getTaxValue(policy: OnyxEntry<Policy>, transaction: OnyxEntry<Transacti
return Object.values(transformedTaxRates(policy, transaction)).find((taxRate) => taxRate.code === taxCode)?.value;
}

/**
* Computes tax amount, code, and value when a workspace distance expense uses a given mileage rate.
*/
function getDistanceRateTaxUpdates(
policy: OnyxEntry<Policy>,
transaction: OnyxEntry<Transaction>,
customUnitRateID: string,
distanceUnit?: Unit,
): {taxAmount: number; taxCode: string; taxValue: string | undefined} {
const taxCode = getDefaultTaxCode(policy, transaction, undefined, customUnitRateID) ?? '';
const taxableAmount = DistanceRequestUtils.getTaxableAmount(policy, customUnitRateID, getDistanceInMeters(transaction, distanceUnit ?? transaction?.comment?.customUnit?.distanceUnit));
const taxValue = taxCode ? getTaxValue(policy, transaction, taxCode) : undefined;
const mileageRates = DistanceRequestUtils.getMileageRates(policy);
const rateCurrency = mileageRates[customUnitRateID]?.currency ?? transaction?.currency ?? CONST.CURRENCY.USD;
const taxAmount = convertToBackendAmount(calculateTaxAmount(taxValue, taxableAmount, getCurrencyDecimals(rateCurrency)));

return {taxAmount, taxCode, taxValue};
}

/**
* Returns the maximum allowed tax amount (in the smallest currency units) for a transaction,
* i.e. the tax computed from the selected tax rate (or the policy default) and the expense amount.
Expand Down Expand Up @@ -2939,6 +2959,7 @@ export {
transformedTaxRates,
getTaxValue,
getCalculatedTaxAmount,
getDistanceRateTaxUpdates,
getTaxName,
getTaxRateTitle,
hasTaxRateWithMatchingValue,
Expand Down
147 changes: 139 additions & 8 deletions src/libs/actions/IOU/UpdateMoneyRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import DistanceRequestUtils from '@libs/DistanceRequestUtils';
import {getMicroSecondOnyxErrorWithTranslationKey} from '@libs/ErrorUtils';
import {buildNextStepNew, buildOptimisticNextStep} from '@libs/NextStepUtils';
import {rand64} from '@libs/NumberUtils';
import {hasDependentTags, isGroupPolicy} from '@libs/PolicyUtils';
import {hasDependentTags, isGroupPolicy, isTaxTrackingEnabled} from '@libs/PolicyUtils';
import type {TransactionDetails} from '@libs/ReportUtils';
import {
buildOptimisticModifiedExpenseReportAction,
Expand All @@ -28,6 +28,7 @@ import {
import {
getAmount,
getClearedPendingFields,
getDistanceRateTaxUpdates,
getMerchant,
getUpdatedTransaction,
hasSubmissionBlockingViolationInReport,
Expand Down Expand Up @@ -75,6 +76,7 @@ type UpdateMoneyRequestDateParams = {
isOffline: boolean;
hash?: number;
delegateAccountID: number | undefined;
policyForTrackExpense?: OnyxEntry<OnyxTypes.Policy>;
};

type SearchSnapshotOnyxData = {
Expand Down Expand Up @@ -142,6 +144,64 @@ function getSearchSnapshotUpdates({
};
}

function getRecalculatedDistanceRateIDForExpenseDate({
transaction,
transactionThreadReport,
parentReport,
policy,
expenseDate,
}: {
transaction: OnyxEntry<OnyxTypes.Transaction>;
transactionThreadReport: OnyxEntry<OnyxTypes.Report>;
parentReport: OnyxEntry<OnyxTypes.Report>;
policy: OnyxEntry<OnyxTypes.Policy>;
expenseDate: string;
}): string | undefined {
if (!transaction || !isDistanceRequestTransactionUtils(transaction)) {
return undefined;
}

const isTrackExpense = isTrackExpenseReport(transactionThreadReport) && isSelfDM(parentReport);
const isWorkspaceDistanceExpense = isExpenseReport(parentReport);

if (!isWorkspaceDistanceExpense && !isTrackExpense) {
return undefined;
}

if (!policy || !isGroupPolicy(policy)) {
return undefined;
}

const currentRateID = transaction.comment?.customUnit?.customUnitRateID;
if (!currentRateID || currentRateID === CONST.CUSTOM_UNITS.FAKE_P2P_ID) {
return undefined;
}

const currentMileageRate = DistanceRequestUtils.getRateByCustomUnitRateID({customUnitRateID: currentRateID, policy});
if (currentMileageRate && currentMileageRate.enabled !== false && DistanceRequestUtils.isRateEligibleForDate(currentMileageRate, expenseDate)) {
return undefined;
}

const newRateID = DistanceRequestUtils.getCustomUnitRateID({
reportID: parentReport?.reportID,
isPolicyExpenseChat: isWorkspaceDistanceExpense,
isTrackDistanceExpense: isTrackExpense,
Comment thread
Krishna2323 marked this conversation as resolved.
Resolved
policy,
expenseDate,
});

if (newRateID === currentRateID || newRateID === CONST.CUSTOM_UNITS.FAKE_P2P_ID) {
return undefined;
}

const newMileageRate = DistanceRequestUtils.getRateByCustomUnitRateID({customUnitRateID: newRateID, policy});
if (!newMileageRate || !DistanceRequestUtils.isRateEligibleForDate(newMileageRate, expenseDate)) {
return undefined;
}
Comment thread
Krishna2323 marked this conversation as resolved.
Resolved

return newRateID;
}

/** Updates the created date of an expense */
function updateMoneyRequestDate({
transactionID,
Expand All @@ -160,21 +220,64 @@ function updateMoneyRequestDate({
isOffline,
hash,
delegateAccountID,
policyForTrackExpense,
}: UpdateMoneyRequestDateParams) {
const transaction = getAllTransactions()[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`];
const isTrackExpense = isTrackExpenseReport(transactionThreadReport) && isSelfDM(parentReport);
const effectivePolicy = isTrackExpense ? policyForTrackExpense : policy;
const newRateID = getRecalculatedDistanceRateIDForExpenseDate({
transaction,
transactionThreadReport,
parentReport,
policy: effectivePolicy,
expenseDate: value,
});
const currentRateID = transaction?.comment?.customUnit?.customUnitRateID;
const shouldRecalculateRate = !!(newRateID && newRateID !== currentRateID && transaction && effectivePolicy);

if (shouldRecalculateRate) {
const distanceRateTaxUpdates =
!isTrackExpense && isTaxTrackingEnabled(true, effectivePolicy, true) && transaction ? getDistanceRateTaxUpdates(effectivePolicy, transaction, newRateID) : undefined;

updateMoneyRequestDistanceRate({
transaction,
transactionThreadReport,
parentReport,
rateID: newRateID,
created: value,
policy: effectivePolicy,
policyTagList: policyTags,
policyCategories,
currentUserAccountIDParam,
currentUserEmailParam,
isASAPSubmitBetaEnabled,
parentReportNextStep,
delegateAccountID,
hash,
transactions,
transactionViolations,
isOffline,
updatedTaxAmount: distanceRateTaxUpdates?.taxAmount,
updatedTaxCode: distanceRateTaxUpdates?.taxCode,
updatedTaxValue: distanceRateTaxUpdates?.taxValue,
});
return;
}

const transactionChanges: TransactionChanges = {
created: value,
};
let data: UpdateMoneyRequestData<UpdateMoneyRequestDataKeys>;

if (isTrackExpenseReport(transactionThreadReport) && isSelfDM(parentReport)) {
data = getUpdateTrackExpenseParams(transactionID, transactionThreadReport?.reportID, transactionChanges, policy, delegateAccountID, hash);
if (isTrackExpense) {
data = getUpdateTrackExpenseParams(transactionID, transactionThreadReport?.reportID, transactionChanges, effectivePolicy, delegateAccountID, hash);
} else {
data = getUpdateMoneyRequestParams({
transactionID,
transactionThreadReport,
iouReport: parentReport,
transactionChanges,
policy,
policy: effectivePolicy,
policyTagList: policyTags,
// TODO: Replace getPolicyTagsData (https://github.com/Expensify/App/issues/72721) with useOnyx hook
reportPolicyTags: getPolicyTagsData(parentReport?.policyID),
Expand Down Expand Up @@ -1032,6 +1135,7 @@ function updateMoneyRequestDistanceRate({
transactionThreadReport,
parentReport,
rateID,
created,
policy,
policyTagList,
policyCategories,
Expand All @@ -1043,11 +1147,17 @@ function updateMoneyRequestDistanceRate({
updatedTaxValue,
parentReportNextStep,
delegateAccountID,
hash,
transactions,
transactionViolations,
isOffline,
shouldBuildOptimisticModifiedExpenseReportAction = true,
}: {
transaction: OnyxEntry<OnyxTypes.Transaction>;
transactionThreadReport: OnyxEntry<OnyxTypes.Report>;
parentReport: OnyxEntry<OnyxTypes.Report>;
rateID: string;
created?: string;
policy: OnyxEntry<OnyxTypes.Policy>;
policyTagList: OnyxEntry<OnyxTypes.PolicyTagLists>;
policyCategories: OnyxEntry<OnyxTypes.PolicyCategories>;
Expand All @@ -1059,9 +1169,15 @@ function updateMoneyRequestDistanceRate({
updatedTaxValue?: string;
parentReportNextStep: OnyxEntry<OnyxTypes.ReportNextStepDeprecated>;
delegateAccountID: number | undefined;
hash?: number;
transactions?: OnyxCollection<OnyxTypes.Transaction>;
transactionViolations?: OnyxCollection<OnyxTypes.TransactionViolations>;
isOffline?: boolean;
shouldBuildOptimisticModifiedExpenseReportAction?: boolean;
}) {
const transactionChanges: TransactionChanges = {
customUnitRateID: rateID,
...(created ? {created} : {}),
...(typeof updatedTaxAmount === 'number' ? {taxAmount: updatedTaxAmount} : {}),
...(updatedTaxCode ? {taxCode: updatedTaxCode} : {}),
...(updatedTaxValue ? {taxValue: updatedTaxValue} : {}),
Expand All @@ -1083,7 +1199,15 @@ function updateMoneyRequestDistanceRate({
let data: UpdateMoneyRequestData<UpdateMoneyRequestDataKeys>;

if (isTrackExpenseReport(transactionThreadReport) && isSelfDM(parentReport)) {
data = getUpdateTrackExpenseParams(transaction?.transactionID, transactionThreadReport?.reportID, transactionChanges, policy, delegateAccountID);
data = getUpdateTrackExpenseParams(
transaction?.transactionID,
transactionThreadReport?.reportID,
transactionChanges,
policy,
delegateAccountID,
hash,
shouldBuildOptimisticModifiedExpenseReportAction,
);
} else {
data = getUpdateMoneyRequestParams({
transactionID: transaction?.transactionID,
Expand All @@ -1099,13 +1223,20 @@ function updateMoneyRequestDistanceRate({
currentUserEmailParam,
isASAPSubmitBetaEnabled,
iouReportNextStep: parentReportNextStep,
isOffline,
hash,
delegateAccountID,
shouldBuildOptimisticModifiedExpenseReportAction,
});
if (created && transaction?.transactionID && transactions && transactionViolations) {
removeTransactionFromDuplicateTransactionViolation(data.onyxData, transaction.transactionID, transactions, transactionViolations);
}
}
const {params, onyxData} = data;
// `taxAmount` & `taxCode` only needs to be updated in the optimistic data, so we need to remove them from the params
const {taxAmount, taxCode, ...paramsWithoutTaxUpdated} = params;
API.write(WRITE_COMMANDS.UPDATE_MONEY_REQUEST_DISTANCE_RATE, paramsWithoutTaxUpdated, onyxData);
// `taxAmount`, `taxCode`, and optionally `created` only need to be updated in the optimistic data, so we need to remove them from the params
const {taxAmount, taxCode, created: createdParam, ...paramsWithoutOptimisticOnlyData} = params;
const paramsForAPI = createdParam ? {...paramsWithoutOptimisticOnlyData, created: createdParam} : paramsWithoutOptimisticOnlyData;
API.write(WRITE_COMMANDS.UPDATE_MONEY_REQUEST_DISTANCE_RATE, paramsForAPI, onyxData);
}

type UpdateMoneyRequestAmountAndCurrencyParams = {
Expand Down
3 changes: 3 additions & 0 deletions src/libs/actions/TransactionInlineEdit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ type GetIouParamsInput = {
parentReportAction: OnyxEntry<ReportAction>;
transactionThreadReport: OnyxEntry<Report>;
policy: OnyxEntry<Policy>;
policyForTrackExpense?: OnyxEntry<Policy>;
policyCategories: OnyxEntry<PolicyCategories>;
policyTags: OnyxEntry<PolicyTagLists>;
policyRecentlyUsedCategories: OnyxEntry<RecentlyUsedCategories>;
Expand Down Expand Up @@ -205,6 +206,7 @@ function getIouParamsForTransaction({
parentReportAction,
transactionThreadReport,
policy,
policyForTrackExpense,
policyCategories,
policyTags,
policyRecentlyUsedCategories,
Expand Down Expand Up @@ -267,6 +269,7 @@ function getIouParamsForTransaction({
transactionThreadReport: resolvedTransactionThreadReport,
parentReport: resolvedParentReport,
policy,
policyForTrackExpense,
policyCategories,
parentReportNextStep,
currentUserAccountIDParam: currentUserAccountID,
Expand Down
4 changes: 3 additions & 1 deletion src/pages/iou/request/step/IOURequestStepDate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,16 @@ function IOURequestStepDate({
const isTransactionDraft = shouldUseTransactionDraft(action);

if (isEditing) {
const effectivePolicy = isTrackExpense ? policyForTrackExpense : policy;
updateMoneyRequestDate({
transactionID,
transactionThreadReport: report,
parentReport,
transactions: duplicateTransactions,
transactionViolations: duplicateTransactionViolations,
value: newCreated,
policy,
policy: effectivePolicy,
policyForTrackExpense,
policyTags,
policyCategories,
currentUserAccountIDParam: currentUserPersonalDetails.accountID,
Expand Down
Loading
Loading