App icon indicating copy to clipboard operation
App copied to clipboard

[$175] Tax amount is shown 0 even if the tax rate is not 0

Open applause-bot[bot] opened this issue 1 month ago β€’ 34 comments

If you haven’t already, check out our contributing guidelines for onboarding. To join our Slack channel, fill out this form.


Version Number: v9.5.26-0 Reproducible in staging?: Yes Reproducible in production?: Yes If this was caught during regression testing, add the test name, ID and link from BrowserStack: https://github.com/Expensify/App/pull/101674 Email or phone of affected tester (no customers): [email protected] Issue reported by: Applause Internal Team Bug source: Pull Request QA execution Device used: Windows 11 pro /Chrome , IPhone 15 pro/ IOS 27.0.1 App Component: Money Requests

Action Performed:

Prerequisites:

  • Use an Expensifail account with a workspace that has Taxes, Distance rates, and Rules enabled.
  • Under Taxes, create a non-default rate TaxA at 10%. Leave the workspace default as Tax exempt (0%).
  • Under Distance rates, enable Track tax and create a rate Rate-NoTax, leaving its tax rate unset.
  • Under Rules > Add rule > Apply expense defaults, add a rule with Category Car and Tax TaxA. Save.

Steps:

  1. Navigate to staging.new.expensify.com
  2. Open the workspace chat and start a distance expense.
  3. Set a route and continue to the confirm page.
  4. Set Rate to Rate-NoTax.
  5. Set Category to Car.

Expected Result:

The Tax field should show TaxA (10%) and Tax amount is 10% of the expense amount, not 0

Actual Result:

Tax amount is zero

Workaround:

Unknown

Platforms:

  • [ ] Android: App
  • [ ] Android: mWeb Chrome
  • [x] iOS: App
  • [ ] iOS: mWeb Safari
  • [ ] iOS: mWeb Chrome
  • [x] Windows: Chrome
  • [ ] MacOS: Chrome Safari

Screenshots/Videos

https://github.com/user-attachments/assets/17eb19b3-5bc1-442c-b87a-c229df77fde2

View all open jobs on GitHub

Applause Internal Information Exported by: Rizky Gunawan Bug ID: 7300056

Upwork Automation - Do Not Edit
Issue OwnerCurrent Issue Owner: @getusha

applause-bot[bot] avatar Oct 30 '26 04:10 applause-bot[bot]

While Applause doesn't embed attachments for this bug, here are the links for quick view: https://platform.applause.com/services/links/v1/external/f3eee2bbb69e59e309e73527201cdac88126cc225529a49b6651ea7cb3d059da

applause-bot[bot] avatar Oct 30 '26 04:10 applause-bot[bot]

Proposal

What is the root cause of that problem?

The tax rate is now correct, but the amount it is applied to is 0.

For distance expenses, tax is only charged on the rate's "Tax reclaimable on" share. getTaxableAmount multiplies the distance amount by the rate's taxClaimablePercentage. It falls back to 0 when that field is unset.

Rate-NoTax has no tax rate. The workspace page only shows "Tax reclaimable on" after a rate has a tax rate (PolicyDistanceRateDetailsPage.tsx:273). So on this rate the field can never be set, and the taxable amount is always 0.

Apply category tax rules to distance expenses without a rate tax (deployed in 9.5.26-0) made the category rule pick TaxA for these rates. It still runs the amount through getTaxableAmount in useTaxAmount.ts:94 and tax.ts:158. The result is 10% Γ— 0 = 0. That PR's unit tests only check the tax code and value, not the amount.

What changes do you think we should make in order to solve the problem?

When a distance rate has no selectable tax rate of its own, tax the full distance amount instead of 0.

  1. In DistanceRequestUtils.getTaxableAmount, use 1 as the claimable share when the rate has no selectable taxRateExternalID. Keep the current behavior for rates that do have a tax rate.
  2. Apply the same rule in the split flow. SplitExpenseItems.ts:327 copies the same taxClaimablePercentage ?? 0 logic.
  3. Add unit tests that check taxAmount for a category-rule tax on a rate with no tax: 10% of the distance amount.

What alternative solutions did you explore? (Optional)

Default taxClaimablePercentage to 1 for every rate. This also changes rates that have a tax rate but no reclaimable amount. Today those produce 0 tax, and changing that is outside this bug's scope.

Open question and confidence
  • Confidence: medium. The code path is clear, but I did not reproduce it in the app.
  • Product should confirm that a rule-applied tax on a rate with no tax rate covers the full amount. The issue's expected result and the PR's test steps both say so.
  • The backend may recalculate distance tax when the expense is created. If it uses the same reclaimable share, it may also need a change outside App.
Related issue

Next Steps for Contributor+ team: To accept: @MelvinBot implement [this](https://github.com/Expensify/App/issues/105451) to create a draft PR. To refine: @MelvinBot <your feedback> To reject: Explain why you are rejecting Melvin's proposal.


view run

MelvinBot avatar Oct 30 '26 04:10 MelvinBot

Triggered auto assignment to Contributor-plus team member for initial proposal review - @aimane-chnaif (External)

melvin-bot[bot] avatar Oct 30 '26 04:10 melvin-bot[bot]

Job added to Upwork: https://www.upwork.com/jobs/~022116020332749301136

melvin-bot[bot] avatar Oct 30 '26 04:10 melvin-bot[bot]

Please re-state the problem that we are trying to solve in this issue.

When creating a distance expense using a distance rate that does not have its own tax rate configured (Rate-NoTax), a category-based expense rule correctly applies TaxA (10%). However, the calculated tax amount remains 0 instead of being calculated against the full distance expense amount.

The expected behaviour is that when a tax is applied through a category rule and the selected distance rate has no active rate-level tax, the tax should be calculated on 100% of the distance expense amount.

What is the root cause of that problem?

In src/libs/DistanceRequestUtils.ts, the getTaxableAmount function calculates the taxable amount by multiplying the distance expense amount by the rate's taxClaimablePercentage.

When taxClaimablePercentage is undefined, the calculation falls back to CONST.DEFAULT_NUMBER_ID (0).

For distance rates without a rate-level tax, the workspace settings do not expose the "Tax reclaimable on" field, so this percentage remains undefined. Consequently, the taxable amount becomes zero even when a category-based tax has been applied correctly.

There is also a related issue in src/libs/actions/IOU/SplitExpenseItems.ts, where the same fallback calculation is duplicated.

Simply changing the fallback to 1 would introduce a regression for distance rates that have an active rate-level tax but an undefined reclaimable percentage. Additionally, checking only whether taxRateExternalID exists would not handle stale tax IDs pointing to disabled or deleted tax rates.

What changes do you think we should make in order to solve the problem?

  1. Introduce a reusable getTaxClaimablePercentage helper in src/libs/DistanceRequestUtils.ts.

  2. Determine whether the distance rate has an active, selectable rate-level tax using the existing isSelectableTaxCode utility.

  3. Return 1 (100%) when the distance rate has no active rate-level tax, including when its tax ID is missing or no longer selectable.

  4. When an active rate-level tax exists, preserve the existing taxClaimablePercentage value and its current fallback to CONST.DEFAULT_NUMBER_ID. This ensures that existing reclaimability behaviour remains unchanged.

  5. Reuse this helper in both getTaxableAmount and src/libs/actions/IOU/SplitExpenseItems.ts so regular and split distance expenses follow the same calculation logic.

  6. Add regression tests covering:

    • Distance rates without a rate-level tax.
    • Distance rates with disabled or unavailable rate-level tax IDs.
    • Active rate-level taxes with a configured reclaimable percentage.
    • Active rate-level taxes with an undefined reclaimable percentage.
    • Split distance expenses with category-based taxes.
  7. Verify that the calculated tax remains correct after saving and reopening a distance expense, and investigate whether any backend recalculation requires a corresponding change.

What alternative solutions did you explore?

Changing the fallback directly from CONST.DEFAULT_NUMBER_ID to 1 was considered, but it would incorrectly change the behaviour of existing distance rates with active rate-level taxes and an undefined reclaimable percentage.

Checking only whether taxRateExternalID is present was also considered, but this would not handle stale tax IDs associated with disabled or deleted tax rates.

Centralizing the decision in a reusable helper provides a consistent calculation across the regular distance expense, tax update, and split expense flows without changing the existing getTaxableAmount function signature.

Aswathypalakkal avatar Oct 30 '26 05:10 Aswathypalakkal

πŸ“£ @Aswathypalakkal! πŸ“£ Hey, it seems we don’t have your contributor details yet! You'll only have to do this once, and this is how we'll hire you on Upwork. Please follow these steps:

  1. Make sure you've read and understood the contributing guidelines.
  2. Get the email address used to login to your Expensify account. If you don't already have an Expensify account, create one here. If you have multiple accounts (e.g. one for testing), please use your main account email.
  3. Get the link to your Upwork profile. It's necessary because we only pay via Upwork. You can access it by logging in, and then clicking on your name. It'll look like this. If you don't already have an account, sign up for one here.
  4. Copy the format below and paste it in a comment on this issue. Replace the placeholder text with your actual details. Screen Shot 2022-11-16 at 4 42 54 PM Format:
Contributor details
Your Expensify account email: <REPLACE EMAIL HERE>
Upwork Profile Link: <REPLACE LINK HERE>

melvin-bot[bot] avatar Oct 30 '26 05:10 melvin-bot[bot]

Proposal

Please re-state the problem that we are trying to solve in this issue.

When creating a distance expense using a distance rate that has no rate-level tax rate configured (Rate-NoTax) and selecting a category (Car) that has an expense default rule applying TaxA (10%), the Tax field displays TaxA (10%) as expected, but the calculated Tax amount displays 0 instead of 10% of the distance expense amount.

What is the root cause of that problem?

  1. In src/libs/DistanceRequestUtils.ts (getTaxableAmount, lines 810–821), the taxable amount for a distance expense is calculated by multiplying the reimbursable distance amount by customUnitRate.attributes?.taxClaimablePercentage, falling back to CONST.DEFAULT_NUMBER_ID (0):
    const taxClaimablePercentage = customUnitRate.attributes?.taxClaimablePercentage ?? CONST.DEFAULT_NUMBER_ID;
    return amount * taxClaimablePercentage;
    
  2. In the workspace distance rate settings (PolicyDistanceRateDetailsPage.tsx:273), the "Tax reclaimable on" (taxClaimablePercentage) field is only shown and editable when the distance rate has its own rate-level tax configured (!!taxRate). On a rate where tax is unset (Rate-NoTax), taxClaimablePercentage is never configured and remains undefined.
  3. In PR #101674 ("Apply category tax rules to distance expenses without a rate tax"), getDistanceCategoryTaxCode / getDistanceRateTaxUpdates in src/libs/TransactionUtils/tax.ts and useTaxAmount.ts were updated so that a category tax rule correctly applies TaxA (10%) when the distance rate has no selectable rate-level tax rate (!hasSelectableRateTaxCode(policy, customUnitRateID)).
  4. However, both useTaxAmount.ts (line 77) and getDistanceRateTaxUpdates (tax.ts, line 118) pass the distance through DistanceRequestUtils.getTaxableAmount(policy, customUnitRateID, taxableDistance). Because taxClaimablePercentage is undefined, getTaxableAmount multiplies the amount by 0, causing calculateTaxAmount('10%', 0, decimals) to evaluate to 0.
  5. PR #101674's tests in tests/unit/TransactionUtilsTest.ts only asserted categoryTaxCode and categoryTaxValue, while tests/unit/hooks/useTaxAmount.test.ts mocked getTaxableAmount returning 100, which masked the regression.
  6. In addition, src/libs/actions/IOU/SplitExpenseItems.ts (line 327) duplicates splitAmount * (splitCustomUnitRate?.attributes?.taxClaimablePercentage ?? CONST.DEFAULT_NUMBER_ID), which produces the same 0 tax amount when splitting distance expenses.

Key Distinction / Flaw in Prior Proposals:

  • Simply falling back to 1 unconditionally breaks existing test tests/unit/DistanceRequestUtilsTest.ts:911 (and IOURequestStepConfirmationPageTest.tsx:1076), where a distance rate explicitly has a rate-level tax configured but "Tax reclaimable on" was omitted / left at 0.
  • Checking if (!hasActiveRateTaxCode) return 1; before checking taxClaimablePercentage (as proposed in earlier comments) creates a critical regression in tests/actions/IOU/SplitExpenseItemsTest.ts:264 (applies the distance rate's claimable percentage to the fallback tax). In that test, a distance rate has a configured taxClaimablePercentage: 0.5 whose rate-level tax was disabled; overwriting the configured percentage with 1 breaks the test expectation (expected 455, received 909).
  • The configured taxClaimablePercentage must always be respected first when defined (customUnitRate?.attributes?.taxClaimablePercentage ?? ...). Only when taxClaimablePercentage is undefined should the fallback check whether the rate has an active selectable rate-level tax (0) or not (1).

What changes do you think we should make in order to solve the problem?

1. Centralize hasSelectableRateTaxCode in src/libs/PolicyUtils/tax.ts & export via PolicyUtils

In src/libs/PolicyUtils/tax.ts, export hasSelectableRateTaxCode so it can be shared across TransactionUtils, DistanceRequestUtils, and SplitExpenseItems:

/**
 * Whether a distance rate carries a tax rate the user can still pick. Disabling a tax rate leaves the rate pointing at
 * it, so a rate the user can no longer pick is treated the same as a rate that never had one.
 */
function hasSelectableRateTaxCode(policy: OnyxEntry<Policy>, customUnitRateID: string): boolean {
    const rateTaxCode = getDistanceRateCustomUnitRate(policy, customUnitRateID)?.attributes?.taxRateExternalID;
    if (!rateTaxCode) {
        return false;
    }
    return !policy?.taxRates?.taxes || isSelectableTaxCode(policy, rateTaxCode);
}

(Export hasSelectableRateTaxCode from src/libs/PolicyUtils/index.ts and replace the local private helper in src/libs/TransactionUtils/tax.ts with this import).

2. Introduce getTaxClaimablePercentage in src/libs/DistanceRequestUtils.ts and update getTaxableAmount

In src/libs/DistanceRequestUtils.ts:

/**
 * Get the tax claimable percentage for a distance rate.
 * When a distance rate has no selectable rate-level tax rate (e.g. Rate-NoTax or a disabled tax rate),
 * taxes applied via category rules, workspace defaults, or manual selection tax 100% (1) of the distance amount.
 * When a rate has an active selectable rate-level tax, use the configured taxClaimablePercentage, falling back to 0.
 */
function getTaxClaimablePercentage(policy: OnyxEntry<Policy>, customUnitRateID: string): number {
    const customUnitRate = getDistanceRateCustomUnitRate(policy, customUnitRateID);
    return customUnitRate?.attributes?.taxClaimablePercentage ?? (hasSelectableRateTaxCode(policy, customUnitRateID) ? CONST.DEFAULT_NUMBER_ID : 1);
}

function getTaxableAmount(policy: OnyxEntry<Policy>, customUnitRateID: string, distance: number) {
    const distanceUnit = getDistanceRateCustomUnit(policy);
    const customUnitRate = getDistanceRateCustomUnitRate(policy, customUnitRateID);
    if (!distanceUnit?.customUnitID || !customUnitRate) {
        return 0;
    }
    const unit = distanceUnit?.attributes?.unit ?? CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES;
    const rate = customUnitRate?.rate ?? CONST.DEFAULT_NUMBER_ID;
    const amount = getDistanceRequestAmount(distance, unit, rate);
    const taxClaimablePercentage = getTaxClaimablePercentage(policy, customUnitRateID);
    return amount * taxClaimablePercentage;
}

Export getTaxClaimablePercentage on DistanceRequestUtils.

3. Reuse getTaxClaimablePercentage in src/libs/actions/IOU/SplitExpenseItems.ts

In src/libs/actions/IOU/SplitExpenseItems.ts (lines 326–328):

const taxableAmount = isDistanceRequestTransactionUtils(transaction) && splitCustomUnitRateID
    ? splitAmount * DistanceRequestUtils.getTaxClaimablePercentage(policy, splitCustomUnitRateID)
    : splitAmount;

This ensures split distance expenses, regular distance expenses, and rate updates share identical calculation rules without code duplication.


What alternative solutions did you explore? (Optional)

  1. Passing a boolean flag (e.g. isCategoryTax) into getTaxableAmount: Rejected because manual tax selections and workspace default taxes on distance rates without a rate-level tax also need 100% taxable amount, and changing getTaxableAmount's signature breaks multiple callers and hooks.
  2. Hardcoding taxClaimablePercentage ?? 1: Rejected because it regresses distance rates configured with active rate-level taxes where the admin intentionally left "Tax reclaimable on" unset (tested in DistanceRequestUtilsTest.ts:911).
  3. Checking !hasActiveRateTaxCode before taxClaimablePercentage: Rejected because it regresses tests/actions/IOU/SplitExpenseItemsTest.ts:264, overwriting explicit 0.5 percentages with 1 whenever the rate tax is disabled.

Automated Tests

1. In tests/unit/DistanceRequestUtilsTest.ts

Add test cases in describe('getTaxableAmount'):

it('should return 100% of distance amount when distance rate has no rate-level tax configured', () => {
    // Given a policy distance rate without a rate-level tax (Rate-NoTax)
    const rateWithoutTaxID = 'B593F3FBBB0BD'; // attributes: {}
    const expectedAmount = DistanceRequestUtils.getDistanceRequestAmount(totalDistance, distanceUnit, 900);

    // When computing the taxable amount
    const result = DistanceRequestUtils.getTaxableAmount(FAKE_POLICY, rateWithoutTaxID, totalDistance);

    // Then the full distance expense amount is taxable (factor 1)
    expect(result).toBe(expectedAmount);
});

it('should return 100% of distance amount when distance rate tax rate is disabled and reclaimable percentage is undefined', () => {
    // Given a policy distance rate whose tax rate is disabled and reclaimable percentage is undefined
    const policyWithDisabledTaxRate: Policy = {
        ...FAKE_POLICY,
        taxRates: {
            ...CONST.DEFAULT_TAX,
            taxes: {
                ...CONST.DEFAULT_TAX.taxes,
                id_TAX_RATE_1: {name: 'Tax Rate 1', value: '10%', isDisabled: true},
            },
        },
    };
    const expectedAmount = DistanceRequestUtils.getDistanceRequestAmount(totalDistance, distanceUnit, 72.5);

    // When computing the taxable amount
    const result = DistanceRequestUtils.getTaxableAmount(policyWithDisabledTaxRate, customUnitRateIDWithOutTaxClaimablePercentage, totalDistance);

    // Then the fallback is 1 because the rate has no active selectable rate tax
    expect(result).toBe(expectedAmount);
});

2. In tests/unit/TransactionUtilsTest.ts

In describe('distance expenses'), update the test case to assert categoryTaxAmount:

it('should apply the category rule when distance tax tracking is on and the rate has no tax rate of its own', () => {
    // Given a policy whose distance rates track tax, where the selected rate has no tax rate of its own
    const category = 'Advertising';
    const fakePolicy = buildDistancePolicy(category, true);

    // When retrieving the tax from the associated category
    const {categoryTaxCode, categoryTaxAmount, categoryTaxValue} = TransactionUtils.getCategoryTaxDetails(
        category,
        buildDistanceTransaction(),
        fakePolicy,
        getCurrencyDecimalsLocal,
    );

    // Then the category rule fills the slot the rate left empty, and tax amount is calculated on 100% of the distance amount
    expect(categoryTaxCode).toBe('id_TAX_RATE_1');
    expect(categoryTaxValue).toBe('5%');
    expect(categoryTaxAmount).toBeGreaterThan(0);
});

Contributor details

Your Expensify account email: [email protected]
Upwork Profile Link: https://www.upwork.com/freelancers/~014bb7d4c250a1fe03

Abdibaye avatar Oct 30 '26 06:10 Abdibaye

⚠️ Missing/invalid email or upwork profile link. Please make sure you add both your Expensify email and Upwork profile link in the format specified.

melvin-bot[bot] avatar Oct 30 '26 06:10 melvin-bot[bot]

πŸ“£ @jashory! πŸ“£ Hey, it seems we don’t have your contributor details yet! You'll only have to do this once, and this is how we'll hire you on Upwork. Please follow these steps:

  1. Make sure you've read and understood the contributing guidelines.
  2. Get the email address used to login to your Expensify account. If you don't already have an Expensify account, create one here. If you have multiple accounts (e.g. one for testing), please use your main account email.
  3. Get the link to your Upwork profile. It's necessary because we only pay via Upwork. You can access it by logging in, and then clicking on your name. It'll look like this. If you don't already have an account, sign up for one here.
  4. Copy the format below and paste it in a comment on this issue. Replace the placeholder text with your actual details. Screen Shot 2022-11-16 at 4 42 54 PM Format:
Contributor details
Your Expensify account email: <REPLACE EMAIL HERE>
Upwork Profile Link: <REPLACE LINK HERE>

melvin-bot[bot] avatar Oct 31 '26 21:10 melvin-bot[bot]

Proposal

Please re-state the problem that we are trying to solve in this issue.

When creating or editing a distance expense in a workspace where:

  1. Taxes and Distance rates are enabled,
  2. A distance rate (Rate-NoTax) has no rate-level tax configured (attributes.taxRateExternalID is undefined),
  3. A category (Car) with an expense default tax rule (e.g. TaxA (10%)) is selected, The Tax rate correctly displays TaxA (10%), but the calculated Tax amount is displayed as 0 instead of 10% of the distance expense amount.

What is the root cause of that problem?

  1. In src/libs/DistanceRequestUtils.ts (getTaxableAmount):
    function getTaxableAmount(policy: OnyxEntry<Policy>, customUnitRateID: string, distance: number) {
        ...
        const amount = getDistanceRequestAmount(distance, unit, rate);
        const taxClaimablePercentage = customUnitRate.attributes?.taxClaimablePercentage ?? CONST.DEFAULT_NUMBER_ID;
        return amount * taxClaimablePercentage;
    }
    
  2. When a distance rate does not have a rate-level tax configured (Rate-NoTax), the workspace distance rate settings do not show or set taxClaimablePercentage (PolicyDistanceRateDetailsPage.tsx:273 only renders the claimable percentage input when !!taxRate). As a result, customUnitRate.attributes?.taxClaimablePercentage is undefined.
  3. Because taxClaimablePercentage ?? CONST.DEFAULT_NUMBER_ID defaults undefined to 0, getTaxableAmount returns amount * 0 = 0.
  4. In useTaxAmount.ts (line 77) and getDistanceRateTaxUpdates (tax.ts, line 118), the taxable distance amount returned from getTaxableAmount is 0, so calculateTaxAmount(taxPercentage, 0, decimals) evaluates to 0.
  5. For distance rates without a rate-level tax, taxes applied via category rules, workspace defaults, or manual tax selections apply to 100% of the distance amount. Defaulting taxClaimablePercentage to 0 when there is no rate-level tax causes the tax amount to zero out.

What changes do you think we should make in order to solve the problem?

  1. In src/libs/DistanceRequestUtils.ts: In getTaxableAmount, check whether the custom unit rate has an active selectable rate-level tax (reusing hasSelectableRateTaxCode from src/libs/PolicyUtils/tax.ts):

    • When the rate has an active selectable rate-level tax, fall back to CONST.DEFAULT_NUMBER_ID (0) if taxClaimablePercentage is undefined (preserving existing rate-level tax behavior and passing DistanceRequestUtilsTest.ts:911).
    • When the rate has no selectable rate-level tax (such as Rate-NoTax or disabled rate taxes), fall back to 1 (100% taxable amount):
    function getTaxableAmount(policy: OnyxEntry<Policy>, customUnitRateID: string, distance: number) {
        const distanceUnit = getDistanceRateCustomUnit(policy);
        const customUnitRate = getDistanceRateCustomUnitRate(policy, customUnitRateID);
        if (!distanceUnit?.customUnitID || !customUnitRate) {
            return 0;
        }
        const unit = distanceUnit?.attributes?.unit ?? CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES;
        const rate = customUnitRate?.rate ?? CONST.DEFAULT_NUMBER_ID;
        const amount = getDistanceRequestAmount(distance, unit, rate);
        const hasRateTax = hasSelectableRateTaxCode(policy, customUnitRateID);
        const taxClaimablePercentage = customUnitRate.attributes?.taxClaimablePercentage ?? (hasRateTax ? CONST.DEFAULT_NUMBER_ID : 1);
        return amount * taxClaimablePercentage;
    }
    
  2. In src/libs/actions/IOU/SplitExpenseItems.ts: Update line 327 to use the same logic when calculating taxable amount for split distance expenses:

    const hasRateTax = hasSelectableRateTaxCode(policy, splitCustomUnitRateID);
    const taxClaimablePercentage = splitCustomUnitRate?.attributes?.taxClaimablePercentage ?? (hasRateTax ? CONST.DEFAULT_NUMBER_ID : 1);
    const taxableAmount = isDistanceRequestTransactionUtils(transaction) && splitCustomUnitRateID
        ? splitAmount * taxClaimablePercentage
        : splitAmount;
    

Automated Tests

In tests/unit/DistanceRequestUtilsTest.ts: Add a test case in describe('getTaxableAmount'):

it('should return 100% of distance amount when distance rate has no rate-level tax configured', () => {
    // Given a rate without rate-level tax (attributes: {})
    const result = DistanceRequestUtils.getTaxableAmount(FAKE_POLICY, 'B593F3FBBB0BD', totalDistance);
    const expectedAmount = DistanceRequestUtils.getDistanceRequestAmount(totalDistance, distanceUnit, 900);
    expect(result).toBe(expectedAmount);
});

What alternative solutions did you explore? (Optional)

  • Unconditionally defaulting taxClaimablePercentage ?? 1: Rejected because it breaks tests/unit/DistanceRequestUtilsTest.ts:911 where distance rates with rate-level taxes intentionally omit the claimable percentage. Conditioning on hasSelectableRateTaxCode correctly preserves rate-level tax behavior while ensuring category rules and manual taxes apply to 100% of non-taxed distance rates.

Contributor details

Your Expensify account email: [email protected] Upwork Profile Link: https://www.upwork.com/freelancers/~0105126232377c4c5c

ayolyn avatar Nov 01 '26 10:11 ayolyn

βœ… Contributor details stored successfully. Thank you for contributing to Expensify!

melvin-bot[bot] avatar Nov 01 '26 10:11 melvin-bot[bot]

Contributor details Your Expensify account email: [email protected] Upwork Profile Link: https://www.upwork.com/freelancers/~015b34e130d9aad41d

Proposal

Please re-state the problem that we are trying to solve in this issue.

When creating or editing a distance expense with a distance rate that has no rate-level tax configured (Rate-NoTax) and selecting a category with an expense rule applying a tax rate (e.g., TaxA 10%), the Tax field correctly updates to TaxA (10%), but the Tax amount displays as 0 instead of 10% of the distance expense amount.

What is the root cause of that problem?

In src/libs/DistanceRequestUtils.ts:819-820 (getTaxableAmount):

const taxClaimablePercentage = customUnitRate?.attributes?.taxClaimablePercentage ?? 0;
return amount * taxClaimablePercentage;
  1. In PolicyDistanceRateDetailsPage.tsx:273, the "Tax reclaimable on" field is only displayed and editable when a distance rate has a rate-level tax rate selected (taxRateExternalID). When a distance rate has no tax rate (Rate-NoTax), taxClaimablePercentage is undefined.
  2. In PR #101674, category rules were enabled to apply tax to distance expenses without rate taxes. However, getTaxableAmount falls back to 0 when taxClaimablePercentage is unset (amount * 0 = 0). Consequently, in useTaxAmount.ts and tax.ts, calculating 10% * taxableAmount results in 10% * 0 = 0.
  3. The identical unhandled fallback exists in src/libs/actions/IOU/SplitExpenseItems.ts:327:
const taxableAmount = isDistanceRequestTransactionUtils(transaction)
    ? splitAmount * (splitCustomUnitRate?.attributes?.taxClaimablePercentage ?? 0)
    : splitAmount;

What changes do you think we should make in order to solve the problem?

  1. In src/libs/DistanceRequestUtils.ts: When a distance rate does not have a selectable rate-level taxRateExternalID, any tax applied to the distance expense (via category rules, default policy tax, or manual entry) should apply to 100% (1) of the distance amount. Only when a selectable rate-level tax exists should it respect taxClaimablePercentage ?? 0:

    function getTaxClaimablePercentage(policy: OnyxEntry<Policy>, customUnitRate: OnyxEntry<Rate>): number {
        if (!customUnitRate) {
            return CONST.DEFAULT_NUMBER_ID;
        }
        const rateTaxCode = customUnitRate.attributes?.taxRateExternalID;
        const taxes = policy?.taxRates?.taxes;
        const rateTaxRate = rateTaxCode ? taxes?.[rateTaxCode] : undefined;
        const hasSelectableRateTax =
            !!rateTaxCode &&
            !!rateTaxRate &&
            !rateTaxRate.isDisabled &&
            rateTaxRate.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE;
    
        if (!hasSelectableRateTax) {
            return 1;
        }
        return customUnitRate.attributes?.taxClaimablePercentage ?? CONST.DEFAULT_NUMBER_ID;
    }
    
    function getTaxableAmount(policy: OnyxEntry<Policy>, customUnitRateID: string, distance: number) {
        const distanceUnit = getDistanceRateCustomUnit(policy);
        const customUnitRate = getDistanceRateCustomUnitRate(policy, customUnitRateID);
        if (!distanceUnit?.customUnitID || !customUnitRate) {
            return 0;
        }
        const unit = distanceUnit?.attributes?.unit ?? CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES;
        const rate = customUnitRate?.rate ?? CONST.DEFAULT_NUMBER_ID;
        const amount = getDistanceRequestAmount(distance, unit, rate);
        const taxClaimablePercentage = getTaxClaimablePercentage(policy, customUnitRate);
        return amount * taxClaimablePercentage;
    }
    
  2. In src/libs/actions/IOU/SplitExpenseItems.ts:326-328: Use DistanceRequestUtils.getTaxClaimablePercentage(policy, splitCustomUnitRate):

    const taxableAmount = isDistanceRequestTransactionUtils(transaction)
        ? splitAmount * DistanceRequestUtils.getTaxClaimablePercentage(policy, splitCustomUnitRate)
        : splitAmount;
    
  3. Automated Unit Tests: Add unit tests in tests/unit/TaxUtilsTest.ts and tests/unit/DistanceRequestUtilsTest.ts:

    • Test that for distance rates without a rate-level tax, getTaxableAmount evaluates to 100% of the distance amount when a category rule tax is present.
    • Test that distance rates with an active rate-level tax preserve their configured taxClaimablePercentage.

What alternative solutions did you explore? (Optional)

Defaulting taxClaimablePercentage to 1 unconditionally across all rates was explored, but rejected because rates that explicitly configure a rate tax without reclaimable percentage would be unintentionally changed. Gating the 1 fallback strictly to rates without selectable rate-level tax preserves existing behavior.

sumit-2007-git avatar Nov 01 '26 14:11 sumit-2007-git

βœ… Contributor details stored successfully. Thank you for contributing to Expensify!

melvin-bot[bot] avatar Nov 01 '26 14:11 melvin-bot[bot]

Proposal

Please re-state the problem that we are trying to solve in this issue.

When a workspace has Distance rates and Taxes enabled, and an admin configures a custom tax rate on a distance rate without manually editing the Tax claimable percentage (leaving customUnitRate.attributes.taxClaimablePercentage as undefined after setting the tax rate), creating a distance expense with that rate displays and calculates a tax amount of $0.00 even though a non-zero tax rate is selected.

What is the root cause of that problem?

  1. In src/libs/DistanceRequestUtils.ts:810–821, getTaxableAmount calculates the taxable portion of a distance expense using:
    const amount = getDistanceRequestAmount(distance, unit, rate);
    const taxClaimablePercentage = customUnitRate.attributes?.taxClaimablePercentage ?? CONST.DEFAULT_NUMBER_ID;
    return amount * taxClaimablePercentage;
    
  2. Likewise, in src/libs/actions/IOU/SplitExpenseItems.ts:326–328, initSplitExpenseItemData computes the taxable amount for distance split expenses when resolving stale tax values using the same fallback:
    const taxableAmount = isDistanceRequestTransactionUtils(transaction)
        ? splitAmount * (splitCustomUnitRate?.attributes?.taxClaimablePercentage ?? CONST.DEFAULT_NUMBER_ID)
        : splitAmount;
    
  3. Because CONST.DEFAULT_NUMBER_ID is 0, whenever customUnitRate.attributes?.taxClaimablePercentage is undefined (for example, when a tax rate is assigned to a distance rate before a claimable percentage is explicitly set, or when a distance expense uses a selectable workspace tax rate while taxClaimablePercentage is unset), getTaxableAmount multiplies amount by 0 and returns 0.
  4. Consequently, both src/components/MoneyRequestConfirmationList/hooks/useTaxAmount.ts:94–100 and src/libs/TransactionUtils/tax.ts:158–162 (getDistanceRateTaxUpdates) pass taxableAmount = 0 into calculateTaxAmount, resulting in a $0.00 tax amount despite a valid non-zero tax rate.

What changes do you think we should make in order to solve the problem?

We need a single, consistent helper that resolves the effective taxClaimablePercentage for a distance rate without breaking either:

Specifically:

  1. Add getDistanceRateTaxClaimablePercentage in src/libs/PolicyUtils/customUnits.ts (re-exported via src/libs/PolicyUtils/index.ts:11):
    • If customUnitRate?.attributes?.taxClaimablePercentage !== undefined, return customUnitRate.attributes.taxClaimablePercentage.
    • Otherwise, check whether distance tax tracking is enabled (getDistanceRateCustomUnit(policy)?.attributes?.taxEnabled) and whether either customUnitRate?.attributes?.taxRateExternalID or policy?.taxRates?.defaultExternalID is a selectable tax code on policy (isSelectableTaxCode). If so, default the claimable percentage to 1 (100%); otherwise return CONST.DEFAULT_NUMBER_ID (0).
    function getDistanceRateTaxClaimablePercentage(policy: OnyxEntry<Policy>, customUnitRate: Rate | undefined): number {
        if (customUnitRate?.attributes?.taxClaimablePercentage !== undefined) {
            return customUnitRate.attributes.taxClaimablePercentage;
        }
        const distanceUnit = getDistanceRateCustomUnit(policy);
        if (!distanceUnit?.attributes?.taxEnabled) {
            return CONST.DEFAULT_NUMBER_ID;
        }
        const rateTaxCode = customUnitRate?.attributes?.taxRateExternalID;
        const defaultTaxCode = policy?.taxRates?.defaultExternalID;
        if (isSelectableTaxCode(policy, rateTaxCode) || isSelectableTaxCode(policy, defaultTaxCode)) {
            return 1;
        }
        return CONST.DEFAULT_NUMBER_ID;
    }
    
  2. Update getTaxableAmount in src/libs/DistanceRequestUtils.ts:819:
    -    const taxClaimablePercentage = customUnitRate.attributes?.taxClaimablePercentage ?? CONST.DEFAULT_NUMBER_ID;
    +    const taxClaimablePercentage = getDistanceRateTaxClaimablePercentage(policy, customUnitRate);
    
  3. Update initSplitExpenseItemData in src/libs/actions/IOU/SplitExpenseItems.ts:326–328:
    -            const taxableAmount = isDistanceRequestTransactionUtils(transaction)
    -                ? splitAmount * (splitCustomUnitRate?.attributes?.taxClaimablePercentage ?? CONST.DEFAULT_NUMBER_ID)
    -                : splitAmount;
    +            const taxableAmount = isDistanceRequestTransactionUtils(transaction)
    +                ? splitAmount * getDistanceRateTaxClaimablePercentage(policy, splitCustomUnitRate)
    +                : splitAmount;
    
  4. Add unit tests in tests/unit/DistanceRequestUtilsTest.ts and tests/actions/IOU/SplitExpenseItemsTest.ts:
    • Verify getTaxableAmount returns 0 when taxClaimablePercentage is undefined and no selectable tax rate exists on policy (preserving DistanceRequestUtilsTest.ts:911), and returns 100% of the distance request amount when taxClaimablePercentage is undefined but the rate or policy has an active selectable tax rate.

What alternative solutions did you explore? (Optional)

  • Replacing ?? CONST.DEFAULT_NUMBER_ID directly with ?? 1 in DistanceRequestUtils.ts:819: Rejected because when a policy has no active selectable tax rates (such as FAKE_POLICY in DistanceRequestUtilsTest.ts:911–914), getTaxableAmount must return 0 when taxClaimablePercentage is undefined.
  • Falling back to 1 only when hasSelectableRateTaxCode(policy, customUnitRateID) is true: Rejected because when a distance rate's own taxRateExternalID is disabled or unset and the expense uses a category tax rule or the workspace's defaultExternalID (as tested in SplitExpenseItemsTest.ts:264–278), hasSelectableRateTaxCode is false even though a valid tax rate applies to the distance expense.

duongynhi000005-oss avatar Nov 01 '26 17:11 duongynhi000005-oss

@aimane-chnaif Whoops! This issue is 2 days overdue. Let's get this updated quick!

melvin-bot[bot] avatar Nov 02 '26 23:11 melvin-bot[bot]

@aimane-chnaif Whoops! This issue is 2 days overdue. Let's get this updated quick!

melvin-bot[bot] avatar Nov 03 '26 01:11 melvin-bot[bot]

@aimane-chnaif Whoops! This issue is 2 days overdue. Let's get this updated quick!

melvin-bot[bot] avatar Nov 03 '26 03:11 melvin-bot[bot]

@aimane-chnaif Eep! 4 days overdue now. Issues have feelings too...

melvin-bot[bot] avatar Nov 05 '26 00:11 melvin-bot[bot]

@aimane-chnaif Huh... This is 4 days overdue. Who can take care of this?

melvin-bot[bot] avatar Nov 05 '26 02:11 melvin-bot[bot]

@aimane-chnaif Eep! 4 days overdue now. Issues have feelings too...

melvin-bot[bot] avatar Nov 05 '26 03:11 melvin-bot[bot]