Skip to content

Commit

Permalink
fix(export): restore automate shipment options settings
Browse files Browse the repository at this point in the history
  • Loading branch information
joerivanveen committed Oct 23, 2024
1 parent 88cb140 commit 22a1452
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 61 deletions.
15 changes: 1 addition & 14 deletions Block/Sales/NewShipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

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

/**
* @var \Magento\Framework\ObjectManagerInterface
Expand Down Expand Up @@ -76,21 +76,25 @@ 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);
}

/**
* @return int
*/
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 @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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];
Expand Down
20 changes: 10 additions & 10 deletions Model/Sales/TrackTraceHolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 @@ -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]);

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

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

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

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

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

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

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

Expand Down

0 comments on commit 22a1452

Please sign in to comment.