Skip to content
Merged
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
18 changes: 11 additions & 7 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2982,7 +2982,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 @@ -2999,19 +3000,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 @@ -5092,6 +5092,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 @@ -5104,10 +5105,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