diff --git a/Block/Sales/NewShipment.php b/Block/Sales/NewShipment.php index ba1f7cda..ec527373 100755 --- a/Block/Sales/NewShipment.php +++ b/Block/Sales/NewShipment.php @@ -104,20 +104,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 27ebf134..549cdedf 100644 --- a/Helper/ShipmentOptions.php +++ b/Helper/ShipmentOptions.php @@ -33,7 +33,7 @@ class ShipmentOptions /** * @var \MyParcelNL\Magento\Model\Source\DefaultOptions */ - private static $defaultOptions; + private $defaultOptions; /** * @var \Magento\Framework\ObjectManagerInterface @@ -76,13 +76,17 @@ public function __construct( string $carrier, array $options = [] ) { - self::$defaultOptions = $defaultOptions; + $this->defaultOptions = $defaultOptions; $this->helper = $helper; $this->order = $order; $this->objectManager = $objectManager; $this->carrier = $carrier; $this->options = $options; $this->cc = $order->getShippingAddress() ? $order->getShippingAddress()->getCountryId() : null; + + file_put_contents('/Applications/MAMP/htdocs/magento246/var/log/joeri.log', + 'SIGNATURE: ' . $defaultOptions->hasOptionSet(self::SIGNATURE, $carrier) . ', ' . var_export($this->optionIsEnabled(self::SIGNATURE), true) . PHP_EOL + , FILE_APPEND); } /** @@ -90,7 +94,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); } /** @@ -148,7 +152,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; } @@ -277,7 +281,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; } @@ -364,7 +368,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 68806ee7..7f8f10a1 100755 --- a/Model/Sales/TrackTraceHolder.php +++ b/Model/Sales/TrackTraceHolder.php @@ -54,7 +54,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 { @@ -341,7 +341,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]); @@ -449,7 +449,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,7 +491,7 @@ private function getCarrierFromOptions(array $options): ?string if (array_key_exists('carrier', $options) && $options['carrier']) { $carrier = - DefaultOptions::DEFAULT_OPTION_VALUE === $options['carrier'] ? self::$defaultOptions->getCarrier() + DefaultOptions::DEFAULT_OPTION_VALUE === $options['carrier'] ? $this->defaultOptions->getCarrier() : $options['carrier']; } @@ -516,11 +516,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 71349428..3eb498c0 100755 --- a/Model/Source/DefaultOptions.php +++ b/Model/Source/DefaultOptions.php @@ -56,17 +56,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. @@ -76,14 +76,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 = []; } } @@ -95,22 +95,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); } /** @@ -137,10 +137,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]) && @@ -166,11 +166,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; + } - return '1' === ($settings[$option . '_active'] ?? null); + $fromPrice = $settings[$option . '_from_price'] ?? 0; + $orderAmount = $this->order->getGrandTotal() ?? 0.0; + + return $fromPrice <= $orderAmount; } /** @@ -183,7 +189,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) { @@ -206,13 +212,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; } @@ -241,7 +247,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/'); } /** @@ -251,11 +257,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]; } @@ -269,11 +275,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']; } }