Skip to content

Commit

Permalink
Merge pull request #42128 from Expensify/monil-fixOfflineCurrencyTaxU…
Browse files Browse the repository at this point in the history
…pdate

[CP Staging] Fix tax rate being updated when currency is updated in offline mode

(cherry picked from commit 0d424d2)
  • Loading branch information
Beamanator authored and OSBotify committed May 14, 2024
1 parent 06d98cd commit e955a76
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
18 changes: 11 additions & 7 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2969,7 +2969,8 @@ function getModifiedExpenseOriginalMessage(

// The amount is always a combination of the currency and the number value so when one changes we need to store both
// to match how we handle the modified expense action in oldDot
if ('amount' in transactionChanges || 'currency' in transactionChanges) {
const didAmountOrCurrencyChange = 'amount' in transactionChanges || 'currency' in transactionChanges;
if (didAmountOrCurrencyChange) {
originalMessage.oldAmount = TransactionUtils.getAmount(oldTransaction, isFromExpenseReport);
originalMessage.amount = transactionChanges?.amount ?? transactionChanges.oldAmount;
originalMessage.oldCurrency = TransactionUtils.getCurrency(oldTransaction);
Expand All @@ -2986,19 +2987,22 @@ function getModifiedExpenseOriginalMessage(
originalMessage.tag = transactionChanges?.tag;
}

// We only want to display a tax rate update system message when tax rate is updated by user.
// Tax rate can change as a result of currency update. In such cases, we want to skip displaying a system message, as discussed.
const didTaxCodeChange = 'taxCode' in transactionChanges;
if (didTaxCodeChange && !didAmountOrCurrencyChange) {
originalMessage.oldTaxRate = policy?.taxRates?.taxes[TransactionUtils.getTaxCode(oldTransaction)]?.value;
originalMessage.taxRate = transactionChanges?.taxCode && policy?.taxRates?.taxes[transactionChanges?.taxCode].value;
}

// We only want to display a tax amount update system message when tax amount is updated by user.
// Tax amount can change as a result of amount, currency or tax rate update. In such cases, we want to skip displaying a system message, as discussed.
if ('taxAmount' in transactionChanges && !('amount' in transactionChanges || 'currency' in transactionChanges || 'taxCode' in transactionChanges)) {
if ('taxAmount' in transactionChanges && !(didAmountOrCurrencyChange || didTaxCodeChange)) {
originalMessage.oldTaxAmount = TransactionUtils.getTaxAmount(oldTransaction, isFromExpenseReport);
originalMessage.taxAmount = transactionChanges?.taxAmount;
originalMessage.currency = TransactionUtils.getCurrency(oldTransaction);
}

if ('taxCode' in transactionChanges) {
originalMessage.oldTaxRate = policy?.taxRates?.taxes[TransactionUtils.getTaxCode(oldTransaction)]?.value;
originalMessage.taxRate = transactionChanges?.taxCode && policy?.taxRates?.taxes[transactionChanges?.taxCode].value;
}

if ('billable' in transactionChanges) {
const oldBillable = TransactionUtils.getBillable(oldTransaction);
originalMessage.oldBillable = oldBillable ? Localize.translateLocal('common.billable').toLowerCase() : Localize.translateLocal('common.nonBillable').toLowerCase();
Expand Down
3 changes: 3 additions & 0 deletions src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5052,6 +5052,7 @@ type UpdateMoneyRequestAmountAndCurrencyParams = {
policy?: OnyxEntry<OnyxTypes.Policy>;
policyTagList?: OnyxEntry<OnyxTypes.PolicyTagList>;
policyCategories?: OnyxEntry<OnyxTypes.PolicyCategories>;
taxCode: string;
};

/** Updates the amount and currency fields of an expense */
Expand All @@ -5064,10 +5065,12 @@ function updateMoneyRequestAmountAndCurrency({
policy,
policyTagList,
policyCategories,
taxCode,
}: UpdateMoneyRequestAmountAndCurrencyParams) {
const transactionChanges = {
amount,
currency,
taxCode,
taxAmount,
};
const transactionThreadReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReportID}`] ?? null;
Expand Down
22 changes: 9 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) {
if (!transaction?.amount) {
return 0;
}
const transactionTaxCode = transaction?.taxCode ?? '';
const defaultTaxCode = TransactionUtils.getDefaultTaxCode(policy, transaction) ?? '';
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,9 +281,14 @@ function IOURequestStepAmount({
return;
}

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

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

Expand Down

0 comments on commit e955a76

Please sign in to comment.