File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 /**
You can’t perform that action at this time.
0 commit comments