Skip to content

Commit

Permalink
feat: add package type package small (#825)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark-Ernst authored and joerivanveen committed Mar 14, 2024
1 parent 0656915 commit d9760b7
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 18 deletions.
12 changes: 10 additions & 2 deletions Model/Quote/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public function __construct(

$this->package->setMailboxSettings();
$this->package->setDigitalStampSettings();
$this->package->setPackageSmallSettings();
}

/**
Expand Down Expand Up @@ -135,6 +136,9 @@ private function getPackageType(): string
return AbstractConsignment::PACKAGE_TYPE_DIGITAL_STAMP_NAME;
case AbstractConsignment::PACKAGE_TYPE_MAILBOX_NAME:
$packageType = AbstractConsignment::PACKAGE_TYPE_MAILBOX_NAME;
break;
case AbstractConsignment::PACKAGE_TYPE_PACKAGE_SMALL_NAME:
return AbstractConsignment::PACKAGE_TYPE_PACKAGE_SMALL_NAME;
}
}

Expand Down Expand Up @@ -163,8 +167,9 @@ private function getDeliveryData(): array
continue;
}

$canHaveDigitalStamp = $consignment->canHaveDeliveryType(AbstractConsignment::PACKAGE_TYPE_DIGITAL_STAMP_NAME);
$canHaveMailbox = $consignment->canHaveDeliveryType(AbstractConsignment::PACKAGE_TYPE_MAILBOX_NAME);
$canHaveDigitalStamp = $consignment->canHavePackageType(AbstractConsignment::PACKAGE_TYPE_DIGITAL_STAMP_NAME);
$canHaveMailbox = $consignment->canHavePackageType(AbstractConsignment::PACKAGE_TYPE_MAILBOX_NAME);
$canHavePackageSmall = $consignment->canHavePackageType(AbstractConsignment::PACKAGE_TYPE_PACKAGE_SMALL_NAME);
$canHaveSameDay = $consignment->canHaveExtraOption(AbstractConsignment::SHIPMENT_OPTION_SAME_DAY_DELIVERY);
$canHaveMonday = $consignment->canHaveExtraOption(AbstractConsignment::EXTRA_OPTION_DELIVERY_MONDAY);
$canHaveMorning = $consignment->canHaveDeliveryType(AbstractConsignment::DELIVERY_TYPE_MORNING_NAME);
Expand Down Expand Up @@ -213,6 +218,7 @@ private function getDeliveryData(): array
'pricePickup' => $canHavePickup ? $this->helper->getMethodPrice($carrierPath, 'pickup/fee') : 0,
'pricePackageTypeMailbox' => $canHaveMailbox ? $this->helper->getMethodPrice($carrierPath, 'mailbox/fee', false) : 0,
'pricePackageTypeDigitalStamp' => $canHaveDigitalStamp ? $this->helper->getMethodPrice($carrierPath, 'digital_stamp/fee', false) : 0,
'pricePackageTypePackageSmall' => $canHavePackageSmall ? $this->helper->getMethodPrice($carrierPath, 'package_small/fee', false) : 0,
],
$canHaveSameDay ? [
'cutoffTimeSameDay' => $this->helper->getTimeConfig(
Expand Down Expand Up @@ -313,10 +319,12 @@ public function checkPackageType(string $carrier, ?string $country = null): stri
$country = $country ?? $this->cart->getShippingAddress()->getCountryId();
$canHaveDigitalStamp = $consignment->canHavePackageType(AbstractConsignment::PACKAGE_TYPE_DIGITAL_STAMP_NAME);
$canHaveMailbox = $consignment->canHavePackageType(AbstractConsignment::PACKAGE_TYPE_MAILBOX_NAME);
$canHavePackageSmall = $consignment->canHavePackageType(AbstractConsignment::PACKAGE_TYPE_PACKAGE_SMALL_NAME);

$this->package->setCurrentCountry($country);
$this->package->setDigitalStampActive($canHaveDigitalStamp && $this->helper->getBoolConfig($carrierPath, 'digital_stamp/active'));
$this->package->setMailboxActive($canHaveMailbox && $this->helper->getBoolConfig($carrierPath, 'mailbox/active'));
$this->package->setPackageSmallActive($canHavePackageSmall && $this->helper->getBoolConfig($carrierPath, 'package_small/active'));

return $this->package->selectPackageType($products, $carrierPath);
}
Expand Down
42 changes: 42 additions & 0 deletions Model/Sales/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,21 @@ class Package extends Data implements PackageInterface
*/
private $maxDigitalStampWeight = 0;

/**
* @var float
*/
private $maxPackageSmallWeight = 0;

/**
* @var bool
*/
private $mailboxActive = false;

/**
* @var bool
*/
private $packageSmallActive = false;

/**
* @var bool
*/
Expand Down Expand Up @@ -149,6 +159,24 @@ public function setMailboxActive(bool $mailboxActive): void
$this->mailboxActive = $mailboxActive;
}

/**
* @return bool
*/
public function isPackageSmallActive(): bool
{
return $this->packageSmallActive;
}

/**
* @param bool $packageSmallActive
*
* @return void
*/
public function setPackageSmallActive(bool $packageSmallActive): void
{
$this->packageSmallActive = $packageSmallActive;
}

/**
* @param bool $isActive
*
Expand Down Expand Up @@ -201,6 +229,20 @@ public function setDigitalStampActive(bool $digitalStampActive): void
$this->digitalStampActive = $digitalStampActive;
}

public function getMaxPackageSmallWeight(): float
{
return $this->maxPackageSmallWeight;
}

/**
* @param float $maxWeight
*
* @return void
*/
protected function setMaxPackageSmallWeight(float $maxWeight): void
{
$this->maxPackageSmallWeight = $maxWeight;
}

/**
* @param bool $allProductsFit
Expand Down
60 changes: 55 additions & 5 deletions Model/Sales/Repository/PackageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class PackageRepository extends Package
{
public const DEFAULT_MAXIMUM_MAILBOX_WEIGHT = 2000;
public const MAXIMUM_DIGITAL_STAMP_WEIGHT = 2000;
public const MAXIMUM_PACKAGE_SMALL_WEIGHT = 2000;
public const DEFAULT_LARGE_FORMAT_WEIGHT = 23000;

/**
Expand All @@ -40,11 +41,6 @@ class PackageRepository extends Package
*/
public function selectPackageType(array $products, string $carrierPath): string
{
// When age check is enabled, only packagetype 'package' is possible
if ($this->getAgeCheck($products, $carrierPath)) {
return AbstractConsignment::PACKAGE_TYPE_PACKAGE_NAME;
}

$this->setMailboxPercentage(0);
$weight = 0;
$digitalStamp = true;
Expand Down Expand Up @@ -95,6 +91,10 @@ public function selectPackageType(array $products, string $carrierPath): string
return AbstractConsignment::PACKAGE_TYPE_MAILBOX_NAME;
}

if ($this->fitInPackageSmall()) {
return AbstractConsignment::PACKAGE_TYPE_PACKAGE_SMALL_NAME;
}

return AbstractConsignment::PACKAGE_TYPE_PACKAGE_NAME;
}

Expand Down Expand Up @@ -241,20 +241,70 @@ public function getAgeCheck(array $products, string $carrierPath): bool
public function setDigitalStampSettings(string $carrierPath = self::XML_PATH_POSTNL_SETTINGS): PackageRepository
{
$settings = $this->getConfigValue("{$carrierPath}digital_stamp");

if (null === $settings || ! array_key_exists('active', $settings)) {
$this->_logger->critical("Can't set settings with path: {$carrierPath}digital_stamp");

return $this;
}

$this->setDigitalStampActive('1' === $settings['active']);

if ($this->isDigitalStampActive()) {
$this->setMaxDigitalStampWeight(self::MAXIMUM_DIGITAL_STAMP_WEIGHT);
}

return $this;
}

/**
* Init all package small settings
*
* @param string $carrierPath
*
* @return $this
*/
public function setPackageSmallSettings(string $carrierPath = self::XML_PATH_POSTNL_SETTINGS): PackageRepository
{
$settings = $this->getConfigValue("{$carrierPath}package_small");

if (null === $settings || ! array_key_exists('active', $settings)) {
$this->_logger->critical("Can't set settings with path: {$carrierPath}digital_stamp");

return $this;
}

$this->setPackageSmallActive('1' === $settings['active']);
if ($this->isPackageSmallActive()) {
$weight = abs((float) str_replace(',', '.', $settings['weight'] ?? ''));
$unit = $this->getGeneralConfig('print/weight_indication');

if ('kilo' === $unit) {
$epsilon = 0.00001;
$default = self::MAXIMUM_PACKAGE_SMALL_WEIGHT / 1000.0;
if ($weight < $epsilon) {
$weight = $default;
}
$this->setMaxPackageSmallWeight($weight);
} else {
$weight = (int)$weight;
$this->setMaxPackageSmallWeight($weight ?: self::MAXIMUM_PACKAGE_SMALL_WEIGHT);
}
}

return $this;
}

/**
* @return bool
*/
private function fitInPackageSmall(): bool
{
return AbstractConsignment::CC_BE !== $this->getCurrentCountry()
&& $this->isPackageSmallActive()
&& $this->getWeight() <= $this->getMaxPackageSmallWeight();
}

/**
* @param \Magento\Quote\Model\Quote\Item $product
* @param string $column
Expand Down
25 changes: 15 additions & 10 deletions Model/Sales/TrackTraceHolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ public function convertDataFromMagentoToApi(Track $magentoTrack, array $options)
$order = $shipment->getOrder();
$checkoutData = $order->getData('myparcel_delivery_options') ?? '';
$deliveryOptions = json_decode($checkoutData, true) ?? [];
$deliveryOptions['packageType'] = $options['package_type'];
$deliveryOptions['carrier'] = $this->getCarrierFromOptions($options)
?? $deliveryOptions['carrier']
?? DefaultOptions::getDefaultCarrier()
Expand Down Expand Up @@ -199,6 +198,9 @@ public function convertDataFromMagentoToApi(Track $magentoTrack, array $options)
$this->dataHelper->setOrderStatus($magentoTrack->getOrderId(), Order::STATE_NEW);
}

if (isset($deliveryOptions['packageType'])) {
$options['package_type'] = $deliveryOptions['packageType'];
}
$packageType = $this->getPackageType($options, $magentoTrack, $address);
$dropOffPoint = $this->dataHelper->getDropOffPoint(
CarrierFactory::createFromName($deliveryOptionsAdapter->getCarrier())
Expand Down Expand Up @@ -501,26 +503,29 @@ private function getCarrierFromOptions(array $options): ?string

/**
* @param array $options
* @param string $packageType
* @param Order\Shipment\Track $magentoTrack
* @param object $address
*
* @return int
* @throws LocalizedException
* @throws \Magento\Framework\Exception\LocalizedException
*/
private function getPackageType(array $options, Track $magentoTrack, $address): int
{
// get packagetype from delivery_options and use it for process directly
$packageType = self::$defaultOptions->getPackageType();
// get packagetype from selected radio buttons and check if package type is set
if ($options['package_type'] && 'default' !== $options['package_type']) {
$packageType = $options['package_type'] ?? AbstractConsignment::PACKAGE_TYPE_PACKAGE;
if ($this->getAgeCheck($magentoTrack, $address, $options)) {
return AbstractConsignment::PACKAGE_TYPE_PACKAGE;
}

// get package type from selected radio buttons and check if package type is set
$packageType = $options['package_type'] ?? 'default';
if ('default' === $packageType) {
$packageType = self::$defaultOptions->getPackageType();
}

if (! is_numeric($packageType)) {
$packageType = AbstractConsignment::PACKAGE_TYPES_NAMES_IDS_MAP[$packageType];
$packageType = AbstractConsignment::PACKAGE_TYPES_NAMES_IDS_MAP[$packageType] ?? self::$defaultOptions->getPackageType();
}

return $this->getAgeCheck($magentoTrack, $address, $options) ? AbstractConsignment::PACKAGE_TYPE_PACKAGE
: $packageType;
return $packageType;
}
}
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": "~5.6.5 || ^7.0 || ^8.0",
"myparcelnl/sdk": "~v7.10.0",
"myparcelnl/sdk": "~v7.11.0",
"magento/framework": ">=101.0.8 <102 || >=102.0.1"
},
"require-dev": {
Expand Down
22 changes: 22 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,28 @@
</depends>
</field>
</group>
<group id="package_small" translate="label" type="text" sortOrder="350" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Small Package settings</label>
<field id="active" translate="label" type="select" sortOrder="355" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Automate Small Package</label>
<tooltip>Automatically select package type 'Small Package' for orders under 2000 grams. Package type will be 'Small Package' when a product has setting 'Fit in mailbox' set to 0 and the weight is under 2000 grams.</tooltip>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="weight" translate="label comment" type="text" sortOrder="360" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Small Package weight</label>
<tooltip>Shipments heavier than the weight specified here will not be of package type 'Small Package'.</tooltip>
<depends>
<field id="active">1</field>
</depends>
</field>
<field id="fee" translate="label comment" type="text" sortOrder="365" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Small Package fee</label>
<tooltip>Enter a basic price for a mailbox. The regular price will not affect this price.</tooltip>
<depends>
<field id="active">1</field>
</depends>
</field>
</group>
<group id="morning" translate="label comment" type="text" sortOrder="370" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Morning delivery</label>
<field id="active" translate="label comment" type="select" sortOrder="375" showInDefault="1" showInWebsite="1" showInStore="1">
Expand Down
1 change: 1 addition & 0 deletions i18n/en_US.csv
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ myparcelnl_magento_error_no_shipments_to_process, No MyParcel shipments to proce
no_account_settings, No account settings found. Press the import button in general configuration to fetch account settings.
manage_drop_off_point, Manage your default drop-off point in the
location_page, location manager
package_small, Small package

2 changes: 2 additions & 0 deletions i18n/nl_NL.csv
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ myparcelnl_magento_dhlparcelconnect_settings/delivery, DHL Parcel Connect bezorg
myparcelnl_magento_ups_settings/delivery, UPS bezorging
myparcelnl_magento_dpd_settings/delivery, DPD bezorging
myparcelnl_magento_error_no_shipments_to_process, Geen MyParcel zendingen om te verwerken.
package_small, Klein pakket
Version and support,Versie en support
General settings,Algemene instellingen
API settings,API instellingen
Expand Down Expand Up @@ -262,6 +263,7 @@ This is the type of weight that I use with my products.,Dit is de eenheid die je
Default,Standaard
No,Nee
Yes,Ja

no_account_settings, Geen accountsettings gevonden. Klik op de importeer knop in het algemene configuratie scherm.
manage_drop_off_point, Beheer jouw standaard afleverpunt in de
location_page, locatiewijzer
Expand Down
5 changes: 5 additions & 0 deletions view/adminhtml/web/template/grid/order_massaction.html
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@
name="mypa_package_type" value="3">
<label class="admin__field-label" for="mypa_package_type-letter" trans="Letter"></label>
</div>
<div class="admin__field admin__field-option" id="mypa_container-package_type-package-small">
<input type="radio" class="admin__control-radio" id="mypa_package_type-package-small"
name="mypa_package_type" value="6">
<label class="admin__field-label" for="mypa_package_type-package-small" trans="Small Package"></label>
</div>
</div>
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions view/frontend/web/js/view/delivery-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ define(
methodCodeDeliveryOptionsConfigMap: {
'myparcelnl_magento_postnl_settings/delivery': 'config.carrierSettings.postnl.priceStandardDelivery',
'myparcelnl_magento_postnl_settings/mailbox': 'config.carrierSettings.postnl.pricePackageTypeMailbox',
'myparcelnl_magento_postnl_settings/package_small': 'config.carrierSettings.postnl.pricePackageTypePackageSmall',
'myparcelnl_magento_postnl_settings/digital_stamp': 'config.carrierSettings.postnl.pricePackageTypeDigitalStamp',
'myparcelnl_magento_postnl_settings/morning': 'config.carrierSettings.postnl.priceMorningDelivery',
'myparcelnl_magento_postnl_settings/evening': 'config.carrierSettings.postnl.priceEveningDelivery',
Expand Down

0 comments on commit d9760b7

Please sign in to comment.