Skip to content

Commit

Permalink
Update the taxCode if currency is updated and refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
MonilBhavsar committed May 14, 2024
1 parent 1823d04 commit f34a2ac
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions src/pages/iou/request/step/IOURequestStepAmount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,6 @@ type IOURequestStepAmountProps = IOURequestStepAmountOnyxProps &
transaction: OnyxEntry<OnyxTypes.Transaction>;
};

function getTaxAmount(transaction: OnyxEntry<OnyxTypes.Transaction>, policy: OnyxEntry<OnyxTypes.Policy>, newAmount: number, currency: string) {
if (!transaction?.amount) {
return 0;
}
const transactionTaxCode = transaction?.taxCode ?? '';
const defaultTaxCode = TransactionUtils.getDefaultTaxCode(policy, transaction, currency) ?? '';
const taxPercentage = TransactionUtils.getTaxValue(policy, transaction, transactionTaxCode ?? defaultTaxCode) ?? '';
return CurrencyUtils.convertToBackendAmount(TransactionUtils.calculateTaxAmount(taxPercentage, newAmount));
}

function IOURequestStepAmount({
report,
route: {
Expand Down Expand Up @@ -279,7 +269,8 @@ function IOURequestStepAmount({
}

// If the value hasn't changed, don't request to save changes on the server and just close the modal
if (newAmount === TransactionUtils.getAmount(transaction) && currency === TransactionUtils.getCurrency(transaction)) {
const transactionCurrency = TransactionUtils.getCurrency(transaction);
if (newAmount === TransactionUtils.getAmount(transaction) && currency === transactionCurrency) {
Navigation.dismissModal();
return;
}
Expand All @@ -290,8 +281,11 @@ function IOURequestStepAmount({
return;
}

const taxAmount = getTaxAmount(transaction, policy, newAmount, currency);
const taxCode = TransactionUtils.getDefaultTaxCode(policy, transaction, currency) ?? '';
// 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 taxCode = currency !== transactionCurrency ? TransactionUtils.getDefaultTaxCode(policy, transaction, currency) ?? '' : transactionTaxCode;
const taxPercentage = TransactionUtils.getTaxValue(policy, transaction, taxCode) ?? '';
const taxAmount = CurrencyUtils.convertToBackendAmount(TransactionUtils.calculateTaxAmount(taxPercentage, newAmount));

IOU.updateMoneyRequestAmountAndCurrency({transactionID, transactionThreadReportID: reportID, currency, amount: newAmount, taxAmount, policy, taxCode});
Navigation.dismissModal();
Expand Down

0 comments on commit f34a2ac

Please sign in to comment.