diff --git a/classes/Carrier/Go/Data/Address.php b/classes/Carrier/Go/Data/Address.php new file mode 100644 index 00000000..ea03e962 --- /dev/null +++ b/classes/Carrier/Go/Data/Address.php @@ -0,0 +1,24 @@ +shipment = new Shipment(); + $this->consignorAddress = new Address(); + $this->consigneeAddress = new Address(); + $this->neutralAddress = new NeutralAddress(); + $this->packages = [new Package()]; + } +} \ No newline at end of file diff --git a/classes/Carrier/Go/Data/CreateOrderResponse.php b/classes/Carrier/Go/Data/CreateOrderResponse.php new file mode 100644 index 00000000..f4217407 --- /dev/null +++ b/classes/Carrier/Go/Data/CreateOrderResponse.php @@ -0,0 +1,17 @@ + isset($this->dateFrom) ? $this->dateFrom->format('d.m.Y') : '', + 'timeFrom' => !$this->dateIsAvis && isset($this->dateFrom) ? $this->dateFrom->format('H:i') : '', + 'timeTill' => !$this->dateIsAvis && isset($this->dateTill) ? $this->dateTill->format('H:i') : '', + 'avisFrom' => $this->dateIsAvis && isset($this->dateFrom) ? $this->dateFrom->format('H:i') : '', + 'avisTill' => $this->dateIsAvis && isset($this->dateTill) ? $this->dateTill->format('H:i') : '', + 'weekendOrHolidayIndicator' => $this->deliveryOnHoliday ? 'H' : ($this->deliveryOnSaturday ? 'S' : '') + ]; + } +} \ No newline at end of file diff --git a/classes/Carrier/Go/Data/ErrorResponse.php b/classes/Carrier/Go/Data/ErrorResponse.php new file mode 100644 index 00000000..e1b39eaa --- /dev/null +++ b/classes/Carrier/Go/Data/ErrorResponse.php @@ -0,0 +1,14 @@ + $this->amount > 0 ? (string)$this->amount : '', + 'currency' => $this->amount > 0 ? $this->currency : '', + ]; + } +} \ No newline at end of file diff --git a/classes/Carrier/Go/Data/OrderStatus.php b/classes/Carrier/Go/Data/OrderStatus.php new file mode 100644 index 00000000..3ca7a9b9 --- /dev/null +++ b/classes/Carrier/Go/Data/OrderStatus.php @@ -0,0 +1,13 @@ + $this->length ? (string) $this->length : '', + 'width' => $this->width ? (string) $this->width : '', + 'height' => $this->height ? (string) $this->height : '', + ]; + } +} \ No newline at end of file diff --git a/classes/Carrier/Go/Data/Pickup.php b/classes/Carrier/Go/Data/Pickup.php new file mode 100644 index 00000000..5b5c7ce8 --- /dev/null +++ b/classes/Carrier/Go/Data/Pickup.php @@ -0,0 +1,21 @@ + $this->dateFrom->format('d.m.Y'), + 'timeFrom' => $this->dateFrom->format('H:i'), + 'timeTill' => $this->dateTill->format('H:i'), + ]; + } +} \ No newline at end of file diff --git a/classes/Carrier/Go/Data/Shipment.php b/classes/Carrier/Go/Data/Shipment.php new file mode 100644 index 00000000..a520173a --- /dev/null +++ b/classes/Carrier/Go/Data/Shipment.php @@ -0,0 +1,57 @@ +pickup = new Pickup(); + $this->delivery = new Delivery(); + $this->insurance = new MoneyValue(); + $this->valueOfGoods = new MoneyValue(); + $this->cashOnDelivery = new MoneyValue(); + } + + public function SetService(string $service) { + $this->service = ShipmentService::from($service); + } + + public function jsonSerialize() + { + $array = (array) $this; + $array['dimensions'] = ''; + return array_map(function ($value) { + if (is_bool($value)) + return $value ? 'Yes' : 'No'; + return $value; + }, $array); + } +} \ No newline at end of file diff --git a/classes/Carrier/Go/Data/ShipmentService.php b/classes/Carrier/Go/Data/ShipmentService.php new file mode 100644 index 00000000..6c614f3c --- /dev/null +++ b/classes/Carrier/Go/Data/ShipmentService.php @@ -0,0 +1,17 @@ +baseUrl = self::BASE_URL_TEST; + else + $this->baseUrl = self::BASE_URL_PROD; + } + + public function createOrder(CreateOrderRequest $request): CreateOrderResponse|string { + $curl = curl_init(); + curl_setopt_array($curl, [ + CURLOPT_RETURNTRANSFER => 1, + CURLOPT_URL => $this->baseUrl.'createOrder', + CURLOPT_POST => 1, + CURLOPT_USERNAME => $this->username, + CURLOPT_PASSWORD => $this->password, + CURLOPT_HTTPHEADER => ['Content-Type: application/json'], + CURLOPT_POSTFIELDS => json_encode($request, JSON_THROW_ON_ERROR), + ]); + + $response = json_decode(curl_exec($curl)); + $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); + curl_close($curl); + + if ($code == 200) { + $ret = new CreateOrderResponse(); + $ret->hwbNumber = $response->hwbNumber; + $ret->orderStatus = OrderStatus::from($response->orderStatus); + $ret->pickupDate = new \DateTime($response->pickupDate); + $ret->deliveryDate = new \DateTime($response->deliveryDate); + $ret->hwbOrPackageLabel = $response->hwbOrPackageLabel; + $ret->barcodes = array_map(function ($item) { return $item->barcode; }, $response->package); + return $ret; + } + return $response->message; + } +} \ No newline at end of file diff --git a/www/lib/versandarten/go.php b/www/lib/versandarten/go.php new file mode 100644 index 00000000..d6b7c688 --- /dev/null +++ b/www/lib/versandarten/go.php @@ -0,0 +1,157 @@ +id) || !isset($this->settings->username) || !isset($this->settings->password)) + return; + $this->api = new GoApi($this->settings->username, $this->settings->password, $this->settings->useTestEndpoint ?? true); + } + + public function GetName(): string + { + return "GO!"; + } + + public function AdditionalSettings(): array + { + return [ + 'username' => ['typ' => 'text', 'bezeichnung' => 'API Username:'], + 'password' => ['typ' => 'text', 'bezeichnung' => 'API Password:'], + 'responsibleStation' => ['typ' => 'text', 'bezeichnung' => 'Responsible Station:'], + 'customerId' => ['typ' => 'text', 'bezeichnung' => 'Customer ID:'], + 'useTestEndpoint' => ['typ' => 'checkbox', 'bezeichnung' => 'Testsystem verwenden:', 'default' => true], + 'orderAsDraft' => ['typ' => 'checkbox', 'bezeichnung' => 'Order As Draft:'], + 'labelType' => ['typ' => 'select', 'bezeichnung' => 'Label Type:', 'optionen' => [ + LabelType::PDF_A6->value => 'PDF A6', + LabelType::PDF_A4->value => 'PDF A4', + ]], + 'consignorName1' => ['typ' => 'text', 'bezeichnung' => 'Absender Name1:'], + 'consignorName2' => ['typ' => 'text', 'bezeichnung' => 'Absender Name2:'], + 'consignorName3' => ['typ' => 'text', 'bezeichnung' => 'Absender Name3:'], + 'consignorStreet' => ['typ' => 'text', 'bezeichnung' => 'Absender Straße:'], + 'consignorHouseNumber' => ['typ' => 'text', 'bezeichnung' => 'Absender Hausnummer:'], + 'consignorZipCode' => ['typ' => 'text', 'bezeichnung' => 'Absender PLZ:'], + 'consignorCity' => ['typ' => 'text', 'bezeichnung' => 'Absender Stadt:'], + 'consignorCountry' => ['typ' => 'text', 'bezeichnung' => 'Absender Land:'], + 'consignorPhoneNumber' => ['typ' => 'text', 'bezeichnung' => 'Absender Telefon:'], + 'consignorRemarks' => ['typ' => 'text', 'bezeichnung' => 'Absender Bemerkungen:'], + 'consignorEmail' => ['typ' => 'text', 'bezeichnung' => 'Absender Email:'], + 'defaultPickupFrom' => ['typ' => 'text', 'bezeichnung' => 'Standard Abholzeit von:'], + 'defaultPickupTill' => ['typ' => 'text', 'bezeichnung' => 'Standard Abholzeit bis:'], + ]; + } + + public function CreateShipment(object $json, array $address): CreateShipmentResult + { + $order = new CreateOrderRequest(); + $order->responsibleStation = $this->settings->responsibleStation; + $order->customerId = $this->settings->customerId; + $order->label = LabelType::from($this->settings->labelType); + $order->shipment->orderStatus = $this->settings->orderAsDraft ? OrderStatus::New : OrderStatus::Released; + $order->shipment->SetService($json->product); + $order->shipment->customerReference = $json->order_number; + $order->shipment->weight = floatval($json->weight); + $order->shipment->pickup->dateFrom = new DateTime($this->settings->defaultPickupFrom); + if ($order->shipment->pickup->dateFrom < new DateTime('now')) + $order->shipment->pickup->dateFrom = $order->shipment->pickup->dateFrom->add(new DateInterval('P1D')); + $order->shipment->pickup->dateTill = new DateTime($this->settings->defaultPickupTill); + if ($json->total_insured_value > 0) + $order->shipment->insurance->amount = $json->total_insured_value; + $order->consignorAddress->name1 = $this->settings->consignorName1; + $order->consignorAddress->name2 = $this->settings->consignorName2; + $order->consignorAddress->name3 = $this->settings->consignorName3; + $order->consignorAddress->street = $this->settings->consignorStreet; + $order->consignorAddress->houseNumber = $this->settings->consignorHouseNumber; + $order->consignorAddress->zipCode = $this->settings->consignorZipCode; + $order->consignorAddress->city = $this->settings->consignorCity; + $order->consignorAddress->country = $this->settings->consignorCountry; + $order->consignorAddress->phoneNumber = $this->settings->consignorPhoneNumber; + $order->consignorAddress->remarks = $this->settings->consignorRemarks; + $order->consignorAddress->email = $this->settings->consignorEmail; + switch ($json->addresstype) { + case 0: + $order->consigneeAddress->name1 = $json->company_name; + $order->consigneeAddress->name2 = join( + ';', + array_filter( + [ + $json->contact_name, + $json->company_division + ], + fn(string $item) => !empty(trim($item)) + ) + ); + break; + case 3: + $order->consigneeAddress->name1 = $json->name; + $order->consigneeAddress->name2 = $json->contact_name; + break; + + } + $order->consigneeAddress->name3 = $json->address2; + $order->consigneeAddress->street = $json->street; + $order->consigneeAddress->houseNumber = $json->streetnumber; + $order->consigneeAddress->country = $json->country; + $order->consigneeAddress->zipCode = $json->zip; + $order->consigneeAddress->city = $json->city; + $order->consigneeAddress->email = $json->email; + $order->consigneeAddress->phoneNumber = $json->phone; + $ret = new CreateShipmentResult(); + $result = $this->api->createOrder($order); + if ($result instanceof CreateOrderResponse) { + $ret->Success = true; + $ret->TrackingNumber = $result->hwbNumber; + $ret->TrackingUrl = ''; + $ret->Label = base64_decode($result->hwbOrPackageLabel); + } else { + $ret->Errors[] = $result; + } + return $ret; + } + + public function GetShippingProducts(): array + { + $result = []; + $result[] = Product::Create(ShipmentService::Overnight->value, 'GO! Overnight'); + $result[] = Product::Create(ShipmentService::Worldwide->value, 'GO! Worldwide'); + $result[] = Product::Create(ShipmentService::OvernightLetter->value, 'GO! Overnight - Letter') + ->WithWeight(0, 0.25); + $result[] = Product::Create(ShipmentService::WorldwideLetter->value, 'GO! Worldwide - Letter') + ->WithWeight(0, 0.25); + $result[] = Product::Create(ShipmentService::OvernightCodedDelivery->value, 'GO! Overnight Coded Delivery'); + $result[] = Product::Create(ShipmentService::OvernightCodedLetter->value, 'GO! Overnight Letter Coded Delivery') + ->WithWeight(0, 0.25); + return $result; + } + + public function GetShipmentStatus(string $tracking): ShipmentStatus|null + { + // TODO: Implement GetShipmentStatus() method. + return null; + } + + +}