From 2f90f2fb17e479e73e55f6f5b977fbca9ca08e97 Mon Sep 17 00:00:00 2001 From: Ron Oerlemans Date: Tue, 16 May 2023 09:45:26 +0000 Subject: [PATCH] Release V1.0.41 --- .../{Test.php => TestAuthentication.php} | 6 ++-- .../{Test.php => TestAuthentication.php} | 2 +- Model/Api/Connector.php | 16 ++++++++-- Model/Service/Logic/Shipment.php | 2 +- Model/Service/Preset.php | 4 +++ Observer/Shipment.php | 27 ++++++++++++++++- Ui/Bulk/Actions.php | 30 ++++++++++++++++--- Ui/Column/Labels.php | 8 +++-- composer.json | 2 +- etc/adminhtml/system.xml | 18 ++++++++--- etc/module.xml | 2 +- .../{test.phtml => test_authentication.phtml} | 2 +- .../web/template/servicepoint-info.html | 2 +- 13 files changed, 98 insertions(+), 23 deletions(-) rename Block/Adminhtml/Authentication/{Test.php => TestAuthentication.php} (90%) rename Controller/Adminhtml/Authentication/{Test.php => TestAuthentication.php} (97%) rename view/adminhtml/templates/authentication/{test.phtml => test_authentication.phtml} (99%) diff --git a/Block/Adminhtml/Authentication/Test.php b/Block/Adminhtml/Authentication/TestAuthentication.php similarity index 90% rename from Block/Adminhtml/Authentication/Test.php rename to Block/Adminhtml/Authentication/TestAuthentication.php index 57d12b8..694fdf4 100644 --- a/Block/Adminhtml/Authentication/Test.php +++ b/Block/Adminhtml/Authentication/TestAuthentication.php @@ -2,12 +2,12 @@ namespace DHLParcel\Shipping\Block\Adminhtml\Authentication; -class Test extends \Magento\Config\Block\System\Config\Form\Field +class TestAuthentication extends \Magento\Config\Block\System\Config\Form\Field { /** * @var string */ - protected $_template = 'DHLParcel_Shipping::authentication/test.phtml'; + protected $_template = 'DHLParcel_Shipping::authentication/test_authentication.phtml'; /** * Return element html @@ -30,7 +30,7 @@ protected function _getElementHtml(\Magento\Framework\Data\Form\Element\Abstract */ public function getAjaxUrl() { - return $this->getUrl('dhlparcel_shipping/authentication/test'); + return $this->getUrl('dhlparcel_shipping/authentication/testAuthentication'); } /** diff --git a/Controller/Adminhtml/Authentication/Test.php b/Controller/Adminhtml/Authentication/TestAuthentication.php similarity index 97% rename from Controller/Adminhtml/Authentication/Test.php rename to Controller/Adminhtml/Authentication/TestAuthentication.php index 8b1dd28..ec8e506 100644 --- a/Controller/Adminhtml/Authentication/Test.php +++ b/Controller/Adminhtml/Authentication/TestAuthentication.php @@ -24,7 +24,7 @@ use Magento\Framework\App\ResponseInterface; use Magento\Framework\Controller\ResultFactory; -class Test extends \Magento\Backend\App\Action +class TestAuthentication extends \Magento\Backend\App\Action { /** * Authorization level of a basic admin session diff --git a/Model/Api/Connector.php b/Model/Api/Connector.php index 4e14155..47379d4 100644 --- a/Model/Api/Connector.php +++ b/Model/Api/Connector.php @@ -172,9 +172,17 @@ protected function authenticate($refresh = false) $cacheKey = $this->apiCache->createKey(0, 'accessToken'); $accessToken = $this->apiCache->load($cacheKey); if ($accessToken === false || $refresh === true) { + $apiUser = $this->helper->getConfigData('api/user'); + $key = $this->helper->getConfigData('api/key'); + + if ($apiUser === null || $key === null) { + $this->failedAuthentication = true; + return; + } + $response = $this->post(self::AUTH_API, [ - 'userId' => trim($this->helper->getConfigData('api/user')), - 'key' => trim($this->helper->getConfigData('api/key')), + 'userId' => trim($apiUser), + 'key' => trim($key), ]); if (!empty($response['accessToken'])) { $this->apiCache->save($response['accessToken'], $cacheKey, [], 720); @@ -195,6 +203,10 @@ protected function authenticate($refresh = false) */ public function testAuthenticate($userId, $key) { + if (!isset($userId, $key)) { + return false; + } + $response = $this->post(self::AUTH_API, [ 'userId' => trim($userId), 'key' => trim($key), diff --git a/Model/Service/Logic/Shipment.php b/Model/Service/Logic/Shipment.php index 555814e..472ccda 100644 --- a/Model/Service/Logic/Shipment.php +++ b/Model/Service/Logic/Shipment.php @@ -331,7 +331,7 @@ protected function getShipperAddress($storeId, $group = '') ], 'address' => [ 'countryCode' => $this->helper->getConfigData('shipper/country_code', $storeId), - 'postalCode' => strtoupper($this->helper->getConfigData('shipper/' . $group . 'postal_code', $storeId)), + 'postalCode' => strtoupper($this->helper->getConfigData('shipper/' . $group . 'postal_code', $storeId) ?? ''), 'city' => $this->helper->getConfigData('shipper/' . $group . 'city', $storeId), 'street' => $this->helper->getConfigData('shipper/' . $group . 'street', $storeId), 'number' => $this->helper->getConfigData('shipper/' . $group . 'house_number', $storeId), diff --git a/Model/Service/Preset.php b/Model/Service/Preset.php index 5f2c063..c4c4e3c 100644 --- a/Model/Service/Preset.php +++ b/Model/Service/Preset.php @@ -114,6 +114,10 @@ public function getDefaultOptions($order, $requiredOnly = false) */ public function getMethodKey($order) { + if ($order->getShippingMethod() === null) { + return null; + } + return str_replace('dhlparcel_', '', $order->getShippingMethod()); } diff --git a/Observer/Shipment.php b/Observer/Shipment.php index 68ddd70..b66cef7 100644 --- a/Observer/Shipment.php +++ b/Observer/Shipment.php @@ -108,6 +108,7 @@ protected function processGrid($order) $defaultOptions = $this->additionalServices($defaultOptions); $sizes = $this->capabilityService->getSizes($storeId, $toCountry, $toPostalCode, $toBusiness, array_keys($defaultOptions)); + $sizes = $this->overwriteMailboxType($sizes); if (empty($sizes) || !is_array($sizes)) { $skippableOptions = $this->presetService->filterSkippableDefaults($defaultOptions, $storeId); @@ -126,6 +127,7 @@ protected function processGrid($order) $defaultOptions = array_merge($requiredOptions, $allowedOptions); $sizes = $this->capabilityService->getSizes($storeId, $toCountry, $toPostalCode, $toBusiness, array_keys($defaultOptions)); + $sizes = $this->overwriteMailboxType($sizes); if (empty($sizes) || !is_array($sizes)) { $translations = $this->presetService->getTranslations(); @@ -136,6 +138,7 @@ protected function processGrid($order) $packageKey = ''; $packageWeight = 1000000; + foreach ($sizes as $key => $package) { if (isset($package['minWeightKg']) && isset($package['maxWeightKg'])) { $packageSum = intval($package['minWeightKg']) + intval($package['maxWeightKg']); @@ -147,7 +150,6 @@ protected function processGrid($order) $packageWeight = $packageSum; } } - $pieces = [$this->createPiece($packageKey)]; $options = []; @@ -212,6 +214,29 @@ protected function processForm($orderId) return $tracks; } + protected function overwriteMailboxType($sizes) + { + if ($this->request->getParam('method_override') !== 'mailbox' || !$this->request->getParam('mailbox_type')) { + return $sizes; + } + + $mailboxType = $this->request->getParam('mailbox_type'); + return array_filter($sizes, function ($size) use ($mailboxType) { + if (!isset($size['key'])) { + return true; + } + + if ($mailboxType === 'envelop' && strtolower($size['key']) !== 'envelope') { + return false; + } + + if ($mailboxType === 'no_envelope' && strtolower($size['key']) === 'envelope') { + return false; + } + + return true; + }); + } protected function checkMailboxOverride($options) { if ($this->request->getParam('method_override') == 'mailbox' && array_key_exists('DOOR', $options)) { diff --git a/Ui/Bulk/Actions.php b/Ui/Bulk/Actions.php index 39a5007..aa3bbdd 100644 --- a/Ui/Bulk/Actions.php +++ b/Ui/Bulk/Actions.php @@ -50,9 +50,20 @@ public function jsonSerialize() $this->addOptionStack( $options, 'create_mailbox', - __('Create mailbox labels'), + __('Create mailbox labels (0.5-2kg)'), $this->urlPath . 'create', - ['method_override' => 'mailbox'] + ['method_override' => 'mailbox', 'mailbox_type' => 'no_envelope'] + ); + } + + $enabled = $this->helper->getConfigData('usability/bulk/create_envelope'); + if ($enabled && !$skipCreate) { + $this->addOptionStack( + $options, + 'create_envelope', + __('Create envelope labels (50-500g)'), + $this->urlPath . 'create', + ['method_override' => 'mailbox', 'mailbox_type' => 'envelope'] ); } @@ -72,9 +83,20 @@ public function jsonSerialize() $this->addOptionStack( $options, 'create_mailbox_dhl_only', - __('Create mailbox labels (only for DHL shipping methods)'), + __('Create mailbox labels (0.5-2kg, only for DHL shipping methods)'), + $this->urlPath . 'create', + ['method_override' => 'mailbox', 'dhlparcel_only' => 'true', 'mailbox_type' => 'no_envelope'] + ); + } + + $enabled = $this->helper->getConfigData('usability/bulk/create_envelope_dhl_only'); + if ($enabled && !$skipCreate) { + $this->addOptionStack( + $options, + 'create_envelope_dhl_only', + __('Create envelope labels (50-500g, only for DHL shipping methods)'), $this->urlPath . 'create', - ['method_override' => 'mailbox', 'dhlparcel_only' => 'true'] + ['method_override' => 'envelope', 'dhlparcel_only' => 'true', 'mailbox_type' => 'envelope'] ); } diff --git a/Ui/Column/Labels.php b/Ui/Column/Labels.php index d0f67de..1d9036b 100644 --- a/Ui/Column/Labels.php +++ b/Ui/Column/Labels.php @@ -92,9 +92,11 @@ protected function getLabelLinks($shipment) // Build service options text $serviceOptions = []; $serviceCounter = 0; - foreach (explode(',', $piece->getServiceOptions()) as $serviceOption) { - if ($this->presetService->getTranslation($serviceOption) !== null) { - $serviceOptions[] = sprintf('%s', strtoupper($serviceOption), $this->presetService->getTranslation($serviceOption)) . (++$serviceCounter % 4 ? '' : '
'); + if ($piece->getServiceOptions() !== null) { + foreach (explode(',', $piece->getServiceOptions()) as $serviceOption) { + if ($this->presetService->getTranslation($serviceOption) !== null) { + $serviceOptions[] = sprintf('%s', strtoupper($serviceOption), $this->presetService->getTranslation($serviceOption)) . (++$serviceCounter % 4 ? '' : '
'); + } } } $serviceOptionsText = ''; diff --git a/composer.json b/composer.json index cf04873..1c26755 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "dhlparcel/magento2-plugin", "description": "DHL Parcel plugin for Magento 2", "type": "magento2-module", - "version": "1.0.40", + "version": "1.0.41", "license": [ "OSL-3.0" ], diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 1517d6b..a54aec3 100755 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -64,7 +64,7 @@ - DHLParcel\Shipping\Block\Adminhtml\Authentication\Test + DHLParcel\Shipping\Block\Adminhtml\Authentication\TestAuthentication Make sure to test your filled credentials. We will search for your account ID and Organization ID @@ -120,7 +120,7 @@ showInWebsite="1" showInStore="1"> DHLParcel\Shipping\Model\Config\Source\ShipmentInsurance - Additional transport insurance. + Applicable to both consumer and business shipments, not available for mailbox delivery. When enabled, the selected insurance option is added to your shipments (when available). @@ -1041,7 +1041,12 @@ - + + Magento\Config\Model\Config\Source\Yesno + + + Magento\Config\Model\Config\Source\Yesno - + + Magento\Config\Model\Config\Source\Yesno + + + Magento\Config\Model\Config\Source\Yesno - + diff --git a/view/adminhtml/templates/authentication/test.phtml b/view/adminhtml/templates/authentication/test_authentication.phtml similarity index 99% rename from view/adminhtml/templates/authentication/test.phtml rename to view/adminhtml/templates/authentication/test_authentication.phtml index 20a24e9..cf92244 100644 --- a/view/adminhtml/templates/authentication/test.phtml +++ b/view/adminhtml/templates/authentication/test_authentication.phtml @@ -8,7 +8,7 @@ ?> - +