forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDistanceRequestUtils.ts
412 lines (368 loc) · 15.9 KB
/
DistanceRequestUtils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
import type {OnyxCollection, OnyxEntry} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import type {LocaleContextProps} from '@components/LocaleContextProvider';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {LastSelectedDistanceRates, OnyxInputOrEntry, Report, Transaction} from '@src/types/onyx';
import type {Unit} from '@src/types/onyx/Policy';
import type Policy from '@src/types/onyx/Policy';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import {getCurrencySymbol} from './CurrencyUtils';
import {getDistanceRateCustomUnit, getDistanceRateCustomUnitRate, getPersonalPolicy, getPolicy, getUnitRateValue} from './PolicyUtils';
import {isPolicyExpenseChat} from './ReportUtils';
import {getCurrency, getRateID, isCustomUnitRateIDForP2P} from './TransactionUtils';
type MileageRate = {
customUnitRateID?: string;
rate?: number;
currency?: string;
unit: Unit;
name?: string;
enabled?: boolean;
};
let lastSelectedDistanceRates: OnyxEntry<LastSelectedDistanceRates> = {};
Onyx.connect({
key: ONYXKEYS.NVP_LAST_SELECTED_DISTANCE_RATES,
callback: (value) => {
lastSelectedDistanceRates = value;
},
});
let allReports: OnyxCollection<Report>;
Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT,
waitForCollectionCallback: true,
callback: (value) => {
allReports = value;
},
});
const METERS_TO_KM = 0.001; // 1 kilometer is 1000 meters
const METERS_TO_MILES = 0.000621371; // There are approximately 0.000621371 miles in a meter
function getMileageRates(policy: OnyxInputOrEntry<Policy>, includeDisabledRates = false, selectedRateID?: string): Record<string, MileageRate> {
const mileageRates: Record<string, MileageRate> = {};
if (!policy?.customUnits) {
return mileageRates;
}
const distanceUnit = getDistanceRateCustomUnit(policy);
if (!distanceUnit?.rates) {
return mileageRates;
}
Object.entries(distanceUnit.rates).forEach(([rateID, rate]) => {
if (!includeDisabledRates && rate.enabled === false && (!selectedRateID || rateID !== selectedRateID)) {
return;
}
if (!distanceUnit.attributes) {
return;
}
mileageRates[rateID] = {
rate: rate.rate,
currency: rate.currency,
unit: distanceUnit.attributes.unit,
name: rate.name,
customUnitRateID: rate.customUnitRateID,
enabled: rate.enabled,
};
});
return mileageRates;
}
/**
* Retrieves the default mileage rate based on a given policy.
*
* @param policy - The policy from which to extract the default mileage rate.
*
* @returns An object containing the rate and unit for the default mileage or null if not found.
* @returns [rate] - The default rate for the mileage.
* @returns [currency] - The currency associated with the rate.
* @returns [unit] - The unit of measurement for the distance.
*/
function getDefaultMileageRate(policy: OnyxInputOrEntry<Policy>): MileageRate | undefined {
if (isEmptyObject(policy) || !policy?.customUnits) {
return undefined;
}
const distanceUnit = getDistanceRateCustomUnit(policy);
if (!distanceUnit?.rates || !distanceUnit.attributes) {
return;
}
const mileageRates = Object.values(getMileageRates(policy));
const distanceRate = mileageRates.find((rate) => rate.name === CONST.CUSTOM_UNITS.DEFAULT_RATE) ?? mileageRates.at(0) ?? ({} as MileageRate);
return {
customUnitRateID: distanceRate.customUnitRateID,
rate: distanceRate.rate,
currency: distanceRate.currency,
unit: distanceUnit.attributes.unit,
name: distanceRate.name,
};
}
/**
* Converts a given distance in meters to the specified unit (kilometers or miles).
*
* @param distanceInMeters - The distance in meters to be converted.
* @param unit - The desired unit of conversion, either 'km' for kilometers or 'mi' for miles.
*
* @returns The converted distance in the specified unit.
*/
function convertDistanceUnit(distanceInMeters: number, unit: Unit): number {
switch (unit) {
case CONST.CUSTOM_UNITS.DISTANCE_UNIT_KILOMETERS:
return distanceInMeters * METERS_TO_KM;
case CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES:
return distanceInMeters * METERS_TO_MILES;
default:
throw new Error('Unsupported unit. Supported units are "mi" or "km".');
}
}
/**
* @param distanceInMeters Distance traveled
* @param unit Unit that should be used to display the distance
* @returns The distance in requested units, rounded to 2 decimals
*/
function getRoundedDistanceInUnits(distanceInMeters: number, unit: Unit): string {
const convertedDistance = convertDistanceUnit(distanceInMeters, unit);
return convertedDistance.toFixed(2);
}
/**
* @param unit Unit that should be used to display the distance
* @param rate Expensable amount allowed per unit
* @param currency The currency associated with the rate
* @param translate Translate function
* @param toLocaleDigit Function to convert to localized digit
* @param useShortFormUnit If true, the unit will be returned in short form (e.g., "mi", "km").
* @returns A string that displays the rate used for expense calculation
*/
function getRateForDisplay(
unit: Unit | undefined,
rate: number | undefined,
currency: string | undefined,
translate: LocaleContextProps['translate'],
toLocaleDigit: LocaleContextProps['toLocaleDigit'],
isOffline?: boolean,
useShortFormUnit?: boolean,
): string {
if (isOffline && !rate) {
return translate('iou.defaultRate');
}
if (!rate || !currency || !unit) {
return translate('iou.fieldPending');
}
const singularDistanceUnit = unit === CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES ? translate('common.mile') : translate('common.kilometer');
const formattedRate = getUnitRateValue(toLocaleDigit, {rate}, useShortFormUnit);
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
const currencySymbol = getCurrencySymbol(currency) || `${currency} `;
return `${currencySymbol}${formattedRate} / ${useShortFormUnit ? unit : singularDistanceUnit}`;
}
/**
* @param hasRoute Whether the route exists for the distance expense
* @param distanceInMeters Distance traveled
* @param unit Unit that should be used to display the distance
* @param rate Expensable amount allowed per unit
* @param translate Translate function
* @param useShortFormUnit If true, the unit will be returned in short form (e.g., "mi", "km").
* @returns A string that describes the distance traveled
*/
function getDistanceForDisplay(
hasRoute: boolean,
distanceInMeters: number,
unit: Unit | undefined,
rate: number | undefined,
translate: LocaleContextProps['translate'],
useShortFormUnit?: boolean,
): string {
if (!hasRoute || !rate || !unit || !distanceInMeters) {
return translate('iou.fieldPending');
}
const distanceInUnits = getRoundedDistanceInUnits(distanceInMeters, unit);
if (useShortFormUnit) {
return `${distanceInUnits} ${unit}`;
}
const distanceUnit = unit === CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES ? translate('common.miles') : translate('common.kilometers');
const singularDistanceUnit = unit === CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES ? translate('common.mile') : translate('common.kilometer');
const unitString = distanceInUnits === '1' ? singularDistanceUnit : distanceUnit;
return `${distanceInUnits} ${unitString}`;
}
/**
* @param hasRoute Whether the route exists for the distance expense
* @param distanceInMeters Distance traveled
* @param unit Unit that should be used to display the distance
* @param rate Expensable amount allowed per unit
* @param currency The currency associated with the rate
* @param translate Translate function
* @param toLocaleDigit Function to convert to localized digit
* @returns A string that describes the distance traveled and the rate used for expense calculation
*/
function getDistanceMerchant(
hasRoute: boolean,
distanceInMeters: number,
unit: Unit | undefined,
rate: number | undefined,
currency: string,
translate: LocaleContextProps['translate'],
toLocaleDigit: LocaleContextProps['toLocaleDigit'],
): string {
if (!hasRoute || !rate) {
return translate('iou.fieldPending');
}
const distanceInUnits = getDistanceForDisplay(hasRoute, distanceInMeters, unit, rate, translate, true);
const ratePerUnit = getRateForDisplay(unit, rate, currency, translate, toLocaleDigit, undefined, true);
return `${distanceInUnits} @ ${ratePerUnit}`;
}
function ensureRateDefined(rate: number | undefined): asserts rate is number {
if (rate !== undefined) {
return;
}
throw new Error('All default P2P rates should have a rate defined');
}
/**
* Retrieves the rate and unit for a P2P distance expense for a given currency.
*
* @param currency
* @returns The rate and unit in MileageRate object.
*/
function getRateForP2P(currency: string, transaction: OnyxEntry<Transaction>): MileageRate {
const currencyWithExistingRate = CONST.CURRENCY_TO_DEFAULT_MILEAGE_RATE[currency] ? currency : CONST.CURRENCY.USD;
const mileageRate = CONST.CURRENCY_TO_DEFAULT_MILEAGE_RATE[currencyWithExistingRate];
ensureRateDefined(mileageRate.rate);
// Ensure the rate is updated when the currency changes, otherwise use the stored rate
const rate = getCurrency(transaction) === currency ? transaction?.comment?.customUnit?.defaultP2PRate ?? mileageRate.rate : mileageRate.rate;
return {
...mileageRate,
currency: currencyWithExistingRate,
rate,
};
}
/**
* Calculates the expense amount based on distance, unit, and rate.
*
* @param distance - The distance traveled in meters
* @param unit - The unit of measurement for the distance
* @param rate - Rate used for calculating the expense amount
* @returns The computed expense amount (rounded) in "cents".
*/
function getDistanceRequestAmount(distance: number, unit: Unit, rate: number): number {
const convertedDistance = convertDistanceUnit(distance, unit);
const roundedDistance = parseFloat(convertedDistance.toFixed(2));
return Math.round(roundedDistance * rate);
}
/**
* Converts the distance from kilometers or miles to meters.
*
* @param distance - The distance to be converted.
* @param unit - The unit of measurement for the distance.
* @returns The distance in meters.
*/
function convertToDistanceInMeters(distance: number, unit: Unit): number {
if (unit === CONST.CUSTOM_UNITS.DISTANCE_UNIT_KILOMETERS) {
return distance / METERS_TO_KM;
}
return distance / METERS_TO_MILES;
}
/**
* Returns custom unit rate ID for the distance transaction
*/
function getCustomUnitRateID(reportID?: string) {
let customUnitRateID: string = CONST.CUSTOM_UNITS.FAKE_P2P_ID;
if (!reportID) {
return customUnitRateID;
}
const report = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`];
const parentReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${report?.parentReportID}`];
const policy = getPolicy(report?.policyID ?? parentReport?.policyID);
if (isEmptyObject(policy)) {
return customUnitRateID;
}
if (isPolicyExpenseChat(report) || isPolicyExpenseChat(parentReport)) {
const distanceUnit = Object.values(policy.customUnits ?? {}).find((unit) => unit.name === CONST.CUSTOM_UNITS.NAME_DISTANCE);
const lastSelectedDistanceRateID = lastSelectedDistanceRates?.[policy.id];
const lastSelectedDistanceRate = lastSelectedDistanceRateID ? distanceUnit?.rates[lastSelectedDistanceRateID] : undefined;
if (lastSelectedDistanceRate?.enabled && lastSelectedDistanceRateID) {
customUnitRateID = lastSelectedDistanceRateID;
} else {
const defaultMileageRate = getDefaultMileageRate(policy);
if (!defaultMileageRate?.customUnitRateID) {
return customUnitRateID;
}
customUnitRateID = defaultMileageRate.customUnitRateID;
}
}
return customUnitRateID;
}
/**
* Get taxable amount from a specific distance rate, taking into consideration the tax claimable amount configured for the distance rate
*/
function getTaxableAmount(policy: OnyxEntry<Policy>, customUnitRateID: string, distance: number) {
const distanceUnit = getDistanceRateCustomUnit(policy);
const customUnitRate = getDistanceRateCustomUnitRate(policy, customUnitRateID);
if (!distanceUnit?.customUnitID || !customUnitRate) {
return 0;
}
const unit = distanceUnit?.attributes?.unit ?? CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES;
const rate = customUnitRate?.rate ?? CONST.DEFAULT_NUMBER_ID;
const amount = getDistanceRequestAmount(distance, unit, rate);
const taxClaimablePercentage = customUnitRate.attributes?.taxClaimablePercentage ?? CONST.DEFAULT_NUMBER_ID;
return amount * taxClaimablePercentage;
}
function getDistanceUnit(transaction: OnyxEntry<Transaction>, mileageRate: OnyxEntry<MileageRate>): Unit {
return transaction?.comment?.customUnit?.distanceUnit ?? mileageRate?.unit ?? CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES;
}
/**
* Get the selected rate for a transaction, from the policy or P2P default rate.
* Use the distanceUnit stored on the transaction by default to prevent policy changes modifying existing transactions. Otherwise, get the unit from the rate.
*/
function getRate({
transaction,
policy,
policyDraft,
useTransactionDistanceUnit = true,
}: {
transaction: OnyxEntry<Transaction>;
policy: OnyxEntry<Policy>;
policyDraft?: OnyxEntry<Policy>;
useTransactionDistanceUnit?: boolean;
}): MileageRate {
let mileageRates = getMileageRates(policy, true, transaction?.comment?.customUnit?.customUnitRateID);
if (isEmptyObject(mileageRates) && policyDraft) {
mileageRates = getMileageRates(policyDraft, true, transaction?.comment?.customUnit?.customUnitRateID);
}
const policyCurrency = policy?.outputCurrency ?? getPersonalPolicy()?.outputCurrency ?? CONST.CURRENCY.USD;
const defaultMileageRate = getDefaultMileageRate(policy);
const customUnitRateID = getRateID(transaction);
const customMileageRate = customUnitRateID ? mileageRates?.[customUnitRateID] : defaultMileageRate;
const mileageRate = isCustomUnitRateIDForP2P(transaction) ? getRateForP2P(policyCurrency, transaction) : customMileageRate;
const unit = getDistanceUnit(useTransactionDistanceUnit ? transaction : undefined, mileageRate);
return {
...mileageRate,
unit,
currency: mileageRate?.currency ?? policyCurrency,
};
}
/**
* Get the updated distance unit from the selected rate instead of the distanceUnit stored on the transaction.
* Useful for updating the transaction distance unit when the distance or rate changes.
*
* For example, if an expense is '10 mi @ $1.00 / mi' and the rate is updated to '$1.00 / km',
* then the updated distance unit should be 'km' from the updated rate, not 'mi' from the currently stored transaction distance unit.
*/
function getUpdatedDistanceUnit({transaction, policy, policyDraft}: {transaction: OnyxEntry<Transaction>; policy: OnyxEntry<Policy>; policyDraft?: OnyxEntry<Policy>}) {
return getRate({transaction, policy, policyDraft, useTransactionDistanceUnit: false}).unit;
}
/**
* Get the mileage rate by its ID in the form it's configured for the policy.
* If not found, return undefined.
*/
function getRateByCustomUnitRateID({customUnitRateID, policy}: {customUnitRateID: string; policy: OnyxEntry<Policy>}): MileageRate | undefined {
return getMileageRates(policy, true, customUnitRateID)[customUnitRateID];
}
export default {
getDefaultMileageRate,
getDistanceMerchant,
getDistanceRequestAmount,
getRateForDisplay,
getMileageRates,
getDistanceForDisplay,
getRateForP2P,
getCustomUnitRateID,
convertToDistanceInMeters,
getTaxableAmount,
getDistanceUnit,
getUpdatedDistanceUnit,
getRate,
getRateByCustomUnitRateID,
};
export type {MileageRate};