-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Fix currency handling - always handle backend amounts as if they are in cents #24175
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
b77293a
Fix currency handling
aldo-expensify cf1f0d0
Stop passing currency
aldo-expensify 1921074
Fix offline behaviour
aldo-expensify 538524b
Remove unused stuff
aldo-expensify 5a9d343
RSD has 2 decimals now
aldo-expensify f72fe23
More tests / make RSD consistent with backend
aldo-expensify dfbc548
Fix paypal payment url
aldo-expensify a3b3ff7
Split based on currency
aldo-expensify 5216fc3
Merge branch 'main' of github.com:Expensify/App into aldo_fix-decimals
aldo-expensify 96e6657
Style
aldo-expensify 45dfc06
Cap decimal places on split and add tests
aldo-expensify 2f218af
Merge branch 'main' of github.com:Expensify/App into aldo_fix-decimals
aldo-expensify 8c7770d
Avoid precision errors and add test
aldo-expensify bb6695e
Merge branch 'main' of github.com:Expensify/App into aldo_fix-decimals
aldo-expensify 3d6f57a
Style
aldo-expensify 1d465e2
Fix comment
aldo-expensify File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,31 @@ | ||
| import _ from 'underscore'; | ||
| import CONST from '../CONST'; | ||
| import * as ReportActionsUtils from './ReportActionsUtils'; | ||
| import * as CurrencyUtils from './CurrencyUtils'; | ||
|
|
||
| /** | ||
| * Calculates the amount per user given a list of participants | ||
| * | ||
| * @param {Number} numberOfParticipants - Number of participants in the chat. It should not include the current user. | ||
| * @param {Number} total - IOU total amount in the smallest units of the currency | ||
| * @param {Number} total - IOU total amount in backend format (cents, no matter the currency) | ||
| * @param {String} currency - This is used to know how many decimal places are valid to use when splitting the total | ||
| * @param {Boolean} isDefaultUser - Whether we are calculating the amount for the current user | ||
| * @returns {Number} | ||
| */ | ||
| function calculateAmount(numberOfParticipants, total, isDefaultUser = false) { | ||
| function calculateAmount(numberOfParticipants, total, currency, isDefaultUser = false) { | ||
| // Since the backend can maximum store 2 decimal places, any currency with more than 2 decimals | ||
| // has to be capped to 2 decimal places | ||
| const currencyUnit = Math.min(100, CurrencyUtils.getCurrencyUnit(currency)); | ||
| const totalInCurrencySubunit = Math.round((total / 100) * currencyUnit); | ||
|
Contributor
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. Due to this rounding up, the amount mismatched when doing split. See #52310 for more context. |
||
| const totalParticipants = numberOfParticipants + 1; | ||
| const amountPerPerson = Math.round(total / totalParticipants); | ||
| const amountPerPerson = Math.round(totalInCurrencySubunit / totalParticipants); | ||
| let finalAmount = amountPerPerson; | ||
| if (isDefaultUser) { | ||
| const sumAmount = amountPerPerson * totalParticipants; | ||
| const difference = total - sumAmount; | ||
| finalAmount = total !== sumAmount ? amountPerPerson + difference : amountPerPerson; | ||
| const difference = totalInCurrencySubunit - sumAmount; | ||
| finalAmount = totalInCurrencySubunit !== sumAmount ? amountPerPerson + difference : amountPerPerson; | ||
| } | ||
| return finalAmount; | ||
| return Math.round((finalAmount * 100) / currencyUnit); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
The library used in the backend and the library used in the frontend (Intl) format the RSD currency with 0 decimals. A client needed decimals, so we updated the backend to display RSD with two decimals.
Now, we have to force the frontend to also do the same so we don't get bug reports because of inconsistent behaviour.
I ONLY enabled this for RSD because otherwise the formatting will be inconsistent between backend and frontend for currencies with more than 2 decimals.