Skip to content
Closed
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
12 changes: 11 additions & 1 deletion src/components/MoneyRequestConfirmationList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import type * as OnyxTypes from '@src/types/onyx';
import type {Participant} from '@src/types/onyx/IOU';
import type {PaymentMethodType} from '@src/types/onyx/OriginalMessage';
import type {SplitShares} from '@src/types/onyx/Transaction';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import ButtonWithDropdownMenu from './ButtonWithDropdownMenu';
import type {DropdownOption} from './ButtonWithDropdownMenu/types';
import ConfirmedRoute from './ConfirmedRoute';
Expand Down Expand Up @@ -183,7 +184,7 @@ const getTaxAmount = (transaction: OnyxEntry<OnyxTypes.Transaction>, policy: Ony
if (isDistanceRequest) {
return DistanceRequestUtils.calculateTaxAmount(policy, transaction, TransactionUtils.getRateID(transaction) ?? '');
}
const defaultTaxCode = TransactionUtils.getDefaultTaxCode(policy, transaction) ?? '';
const defaultTaxCode = TransactionUtils.getDefaultTaxCode(policy, TransactionUtils.getCurrency(transaction)) ?? '';
const taxPercentage = TransactionUtils.getTaxValue(policy, transaction, transaction?.taxCode ?? defaultTaxCode) ?? '';
return TransactionUtils.calculateTaxAmount(taxPercentage, transaction?.amount ?? 0);
};
Expand Down Expand Up @@ -304,6 +305,15 @@ function MoneyRequestConfirmationList({

const shouldShowTax = isTaxTrackingEnabled(isPolicyExpenseChat, policy, isDistanceRequest) && !isTypeInvoice;

const transactionCurrency = TransactionUtils.getCurrency(transaction);
useEffect(() => {
if (isEmptyObject(policy) || !shouldShowTax || !taxRates) {
return;
}
const defaultTaxCode = TransactionUtils.getDefaultTaxCode(policy, transactionCurrency);
IOU.setMoneyRequestTaxRate(transactionID, defaultTaxCode ?? '');

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing the function IOU.setMoneyRequestTaxRate to receive only the currency instead of the whole transaction object allow us to only watch the currency instead of the transaction object, which is what we want to react to if it changes.

}, [policy, shouldShowTax, taxRates, transactionCurrency, transactionID]);

// A flag for showing the billable field
const shouldShowBillable = policy?.disabledFields?.defaultBillable === false;
const isMovingTransactionFromTrackExpense = IOUUtils.isMovingTransactionFromTrackExpense(action);
Expand Down
8 changes: 4 additions & 4 deletions src/libs/TransactionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -698,10 +698,10 @@ function getRateID(transaction: OnyxEntry<Transaction>): string | undefined {
* Gets the tax code based on selected currency.
* Returns policy default tax rate if transaction is in policy default currency, otherwise returns foreign default tax rate
*/
function getDefaultTaxCode(policy: OnyxEntry<Policy>, transaction: OnyxEntry<Transaction>, currency?: string | undefined) {
function getDefaultTaxCode(policy: OnyxEntry<Policy>, currency: string) {
const defaultExternalID = policy?.taxRates?.defaultExternalID;
const foreignTaxDefault = policy?.taxRates?.foreignTaxDefault;
return policy?.outputCurrency === (currency ?? getCurrency(transaction)) ? defaultExternalID : foreignTaxDefault;
return policy?.outputCurrency === currency ? defaultExternalID : foreignTaxDefault;
}

/**
Expand All @@ -719,7 +719,7 @@ function transformedTaxRates(policy: OnyxEntry<Policy> | undefined, transaction?
return defaultExternalID;
}

return policy && getDefaultTaxCode(policy, transaction);
return policy && getDefaultTaxCode(policy, getCurrency(transaction));
};

const getModifiedName = (data: TaxRate, code: string) =>
Expand All @@ -746,7 +746,7 @@ function getWorkspaceTaxesSettingsName(policy: OnyxEntry<Policy>, taxCode: strin
* Gets the tax name
*/
function getTaxName(policy: OnyxEntry<Policy>, transaction: OnyxEntry<Transaction>) {
const defaultTaxCode = getDefaultTaxCode(policy, transaction);
const defaultTaxCode = getDefaultTaxCode(policy, getCurrency(transaction));
return Object.values(transformedTaxRates(policy, transaction)).find((taxRate) => taxRate.code === (transaction?.taxCode ?? defaultTaxCode))?.modifiedName;
}

Expand Down
2 changes: 1 addition & 1 deletion src/pages/iou/request/step/IOURequestStepAmount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ function IOURequestStepAmount({

// If currency has changed, then we get the default tax rate based on currency, otherwise we use the current tax rate selected in transaction, if we have it.
const transactionTaxCode = transaction?.taxCode ?? '';
const defaultTaxCode = TransactionUtils.getDefaultTaxCode(policy, transaction, currency) ?? '';
const defaultTaxCode = TransactionUtils.getDefaultTaxCode(policy, currency ?? TransactionUtils.getCurrency(transaction)) ?? '';
const taxCode = (currency !== transactionCurrency ? defaultTaxCode : transactionTaxCode) ?? defaultTaxCode;
const taxPercentage = TransactionUtils.getTaxValue(policy, transaction, taxCode) ?? '';
const taxAmount = CurrencyUtils.convertToBackendAmount(TransactionUtils.calculateTaxAmount(taxPercentage, newAmount));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function IOURequestStepConfirmation({
const receiptFilename = transaction?.filename;
const receiptPath = transaction?.receipt?.source;
const receiptType = transaction?.receipt?.type;
const defaultTaxCode = TransactionUtils.getDefaultTaxCode(policy, transaction);
const defaultTaxCode = TransactionUtils.getDefaultTaxCode(policy, TransactionUtils.getCurrency(transaction));
const transactionTaxCode = (transaction?.taxCode ? transaction?.taxCode : defaultTaxCode) ?? '';
const transactionTaxAmount = transaction?.taxAmount ?? 0;
const isSharingTrackExpense = action === CONST.IOU.ACTION.SHARE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function getTaxAmount(transaction: OnyxEntry<Transaction>, policy: OnyxEntry<Pol
}
const transactionTaxAmount = TransactionUtils.getAmount(transaction);
const transactionTaxCode = transaction?.taxCode ?? '';
const defaultTaxCode = TransactionUtils.getDefaultTaxCode(policy, transaction, currency) ?? '';
const defaultTaxCode = TransactionUtils.getDefaultTaxCode(policy, currency ?? TransactionUtils.getCurrency(transaction)) ?? '';
const getTaxValue = (taxCode: string) => TransactionUtils.getTaxValue(policy, transaction, taxCode);
const defaultTaxValue = getTaxValue(defaultTaxCode);
const moneyRequestTaxPercentage = (transactionTaxCode ? getTaxValue(transactionTaxCode) : defaultTaxValue) ?? '';
Expand Down