From 7c501e7832c5e3c3fd80a26fc3e56d9b11213511 Mon Sep 17 00:00:00 2001 From: Joeri van Veen Date: Fri, 26 Apr 2024 14:37:59 +0200 Subject: [PATCH 1/6] fix(export): allow signature or only_recipient for be INT-455 --- Block/Sales/NewShipment.php | 11 ------ Helper/ShipmentOptions.php | 21 ++++++---- Model/Sales/TrackTraceHolder.php | 6 +-- Model/Source/DefaultOptions.php | 5 +++ view/adminhtml/templates/new_shipment.phtml | 44 +++++++++++++-------- 5 files changed, 48 insertions(+), 39 deletions(-) diff --git a/Block/Sales/NewShipment.php b/Block/Sales/NewShipment.php index 1dd48e72..9fcad763 100755 --- a/Block/Sales/NewShipment.php +++ b/Block/Sales/NewShipment.php @@ -106,17 +106,6 @@ public function hasDefaultOption(string $option, string $carrier): bool return $this->defaultOptions->hasDefault($option, $carrier); } - /** - * @param string $option 'large_format' - * @param string $carrier - * - * @return bool - */ - public function hasDefaultLargeFormat(string $option, string $carrier): bool - { - return $this->defaultOptions->hasDefaultLargeFormat($carrier, $option); - } - /** * Get default value of age check * diff --git a/Helper/ShipmentOptions.php b/Helper/ShipmentOptions.php index 54fdb1b6..27ebf134 100644 --- a/Helper/ShipmentOptions.php +++ b/Helper/ShipmentOptions.php @@ -55,6 +55,11 @@ class ShipmentOptions */ private $order; + /** + * @var string|null + */ + private $cc; + /** * @param \MyParcelNL\Magento\Model\Source\DefaultOptions $defaultOptions * @param \MyParcelNL\Magento\Helper\Data $helper @@ -77,6 +82,7 @@ public function __construct( $this->objectManager = $objectManager; $this->carrier = $carrier; $this->options = $options; + $this->cc = $order->getShippingAddress() ? $order->getShippingAddress()->getCountryId() : null; } /** @@ -92,6 +98,10 @@ public function getInsurance(): int */ public function hasSignature(): bool { + if (AbstractConsignment::CC_BE === $this->cc && $this->hasOnlyRecipient()) { + return false; + } + $signatureFromOptions = self::getValueOfOptionWhenSet(self::SIGNATURE, $this->options); return $signatureFromOptions ?? $this->optionIsEnabled(self::SIGNATURE); @@ -132,10 +142,7 @@ public function hasReturn(): bool */ public function hasAgeCheck(): bool { - $countryId = $this->order->getShippingAddress() - ->getCountryId(); - - if (AbstractConsignment::CC_NL !== $countryId) { + if (AbstractConsignment::CC_NL !== $this->cc) { return false; } @@ -265,14 +272,12 @@ public static function getValueOfOptionWhenSet(string $key, array $options): ?bo */ public function hasLargeFormat(): bool { - $countryId = $this->order->getShippingAddress()->getCountryId(); - - if (! in_array($countryId, AbstractConsignment::EURO_COUNTRIES)) { + if (! in_array($this->cc, AbstractConsignment::EURO_COUNTRIES)) { return false; } $largeFormatFromOptions = self::getValueOfOptionWhenSet(self::LARGE_FORMAT, $this->options); - $largeFormatFromSettings = self::$defaultOptions->hasDefaultLargeFormat($this->carrier, self::LARGE_FORMAT); + $largeFormatFromSettings = self::$defaultOptions->hasDefault(self::LARGE_FORMAT, $this->carrier); return $largeFormatFromOptions ?? $largeFormatFromSettings; } diff --git a/Model/Sales/TrackTraceHolder.php b/Model/Sales/TrackTraceHolder.php index b2dd45ca..3e2d7498 100755 --- a/Model/Sales/TrackTraceHolder.php +++ b/Model/Sales/TrackTraceHolder.php @@ -175,7 +175,7 @@ public function convertDataFromMagentoToApi(Track $magentoTrack, array $options) $this->consignment = (ConsignmentFactory::createByCarrierName($deliveryOptionsAdapter->getCarrier())) ->setApiKey($apiKey) - ->setReferenceId($shipment->getEntityId()) + ->setReferenceIdentifier($shipment->getEntityId()) ->setConsignmentId($magentoTrack->getData('myparcel_consignment_id')) ->setCountry($address->getCountryId()) ->setCompany(self::$defaultOptions->getMaxCompanyName($address->getCompany())) @@ -227,13 +227,13 @@ public function convertDataFromMagentoToApi(Track $magentoTrack, array $options) ->setAgeCheck($this->shipmentOptionsHelper->hasAgeCheck()) ->setInsurance($this->shipmentOptionsHelper->getInsurance()) ->setInvoice( - $magentoTrack->getShipment() + $shipment ->getOrder() ->getIncrementId() ) ->setSaveRecipientAddress(false); - if ($deliveryOptionsAdapter->isPickup()) { + if ($deliveryOptionsAdapter->isPickup() && $pickupLocationAdapter) { $this->consignment ->setPickupPostalCode($pickupLocationAdapter->getPostalCode()) ->setPickupStreet($pickupLocationAdapter->getStreet()) diff --git a/Model/Source/DefaultOptions.php b/Model/Source/DefaultOptions.php index a00af564..d74c9484 100755 --- a/Model/Source/DefaultOptions.php +++ b/Model/Source/DefaultOptions.php @@ -17,6 +17,7 @@ use Magento\Sales\Model\Order; use MyParcelNL\Magento\Helper\Checkout; use MyParcelNL\Magento\Helper\Data; +use MyParcelNL\Magento\Helper\ShipmentOptions; use MyParcelNL\Magento\Model\Sales\Repository\PackageRepository; use MyParcelNL\Sdk\src\Factory\DeliveryOptionsAdapterFactory; use MyParcelNL\Sdk\src\Model\Carrier\AbstractCarrier; @@ -82,6 +83,10 @@ public function __construct(Order $order, Data $helper) */ public function hasDefault(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) && diff --git a/view/adminhtml/templates/new_shipment.phtml b/view/adminhtml/templates/new_shipment.phtml index 5d00ae4a..a2d5bc7b 100755 --- a/view/adminhtml/templates/new_shipment.phtml +++ b/view/adminhtml/templates/new_shipment.phtml @@ -1,6 +1,7 @@ @@ -18,7 +19,8 @@ use MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment; getNewShipmentForm()->getCarrierSpecificAbstractConsignments() as $abstractConsignment) { +$form = $block->getNewShipmentForm(); +foreach ($form->getCarrierSpecificAbstractConsignments() as $abstractConsignment) { $abstractConsignment->setCountry($block->getCountry()); $carrier = $abstractConsignment->getCarrier(); $carrierName = $carrier->getName(); @@ -57,9 +59,7 @@ foreach ($block->getNewShipmentForm()->getCarrierSpecificAbstractConsignments() continue; } } - $packageTypeHuman = $block->getNewShipmentForm()::PACKAGE_TYPE_HUMAN_MAP[$packageTypeId] - ?? - $packageTypeName; + $packageTypeHuman = $form::PACKAGE_TYPE_HUMAN_MAP[$packageTypeId] ?? $packageTypeName; ?>
getNewShipmentForm()->getCarrierSpecificAbstractConsignments() title=""> ' . __($label_amount) . ''; + echo ''; $label_amount++; } ?>
getCountry(); - try { $abstractConsignment->setPackageType($packageTypeId); } catch (\Exception $e) { continue; } foreach ($abstractConsignment->getAllowedShipmentOptions() as $shipmentOption) { - if ( - $isInternational - || AbstractConsignment::SHIPMENT_OPTION_INSURANCE === $shipmentOption + if (AbstractConsignment::SHIPMENT_OPTION_INSURANCE === $shipmentOption || ! $abstractConsignment->canHaveShipmentOption($shipmentOption) ) { continue; } - $isActive = ($abstractConsignment::SHIPMENT_OPTION_LARGE_FORMAT === $shipmentOption) - ? $block->hasDefaultLargeFormat($shipmentOption, $carrier->getName()) - : $block->hasDefaultOption($shipmentOption, $carrier->getName()); + /** + * business logic determining what shipment options to show, if any + */ + if (AbstractConsignment::CC_BE === $block->getCountry()) { + if (CarrierPostNL::NAME === $carrierName) { + // allow only_recipient and signature, but only one active at a time + if (!in_array($shipmentOption, [ + AbstractConsignment::SHIPMENT_OPTION_ONLY_RECIPIENT, + AbstractConsignment::SHIPMENT_OPTION_SIGNATURE], true) + ) { + continue; + } + } else { + continue; + } + } elseif (AbstractConsignment::CC_NL !== $block->getCountry()) { + continue; + } ?>
getNewShipmentForm()->getCarrierSpecificAbstractConsignments() name="mypa_" value="1" type="checkbox" - hasDefaultOption($shipmentOption, $carrierName) ? 'checked="checked"' : '' ?> />
@@ -201,7 +211,7 @@ foreach ($block->getNewShipmentForm()->getCarrierSpecificAbstractConsignments() - getDefaultInsurance($carrier->getName()); ?> + getDefaultInsurance($carrierName); ?>