diff --git a/Block/Sales/View.php b/Block/Sales/View.php index 7b5b896f..a0a523c3 100755 --- a/Block/Sales/View.php +++ b/Block/Sales/View.php @@ -22,30 +22,11 @@ use Magento\Framework\App\ObjectManager; use Magento\Sales\Block\Adminhtml\Order\AbstractOrder; use MyParcelNL\Magento\Helper\Checkout as CheckoutHelper; -use MyParcelNL\Magento\Model\Quote\Checkout; +use MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter; +use MyParcelNL\Sdk\src\Factory\DeliveryOptionsAdapterFactory; class View extends AbstractOrder { - /** - * @var \Magento\Framework\ObjectManagerInterface - */ - private $objectManager; - - /** - * @var \MyParcelNL\Magento\Helper\Order - */ - private $helper; - - /** - * Constructor - */ - public function _construct() - { - $this->objectManager = ObjectManager::getInstance(); - $this->helper = $this->objectManager->get('\MyParcelNL\Magento\Helper\Order'); - parent::_construct(); - } - /** * Collect options selected at checkout and calculate type consignment * @@ -53,51 +34,86 @@ public function _construct() * @throws \Magento\Framework\Exception\LocalizedException * @throws \Exception */ - public function getCheckoutOptionsHtml() + public function getCheckoutOptionsHtml(): string { - $html = false; $order = $this->getOrder(); /** @var object $data Data from checkout */ - $data = $order->getData(CheckoutHelper::FIELD_DELIVERY_OPTIONS) !== null ? json_decode($order->getData(CheckoutHelper::FIELD_DELIVERY_OPTIONS), true) : false; - - $date = new DateTime($data['date'] ?? ''); - $dateTime = $date->format('d-m-Y H:i'); - - if ($this->helper->isPickupLocation($data)) { - if (is_array($data) && key_exists('pickupLocation', $data)) { - $html .= __($data['carrier'] . ' location:') . ' ' . $dateTime; - if ($data['deliveryType'] != 'pickup') { - $html .= ', ' . __($data['deliveryType']); - } - $html .= ', ' . $data['pickupLocation']['location_name'] . ', ' . $data['pickupLocation']['city'] . ' (' . $data['pickupLocation']['postal_code'] . ')'; - } else { - /** Old data from orders before version 1.6.0 */ - $html .= __('MyParcel options data not found'); + $data = $order->getData(CheckoutHelper::FIELD_DELIVERY_OPTIONS) !== null ? json_decode($order->getData(CheckoutHelper::FIELD_DELIVERY_OPTIONS), true) : null; + + if (! is_array($data)) { + return ''; + } + + $deliveryOptions = DeliveryOptionsAdapterFactory::create((array) $data); + $returnString = ''; + + try { + if ($deliveryOptions->isPickup()) { + $returnString = htmlentities($this->getCheckoutOptionsPickupHtml($deliveryOptions)); } - } else { - if (is_array($data) && key_exists('date', $data)) { - if (key_exists('packageType', $data)) { - $html .= __($data['packageType'] . ' '); - } - - $html .= __('Deliver:') . ' ' . $dateTime; - - if (key_exists('shipmentOptions', $data)) { - if (key_exists('signature', $data['shipmentOptions']) && $data['shipmentOptions']['signature']) { - $html .= ', ' . __('Signature on receipt'); - } - if (key_exists('only_recipient', $data['shipmentOptions']) && $data['shipmentOptions']['only_recipient']) { - $html .= ', ' . __('Home address only'); - } - } + + if ($deliveryOptions->getDate()) { + $returnString = htmlentities($this->getCheckoutOptionsDeliveryHtml($deliveryOptions)); } + } catch (\Throwable $e) { + ObjectManager::getInstance()->get(CheckoutHelper::class)->log($e->getMessage()); + $returnString = __('MyParcel options data not found'); + } + + return $returnString; + } + + /** + * @param AbstractDeliveryOptionsAdapter $deliveryOptions + * @return string + */ + private function getCheckoutOptionsPickupHtml(AbstractDeliveryOptionsAdapter $deliveryOptions): string { + ob_start(); + + echo __("{$deliveryOptions->getCarrier()} location:"), ' '; + + if ('pickup' !== $deliveryOptions->getDeliveryType()) { + echo __($deliveryOptions->getDeliveryType()), ', '; } - if (is_array($data) && key_exists('browser', $data)) { - $html = ' ' . $html . ''; + $pickupLocation = $deliveryOptions->getPickupLocation(); + + if (null !== $pickupLocation) { + echo $pickupLocation->getLocationName(), ', '; + echo $pickupLocation->getCity(), ' (', $pickupLocation->getPostalCode(), ')'; + } + + return ob_get_clean(); + } + + /** + * @param AbstractDeliveryOptionsAdapter $deliveryOptions + * @return string + * @throws \Exception + */ + private function getCheckoutOptionsDeliveryHtml(AbstractDeliveryOptionsAdapter $deliveryOptions): string { + ob_start(); + + if ($deliveryOptions->getPackageType()) { + echo __($deliveryOptions->getPackageType()), ' '; + } + + $date = new DateTime($deliveryOptions->getDate() ?? ''); + + echo __('Deliver:'), ' ', $date->format('d-m-Y H:i'); + + $shipmentOptions = $deliveryOptions->getShipmentOptions(); + + if (null !== $shipmentOptions) { + if ($shipmentOptions->hasSignature()) { + echo ', ', __('Signature on receipt'); + } + if ($shipmentOptions->hasOnlyRecipient()) { + echo ', ', __('Home address only'); + } } - return $html !== false ? '
' . $html : ''; + return ob_get_clean(); } } diff --git a/Helper/Order.php b/Helper/Order.php deleted file mode 100755 index fd681958..00000000 --- a/Helper/Order.php +++ /dev/null @@ -1,38 +0,0 @@ - - * @copyright 2010-2016 MyParcel - * @license http://creativecommons.org/licenses/by-nc-nd/3.0/nl/deed.en_US CC BY-NC-ND 3.0 NL - * @link https://github.com/myparcelnl/magento - * @since File available since Release v0.1.0 - */ - -namespace MyParcelNL\Magento\Helper; - -use Magento\Framework\App\Helper\AbstractHelper; - -class Order extends AbstractHelper -{ - /** - * Checks if the given shipping method is a pickup location - * - * @param $myparcelDeliveryOptions - * - * @return bool - */ - public function isPickupLocation($myparcelDeliveryOptions) - { - if (is_array($myparcelDeliveryOptions) && key_exists('isPickup', $myparcelDeliveryOptions) && $myparcelDeliveryOptions['isPickup']) { - return true; - } - - return false; - } -} diff --git a/Model/Source/DefaultOptions.php b/Model/Source/DefaultOptions.php index 3f013386..a00af564 100755 --- a/Model/Source/DefaultOptions.php +++ b/Model/Source/DefaultOptions.php @@ -13,11 +13,12 @@ namespace MyParcelNL\Magento\Model\Source; +use BadMethodCallException; use Magento\Sales\Model\Order; use MyParcelNL\Magento\Helper\Checkout; use MyParcelNL\Magento\Helper\Data; -use MyParcelNL\Magento\Model\Sales\Package; use MyParcelNL\Magento\Model\Sales\Repository\PackageRepository; +use MyParcelNL\Sdk\src\Factory\DeliveryOptionsAdapterFactory; use MyParcelNL\Sdk\src\Model\Carrier\AbstractCarrier; use MyParcelNL\Sdk\src\Model\Carrier\CarrierFactory; use MyParcelNL\Sdk\src\Model\Carrier\CarrierPostNL; @@ -62,9 +63,13 @@ public function __construct(Order $order, Data $helper) { self::$helper = $helper; self::$order = $order; - $options = self::$order->getData(Checkout::FIELD_DELIVERY_OPTIONS) ?? ''; - - self::$chosenOptions = json_decode($options, true) ?? []; + try { + self::$chosenOptions = DeliveryOptionsAdapterFactory::create( + (array) json_decode($order->getData(Checkout::FIELD_DELIVERY_OPTIONS), true) + )->toArray(); + } catch (BadMethodCallException $e) { + self::$chosenOptions = []; + } } /** @@ -86,15 +91,18 @@ public function hasDefault(string $option, string $carrier): bool return true; } - $total = self::$order->getGrandTotal(); - $settings = self::$helper->getStandardConfig($carrier, 'default_options'); + $total = self::$order->getGrandTotal(); + $settings = self::$helper->getStandardConfig($carrier, 'default_options'); + $activeKey = "{$option}_active"; - if (! isset($settings[$option . '_active'])) { + if (! isset($settings[$activeKey])) { return false; } - return '1' === $settings[$option . '_active'] - && (! ($settings[$option . '_from_price'] ?? false) || $total > (int) $settings[$option . '_from_price']); + $priceKey = "{$option}_from_price"; + + return '1' === $settings[$activeKey] + && (! ($settings[$priceKey] ?? false) || $total > (int) $settings[$priceKey]); } /** @@ -124,17 +132,19 @@ public function hasDefaultLargeFormat(string $carrier, string $option): bool $price = self::$order->getGrandTotal(); $weight = self::$helper->convertToGrams(self::$order->getWeight()); - $settings = self::$helper->getStandardConfig($carrier, 'default_options'); - if (isset($settings[$option . '_active']) && - 'weight' === $settings[$option . '_active'] && + $settings = self::$helper->getStandardConfig($carrier, 'default_options'); + $activeKey = "{$option}_active"; + + if (isset($settings[$activeKey]) && + 'weight' === $settings[$activeKey] && $weight >= PackageRepository::DEFAULT_LARGE_FORMAT_WEIGHT ) { return true; } - if (isset($settings[$option . '_active']) && - 'price' === $settings[$option . '_active'] && - $price >= $settings[$option . '_from_price'] + if (isset($settings[$activeKey]) && + 'price' === $settings[$activeKey] && + $price >= $settings["{$option}_from_price"] ) { return true; } diff --git a/composer.json b/composer.json index 90e32f39..76b60d6e 100755 --- a/composer.json +++ b/composer.json @@ -30,7 +30,7 @@ "type": "magento2-module", "require": { "php": "~5.6.5 || ^7.0 || ^8.0", - "myparcelnl/sdk": "~v7.11.0", + "myparcelnl/sdk": "~v7.13.1", "magento/framework": ">=101.0.8 <102 || >=102.0.1" }, "require-dev": { diff --git a/i18n/nl_NL.csv b/i18n/nl_NL.csv index efe5f00b..2542ee22 100755 --- a/i18n/nl_NL.csv +++ b/i18n/nl_NL.csv @@ -28,8 +28,6 @@ status_99,Inactief - Onbekende status myparcelnl_magento_postnl_settings/delivery_title, postnl bezorging myparcelnl_magento_postnl_settings/pickup_title, postnl ophaallocatie myparcelnl_magento_postnl_settings/delivery/signature_title, postnl bezorging met handtekening -myparcelnl_magento_dpd_settings/delivery_title, DPD bezorging -myparcelnl_magento_dpd_settings/pickup_title, DPD ophaallocatie myparcelnl_magento_postnl_settings/pickup, PostNL ophaallocatie myparcelnl_magento_postnl_settings/delivery, PostNL bezorging myparcelnl_magento_postnl_settings/delivery/signature, PostNL bezorging met handtekening voor ontvangst @@ -53,7 +51,10 @@ myparcelnl_magento_dhlparcelconnect_settings/delivery, DHL Parcel Connect bezorg myparcelnl_magento_dhleuroplus_settings/delivery, DHL Europlus bezorging myparcelnl_magento_dhlparcelconnect_settings/delivery, DHL Parcel Connect bezorging myparcelnl_magento_ups_settings/delivery, UPS bezorging +myparcelnl_magento_dpd_settings/delivery_title, DPD bezorging +myparcelnl_magento_dpd_settings/pickup_title, DPD ophaallocatie myparcelnl_magento_dpd_settings/delivery, DPD bezorging +myparcelnl_magento_dpd_settings/pickup, DPD ophaallocatie myparcelnl_magento_error_no_shipments_to_process, Geen MyParcel zendingen om te verwerken. package_small, Klein pakket show_total_price, Toon totaalprijs