Skip to content

Commit 207811f

Browse files
committed
patch and fix the findServiceRateFeeByDistance method
1 parent 38ee6fc commit 207811f

1 file changed

Lines changed: 11 additions & 17 deletions

File tree

server/src/Models/ServiceRate.php

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,29 +1002,23 @@ public function quote(Payload $payload)
10021002
*/
10031003
public function findServiceRateFeeByDistance(int $totalDistance): ?ServiceRateFee
10041004
{
1005-
$this->load('rateFees');
1005+
$this->loadMissing('rateFees');
10061006

1007-
$distanceInKms = round($totalDistance / 1000);
1008-
$distanceFee = null;
1007+
// Convert meters to kilometers WITHOUT rounding up
1008+
$distanceInKm = $totalDistance / 1000;
10091009

1010-
foreach ($this->rateFees as $rateFee) {
1011-
$previousRateFee = $rateFee;
1010+
// Ensure predictable order
1011+
$rateFees = $this->rateFees->sortBy('distance');
10121012

1013-
if ($distanceInKms > $rateFee->distance) {
1014-
continue;
1015-
} elseif ($rateFee->distance > $distanceInKms) {
1016-
$distanceFee = $previousRateFee;
1017-
} else {
1018-
$distanceFee = $rateFee;
1013+
// Find the first tier that covers the distance
1014+
foreach ($rateFees as $rateFee) {
1015+
if ($distanceInKm <= $rateFee->distance) {
1016+
return $rateFee;
10191017
}
10201018
}
10211019

1022-
// if no distance fee use the last
1023-
if ($distanceFee === null) {
1024-
$distanceFee = $this->rateFees->sortByDesc('distance')->first();
1025-
}
1026-
1027-
return $distanceFee;
1020+
// If distance exceeds all tiers, use the largest tier
1021+
return $rateFees->last();
10281022
}
10291023

10301024
/**

0 commit comments

Comments
 (0)