2022-10-21 10:46:09 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use Xentral\Carrier\SendCloud\Data\Document;
|
|
|
|
use Xentral\Carrier\SendCloud\Data\ParcelCreation;
|
2022-10-29 21:38:28 +02:00
|
|
|
use Xentral\Carrier\SendCloud\Data\ParcelItem;
|
2022-10-21 10:46:09 +02:00
|
|
|
use Xentral\Carrier\SendCloud\Data\ParcelResponse;
|
|
|
|
use Xentral\Carrier\SendCloud\SendCloudApi;
|
|
|
|
use Xentral\Carrier\SendCloud\Data\SenderAddress;
|
|
|
|
use Xentral\Carrier\SendCloud\Data\ShippingProduct;
|
|
|
|
use Xentral\Carrier\SendCloud\Data\ShippingMethod;
|
2022-11-02 22:37:04 +01:00
|
|
|
use Xentral\Carrier\SendCloud\SendcloudApiException;
|
2022-11-14 22:29:42 +01:00
|
|
|
use Xentral\Modules\ShippingMethod\Model\CreateShipmentResult;
|
|
|
|
use Xentral\Modules\ShippingMethod\Model\Product;
|
2022-10-21 10:46:09 +02:00
|
|
|
|
|
|
|
require_once dirname(__DIR__) . '/class.versanddienstleister.php';
|
|
|
|
|
|
|
|
class Versandart_sendcloud extends Versanddienstleister
|
|
|
|
{
|
2022-10-27 13:17:19 +02:00
|
|
|
protected SendCloudApi $api;
|
|
|
|
protected array $options;
|
2022-10-21 10:46:09 +02:00
|
|
|
|
2022-10-27 13:17:19 +02:00
|
|
|
public function __construct(Application $app, ?int $id)
|
2022-10-21 10:46:09 +02:00
|
|
|
{
|
2022-10-27 13:17:19 +02:00
|
|
|
parent::__construct($app, $id);
|
|
|
|
if (!isset($this->id))
|
|
|
|
return;
|
2022-10-21 10:46:09 +02:00
|
|
|
$this->api = new SendCloudApi($this->settings->public_key, $this->settings->private_key);
|
|
|
|
}
|
|
|
|
|
2022-11-14 22:29:42 +01:00
|
|
|
public function GetName(): string
|
|
|
|
{
|
|
|
|
return "SendCloud";
|
|
|
|
}
|
|
|
|
|
2022-10-21 10:46:09 +02:00
|
|
|
protected function FetchOptionsFromApi()
|
|
|
|
{
|
2022-11-02 22:37:04 +01:00
|
|
|
if (isset($this->options))
|
|
|
|
return;
|
|
|
|
try {
|
|
|
|
$list = $this->api->GetSenderAddresses();
|
|
|
|
foreach ($list as $item) {
|
|
|
|
/* @var SenderAddress $item */
|
|
|
|
$senderAddresses[$item->Id] = $item;
|
|
|
|
}
|
|
|
|
$senderCountry = $senderAddresses[$this->settings->sender_address]->Country ?? 'DE';
|
|
|
|
$list = $this->api->GetShippingProducts($senderCountry);
|
|
|
|
foreach ($list as $item) {
|
|
|
|
/* @var ShippingProduct $item */
|
|
|
|
$shippingProducts[$item->Code] = $item;
|
|
|
|
}
|
|
|
|
} catch (SendcloudApiException $e) {
|
|
|
|
$this->app->Tpl->addMessage('error', $e->getMessage());
|
2022-10-21 10:46:09 +02:00
|
|
|
}
|
|
|
|
$this->options['senders'] = array_map(fn(SenderAddress $x) => strval($x), $senderAddresses ?? []);
|
|
|
|
$this->options['products'] = array_map(fn(ShippingProduct $x) => $x->Name, $shippingProducts ?? []);
|
|
|
|
$this->options['products'][0] = '';
|
2022-10-29 21:38:28 +02:00
|
|
|
$this->options['selectedProduct'] = $shippingProducts[$this->settings->shipping_product] ?? [];
|
2022-10-27 13:17:19 +02:00
|
|
|
natcasesort($this->options['products']);
|
2022-10-21 10:46:09 +02:00
|
|
|
}
|
|
|
|
|
2022-10-27 13:17:19 +02:00
|
|
|
public function AdditionalSettings(): array
|
2022-10-21 10:46:09 +02:00
|
|
|
{
|
|
|
|
$this->FetchOptionsFromApi();
|
|
|
|
return [
|
|
|
|
'public_key' => ['typ' => 'text', 'bezeichnung' => 'API Public Key:'],
|
|
|
|
'private_key' => ['typ' => 'text', 'bezeichnung' => 'API Private Key:'],
|
|
|
|
'sender_address' => ['typ' => 'select', 'bezeichnung' => 'Absender-Adresse:', 'optionen' => $this->options['senders']],
|
|
|
|
'shipping_product' => ['typ' => 'select', 'bezeichnung' => 'Versand-Produkt:', 'optionen' => $this->options['products']],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-11-14 22:29:42 +01:00
|
|
|
public function CreateShipment(object $json, array $address): CreateShipmentResult
|
2022-10-21 10:46:09 +02:00
|
|
|
{
|
2022-11-14 22:29:42 +01:00
|
|
|
$parcel = new ParcelCreation();
|
|
|
|
$parcel->SenderAddressId = $this->settings->sender_address;
|
|
|
|
$parcel->ShippingMethodId = $json->product;
|
|
|
|
$parcel->Name = $json->name;
|
|
|
|
$parcel->CompanyName = $json->companyname;
|
|
|
|
$parcel->Country = $json->country;
|
|
|
|
$parcel->PostalCode = $json->zip;
|
|
|
|
$parcel->City = $json->city;
|
|
|
|
$parcel->Address = $json->street;
|
|
|
|
$parcel->Address2 = $json->address2;
|
|
|
|
$parcel->HouseNumber = $json->streetnumber;
|
|
|
|
$parcel->EMail = $json->email;
|
|
|
|
$parcel->Telephone = $json->phone;
|
|
|
|
$parcel->CountryState = $json->state;
|
|
|
|
$parcel->CustomsInvoiceNr = $json->invoice_number;
|
|
|
|
$parcel->CustomsShipmentType = $json->shipment_type;
|
|
|
|
$parcel->TotalInsuredValue = $json->total_insured_value;
|
|
|
|
$parcel->OrderNumber = $json->order_number;
|
|
|
|
foreach ($json->positions as $pos) {
|
|
|
|
$item = new ParcelItem();
|
|
|
|
$item->HsCode = $pos->zolltarifnummer;
|
|
|
|
$item->Description = $pos->bezeichnung;
|
|
|
|
$item->Quantity = $pos->menge;
|
|
|
|
$item->OriginCountry = $pos->herkunftsland;
|
|
|
|
$item->Price = $pos->zolleinzelwert;
|
|
|
|
$item->Weight = $pos->zolleinzelgewicht * 1000;
|
|
|
|
$parcel->ParcelItems[] = $item;
|
|
|
|
}
|
|
|
|
$parcel->Weight = floatval($json->weight) * 1000;
|
|
|
|
$ret = new CreateShipmentResult();
|
|
|
|
try {
|
|
|
|
$result = $this->api->CreateParcel($parcel);
|
|
|
|
if ($result instanceof ParcelResponse) {
|
|
|
|
$ret->Success = true;
|
|
|
|
$ret->TrackingNumber = $result->TrackingNumber;
|
|
|
|
$ret->TrackingUrl = $result->TrackingUrl;
|
2022-10-29 21:38:28 +02:00
|
|
|
|
2022-11-14 22:29:42 +01:00
|
|
|
$doc = $result->GetDocumentByType(Document::TYPE_LABEL);
|
|
|
|
$ret->Label = $this->api->DownloadDocument($doc);
|
2022-10-21 10:46:09 +02:00
|
|
|
|
2022-11-14 22:29:42 +01:00
|
|
|
$doc = $result->GetDocumentByType(Document::TYPE_CN23);
|
|
|
|
$ret->ExportDocuments = $this->api->DownloadDocument($doc);
|
|
|
|
} else {
|
|
|
|
$ret->Errors[] = $result;
|
2022-10-21 10:46:09 +02:00
|
|
|
}
|
2022-11-14 22:29:42 +01:00
|
|
|
} catch (SendcloudApiException $e) {
|
|
|
|
$ret->Errors[] = $e->getMessage();
|
2022-10-21 10:46:09 +02:00
|
|
|
}
|
2022-11-14 22:29:42 +01:00
|
|
|
return $ret;
|
|
|
|
}
|
2022-10-21 10:46:09 +02:00
|
|
|
|
2022-11-14 22:29:42 +01:00
|
|
|
public function GetShippingProducts(): array
|
|
|
|
{
|
2022-10-21 10:46:09 +02:00
|
|
|
$this->FetchOptionsFromApi();
|
|
|
|
/** @var ShippingProduct $product */
|
|
|
|
$product = $this->options['selectedProduct'];
|
2022-11-14 22:29:42 +01:00
|
|
|
$result = [];
|
2022-10-21 10:46:09 +02:00
|
|
|
/** @var ShippingMethod $item */
|
2022-11-14 22:29:42 +01:00
|
|
|
foreach ($product->ShippingMethods as $item) {
|
|
|
|
$p = new Product();
|
|
|
|
$p->Id = $item->Id;
|
|
|
|
$p->Name = $item->Name;
|
|
|
|
$result[] = $p;
|
|
|
|
}
|
|
|
|
return $result;
|
2022-10-21 10:46:09 +02:00
|
|
|
}
|
|
|
|
|
2022-11-14 22:29:42 +01:00
|
|
|
|
2022-10-21 10:46:09 +02:00
|
|
|
}
|