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 51 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 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
da07aac
feat: introduce matrix object and use for price calculation (wip)
joerivanveen Jan 14, 2025
0f6cc7f
feat: introduce matrix object and use for price calculation (wip)
joerivanveen Jan 14, 2025
e478329
fix: simplify ispickup removing non existent options
joerivanveen Jan 14, 2025
6f9a752
chore: set version to 5.0.0
joerivanveen Jan 14, 2025
56def75
fix: prevent undefined constant error
joerivanveen Jan 14, 2025
9256408
feat: display price according to tax settings
joerivanveen Jan 16, 2025
7c8379b
fix: return base price to magento when required
joerivanveen Jan 16, 2025
41b9455
fix: handle tax better conforming to magento
joerivanveen Jan 16, 2025
36d89b2
fix: try to get better amounts
joerivanveen Jan 16, 2025
2d7552c
fix: have the carrier show up on cases sensitive oses
joerivanveen Jan 17, 2025
c5efcd6
fix: vat calculation stabilizing (wip)
joerivanveen Jan 17, 2025
34016e3
fix: vat calculation stabilizing (wip)
joerivanveen Jan 17, 2025
e324302
chore: remove debug statements and comments
joerivanveen Jan 20, 2025
ef5f4b0
fix: default to display including tax in Delivery Options
joerivanveen Jan 20, 2025
66fdcf2
fix: fix myparcel chosen delivery method title
joerivanveen Jan 20, 2025
3ab5340
fix: wait for dom to render delivery options
joerivanveen Jan 20, 2025
94ecdc6
refactor: simplify javascript a bit
joerivanveen Jan 20, 2025
1c4195f
refactor: simplify javascript a bit
joerivanveen Jan 20, 2025
a5b045a
refactor: simplify javascript a bit
joerivanveen Jan 20, 2025
e2f6d55
feat: building frontend matrix (wip)
joerivanveen Jan 28, 2025
8d806fe
fix: adjust to new sdk major version
joerivanveen Jan 30, 2025
a297dd5
fix: conform more simply to max length in consignment
joerivanveen Feb 3, 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
2 changes: 1 addition & 1 deletion Adapter/DeliveryOptionsFromOrderAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace MyParcelNL\Magento\Adapter;

use MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter;
use MyParcelNL\Sdk\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter;

class DeliveryOptionsFromOrderAdapter extends AbstractDeliveryOptionsAdapter
{
Expand Down
2 changes: 1 addition & 1 deletion Adapter/OrderLineOptionsFromOrderAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace MyParcelNL\Magento\Adapter;

use MyParcelNL\Sdk\src\Model\Fulfilment\OrderLine;
use MyParcelNL\Sdk\Model\Fulfilment\OrderLine;

class OrderLineOptionsFromOrderAdapter extends OrderLine
{
Expand Down
19 changes: 11 additions & 8 deletions Adapter/ShipmentOptionsFromAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

namespace MyParcelNL\Magento\Adapter;

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

class ShipmentOptionsFromAdapter extends 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);
}
}
2 changes: 1 addition & 1 deletion Block/DataProviders/Email/Shipment/TrackingUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Magento\Framework\App\ObjectManager;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Shipment\Track;
use MyParcelNL\Sdk\src\Helper\TrackTraceUrl;
use MyParcelNL\Sdk\Helper\TrackTraceUrl;

// For Magento version < 2.3.2 the TrackingUrl is not exist. Therefore, it must be checked if the class exists and so that the class can be extended.
if (class_exists('\Magento\Sales\Block\DataProviders\Email\Shipment\TrackingUrl')) {
Expand Down
135 changes: 72 additions & 63 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\Sdk\src\Model\Consignment\AbstractConsignment;
use MyParcelNL\Magento\Service\Config;
use MyParcelNL\Magento\Service\Weight;
use MyParcelNL\Sdk\Model\Carrier\CarrierPostNL;
use MyParcelNL\Sdk\Model\Carrier\CarrierUPS;
use MyParcelNL\Sdk\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 All @@ -160,7 +129,7 @@ public function getPackageType()
*/
public function getCarrier(): string
{
return $this->defaultOptions->getCarrier();
return $this->defaultOptions->getCarrierName();
}

/**
Expand All @@ -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();
}
}
43 changes: 32 additions & 11 deletions Block/Sales/NewShipmentForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

namespace MyParcelNL\Magento\Block\Sales;

use MyParcelNL\Sdk\src\Factory\ConsignmentFactory;
use MyParcelNL\Sdk\src\Model\Carrier\CarrierDHLEuroplus;
use MyParcelNL\Sdk\src\Model\Carrier\CarrierDHLForYou;
use MyParcelNL\Sdk\src\Model\Carrier\CarrierDHLParcelConnect;
use MyParcelNL\Sdk\src\Model\Carrier\CarrierDPD;
use MyParcelNL\Sdk\src\Model\Carrier\CarrierFactory;
use MyParcelNL\Sdk\src\Model\Carrier\CarrierPostNL;
use MyParcelNL\Sdk\src\Model\Carrier\CarrierUPS;
use MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment;
use Exception;
use MyParcelNL\Sdk\Factory\ConsignmentFactory;
use MyParcelNL\Sdk\Model\Carrier\CarrierDHLEuroplus;
use MyParcelNL\Sdk\Model\Carrier\CarrierDHLForYou;
use MyParcelNL\Sdk\Model\Carrier\CarrierDHLParcelConnect;
use MyParcelNL\Sdk\Model\Carrier\CarrierDPD;
use MyParcelNL\Sdk\Model\Carrier\CarrierFactory;
use MyParcelNL\Sdk\Model\Carrier\CarrierPostNL;
use MyParcelNL\Sdk\Model\Carrier\CarrierUPS;
use MyParcelNL\Sdk\Model\Consignment\AbstractConsignment;

class NewShipmentForm
{
Expand All @@ -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;
}
}
6 changes: 3 additions & 3 deletions Block/Sales/OrderAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
namespace MyParcelNL\Magento\Block\Sales;

use Magento\Backend\Block\Template\Context;
use MyParcelNL\Sdk\src\Model\Consignment\BaseConsignment;
use MyParcelNL\Sdk\Model\Consignment\BaseConsignment;
use \Magento\Framework\Registry;

class OrderAction extends OrdersAction
Expand All @@ -25,14 +25,14 @@ class OrderAction extends OrdersAction
*/
private $order;
/**
* @var \MyParcelNL\Sdk\src\Model\Consignment\BaseConsignment
* @var \MyParcelNL\Sdk\Model\Consignment\BaseConsignment
*/
private $consignment;

/**
* @param Context $context
* @param \Magento\Framework\Registry $registry
* @param \MyParcelNL\Sdk\src\Model\Consignment\BaseConsignment $consignment
* @param \MyParcelNL\Sdk\Model\Consignment\BaseConsignment $consignment
* @param array $data
*/
public function __construct(
Expand Down
Loading