-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
fix: Distance rate- WS owner can create multiple distance rate with same amount. #53104
base: main
Are you sure you want to change the base?
Changes from 3 commits
a6dbf38
644aac7
022a7c2
955f9d6
9931630
90770ad
b0cebbc
4c547c8
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 |
---|---|---|
|
@@ -11,14 +11,33 @@ type RateValueForm = typeof ONYXKEYS.FORMS.POLICY_CREATE_DISTANCE_RATE_FORM | ty | |
|
||
type TaxReclaimableForm = typeof ONYXKEYS.FORMS.POLICY_DISTANCE_RATE_TAX_RECLAIMABLE_ON_EDIT_FORM; | ||
|
||
function validateRateValue(values: FormOnyxValues<RateValueForm>, currency: string, toLocaleDigit: (arg: string) => string): FormInputErrors<RateValueForm> { | ||
function validateRateValue( | ||
values: FormOnyxValues<RateValueForm>, | ||
customUnitRates: Record<string, Rate>, | ||
currency: string, | ||
toLocaleDigit: (arg: string) => string, | ||
currentRateValue?: number, | ||
): FormInputErrors<RateValueForm> { | ||
const errors: FormInputErrors<RateValueForm> = {}; | ||
const parsedRate = MoneyRequestUtils.replaceAllDigits(values.rate, toLocaleDigit); | ||
const decimalSeparator = toLocaleDigit('.'); | ||
const ratesList = Object.values(customUnitRates); | ||
// The following logic replicates the backend's handling of rates: | ||
// - Multiply the rate by 100 (CUSTOM_UNIT_RATE_BASE_OFFSET) to scale it, ensuring precision. | ||
// - This ensures rates are converted as follows: | ||
// 12 -> 1200 | ||
// 12.1 -> 1210 | ||
// 12.01 -> 1201 | ||
// 12.001 -> 1200.1 | ||
// 12.0001 -> 1200.01 | ||
// - Using parseFloat and toFixed(10) retains the necessary precision. | ||
const convertedRate = parseFloat((Number(values.rate || 0) * CONST.POLICY.CUSTOM_UNIT_RATE_BASE_OFFSET).toFixed(10)); | ||
|
||
// Allow one more decimal place for accuracy | ||
const rateValueRegex = RegExp(String.raw`^-?\d{0,8}([${getPermittedDecimalSeparator(decimalSeparator)}]\d{0,${CONST.MAX_TAX_RATE_DECIMAL_PLACES}})?$`, 'i'); | ||
if (!rateValueRegex.test(parsedRate) || parsedRate === '') { | ||
if (ratesList.some((r) => r.rate === convertedRate && !(currentRateValue && currentRateValue === r.rate))) { | ||
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. Let's remove !(currentRateValue && currentRateValue === r.rate and filter currentRateValue out from convertedRate 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. @DylanDylann, sorry I haven't understood what you mean, can you please explain once again? 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. Ahh I made a mistake. I mean that we should remove currentRateValue from ratesList and don't need to check |
||
errors.rate = Localize.translateLocal('workspace.perDiem.errors.existingRateError', {rate: NumberUtils.parseFloatAnyLocale(parsedRate)}); | ||
} else if (!rateValueRegex.test(parsedRate) || parsedRate === '') { | ||
errors.rate = Localize.translateLocal('common.error.invalidRateError'); | ||
} else if (NumberUtils.parseFloatAnyLocale(parsedRate) <= 0) { | ||
errors.rate = Localize.translateLocal('common.error.lowRateError'); | ||
|
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.