From 9b03b617d2aa19bd216ce9a6436469e8039e555d Mon Sep 17 00:00:00 2001 From: Joeri van Veen Date: Thu, 24 Oct 2024 14:48:09 +0200 Subject: [PATCH] fix(export): restore automate shipment options settings (#869) --- Block/Sales/NewShipment.php | 15 +------ Helper/ShipmentOptions.php | 15 ++++--- Model/Sales/TrackTraceHolder.php | 24 +++++------ Model/Source/DefaultOptions.php | 68 +++++++++++++++++--------------- composer.json | 2 +- 5 files changed, 58 insertions(+), 66 deletions(-) diff --git a/Block/Sales/NewShipment.php b/Block/Sales/NewShipment.php index 216159b1..aed09aaa 100755 --- a/Block/Sales/NewShipment.php +++ b/Block/Sales/NewShipment.php @@ -106,20 +106,7 @@ public function __construct( */ public function hasDefaultOption(string $option, string $carrier): bool { - return $this->defaultOptions->hasDefault($option, $carrier); - } - - /** - * Get default value of age check - * - * @param string $carrier - * @param string $option - * - * @return bool - */ - public function hasDefaultOptionsWithoutPrice(string $carrier, string $option): bool - { - return $this->defaultOptions->hasDefaultOptionsWithoutPrice($carrier, $option); + return $this->defaultOptions->hasOptionSet($option, $carrier); } /** diff --git a/Helper/ShipmentOptions.php b/Helper/ShipmentOptions.php index f41a8f0a..b1bb82e0 100644 --- a/Helper/ShipmentOptions.php +++ b/Helper/ShipmentOptions.php @@ -36,7 +36,7 @@ class ShipmentOptions /** * @var \MyParcelNL\Magento\Model\Source\DefaultOptions */ - private static $defaultOptions; + private $defaultOptions; /** * @var \Magento\Framework\ObjectManagerInterface @@ -78,9 +78,8 @@ public function __construct( ObjectManagerInterface $objectManager, string $carrier, array $options = [] - ) - { - self::$defaultOptions = $defaultOptions; + ) { + $this->defaultOptions = $defaultOptions; $this->helper = $helper; $this->order = $order; $this->objectManager = $objectManager; @@ -94,7 +93,7 @@ public function __construct( */ public function getInsurance(): int { - return $this->options['insurance'] ?? self::$defaultOptions->getDefaultInsurance($this->carrier); + return $this->options['insurance'] ?? $this->defaultOptions->getDefaultInsurance($this->carrier); } /** @@ -167,7 +166,7 @@ public function hasAgeCheck(): bool $ageCheckFromOptions = self::getValueOfOptionWhenSet(self::AGE_CHECK, $this->options); $ageCheckOfProduct = self::getAgeCheckFromProduct($this->order->getItems()); - $ageCheckFromSettings = self::$defaultOptions->hasDefaultOptionsWithoutPrice($this->carrier, self::AGE_CHECK); + $ageCheckFromSettings = $this->defaultOptions->hasDefaultOption($this->carrier, self::AGE_CHECK); return $ageCheckFromOptions ?? $ageCheckOfProduct ?? $ageCheckFromSettings; } @@ -297,7 +296,7 @@ public function hasLargeFormat(): bool } $largeFormatFromOptions = self::getValueOfOptionWhenSet(self::LARGE_FORMAT, $this->options); - $largeFormatFromSettings = self::$defaultOptions->hasDefault(self::LARGE_FORMAT, $this->carrier); + $largeFormatFromSettings = $this->defaultOptions->hasOptionSet(self::LARGE_FORMAT, $this->carrier); return $largeFormatFromOptions ?? $largeFormatFromSettings; } @@ -384,7 +383,7 @@ public function getItemsCollectionByShipmentId($shipmentId): array private function optionIsEnabled($optionKey): bool { if (! isset($this->options[$optionKey])) { - return self::$defaultOptions->hasDefault($optionKey, $this->carrier); + return $this->defaultOptions->hasOptionSet($optionKey, $this->carrier); } return (bool) $this->options[$optionKey]; diff --git a/Model/Sales/TrackTraceHolder.php b/Model/Sales/TrackTraceHolder.php index 24dc5184..2ae29e4d 100755 --- a/Model/Sales/TrackTraceHolder.php +++ b/Model/Sales/TrackTraceHolder.php @@ -53,7 +53,7 @@ class TrackTraceHolder /** * @var \MyParcelNL\Magento\Model\Source\DefaultOptions */ - private static $defaultOptions; + private $defaultOptions; /** * @var \MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment|null @@ -106,7 +106,7 @@ public function __construct( $this->objectManager = $objectManager; $this->dataHelper = $helper; $this->messageManager = $this->objectManager->create('Magento\Framework\Message\ManagerInterface'); - self::$defaultOptions = new DefaultOptions( + $this->defaultOptions = new DefaultOptions( $order, $this->dataHelper ); @@ -145,7 +145,7 @@ public function convertDataFromMagentoToApi(Track $magentoTrack, array $options) ->getName(); $totalWeight = $options['digital_stamp_weight'] !== null ? (int) $options['digital_stamp_weight'] - : (int) self::$defaultOptions->getDigitalStampDefaultWeight(); + : (int) $this->defaultOptions->getDigitalStampDefaultWeight(); try { // create new instance from known json @@ -165,7 +165,7 @@ public function convertDataFromMagentoToApi(Track $magentoTrack, array $options) $this->validateApiKey($apiKey); $this->carrier = $deliveryOptionsAdapter->getCarrier(); $this->shipmentOptionsHelper = new ShipmentOptions( - self::$defaultOptions, + $this->defaultOptions, $this->dataHelper, $order, $this->objectManager, @@ -178,7 +178,7 @@ public function convertDataFromMagentoToApi(Track $magentoTrack, array $options) ->setReferenceIdentifier($shipment->getEntityId()) ->setConsignmentId($magentoTrack->getData('myparcel_consignment_id')) ->setCountry($address->getCountryId()) - ->setCompany(self::$defaultOptions->getMaxCompanyName($address->getCompany())) + ->setCompany($this->defaultOptions->getMaxCompanyName($address->getCompany())) ->setPerson($address->getName()); try { @@ -342,7 +342,7 @@ private function calculateTotalWeight(Track $magentoTrack, int $totalWeight = 0) return $this; } - $weightFromSettings = (int) self::$defaultOptions->getDigitalStampDefaultWeight(); + $weightFromSettings = (int) $this->defaultOptions->getDigitalStampDefaultWeight(); if ($weightFromSettings) { $this->consignment->setPhysicalProperties(["weight" => $weightFromSettings]); @@ -450,7 +450,7 @@ private function getAgeCheck(Track $magentoTrack, $address, array $options = []) $ageCheckFromOptions = ShipmentOptions::getValueOfOptionWhenSet('age_check', $options); $ageCheckOfProduct = ShipmentOptions::getAgeCheckFromProduct($magentoTrack); - $ageCheckFromSettings = self::$defaultOptions->hasDefaultOptionsWithoutPrice($this->carrier, 'age_check'); + $ageCheckFromSettings = $this->defaultOptions->hasDefaultOption($this->carrier, 'age_check'); return $ageCheckFromOptions ?? $ageCheckOfProduct ?? $ageCheckFromSettings; } @@ -491,9 +491,9 @@ private function getCarrierFromOptions(array $options): ?string $carrier = null; if (array_key_exists('carrier', $options) && $options['carrier']) { - $carrier - = DefaultOptions::DEFAULT_OPTION_VALUE === $options['carrier'] ? self::$defaultOptions->getCarrier() - : $options['carrier']; + $carrier = + DefaultOptions::DEFAULT_OPTION_VALUE === $options['carrier'] ? $this->defaultOptions->getCarrier() + : $options['carrier']; } return $carrier; @@ -517,11 +517,11 @@ private function getPackageType(Track $magentoTrack, $address, array $options, a // get package type from selected radio buttons, try to get from delivery options when default or not set $packageType = $options['package_type'] ?? 'default'; if ('default' === $packageType) { - $packageType = $deliveryOptions['packageType'] ?? self::$defaultOptions->getPackageType(); + $packageType = $deliveryOptions['packageType'] ?? $this->defaultOptions->getPackageType(); } if (! is_numeric($packageType)) { - $packageType = AbstractConsignment::PACKAGE_TYPES_NAMES_IDS_MAP[$packageType] ?? self::$defaultOptions->getPackageType(); + $packageType = AbstractConsignment::PACKAGE_TYPES_NAMES_IDS_MAP[$packageType] ?? $this->defaultOptions->getPackageType(); } return $packageType; diff --git a/Model/Source/DefaultOptions.php b/Model/Source/DefaultOptions.php index 655ade58..7f62bf6d 100755 --- a/Model/Source/DefaultOptions.php +++ b/Model/Source/DefaultOptions.php @@ -55,17 +55,17 @@ class DefaultOptions /** * @var Data */ - private static $helper; + private $helper; /** * @var Order */ - private static $order; + private $order; /** * @var array */ - private static $chosenOptions; + private $chosenOptions; /** * Insurance constructor. @@ -75,14 +75,14 @@ class DefaultOptions */ public function __construct(Order $order, Data $helper) { - self::$helper = $helper; - self::$order = $order; + $this->helper = $helper; + $this->order = $order; try { - self::$chosenOptions = DeliveryOptionsAdapterFactory::create( + $this->chosenOptions = DeliveryOptionsAdapterFactory::create( (array) json_decode($order->getData(Checkout::FIELD_DELIVERY_OPTIONS), true) )->toArray(); } catch (Exception $e) { - self::$chosenOptions = []; + $this->chosenOptions = []; } } @@ -94,22 +94,22 @@ public function __construct(Order $order, Data $helper) * * @return bool */ - public function hasDefault(string $option, string $carrier): bool + public function hasOptionSet(string $option, string $carrier): bool { if (AbstractConsignment::SHIPMENT_OPTION_LARGE_FORMAT === $option) { return $this->hasDefaultLargeFormat($carrier, $option); } // Check that the customer has already chosen this option in the checkout - if (is_array(self::$chosenOptions) && - array_key_exists('shipmentOptions', self::$chosenOptions) && - array_key_exists($option, self::$chosenOptions['shipmentOptions']) && - self::$chosenOptions['shipmentOptions'][$option] + if (is_array($this->chosenOptions) && + array_key_exists('shipmentOptions', $this->chosenOptions) && + array_key_exists($option, $this->chosenOptions['shipmentOptions']) && + $this->chosenOptions['shipmentOptions'][$option] ) { return true; } - return false; + return $this->hasDefaultOption($carrier, $option); } /** @@ -136,10 +136,10 @@ public function getMaxCompanyName(?string $company): ?string */ public function hasDefaultLargeFormat(string $carrier, string $option): bool { - $price = self::$order->getGrandTotal(); - $weight = self::$helper->convertToGrams(self::$order->getWeight()); + $price = $this->order->getGrandTotal(); + $weight = $this->helper->convertToGrams($this->order->getWeight()); - $settings = self::$helper->getStandardConfig($carrier, 'default_options'); + $settings = $this->helper->getStandardConfig($carrier, 'default_options'); $activeKey = "{$option}_active"; if (isset($settings[$activeKey]) && @@ -165,11 +165,17 @@ public function hasDefaultLargeFormat(string $carrier, string $option): bool * * @return bool */ - public function hasDefaultOptionsWithoutPrice(string $carrier, string $option): bool + public function hasDefaultOption(string $carrier, string $option): bool { - $settings = self::$helper->getStandardConfig($carrier, 'default_options'); + $settings = $this->helper->getStandardConfig($carrier, 'default_options'); + if ('1' !== ($settings[$option . '_active'] ?? null)) { + return false; + } + + $fromPrice = $settings[$option . '_from_price'] ?? 0; + $orderAmount = $this->order->getGrandTotal() ?? 0.0; - return '1' === ($settings[$option . '_active'] ?? null); + return $fromPrice <= $orderAmount; } /** @@ -182,7 +188,7 @@ public function hasDefaultOptionsWithoutPrice(string $carrier, string $option): */ public function getDefaultInsurance(string $carrier): int { - $shippingAddress = self::$order->getShippingAddress(); + $shippingAddress = $this->order->getShippingAddress(); $shippingCountry = $shippingAddress ? $shippingAddress->getCountryId() : AbstractConsignment::CC_NL; if (AbstractConsignment::CC_NL === $shippingCountry) { @@ -205,13 +211,13 @@ public function getDefaultInsurance(string $carrier): int */ private function getInsurance(string $carrierName, string $priceKey, string $shippingCountry): int { - $total = self::$order->getGrandTotal(); - $settings = self::$helper->getStandardConfig($carrierName, 'default_options'); + $total = $this->order->getGrandTotal(); + $settings = $this->helper->getStandardConfig($carrierName, 'default_options'); $totalAfterPercentage = $total * (($settings[self::INSURANCE_PERCENTAGE] ?? 0) / 100); if (! isset($settings[$priceKey]) - || $settings[$priceKey] === 0 - || $totalAfterPercentage < $settings[self::INSURANCE_FROM_PRICE]) { + || (int) $settings[$priceKey] === 0 + || $totalAfterPercentage < (int) $settings[self::INSURANCE_FROM_PRICE]) { return 0; } @@ -240,7 +246,7 @@ private function getInsurance(string $carrierName, string $priceKey, string $shi */ public function getDigitalStampDefaultWeight(): string { - return self::$helper->getCarrierConfig('digital_stamp/default_weight', 'myparcelnl_magento_postnl_settings/'); + return $this->helper->getCarrierConfig('digital_stamp/default_weight', 'myparcelnl_magento_postnl_settings/'); } /** @@ -250,11 +256,11 @@ public function getDigitalStampDefaultWeight(): string */ public function getPackageType(): int { - if (self::$chosenOptions) { - $keyIsPresent = array_key_exists('packageType', self::$chosenOptions); + if ($this->chosenOptions) { + $keyIsPresent = array_key_exists('packageType', $this->chosenOptions); if ($keyIsPresent) { - $packageType = self::$chosenOptions['packageType']; + $packageType = $this->chosenOptions['packageType']; return AbstractConsignment::PACKAGE_TYPES_NAMES_IDS_MAP[$packageType]; } @@ -268,11 +274,11 @@ public function getPackageType(): int */ public function getCarrier(): string { - if (self::$chosenOptions) { - $keyIsPresent = array_key_exists('carrier', self::$chosenOptions); + if ($this->chosenOptions) { + $keyIsPresent = array_key_exists('carrier', $this->chosenOptions); if ($keyIsPresent) { - return self::$chosenOptions['carrier']; + return $this->chosenOptions['carrier']; } } diff --git a/composer.json b/composer.json index c748ab8a..4ecd0382 100755 --- a/composer.json +++ b/composer.json @@ -30,7 +30,7 @@ "type": "magento2-module", "require": { "php": "^7.1 || ^8.0", - "myparcelnl/sdk": "~v7.15.3", + "myparcelnl/sdk": "~v7.18", "magento/framework": ">=101.0.8 <102 || >=102.0.1" }, "require-dev": {