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

feat: add myparcel delivery method #864

Draft
wants to merge 29 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
4b106f0
feat: add myparcel delivery method
joerivanveen Sep 3, 2024
febf6c2
wip
joerivanveen Sep 26, 2024
d3222c4
wip
joerivanveen Sep 26, 2024
e64f0b0
wip
joerivanveen Sep 26, 2024
1352c8c
wip
joerivanveen Sep 27, 2024
17d298d
wip
joerivanveen Sep 27, 2024
8d216be
wip
joerivanveen Sep 27, 2024
ec2cc8d
fix: prevent delivery date for small package outside nl
joerivanveen Oct 8, 2024
4cc6ad2
wip
joerivanveen Oct 10, 2024
3dbeef7
wip
joerivanveen Oct 10, 2024
8a90232
wip
joerivanveen Oct 14, 2024
e8c83fe
fix: restore default options
joerivanveen Oct 31, 2024
a5c0302
feat: add receipt code
joerivanveen Oct 31, 2024
f6ee041
feat(carriers): add ups shipping options
joerivanveen Nov 12, 2024
c4c942f
fix: improve service name and fix receipt code option
joerivanveen Nov 25, 2024
248f7d4
fix: prevent random error messages from halting shipment creation
joerivanveen Dec 19, 2024
6be9c45
feat: show error message when received from api
joerivanveen Dec 23, 2024
6f2db4a
fix: fix order of options for postnl, remove non existent option from…
joerivanveen Dec 23, 2024
37dd95f
feat: remove weight from large format option
joerivanveen Dec 23, 2024
4a462ec
fix: get quote from request to prevent infinite loop
joerivanveen Dec 23, 2024
db2a09a
wip
joerivanveen Dec 23, 2024
e6c9ebc
refactor: put quote helper methods in a trait for more logical re-use
joerivanveen Dec 23, 2024
e62b3e1
fix: remove some useless logging
joerivanveen Dec 23, 2024
a1a162e
wip
joerivanveen Dec 24, 2024
efcaf96
fix(checkout): save and retrieve chosen shipping method
joerivanveen Dec 31, 2024
de6b59b
wip
joerivanveen Dec 31, 2024
cf15a44
fix: remember free shipping available during checkout
joerivanveen Jan 2, 2025
d4dc313
refactor: improve inline documentation and code style
joerivanveen Jan 2, 2025
00e1af5
refactor: simplify javascript wip
joerivanveen Jan 2, 2025
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: 9 additions & 6 deletions Adapter/ShipmentOptionsFromAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MyParcelNL\Magento\Adapter;

use MyParcelNL\Magento\Helper\ShipmentOptions;
use MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter;
use MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractShipmentOptionsAdapter;

Expand All @@ -17,11 +18,13 @@ class ShipmentOptionsFromAdapter extends AbstractShipmentOptionsAdapter
public function __construct(array $inputData)
{
$options = $inputData ?? [];
$this->signature = (bool) ($options['signature'] ?? false);
$this->only_recipient = (bool) ($options['only_recipient'] ?? false);
$this->large_format = (bool) ($options['large_format'] ?? false);
$this->age_check = (bool) ($options['age_check'] ?? false);
$this->return = (bool) ($options['return'] ?? false);
$this->insurance = (int) ($options['insurance'] ?? self::DEFAULT_INSURANCE);
$this->signature = (bool) ($options[ShipmentOptions::SIGNATURE] ?? false);
$this->collect = (bool) ($options[ShipmentOptions::COLLECT] ?? false);
$this->receipt_code = (bool) ($options[ShipmentOptions::RECEIPT_CODE] ?? false);
$this->only_recipient = (bool) ($options[ShipmentOptions::ONLY_RECIPIENT] ?? false);
$this->large_format = (bool) ($options[ShipmentOptions::LARGE_FORMAT] ?? false);
$this->age_check = (bool) ($options[ShipmentOptions::AGE_CHECK] ?? false);
$this->return = (bool) ($options[ShipmentOptions::RETURN] ?? false);
$this->insurance = (int) ($options[ShipmentOptions::INSURANCE] ?? self::DEFAULT_INSURANCE);
}
}
131 changes: 70 additions & 61 deletions Block/Sales/NewShipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,116 +14,85 @@

namespace MyParcelNL\Magento\Block\Sales;

use Exception;
use Magento\Backend\Block\Template\Context;
use Magento\CatalogInventory\Api\StockConfigurationInterface;
use Magento\CatalogInventory\Api\StockRegistryInterface;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Registry;
use Magento\Sales\Block\Adminhtml\Items\AbstractItems;
use MyParcelNL\Magento\Helper\Checkout;
use MyParcelNL\Magento\Model\Sales\MagentoOrderCollection;
use MyParcelNL\Magento\Model\Source\DefaultOptions;
use MyParcelNL\Magento\Helper\Data;
use MyParcelNL\Magento\Model\Sales\TrackTraceHolder;
use MyParcelNL\Magento\Service\Config;
use MyParcelNL\Magento\Service\Weight;
use MyParcelNL\Sdk\src\Model\Carrier\CarrierPostNL;
use MyParcelNL\Sdk\src\Model\Carrier\CarrierUPS;
use MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment;

class NewShipment extends AbstractItems
{
/**
* @var \MyParcelNL\Magento\Helper\Data
*/
private $dataHelper;

/**
* @var \Magento\Sales\Model\Order
*/
private $order;

/**
* @var \Magento\Framework\ObjectManagerInterface
*/
private $objectManager;

/**
* @var \MyParcelNL\Magento\Model\Source\DefaultOptions
*/
private $defaultOptions;
private DefaultOptions $defaultOptions;

/**
* @var \MyParcelNL\Magento\Block\Sales\NewShipmentForm
*/
private $form;
private NewShipmentForm $form;

/**
* @var \MyParcelNL\Magento\Model\Sales\MagentoOrderCollection
*/
private $orderCollection;

/**
* @var mixed
*/
private $request;
private MagentoOrderCollection $orderCollection;

/**
* @param \Magento\Backend\Block\Template\Context $context
* @param \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
* @param \Magento\CatalogInventory\Api\StockConfigurationInterface $stockConfiguration
* @param \Magento\Framework\Registry $registry
* @param \Magento\Framework\ObjectManagerInterface $objectManager
* @param Context $context
* @param StockRegistryInterface $stockRegistry
* @param StockConfigurationInterface $stockConfiguration
* @param Registry $registry
*/
public function __construct(
Context $context,
Data $helper,
StockRegistryInterface $stockRegistry,
Context $context,
StockRegistryInterface $stockRegistry,
StockConfigurationInterface $stockConfiguration,
Registry $registry,
ObjectManagerInterface $objectManager
) {
// Set order
Registry $registry,
ObjectManagerInterface $objectManager
)
{
$this->order = $registry->registry('current_shipment')->getOrder();
$this->dataHelper = $helper;
$this->objectManager = $objectManager;
$this->weightService = $objectManager->get(Weight::class);
$this->configService = $objectManager->get(Config::class);
$this->form = new NewShipmentForm();

$this->defaultOptions = new DefaultOptions(
$this->order,
$this->objectManager->get(Data::class)
);
$this->defaultOptions = new DefaultOptions($this->order);

$this->request = $this->objectManager->get('Magento\Framework\App\RequestInterface');
$this->orderCollection = new MagentoOrderCollection($this->objectManager, $this->request);
$request = $objectManager->get('Magento\Framework\App\RequestInterface');
$this->orderCollection = new MagentoOrderCollection($objectManager, $request);

parent::__construct($context, $stockRegistry, $stockConfiguration, $registry);
}

/**
* @param string $option 'signature', 'only_recipient'
* @param string $carrier
* @param string $option 'signature', 'only_recipient'
* @param string $carrier
*
* @return bool
*/
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);
}

/**
* Get default value of insurance based on order grand total
*
* @param string $carrier
* @param string $carrier
*
* @return int
*/
Expand All @@ -138,7 +107,7 @@ public function getDefaultInsurance(string $carrier): int
*/
public function getDigitalStampWeight(): int
{
$weight = $this->dataHelper->convertToGrams($this->order->getWeight() ?? 0.0);
$weight = $this->weightService->convertToGrams($this->order->getWeight() ?? 0.0);

if (0 === $weight) {
$weight = $this->defaultOptions->getDigitalStampDefaultWeight();
Expand Down Expand Up @@ -171,9 +140,49 @@ public function getCountry()
return $this->order->getShippingAddress()->getCountryId();
}

public function getDeliveryType(): int
{
try {
$deliveryTypeName = json_decode($this->order->getData(Config::FIELD_DELIVERY_OPTIONS), true)['deliveryType'];
$deliveryType = AbstractConsignment::DELIVERY_TYPES_NAMES_IDS_MAP[$deliveryTypeName];
} catch (Exception $e) {
$deliveryType = AbstractConsignment::DEFAULT_DELIVERY_TYPE;
}

return $deliveryType;
}

public function consignmentHasShipmentOption(AbstractConsignment $consignment, string $shipmentOption): bool
{
return $this->dataHelper->consignmentHasShipmentOption($consignment, $shipmentOption);
/**
* Business logic determining what shipment options to show, if any.
*/
if (AbstractConsignment::SHIPMENT_OPTION_RECEIPT_CODE === $shipmentOption
&& AbstractConsignment::DELIVERY_TYPE_STANDARD !== $this->getDeliveryType()
) {
return false; // receipt code is only available for standard delivery
}

if (AbstractConsignment::CC_NL === $consignment->getCountry()) {
return $consignment->canHaveShipmentOption($shipmentOption);
}

// For PostNL in Belgium - recipient-only, signature and receipt-code are available
if (AbstractConsignment::CC_BE === $consignment->getCountry() && CarrierPostNL::NAME === $consignment->getCarrierName()) {
return in_array($shipmentOption, [
AbstractConsignment::SHIPMENT_OPTION_ONLY_RECIPIENT,
AbstractConsignment::SHIPMENT_OPTION_SIGNATURE,
AbstractConsignment::SHIPMENT_OPTION_RECEIPT_CODE,
], true);
}

// For UPS shipment options are available for all countries in the EU
if (CarrierUPS::NAME === $consignment->getCarrierName()) {
return true;
}

// No shipment options available in any other cases
return false;
}

/**
Expand All @@ -189,6 +198,6 @@ public function getNewShipmentForm(): NewShipmentForm
*/
public function isOrderManagementEnabled(): bool
{
return TrackTraceHolder::EXPORT_MODE_PPS === $this->orderCollection->getExportMode();
return Config::EXPORT_MODE_PPS === $this->configService->getExportMode();
}
}
25 changes: 23 additions & 2 deletions Block/Sales/NewShipmentForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace MyParcelNL\Magento\Block\Sales;

use Exception;
use MyParcelNL\Sdk\src\Factory\ConsignmentFactory;
use MyParcelNL\Sdk\src\Model\Carrier\CarrierDHLEuroplus;
use MyParcelNL\Sdk\src\Model\Carrier\CarrierDHLForYou;
Expand Down Expand Up @@ -34,24 +35,36 @@ class NewShipmentForm
/**
* @var array
*/
private $shipmentOptionsHumanMap;
private array $shipmentOptionsHumanMap;

/**
* @var array
*/
private array $shipmentOptionsExplanation;


public function __construct()
{
$this->shipmentOptionsHumanMap = [
AbstractConsignment::SHIPMENT_OPTION_SIGNATURE => __('Signature on receipt'),
AbstractConsignment::SHIPMENT_OPTION_RECEIPT_CODE => __('Receiving code'),
AbstractConsignment::SHIPMENT_OPTION_COLLECT => __('Collect package'),
AbstractConsignment::SHIPMENT_OPTION_ONLY_RECIPIENT => __('Home address only'),
AbstractConsignment::SHIPMENT_OPTION_AGE_CHECK => __('Age check 18+'),
AbstractConsignment::SHIPMENT_OPTION_HIDE_SENDER => __('Hide sender'),
AbstractConsignment::SHIPMENT_OPTION_LARGE_FORMAT => __('Large package'),
AbstractConsignment::SHIPMENT_OPTION_RETURN => __('Return if no answer'),
AbstractConsignment::SHIPMENT_OPTION_SAME_DAY_DELIVERY => __('Same day delivery'),
];

$this->shipmentOptionsExplanation = [
AbstractConsignment::SHIPMENT_OPTION_RECEIPT_CODE => __('Insurance is mandatory and will be set. Other shipment options will be removed.'),
];
}

/**
* @return AbstractConsignment[]
* @throws \Exception
* @throws Exception
*/
public function getCarrierSpecificAbstractConsignments(): array
{
Expand All @@ -71,4 +84,12 @@ public function getShipmentOptionsHumanMap(): array
{
return $this->shipmentOptionsHumanMap;
}

/**
* @return array
*/
public function getShipmentOptionsExplanationMap(): array
{
return $this->shipmentOptionsExplanation;
}
}
27 changes: 10 additions & 17 deletions Block/Sales/OrdersAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,23 @@
use Magento\Backend\Block\Template;
use Magento\Backend\Block\Template\Context;
use Magento\Framework\App\ObjectManager;
use MyParcelNL\Magento\Helper\Data;
use MyParcelNL\Magento\Service\Config;

class OrdersAction extends Template
{
/**
* @var \Magento\Framework\ObjectManagerInterface
*/
private $objectManager;

/**
* @var \MyParcelNL\Magento\Helper\Data
*/
private $helper;
private Config $configService;

/**
* @param Context $context
* @param array $data
* @param array $data
*/
public function __construct(
Context $context,
array $data = []
) {
$this->objectManager = ObjectManager::getInstance();
$this->helper = $this->objectManager->get(Data::class);
array $data = []
)
{
$objectManager = ObjectManager::getInstance();
$this->configService = $objectManager->get(Config::class);
parent::__construct($context, $data);
}

Expand All @@ -51,7 +44,7 @@ public function __construct(
*/
public function hasApiKey(): bool
{
return $this->helper->hasApiKey();
return $this->configService->hasApiKey();
}

/**
Expand Down Expand Up @@ -91,7 +84,7 @@ public function getAjaxUrlSendReturnMail(): string
*/
public function getPrintSettings()
{
$settings = $this->helper->getGeneralConfig('print');
$settings = $this->configService->getGeneralConfig('print');

return json_encode($settings);
}
Expand Down
Loading