@@ -249,7 +249,19 @@ function areRequiredFieldsEmpty(transaction: OnyxEntry<Transaction>): boolean {
249
249
/**
250
250
* Given the edit made to the expense, return an updated transaction object.
251
251
*/
252
- function getUpdatedTransaction ( transaction : Transaction , transactionChanges : TransactionChanges , isFromExpenseReport : boolean , shouldUpdateReceiptState = true ) : Transaction {
252
+ function getUpdatedTransaction ( {
253
+ transaction,
254
+ transactionChanges,
255
+ isFromExpenseReport,
256
+ shouldUpdateReceiptState = true ,
257
+ policy = undefined ,
258
+ } : {
259
+ transaction : Transaction ;
260
+ transactionChanges : TransactionChanges ;
261
+ isFromExpenseReport : boolean ;
262
+ shouldUpdateReceiptState ?: boolean ;
263
+ policy ?: OnyxEntry < Policy > ;
264
+ } ) : Transaction {
253
265
// Only changing the first level fields so no need for deep clone now
254
266
const updatedTransaction = lodashDeepClone ( transaction ) ;
255
267
let shouldStopSmartscan = false ;
@@ -281,7 +293,29 @@ function getUpdatedTransaction(transaction: Transaction, transactionChanges: Tra
281
293
282
294
if ( Object . hasOwn ( transactionChanges , 'waypoints' ) ) {
283
295
updatedTransaction . modifiedWaypoints = transactionChanges . waypoints ;
296
+ updatedTransaction . isLoading = true ;
284
297
shouldStopSmartscan = true ;
298
+
299
+ if ( ! transactionChanges . routes ?. route0 ?. geometry ?. coordinates ) {
300
+ // The waypoints were changed, but there is no route – it is pending from the BE and we should mark the fields as pending
301
+ updatedTransaction . amount = CONST . IOU . DEFAULT_AMOUNT ;
302
+ updatedTransaction . modifiedAmount = CONST . IOU . DEFAULT_AMOUNT ;
303
+ updatedTransaction . modifiedMerchant = Localize . translateLocal ( 'iou.fieldPending' ) ;
304
+ } else {
305
+ const mileageRate = DistanceRequestUtils . getRate ( { transaction : updatedTransaction , policy} ) ;
306
+ const { unit, rate} = mileageRate ;
307
+
308
+ const distanceInMeters = getDistanceInMeters ( transaction , unit ) ;
309
+ const amount = DistanceRequestUtils . getDistanceRequestAmount ( distanceInMeters , unit , rate ?? 0 ) ;
310
+ const updatedAmount = isFromExpenseReport ? - amount : amount ;
311
+ const updatedMerchant = DistanceRequestUtils . getDistanceMerchant ( true , distanceInMeters , unit , rate , transaction . currency , Localize . translateLocal , ( digit ) =>
312
+ toLocaleDigit ( preferredLocale , digit ) ,
313
+ ) ;
314
+
315
+ updatedTransaction . amount = updatedAmount ;
316
+ updatedTransaction . modifiedAmount = updatedAmount ;
317
+ updatedTransaction . modifiedMerchant = updatedMerchant ;
318
+ }
285
319
}
286
320
287
321
if ( Object . hasOwn ( transactionChanges , 'customUnitRateID' ) ) {
@@ -290,20 +324,38 @@ function getUpdatedTransaction(transaction: Transaction, transactionChanges: Tra
290
324
shouldStopSmartscan = true ;
291
325
292
326
const existingDistanceUnit = transaction ?. comment ?. customUnit ?. distanceUnit ;
293
- const allReports = ReportConnection . getAllReports ( ) ;
294
- const report = allReports ?. [ `${ ONYXKEYS . COLLECTION . REPORT } ${ transaction . reportID } ` ] ?? null ;
295
- const policyID = report ?. policyID ?? '' ;
296
- const policy = PolicyUtils . getPolicy ( policyID ) ;
297
327
298
328
// Get the new distance unit from the rate's unit
299
329
const newDistanceUnit = DistanceRequestUtils . getUpdatedDistanceUnit ( { transaction : updatedTransaction , policy} ) ;
330
+ lodashSet ( updatedTransaction , 'comment.customUnit.distanceUnit' , newDistanceUnit ) ;
300
331
301
332
// If the distanceUnit is set and the rate is changed to one that has a different unit, convert the distance to the new unit
302
333
if ( existingDistanceUnit && newDistanceUnit !== existingDistanceUnit ) {
303
334
const conversionFactor = existingDistanceUnit === CONST . CUSTOM_UNITS . DISTANCE_UNIT_MILES ? CONST . CUSTOM_UNITS . MILES_TO_KILOMETERS : CONST . CUSTOM_UNITS . KILOMETERS_TO_MILES ;
304
335
const distance = NumberUtils . roundToTwoDecimalPlaces ( ( transaction ?. comment ?. customUnit ?. quantity ?? 0 ) * conversionFactor ) ;
305
336
lodashSet ( updatedTransaction , 'comment.customUnit.quantity' , distance ) ;
306
- lodashSet ( updatedTransaction , 'pendingFields.merchant' , CONST . RED_BRICK_ROAD_PENDING_ACTION . UPDATE ) ;
337
+ }
338
+
339
+ if ( ! isFetchingWaypointsFromServer ( transaction ) ) {
340
+ // When the waypoints are being fetched from the server, we have no information about the distance, and cannot recalculate the updated amount.
341
+ // Otherwise, recalculate the fields based on the new rate.
342
+
343
+ const oldMileageRate = DistanceRequestUtils . getRate ( { transaction, policy} ) ;
344
+ const updatedMileageRate = DistanceRequestUtils . getRate ( { transaction : updatedTransaction , policy, useTransactionDistanceUnit : false } ) ;
345
+ const { unit, rate} = updatedMileageRate ;
346
+
347
+ const distanceInMeters = getDistanceInMeters ( transaction , oldMileageRate ?. unit ) ;
348
+ const amount = DistanceRequestUtils . getDistanceRequestAmount ( distanceInMeters , unit , rate ?? 0 ) ;
349
+ const updatedAmount = isFromExpenseReport ? - amount : amount ;
350
+ const updatedCurrency = updatedMileageRate . currency ?? CONST . CURRENCY . USD ;
351
+ const updatedMerchant = DistanceRequestUtils . getDistanceMerchant ( true , distanceInMeters , unit , rate , updatedCurrency , Localize . translateLocal , ( digit ) =>
352
+ toLocaleDigit ( preferredLocale , digit ) ,
353
+ ) ;
354
+
355
+ updatedTransaction . amount = updatedAmount ;
356
+ updatedTransaction . modifiedAmount = updatedAmount ;
357
+ updatedTransaction . modifiedMerchant = updatedMerchant ;
358
+ updatedTransaction . modifiedCurrency = updatedCurrency ;
307
359
}
308
360
}
309
361
@@ -861,49 +913,6 @@ function calculateTaxAmount(percentage: string, amount: number, currency: string
861
913
return parseFloat ( taxAmount . toFixed ( decimals ) ) ;
862
914
}
863
915
864
- /**
865
- * Calculates updated amount, currency, and merchant for a distance request with modified waypoints or customUnitRateID
866
- */
867
- function calculateAmountForUpdatedWaypointOrRate (
868
- transaction : OnyxInputOrEntry < Transaction > ,
869
- transactionChanges : TransactionChanges ,
870
- policy : OnyxInputOrEntry < Policy > ,
871
- isFromExpenseReport : boolean ,
872
- ) {
873
- const hasModifiedRouteWithPendingWaypoints = ! isEmptyObject ( transactionChanges . waypoints ) && isEmptyObject ( transactionChanges ?. routes ?. route0 ?. geometry ) ;
874
- const hasModifiedRateWithPendingWaypoints = ! ! transactionChanges ?. customUnitRateID && isFetchingWaypointsFromServer ( transaction ) ;
875
- if ( hasModifiedRouteWithPendingWaypoints || hasModifiedRateWithPendingWaypoints ) {
876
- return {
877
- amount : CONST . IOU . DEFAULT_AMOUNT ,
878
- modifiedAmount : CONST . IOU . DEFAULT_AMOUNT ,
879
- modifiedMerchant : Localize . translateLocal ( 'iou.fieldPending' ) ,
880
- } ;
881
- }
882
-
883
- const customUnitRateID = transactionChanges . customUnitRateID ?? getRateID ( transaction ) ?? '' ;
884
- const mileageRates = DistanceRequestUtils . getMileageRates ( policy , true ) ;
885
- const policyCurrency = policy ?. outputCurrency ?? PolicyUtils . getPersonalPolicy ( ) ?. outputCurrency ?? CONST . CURRENCY . USD ;
886
- const mileageRate = isCustomUnitRateIDForP2P ( transaction )
887
- ? DistanceRequestUtils . getRateForP2P ( policyCurrency , transaction ?? undefined )
888
- : mileageRates ?. [ customUnitRateID ] ?? DistanceRequestUtils . getDefaultMileageRate ( policy ) ;
889
- const { unit, rate, currency} = mileageRate ;
890
-
891
- const distanceInMeters = getDistanceInMeters ( transaction , unit ) ;
892
- const amount = DistanceRequestUtils . getDistanceRequestAmount ( distanceInMeters , unit , rate ?? 0 ) ;
893
- const updatedAmount = isFromExpenseReport ? - amount : amount ;
894
- const updatedCurrency = currency ?? CONST . CURRENCY . USD ;
895
- const updatedMerchant = DistanceRequestUtils . getDistanceMerchant ( true , distanceInMeters , unit , rate , updatedCurrency , Localize . translateLocal , ( digit ) =>
896
- toLocaleDigit ( preferredLocale , digit ) ,
897
- ) ;
898
-
899
- return {
900
- amount : updatedAmount ,
901
- modifiedAmount : updatedAmount ,
902
- modifiedMerchant : updatedMerchant ,
903
- modifiedCurrency : updatedCurrency ,
904
- } ;
905
- }
906
-
907
916
/**
908
917
* Calculates count of all tax enabled options
909
918
*/
@@ -1216,7 +1225,6 @@ function buildTransactionsMergeParams(reviewDuplicates: OnyxEntry<ReviewDuplicat
1216
1225
export {
1217
1226
buildOptimisticTransaction ,
1218
1227
calculateTaxAmount ,
1219
- calculateAmountForUpdatedWaypointOrRate ,
1220
1228
getWorkspaceTaxesSettingsName ,
1221
1229
getDefaultTaxCode ,
1222
1230
transformedTaxRates ,
0 commit comments