Skip to content

Commit 815a020

Browse files
committed
using geocoder into shipment trackers to determine current location
1 parent 7ddc810 commit 815a020

File tree

4 files changed

+238
-142
lines changed

4 files changed

+238
-142
lines changed

app/base/commerce/ShipmentTrackers/DHLTracker.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,34 @@ public function updateShipmentStatus(OrderShipment $shipment): void
225225

226226
$status = $data['shipments'][0]['status']['status'] ?? null;
227227
$location = $data['shipments'][0]['status']['location']['address'] ?? null;
228+
229+
// DHL API does not provide latitude and longitude in this response
228230
$latitude = null;
229231
$longitude = null;
230-
// DHL API does not provide latitude and longitude in this response
232+
233+
// use geocoder class to get lat/lon from location if needed
234+
$geocoder = $this->getGeocoder();
235+
236+
try {
237+
$position = $geocoder->geocode(
238+
trim(
239+
($location['addressLocality'] ?? '') . ' ' .
240+
($location['postalCode'] ?? '') . ' ' .
241+
($location['countryCode'] ?? '')
242+
)
243+
);
244+
245+
$latitude = $position['lat'] ?? null;
246+
$longitude = $position['lon'] ?? null;
247+
} catch (BasicException $e) {
248+
// log error
249+
}
250+
231251

232252
$shipment->setStatus($status)->persist();
253+
254+
if ($latitude !== null && $longitude !== null) {
255+
$shipment->updatePosition($latitude, $longitude);
256+
}
233257
}
234258
}

app/base/commerce/ShipmentTrackers/FedExTracker.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,11 +465,33 @@ public function updateShipmentStatus(OrderShipment $shipment): void
465465
$data = $this->fetchTrackingData($shipment);
466466

467467
$status = $data['output']['completeTrackResults'][0]['trackResults'][0]['latestStatusDetail']['description'] ?? null;
468+
469+
// FedEx API does not provide latitude and longitude in this response
468470
$latitude = null;
469471
$longitude = null;
470-
// FedEx API does not provide latitude and longitude in this response
471472

473+
// use geocoder class to get lat/lon from location if needed
474+
$geocoder = $this->getGeocoder();
475+
476+
try {
477+
$position = $geocoder->geocode(
478+
trim(
479+
($data['output']['completeTrackResults'][0]['trackResults'][0]['destinationLocation']['locationContactAndAddress']['address']['city'] ?? '') . ' ' .
480+
($data['output']['completeTrackResults'][0]['trackResults'][0]['destinationLocation']['locationContactAndAddress']['address']['postalCode'] ?? '') . ' ' .
481+
($data['output']['completeTrackResults'][0]['trackResults'][0]['destinationLocation']['locationContactAndAddress']['address']['countryCode'] ?? '')
482+
)
483+
);
484+
485+
$latitude = $position['lat'] ?? null;
486+
$longitude = $position['lon'] ?? null;
487+
} catch (BasicException $e) {
488+
// log error
489+
}
472490

473491
$shipment->setStatus($status)->persist();
492+
493+
if ($latitude !== null && $longitude !== null) {
494+
$shipment->updatePosition($latitude, $longitude);
495+
}
474496
}
475497
}

app/base/commerce/ShipmentTrackers/GLSTracker.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,42 @@ public function updateShipmentStatus(OrderShipment $shipment): void
126126
*/
127127
$data = $this->fetchTrackingData($shipment);
128128
$status = $data['parcels'][0]['status'] ?? null;
129+
130+
// GLS API does not provide latitude and longitude in this response
129131
$latitude = null;
130132
$longitude = null;
131-
// GLS API does not provide latitude and longitude in this response
133+
134+
// use geocoder class to get lat/lon from location if needed
135+
$geocoder = $this->getGeocoder();
136+
137+
try {
138+
// get latest event location
139+
$events = $data['parcels'][0]['events'] ?? [];
140+
usort($events, function ($a, $b) {
141+
return strtotime($b['timestamp']) <=> strtotime($a['timestamp']);
142+
});
143+
$latestEvent = $events[0] ?? null;
144+
145+
146+
$position = $geocoder->geocode(
147+
trim(
148+
($latestEvent['location'] ?? '') . ' ' .
149+
' ' // no postal code available
150+
($latestEvent['country'] ?? '')
151+
)
152+
);
153+
154+
$latitude = $position['lat'] ?? null;
155+
$longitude = $position['lon'] ?? null;
156+
} catch (BasicException $e) {
157+
// log error
158+
}
159+
132160

133161
$shipment->setStatus($status)->persist();
162+
163+
if ($latitude !== null && $longitude !== null) {
164+
$shipment->updatePosition($latitude, $longitude);
165+
}
134166
}
135167
}

0 commit comments

Comments
 (0)