Merge pull request #156 from exciler/trackingstatus

Tracking Status from Sendcloud
This commit is contained in:
OpenXE-ERP 2024-11-05 12:51:40 +01:00 committed by GitHub
commit 99c74b6380
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 108 additions and 4 deletions

View File

@ -1,7 +1,7 @@
<?php
/*
* SPDX-FileCopyrightText: 2022 Andreas Palm
* SPDX-FileCopyrightText: 2022-2024 Andreas Palm
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
@ -14,6 +14,7 @@ use Xentral\Carrier\SendCloud\Data\ParcelCreation;
use Xentral\Carrier\SendCloud\Data\ParcelResponse;
use Xentral\Carrier\SendCloud\Data\SenderAddress;
use Xentral\Carrier\SendCloud\Data\ShippingProduct;
use Xentral\Modules\ShippingMethod\Model\ShipmentStatus;
class SendCloudApi
{
@ -118,6 +119,31 @@ class SendCloudApi
return $output;
}
/**
* @throws SendcloudApiException
*/
public function GetTrackingStatus(string $trackingNumber): ShipmentStatus|null
{
$uri = self::PROD_BASE_URI . '/tracking/' . $trackingNumber;
$response = $this->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
*/

View File

@ -0,0 +1,14 @@
<?php
// SPDX-FileCopyrightText: 2024 Andreas Palm
//
// SPDX-License-Identifier: AGPL-3.0-only
namespace Xentral\Modules\ShippingMethod\Model;
enum ShipmentStatus
{
case Announced;
case EnRoute;
case Delivered;
}

View File

@ -0,0 +1,43 @@
<?php
// SPDX-FileCopyrightText: 2024 Andreas Palm
//
// SPDX-License-Identifier: LicenseRef-EGPL-3.1
use Xentral\Components\Database\Database;
use Xentral\Modules\ShippingMethod\Model\ShipmentStatus;
error_reporting(E_ERROR);
include_once dirname(__DIR__) . '/xentral_autoloader.php';
if(empty($app) || !($app instanceof ApplicationCore)){
$app = new ApplicationCore();
}
/** @var Database $db */
$db = $app->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']]);
}
}

View File

@ -1,6 +1,6 @@
<?php
/*
* SPDX-FileCopyrightText: 2022 Andreas Palm
* SPDX-FileCopyrightText: 2022-2024 Andreas Palm
* SPDX-FileCopyrightText: 2019 Xentral (c) Xentral ERP Software GmbH, Fuggerstrasse 11, D-86150 Augsburg, Germany
*
* SPDX-License-Identifier: LicenseRef-EGPL-3.1
@ -9,6 +9,7 @@
use Xentral\Modules\ShippingMethod\Model\CreateShipmentResult;
use Xentral\Modules\ShippingMethod\Model\CustomsInfo;
use Xentral\Modules\ShippingMethod\Model\Product;
use Xentral\Modules\ShippingMethod\Model\ShipmentStatus;
abstract class Versanddienstleister
{
@ -504,4 +505,6 @@ abstract class Versanddienstleister
* @return Product[]
*/
public abstract function GetShippingProducts(): array;
public abstract function GetShipmentStatus(string $tracking): ShipmentStatus|null;
}

View File

@ -1,7 +1,7 @@
<?php
/*
* SPDX-FileCopyrightText: 2022 Andreas Palm
* SPDX-FileCopyrightText: 2022-2024 Andreas Palm
*
* SPDX-License-Identifier: LicenseRef-EGPL-3.1
*/
@ -17,6 +17,7 @@ use Xentral\Carrier\Dhl\Data\ShipmentItem;
use Xentral\Carrier\Dhl\DhlApi;
use Xentral\Modules\ShippingMethod\Model\CreateShipmentResult;
use Xentral\Modules\ShippingMethod\Model\Product;
use Xentral\Modules\ShippingMethod\Model\ShipmentStatus;
require_once(dirname(__DIR__).'/class.versanddienstleister.php');
class Versandart_dhl extends Versanddienstleister{
@ -245,4 +246,11 @@ class Versandart_dhl extends Versanddienstleister{
}
return null;
}
public function GetShipmentStatus(string $tracking): ShipmentStatus|null
{
return null;
}
}

View File

@ -1,7 +1,7 @@
<?php
/*
* SPDX-FileCopyrightText: 2022 Andreas Palm
* SPDX-FileCopyrightText: 2022-2024 Andreas Palm
*
* SPDX-License-Identifier: LicenseRef-EGPL-3.1
*/
@ -17,6 +17,7 @@ use Xentral\Carrier\SendCloud\Data\ShippingMethod;
use Xentral\Carrier\SendCloud\SendcloudApiException;
use Xentral\Modules\ShippingMethod\Model\CreateShipmentResult;
use Xentral\Modules\ShippingMethod\Model\Product;
use Xentral\Modules\ShippingMethod\Model\ShipmentStatus;
require_once dirname(__DIR__) . '/class.versanddienstleister.php';
@ -190,5 +191,14 @@ class Versandart_sendcloud extends Versanddienstleister
return $result;
}
public function GetShipmentStatus(string $tracking): ShipmentStatus|null
{
try {
return $this->api->GetTrackingStatus($tracking);
} catch (SendcloudApiException) {
return null;
}
}
}