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-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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-11-02 22:37:04 +01:00
|
|
|
$this->options['customs_shipment_types'] = [
|
|
|
|
0 => 'Geschenk',
|
|
|
|
1 => 'Dokumente',
|
|
|
|
2 => 'Kommerzielle Waren',
|
|
|
|
3 => 'Erprobungswaren',
|
|
|
|
4 => 'Rücksendung'
|
|
|
|
];
|
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-10-29 21:38:28 +02:00
|
|
|
'default_customs_shipment_type' => ['typ' => 'select', 'bezeichnung' => 'Sendungsart:', 'optionen' => $this->options['customs_shipment_types']],
|
2022-10-21 10:46:09 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-10-29 21:38:28 +02:00
|
|
|
public function Paketmarke(string $target, string $docType, int $docId): void
|
2022-10-21 10:46:09 +02:00
|
|
|
{
|
2022-10-29 21:38:28 +02:00
|
|
|
$address = $this->GetAdressdaten($docId, $docType);
|
2022-10-21 10:46:09 +02:00
|
|
|
|
2022-10-29 21:38:28 +02:00
|
|
|
if (isset($_SERVER['HTTP_CONTENT_TYPE']) && ($_SERVER['HTTP_CONTENT_TYPE'] === 'application/json')) {
|
|
|
|
$json = json_decode(file_get_contents('php://input'));
|
|
|
|
$response = [];
|
|
|
|
if ($json->submit == 'print') {
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
$parcel = new ParcelCreation();
|
|
|
|
$parcel->SenderAddressId = $this->settings->sender_address;
|
|
|
|
$parcel->ShippingMethodId = $json->method;
|
|
|
|
$parcel->Name = $json->l_name;
|
|
|
|
$parcel->CompanyName = $json->l_companyname;
|
|
|
|
$parcel->Country = $json->land;
|
|
|
|
$parcel->PostalCode = $json->plz;
|
|
|
|
$parcel->City = $json->ort;
|
|
|
|
$parcel->Address = $json->strasse;
|
|
|
|
$parcel->Address2 = $json->l_address2;
|
|
|
|
$parcel->HouseNumber = $json->hausnummer;
|
|
|
|
$parcel->EMail = $json->email;
|
|
|
|
$parcel->Telephone = $json->telefon;
|
|
|
|
$parcel->CountryState = $json->bundesland;
|
|
|
|
$parcel->CustomsInvoiceNr = $json->invoice_number;
|
|
|
|
$parcel->CustomsShipmentType = $json->sendungsart;
|
|
|
|
$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;
|
2022-11-02 22:37:04 +01:00
|
|
|
try {
|
|
|
|
$result = $this->api->CreateParcel($parcel);
|
|
|
|
if ($result instanceof ParcelResponse) {
|
|
|
|
$sql = "INSERT INTO versand
|
|
|
|
(adresse, lieferschein, versandunternehmen, gewicht, tracking, tracking_link, anzahlpakete)
|
|
|
|
VALUES
|
|
|
|
({$address['addressId']}, {$address['lieferscheinId']}, '$this->type',
|
|
|
|
'$json->weight', '$result->TrackingNumber', '$result->TrackingUrl', 1)";
|
|
|
|
$this->app->DB->Insert($sql);
|
|
|
|
$response['messages'][] = ['class' => 'info', 'text' => "Paketmarke wurde erfolgreich erstellt: $result->TrackingNumber"];
|
2022-10-29 21:38:28 +02:00
|
|
|
|
2022-11-02 22:37:04 +01:00
|
|
|
$doc = $result->GetDocumentByType(Document::TYPE_LABEL);
|
|
|
|
$filename = $this->app->erp->GetTMP() . join('_', ['Sendcloud', $doc->Type, $doc->Size, $result->TrackingNumber]) . '.pdf';
|
|
|
|
file_put_contents($filename, $this->api->DownloadDocument($doc));
|
|
|
|
$this->app->printer->Drucken($this->labelPrinterId, $filename);
|
2022-10-21 10:46:09 +02:00
|
|
|
|
2022-11-02 22:37:04 +01:00
|
|
|
$doc = $result->GetDocumentByType(Document::TYPE_CN23);
|
|
|
|
$filename = $this->app->erp->GetTMP() . join('_', ['Sendcloud', $doc->Type, $doc->Size, $result->TrackingNumber]) . '.pdf';
|
|
|
|
file_put_contents($filename, $this->api->DownloadDocument($doc));
|
|
|
|
$this->app->printer->Drucken($this->documentPrinterId, $filename);
|
|
|
|
} else {
|
|
|
|
$response['messages'][] = ['class' => 'error', 'text' => $result];
|
|
|
|
}
|
|
|
|
echo json_encode($response);
|
|
|
|
} catch (SendcloudApiException $e) {
|
|
|
|
$this->app->Tpl->addMessage('error', $e->getMessage());
|
2022-10-29 21:38:28 +02:00
|
|
|
}
|
|
|
|
$this->app->ExitXentral();
|
2022-10-21 10:46:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-29 21:38:28 +02:00
|
|
|
$address['l_name'] = empty(trim($address['ansprechpartner'])) ? trim($address['name']) : trim($address['ansprechpartner']);
|
|
|
|
$address['l_companyname'] = !empty(trim($address['ansprechpartner'])) ? trim($address['name']) : '';
|
|
|
|
$address['l_address2'] = join(';', array_filter([
|
|
|
|
$address['abteilung'],
|
|
|
|
$address['unterabteilung'],
|
|
|
|
$address['adresszusatz']
|
|
|
|
], fn(string $item) => !empty(trim($item))));
|
2022-10-21 10:46:09 +02:00
|
|
|
|
|
|
|
$this->FetchOptionsFromApi();
|
|
|
|
/** @var ShippingProduct $product */
|
|
|
|
$product = $this->options['selectedProduct'];
|
|
|
|
$methods = [];
|
|
|
|
/** @var ShippingMethod $item */
|
|
|
|
foreach ($product->ShippingMethods as $item)
|
|
|
|
$methods[$item->Id] = $item->Name;
|
2022-10-29 21:38:28 +02:00
|
|
|
$address['method'] = array_key_first($methods);
|
|
|
|
$address['sendungsart'] = $this->settings->default_customs_shipment_type;
|
|
|
|
|
|
|
|
$this->app->Tpl->Set('JSON', json_encode($address));
|
|
|
|
$this->app->Tpl->Set('JSON_COUNTRIES', json_encode($this->app->erp->GetSelectLaenderliste()));
|
|
|
|
$this->app->Tpl->Set('JSON_METHODS', json_encode($methods));
|
|
|
|
$this->app->Tpl->Set('JSON_CUSTOMS_SHIPMENT_TYPES', json_encode($this->options['customs_shipment_types']));
|
2022-10-21 10:46:09 +02:00
|
|
|
$this->app->Tpl->Parse($target, 'versandarten_sendcloud.tpl');
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|