From e3488040ec265b7570f19937cedc4b5721665644 Mon Sep 17 00:00:00 2001 From: Andreas Palm Date: Sun, 21 Jul 2024 12:19:29 +0200 Subject: [PATCH] Tracking Status from Sendcloud --- classes/Carrier/SendCloud/SendCloudApi.php | 28 +++++++++++- .../ShippingMethod/Model/ShipmentStatus.php | 14 ++++++ cronjobs/shipment_tracking.php | 43 +++++++++++++++++++ www/lib/class.versanddienstleister.php | 5 ++- www/lib/versandarten/dhl.php | 10 ++++- www/lib/versandarten/sendcloud.php | 12 +++++- 6 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 classes/Modules/ShippingMethod/Model/ShipmentStatus.php create mode 100644 cronjobs/shipment_tracking.php diff --git a/classes/Carrier/SendCloud/SendCloudApi.php b/classes/Carrier/SendCloud/SendCloudApi.php index 7ae71daa..be52229e 100644 --- a/classes/Carrier/SendCloud/SendCloudApi.php +++ b/classes/Carrier/SendCloud/SendCloudApi.php @@ -1,7 +1,7 @@ sendRequest($uri); + $highest = null; + foreach ($response['body']->statuses as $status) { + switch ($status->parent_status) { + case 'announcing': + case 'ready-to-send': + if ($highest === null) $highest = ShipmentStatus::Announced; + break; + case 'to-sorting': + case 'at-sorting-centre': + case 'shipment-on-route': + case 'driver-on-route': + $highest = ShipmentStatus::EnRoute; + case 'delivered': return ShipmentStatus::Delivered; + } + } + return $highest; + } + /** * @throws SendcloudApiException */ diff --git a/classes/Modules/ShippingMethod/Model/ShipmentStatus.php b/classes/Modules/ShippingMethod/Model/ShipmentStatus.php new file mode 100644 index 00000000..de04c7b4 --- /dev/null +++ b/classes/Modules/ShippingMethod/Model/ShipmentStatus.php @@ -0,0 +1,14 @@ +Container->get('Database'); + +$shipments_sql = "SELECT CONCAT(va.id, ';', va.modul) module, vp.id, vp.tracking, vp.status + FROM versandpakete vp + JOIN versandarten va ON vp.versandart = va.type + WHERE status IN ('neu', 'versendet')"; +$shipments = $db->fetchGroup($shipments_sql); + +foreach ($shipments as $module => $vps) { + list($moduleId, $moduleName) = explode(';', $module,2); + $module = $app->erp->LoadVersandModul($moduleName, $moduleName); + + foreach ($vps as $vp) { + $status = match ($module->GetShipmentStatus($vp['tracking'])) { + ShipmentStatus::Announced => 'neu', + ShipmentStatus::EnRoute => 'versendet', + ShipmentStatus::Delivered => 'abgeschlossen', + default => null, + }; + if ($status === null || $status === $vp['status']) continue; + $db->perform('UPDATE versandpakete SET status = :status WHERE id = :id', + [':status' => $status, ':id' => $vp['id']]); + } +} diff --git a/www/lib/class.versanddienstleister.php b/www/lib/class.versanddienstleister.php index d9aa1b82..7bd7b48f 100644 --- a/www/lib/class.versanddienstleister.php +++ b/www/lib/class.versanddienstleister.php @@ -1,6 +1,6 @@ api->GetTrackingStatus($tracking); + } catch (SendcloudApiException) { + return null; + } + } + }