-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updated total amount when change the different currency #35880
Changes from 12 commits
72dee1d
1e3e91e
d52739b
a6ffc90
b2ce3f0
89abd02
312ddbc
c8a3c4c
7c5889e
296fb19
0626907
271bd23
7d586ef
8796fae
2a48143
9f075c4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -969,6 +969,33 @@ function createDistanceRequest( | |
Report.notifyNewAction(chatReport.reportID, userAccountID); | ||
} | ||
|
||
/** | ||
* Compute the diff amount when we update the transaction | ||
*/ | ||
function computeDiffAmount(iouReport: OnyxEntry<OnyxTypes.Report>, updatedTransaction: OnyxEntry<OnyxTypes.Transaction>, transaction: OnyxEntry<OnyxTypes.Transaction>): number { | ||
if (!iouReport) { | ||
return 0; | ||
} | ||
const isExpenseReport = ReportUtils.isExpenseReport(iouReport); | ||
const updatedCurrency = TransactionUtils.getCurrency(updatedTransaction); | ||
const currentCurrency = TransactionUtils.getCurrency(transaction); | ||
|
||
if (updatedCurrency === iouReport?.currency && currentCurrency !== iouReport?.currency) { | ||
// add the diff with the total if we change the currency from different currency to the currency of the iou report | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If English is not your native language, it can be very hard to use "a" and "the" correctly. In general, we use "a"/"an" if we talk about an entity/person/object from a group/set/category, without having a specific instance in mind (jn the given context). We use "the" when we talk about a specific instance (in the given context). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for your knowledge, noted it and will improve in the future. |
||
return TransactionUtils.getAmount(updatedTransaction, isExpenseReport); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add new helper const oldAmount = TransactionUtils.getAmount(transaction, isExpenseReport);
const updateAmount = TransactionUtils.getAmount(updatedTransaction, isExpenseReport); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated with two variables before the |
||
} | ||
if (updatedCurrency !== iouReport?.currency && currentCurrency === iouReport?.currency) { | ||
// subtract the diff with the total if we change the currency from the currency of iou report to different currency | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I updated this comment. |
||
return -TransactionUtils.getAmount(updatedTransaction, isExpenseReport); | ||
} | ||
if (updatedCurrency === iouReport?.currency && updatedTransaction?.modifiedAmount) { | ||
// get the diff between the updated amount and the current amount if we change the amount and the currency of the transaction is the currency of the report | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't spot any language mistakes! 😃 I suggested to start the other comment with a capital letter, so we could do this here, too, for consistency. We could also say "calculate", as it's more specific than "get".
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I updated this comment. |
||
return TransactionUtils.getAmount(updatedTransaction, isExpenseReport) - TransactionUtils.getAmount(transaction, isExpenseReport); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
/** | ||
* @param transactionChanges | ||
* @param [transactionChanges.created] Present when updated the date field | ||
|
@@ -1050,31 +1077,29 @@ function getUpdateMoneyRequestParams( | |
}); | ||
|
||
// Step 4: Compute the IOU total and update the report preview message (and report header) so LHN amount owed is correct. | ||
// Should only update if the transaction matches the currency of the report, else we wait for the update | ||
// from the server with the currency conversion | ||
let updatedMoneyRequestReport = {...iouReport}; | ||
if (updatedTransaction?.currency === iouReport?.currency && updatedTransaction?.modifiedAmount) { | ||
const diff = TransactionUtils.getAmount(transaction, true) - TransactionUtils.getAmount(updatedTransaction, true); | ||
if (ReportUtils.isExpenseReport(iouReport) && typeof updatedMoneyRequestReport.total === 'number') { | ||
updatedMoneyRequestReport.total += diff; | ||
} else { | ||
updatedMoneyRequestReport = iouReport | ||
? IOUUtils.updateIOUOwnerAndTotal(iouReport, updatedReportAction.actorAccountID ?? -1, diff, TransactionUtils.getCurrency(transaction), false) | ||
: {}; | ||
} | ||
const diff = computeDiffAmount(iouReport, updatedTransaction, transaction); | ||
|
||
updatedMoneyRequestReport.cachedTotal = CurrencyUtils.convertToDisplayString(updatedMoneyRequestReport.total, updatedTransaction.currency); | ||
optimisticData.push({ | ||
onyxMethod: Onyx.METHOD.MERGE, | ||
key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.reportID}`, | ||
value: updatedMoneyRequestReport, | ||
}); | ||
successData.push({ | ||
onyxMethod: Onyx.METHOD.MERGE, | ||
key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.reportID}`, | ||
value: {pendingAction: null}, | ||
}); | ||
if (ReportUtils.isExpenseReport(iouReport) && typeof updatedMoneyRequestReport.total === 'number') { | ||
// For expense report, the amount is negative so we should subtract total from diff | ||
updatedMoneyRequestReport.total -= diff; | ||
} else { | ||
updatedMoneyRequestReport = iouReport | ||
? IOUUtils.updateIOUOwnerAndTotal(iouReport, updatedReportAction.actorAccountID ?? -1, diff, TransactionUtils.getCurrency(transaction), false) | ||
: {}; | ||
} | ||
updatedMoneyRequestReport.cachedTotal = CurrencyUtils.convertToDisplayString(updatedMoneyRequestReport.total, updatedTransaction?.modifiedCurrency); | ||
|
||
optimisticData.push({ | ||
onyxMethod: Onyx.METHOD.MERGE, | ||
key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.reportID}`, | ||
value: updatedMoneyRequestReport, | ||
}); | ||
successData.push({ | ||
onyxMethod: Onyx.METHOD.MERGE, | ||
key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.reportID}`, | ||
value: {pendingAction: null}, | ||
}); | ||
} | ||
|
||
// Optimistically modify the transaction | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this change! It clearly separates the computation of the diff from the surrounding logic of
getUpdateMoneyRequestParams
. That function is already long enough.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, would you have anything against changing this name to
calculateDiffAmount
? "Calculation" is "computing" but with numbers. I should have suggested this name in the first place.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree.