Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(export): restore automate shipment options settings #869

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 1 addition & 14 deletions Block/Sales/NewShipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
15 changes: 7 additions & 8 deletions Helper/ShipmentOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ShipmentOptions
/**
* @var \MyParcelNL\Magento\Model\Source\DefaultOptions
*/
private static $defaultOptions;
private $defaultOptions;

/**
* @var \Magento\Framework\ObjectManagerInterface
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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];
Expand Down
24 changes: 12 additions & 12 deletions Model/Sales/TrackTraceHolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
);
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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]);

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
68 changes: 37 additions & 31 deletions Model/Source/DefaultOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 = [];
}
}

Expand All @@ -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);
}

/**
Expand All @@ -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]) &&
Expand All @@ -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;
}

/**
Expand All @@ -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) {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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/');
}

/**
Expand All @@ -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];
}
Expand All @@ -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'];
}
}

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down