From aff0fdf845d511f1e86f8499b3f8083b88a84507 Mon Sep 17 00:00:00 2001 From: Lucas Hoeffnagel Date: Thu, 27 May 2021 08:44:39 +0200 Subject: [PATCH 01/72] POSTNLM2-1147 - removed house number check, changed to temp address --- view/frontend/web/js/Helper/AddressFinder.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/view/frontend/web/js/Helper/AddressFinder.js b/view/frontend/web/js/Helper/AddressFinder.js index 0f9a245a2..e2cc4051c 100644 --- a/view/frontend/web/js/Helper/AddressFinder.js +++ b/view/frontend/web/js/Helper/AddressFinder.js @@ -65,7 +65,7 @@ define([ housenumber = shippingAddress.street[0].replace(/\D/g,''); } - address = { + var tempAddress = { street: shippingAddress.street[0], postcode: shippingAddress.postcode, housenumber: housenumber, @@ -75,8 +75,8 @@ define([ country: shippingAddress.countryId }; - if (address.country && address.postcode && address.street !== undefined && address.street[0] && address.housenumber) { - return address; + if (tempAddress.country && tempAddress.postcode && tempAddress.street !== undefined && tempAddress.street[0]) { + return tempAddress; } } From 825ec2e4b928945d2e56a4ac446b386f3113bfe1 Mon Sep 17 00:00:00 2001 From: Lucas Hoeffnagel Date: Thu, 27 May 2021 15:23:51 +0200 Subject: [PATCH 02/72] Revert "POSTNLM2-1147 - removed house number check, changed to temp address" This reverts commit aff0fdf845d511f1e86f8499b3f8083b88a84507. --- view/frontend/web/js/Helper/AddressFinder.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/view/frontend/web/js/Helper/AddressFinder.js b/view/frontend/web/js/Helper/AddressFinder.js index e2cc4051c..0f9a245a2 100644 --- a/view/frontend/web/js/Helper/AddressFinder.js +++ b/view/frontend/web/js/Helper/AddressFinder.js @@ -65,7 +65,7 @@ define([ housenumber = shippingAddress.street[0].replace(/\D/g,''); } - var tempAddress = { + address = { street: shippingAddress.street[0], postcode: shippingAddress.postcode, housenumber: housenumber, @@ -75,8 +75,8 @@ define([ country: shippingAddress.countryId }; - if (tempAddress.country && tempAddress.postcode && tempAddress.street !== undefined && tempAddress.street[0]) { - return tempAddress; + if (address.country && address.postcode && address.street !== undefined && address.street[0] && address.housenumber) { + return address; } } From 6d3301c1d4f48ac50bbfd7f6ad6a4c1c967627e3 Mon Sep 17 00:00:00 2001 From: Michiel Vonk Date: Tue, 7 Dec 2021 09:37:42 +0100 Subject: [PATCH 03/72] Bump composer version --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 53dfdd226..8dbdba240 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ }, "type": "magento2-module", "license": "CC-BY-NC-ND-3.0", - "version": "1.10.0", + "version": "1.10.1", "authors": [ { "name": "TIG", From 418ef3c7273fba3adade094c55b482d7667d515b Mon Sep 17 00:00:00 2001 From: Kevin van Dijk Date: Mon, 15 Aug 2022 10:19:09 +0200 Subject: [PATCH 04/72] POSTNLM2-1334 add smart return to shipment page --- Api/Data/ShipmentInterface.php | 12 +++++++ Block/Adminhtml/Shipment/View.php | 33 +++++++++++++++++-- Config/Provider/ReturnOptions.php | 23 +++++++++---- .../CreateShipmentsAndPrintPackingSlip.php | 2 +- ...ateShipmentsConfirmAndPrintPackingSlip.php | 2 +- ...ShipmentsConfirmAndPrintShippingLabels.php | 2 +- .../Shipment/ConfirmAndPrintShippingLabel.php | 2 +- .../Shipment/MassPrintPackingslip.php | 2 +- .../Shipment/MassPrintShippingLabel.php | 2 +- .../Adminhtml/Shipment/PrintPackingslip.php | 2 +- .../Adminhtml/Shipment/PrintShippingLabel.php | 2 +- Model/Shipment.php | 22 ++++++++++++- .../Api/ShipmentManagement/GenerateLabel.php | 2 +- Service/Handler/BarcodeHandler.php | 13 ++++++-- Service/Shipment/Data.php | 27 ++++++++++++++- Setup/UpgradeSchema.php | 4 +++ .../system/postnlsettings/returns.xml | 9 +++++ etc/di.xml | 5 +++ etc/module.xml | 2 +- 19 files changed, 144 insertions(+), 24 deletions(-) diff --git a/Api/Data/ShipmentInterface.php b/Api/Data/ShipmentInterface.php index f3d8a0d4a..38f78c0c1 100644 --- a/Api/Data/ShipmentInterface.php +++ b/Api/Data/ShipmentInterface.php @@ -366,6 +366,18 @@ public function setReturnBarcode($value); */ public function getReturnBarcodes(); + /** + * @param $value + * + * @return TIG\PostNL\Api\Data\ShipmentInterface + */ + public function setIsSmartReturn($value); + + /** + * @return boolean + */ + public function getIsSmartReturn(); + /** * @return \TIG\PostNL\Api\Data\OrderInterface */ diff --git a/Block/Adminhtml/Shipment/View.php b/Block/Adminhtml/Shipment/View.php index 6651b0656..a90cf632a 100644 --- a/Block/Adminhtml/Shipment/View.php +++ b/Block/Adminhtml/Shipment/View.php @@ -74,7 +74,7 @@ public function __construct( parent::__construct($context, $registry, $data); } - + protected function _construct() { parent::_construct(); @@ -108,6 +108,7 @@ private function processButtons() $this->setPostNLPrintLabelButtonData(); $this->setPostNLPrintLabelWithoutConfirmButton(); $this->setPostNLPrintPackingslipButton(); + $this->setPostNLSendSmartReturnButton(); } /** @@ -150,7 +151,7 @@ private function setPostNLPrintLabelWithoutConfirmButton() { $postNLShipment = $this->getPostNLShipment(); $mainBarcode = $postNLShipment->getMainBarcode(); - + $this->buttonList->add( 'postnl_print_without_confirm', [ @@ -185,6 +186,18 @@ private function setPostNLConfirmButton() ); } + private function setPostNLSendSmartReturnButton() + { + $this->buttonList->add( + 'postnl_send_smart_return', + [ + 'label' => __('PostNL - Send Smart Return'), + 'class' => 'save primary', + 'onclick' => 'setLocation(\'' . $this->getSendSmartReturnUrl() . '\')', + ] + ); + } + /** * Set the correct text. */ @@ -262,7 +275,21 @@ private function getCancelConfirmationUrl() ] ); } - + + private function getSendSmartReturnUrl() + { + /** @var PostNLShipment $postNLShipment */ + $postNLShipment = $this->getPostNLShipment(); + + return $this->getUrl( + 'postnl/shipment/GetSmartReturnLabel', + [ + 'postnl_shipment_id' => $postNLShipment->getId(), + 'shipment_id' => $this->getShipment()->getId(), + ] + ); + } + /** * @return \TIG\PostNL\Api\Data\ShipmentInterface|null */ diff --git a/Config/Provider/ReturnOptions.php b/Config/Provider/ReturnOptions.php index b692d57db..a65d8a75f 100644 --- a/Config/Provider/ReturnOptions.php +++ b/Config/Provider/ReturnOptions.php @@ -36,14 +36,15 @@ */ class ReturnOptions extends AbstractConfigProvider { - const XPATH_RETURN_IS_ACTIVE = 'tig_postnl/returns/returns_active'; - const XPATH_RETURN_CITY = 'tig_postnl/returns/city'; - const XPATH_RETURN_COMPANY = 'tig_postnl/returns/company'; - const XPATH_RETURN_STREETNAME = 'tig_postnl/returns/streetname'; - const XPATH_RETURN_HOUSENUMBER = 'tig_postnl/returns/housenumber'; + const XPATH_RETURN_IS_ACTIVE = 'tig_postnl/returns/returns_active'; + const XPATH_RETURN_CITY = 'tig_postnl/returns/city'; + const XPATH_RETURN_COMPANY = 'tig_postnl/returns/company'; + const XPATH_RETURN_STREETNAME = 'tig_postnl/returns/streetname'; + const XPATH_RETURN_HOUSENUMBER = 'tig_postnl/returns/housenumber'; const XPATH_RETURN_FREEPOST_NUMBER = 'tig_postnl/returns/freepost_number'; - const XPATH_RETURN_ZIPCODE = 'tig_postnl/returns/zipcode'; - const XPATH_RETURN_CUSTOMER_CODE = 'tig_postnl/returns/customer_code'; + const XPATH_RETURN_ZIPCODE = 'tig_postnl/returns/zipcode'; + const XPATH_RETURN_CUSTOMER_CODE = 'tig_postnl/returns/customer_code'; + const XPATH_SMART_RETURN_IS_ACTIVE = 'tig_postnl/returns/smart_returns_active'; /** * @return bool @@ -53,6 +54,14 @@ public function isReturnActive() return (bool)$this->getConfigFromXpath(self::XPATH_RETURN_IS_ACTIVE); } + /** + * @return bool + */ + public function isSmartReturnActive() + { + return (bool)$this->getConfigFromXpath(self::XPATH_SMART_RETURN_IS_ACTIVE); + } + /** * @return mixed */ diff --git a/Controller/Adminhtml/Order/CreateShipmentsAndPrintPackingSlip.php b/Controller/Adminhtml/Order/CreateShipmentsAndPrintPackingSlip.php index 0c2878d7e..a1666e8c3 100644 --- a/Controller/Adminhtml/Order/CreateShipmentsAndPrintPackingSlip.php +++ b/Controller/Adminhtml/Order/CreateShipmentsAndPrintPackingSlip.php @@ -186,7 +186,7 @@ private function loadLabels($shipment) $address = $shipment->getShippingAddress(); try { - $this->barcodeHandler->prepareShipment($shipment->getId(), $address->getCountryId()); + $this->barcodeHandler->prepareShipment($shipment->getId(), $address->getCountryId(), false); } catch (LocalizedException $exception) { $this->messageManager->addErrorMessage( __('[POSTNL-0070] - Unable to generate barcode for shipment #%1', $shipment->getIncrementId()) diff --git a/Controller/Adminhtml/Order/CreateShipmentsConfirmAndPrintPackingSlip.php b/Controller/Adminhtml/Order/CreateShipmentsConfirmAndPrintPackingSlip.php index 3408b8490..25028c41f 100644 --- a/Controller/Adminhtml/Order/CreateShipmentsConfirmAndPrintPackingSlip.php +++ b/Controller/Adminhtml/Order/CreateShipmentsConfirmAndPrintPackingSlip.php @@ -201,7 +201,7 @@ private function loadLabel($shipment) $address = $shipment->getShippingAddress(); try { - $this->barcodeHandler->prepareShipment($shipment->getId(), $address->getCountryId()); + $this->barcodeHandler->prepareShipment($shipment->getId(), $address->getCountryId(), false); } catch (LocalizedException $exception) { $this->messageManager->addErrorMessage( __('[POSTNL-0070] - Unable to generate barcode for shipment #%1', $shipment->getIncrementId()) diff --git a/Controller/Adminhtml/Order/CreateShipmentsConfirmAndPrintShippingLabels.php b/Controller/Adminhtml/Order/CreateShipmentsConfirmAndPrintShippingLabels.php index 62c115b3a..9c9e4bb93 100644 --- a/Controller/Adminhtml/Order/CreateShipmentsConfirmAndPrintShippingLabels.php +++ b/Controller/Adminhtml/Order/CreateShipmentsConfirmAndPrintShippingLabels.php @@ -182,7 +182,7 @@ private function loadLabel($shipment) $address = $this->canaryConverter->convert($shipment->getShippingAddress()); try { - $this->barcodeHandler->prepareShipment($shipment->getId(), $address->getCountryId()); + $this->barcodeHandler->prepareShipment($shipment->getId(), $address->getCountryId(), false); $this->setTracks($shipment); $this->setLabel($shipment->getId()); } catch (LocalizedException $exception) { diff --git a/Controller/Adminhtml/Shipment/ConfirmAndPrintShippingLabel.php b/Controller/Adminhtml/Shipment/ConfirmAndPrintShippingLabel.php index b77526fa4..80f168c48 100644 --- a/Controller/Adminhtml/Shipment/ConfirmAndPrintShippingLabel.php +++ b/Controller/Adminhtml/Shipment/ConfirmAndPrintShippingLabel.php @@ -128,7 +128,7 @@ private function getLabels() $countryId = $this->getCountryId($shipment); try { - $this->barcodeHandler->prepareShipment($shipment->getId(), $countryId); + $this->barcodeHandler->prepareShipment($shipment->getId(), $countryId, false); } catch (LocalizedException $exception) { $this->messageManager->addErrorMessage( __('[POSTNL-0070] - Unable to generate barcode for shipment #%1', $shipment->getIncrementId()) diff --git a/Controller/Adminhtml/Shipment/MassPrintPackingslip.php b/Controller/Adminhtml/Shipment/MassPrintPackingslip.php index bec909b03..52fffb6ab 100644 --- a/Controller/Adminhtml/Shipment/MassPrintPackingslip.php +++ b/Controller/Adminhtml/Shipment/MassPrintPackingslip.php @@ -133,7 +133,7 @@ private function loadLabel($shipment) $address = $shipment->getShippingAddress(); try { - $this->barcodeHandler->prepareShipment($shipment->getId(), $address->getCountryId()); + $this->barcodeHandler->prepareShipment($shipment->getId(), $address->getCountryId(), false); } catch (LocalizedException $exception) { $this->messageManager->addErrorMessage( __('[POSTNL-0070] - Unable to generate barcode for shipment #%1', $shipment->getIncrementId()) diff --git a/Controller/Adminhtml/Shipment/MassPrintShippingLabel.php b/Controller/Adminhtml/Shipment/MassPrintShippingLabel.php index 65e88b8dc..2325f0e11 100644 --- a/Controller/Adminhtml/Shipment/MassPrintShippingLabel.php +++ b/Controller/Adminhtml/Shipment/MassPrintShippingLabel.php @@ -139,7 +139,7 @@ private function loadLabel($shipment) $address = $shipment->getShippingAddress(); try { - $this->barcodeHandler->prepareShipment($shipment->getId(), $address->getCountryId()); + $this->barcodeHandler->prepareShipment($shipment->getId(), $address->getCountryId(), false); } catch (LocalizedException $exception) { $this->messageManager->addErrorMessage( __('[POSTNL-0070] - Unable to generate barcode for shipment #%1', $shipment->getIncrementId()) diff --git a/Controller/Adminhtml/Shipment/PrintPackingslip.php b/Controller/Adminhtml/Shipment/PrintPackingslip.php index d72137a7d..40494d5f0 100644 --- a/Controller/Adminhtml/Shipment/PrintPackingslip.php +++ b/Controller/Adminhtml/Shipment/PrintPackingslip.php @@ -106,7 +106,7 @@ private function getPackingslip() $address = $shipment->getShippingAddress(); try { - $this->barcodeHandler->prepareShipment($shipment->getId(), $address->getCountryId()); + $this->barcodeHandler->prepareShipment($shipment->getId(), $address->getCountryId(), false); } catch (LocalizedException $exception) { $this->messageManager->addErrorMessage( __('[POSTNL-0070] - Unable to generate barcode for shipment #%1', $shipment->getIncrementId()) diff --git a/Controller/Adminhtml/Shipment/PrintShippingLabel.php b/Controller/Adminhtml/Shipment/PrintShippingLabel.php index f1737e3ec..1a652ed19 100644 --- a/Controller/Adminhtml/Shipment/PrintShippingLabel.php +++ b/Controller/Adminhtml/Shipment/PrintShippingLabel.php @@ -124,7 +124,7 @@ private function getLabels() $shippingAddress = $shipment->getShippingAddress(); try { - $this->barcodeHandler->prepareShipment($shipment->getId(), $shippingAddress->getCountryId()); + $this->barcodeHandler->prepareShipment($shipment->getId(), $shippingAddress->getCountryId(),false); } catch (LocalizedException $exception) { $this->messageManager->addErrorMessage( __('[POSTNL-0070] - Unable to generate barcode for shipment #%1', $shipment->getIncrementId()) diff --git a/Model/Shipment.php b/Model/Shipment.php index 2de325983..db301d81c 100644 --- a/Model/Shipment.php +++ b/Model/Shipment.php @@ -93,7 +93,9 @@ class Shipment extends AbstractModel implements ShipmentInterface const FIELD_DOWNPARTNER_BARCODE = 'downpartner_barcode'; - const FIELD_RETURN_BARCODE = 'return_barcode'; + const FIELD_RETURN_BARCODE = 'return_barcode'; + + const FIELD_IS_SMART_RETURN = 'is_smart_return'; /** * @var string @@ -960,4 +962,22 @@ public function getReturnBarcode() { return $this->getData(static::FIELD_RETURN_BARCODE); } + + /** + * @param $value + * + * @return $this + */ + public function setIsSmartReturn($value) + { + return $this->setData(static::FIELD_IS_SMART_RETURN, $value); + } + + /** + * @return array|bool|mixed|null + */ + public function getIsSmartReturn() + { + return $this->getData(static::FIELD_IS_SMART_RETURN); + } } diff --git a/Service/Api/ShipmentManagement/GenerateLabel.php b/Service/Api/ShipmentManagement/GenerateLabel.php index 055ce9482..f0eec7eba 100644 --- a/Service/Api/ShipmentManagement/GenerateLabel.php +++ b/Service/Api/ShipmentManagement/GenerateLabel.php @@ -77,7 +77,7 @@ public function generate($shipmentId) $shipment = $postnlShipment->getShipment(); $shippingAddress = $shipment->getShippingAddress(); - $this->barcodeHandler->prepareShipment($shipment->getId(), $shippingAddress->getCountryId()); + $this->barcodeHandler->prepareShipment($shipment->getId(), $shippingAddress->getCountryId(), false); $labels = $this->getLabels->get($shipment->getId(), false); if (empty($labels)) { diff --git a/Service/Handler/BarcodeHandler.php b/Service/Handler/BarcodeHandler.php index fc4e586ed..e9281c4c7 100644 --- a/Service/Handler/BarcodeHandler.php +++ b/Service/Handler/BarcodeHandler.php @@ -131,15 +131,20 @@ public function __construct( * * @throws LocalizedException */ - public function prepareShipment($magentoShipmentId, $countryId) + public function prepareShipment($magentoShipmentId, $countryId, $smartReturns) { $this->countryId = $countryId; + $shipment = $this->shipmentRepository->getByShipmentId($magentoShipmentId); + + if ($smartReturns) { + $shipment->setIsSmartReturn(true); + $this->shipmentRepository->save($shipment); + } if (!$this->validateShipment($magentoShipmentId, $countryId)) { return; } - $shipment = $this->shipmentRepository->getByShipmentId($magentoShipmentId); $magentoShipment = $shipment->getShipment(); $this->storeId = $magentoShipment->getStoreId(); @@ -290,6 +295,10 @@ private function validateShipment($magentoShipmentId, $countryId) return true; } + if ($shipment->getIsSmartReturn()) { + return true; + } + if ($isPrepared) { return false; } diff --git a/Service/Shipment/Data.php b/Service/Shipment/Data.php index 104b45a4b..574eed656 100644 --- a/Service/Shipment/Data.php +++ b/Service/Shipment/Data.php @@ -33,6 +33,7 @@ use TIG\PostNL\Api\Data\ShipmentInterface; use TIG\PostNL\Config\Provider\LabelAndPackingslipOptions; +use TIG\PostNL\Config\Provider\ReturnOptions; use TIG\PostNL\Service\Order\ProductInfo; use TIG\PostNL\Service\Volume\Items\Calculate; use TIG\PostNL\Webservices\Api\DeliveryDateFallback; @@ -70,6 +71,11 @@ class Data */ private $deliveryDateFallback; + /** + * @var ReturnOptions + */ + private $returnOptions; + /** * @param ProductOptions $productOptions * @param ContentDescription $contentDescription @@ -77,6 +83,7 @@ class Data * @param LabelAndPackingslipOptions $labelAndPackingslipOptions * @param Customs $customs * @param DeliveryDateFallback $deliveryDateFallback + * @param ReturnOptions $returnOptions */ public function __construct( ProductOptions $productOptions, @@ -84,7 +91,8 @@ public function __construct( Calculate $calculate, LabelAndPackingslipOptions $labelAndPackingslipOptions, Customs $customs, - DeliveryDateFallback $deliveryDateFallback + DeliveryDateFallback $deliveryDateFallback, + ReturnOptions $returnOptions ) { $this->productOptions = $productOptions; $this->contentDescription = $contentDescription; @@ -92,6 +100,7 @@ public function __construct( $this->labelAndPackingslipOptions = $labelAndPackingslipOptions; $this->customsInfo = $customs; $this->deliveryDateFallback = $deliveryDateFallback; + $this->returnOptions = $returnOptions; } /** @@ -209,6 +218,12 @@ private function setMandatoryShipmentData(ShipmentInterface $shipment, $currentS $shipmentData['ProductOptions'] = $productOptions; } + $smartReturnActive = $this->returnOptions->isSmartReturnActive(); + if ($smartReturnActive && $shipment->getIsSmartReturn()) { + $shipmentData['ProductOptions'] = $this->getSmartReturnOptions(); + $shipmentData['ProductCodeDelivery'] = '2285'; + } + return $shipmentData; } // @codingStandardsIgnoreEnd @@ -283,4 +298,14 @@ private function getGroupData(ShipmentInterface $shipment, $currentShipmentNumbe ] ]; } + + private function getSmartReturnOptions() + { + return [ + 'ProductOption' => [ + 'Characteristic' => '152', + 'Option' => '025' + ] + ]; + } } diff --git a/Setup/UpgradeSchema.php b/Setup/UpgradeSchema.php index 81cd003cd..ac04880df 100644 --- a/Setup/UpgradeSchema.php +++ b/Setup/UpgradeSchema.php @@ -103,6 +103,10 @@ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $con $this->upgradeSchemas($this->upgradeSchemaObjects['v1.9.1'], $setup, $context); } + if (version_compare($context->getVersion(), '1.12.3', '<')) { + $this->upgradeSchemas($this->upgradeSchemaObjects['v1.12.3'], $setup, $context); + } + $setup->endSetup(); } diff --git a/etc/adminhtml/system/postnlsettings/returns.xml b/etc/adminhtml/system/postnlsettings/returns.xml index 550e6b005..2b7ca18bf 100644 --- a/etc/adminhtml/system/postnlsettings/returns.xml +++ b/etc/adminhtml/system/postnlsettings/returns.xml @@ -101,4 +101,13 @@ required-entry + + + + Magento\Config\Model\Config\Source\Yesno + tig_postnl/returns/smart_returns_active + + 1 + + diff --git a/etc/di.xml b/etc/di.xml index edfb5c5eb..a7f0fb217 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -157,6 +157,11 @@ TIG\PostNL\Setup\V120\Schema\UpgradeOrderTable TIG\PostNL\Setup\V120\Schema\InstallMatrixRateTable + + TIG\PostNL\Setup\V1123\Schema\InstallSmartReturnBarcode + TIG\PostNL\Setup\V1123\Schema\InstallSmartReturnLabel + TIG\PostNL\Setup\V1123\Schema\InstallIsSmartReturn + diff --git a/etc/module.xml b/etc/module.xml index 6b49e65e9..22d411a18 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -32,7 +32,7 @@ * --> - + From c1841fb68607d07263a7ff8e1301289a29f5d925 Mon Sep 17 00:00:00 2001 From: Kevin van Dijk Date: Mon, 15 Aug 2022 10:20:27 +0200 Subject: [PATCH 05/72] POSTNLM2-1334 add smart returns --- Config/Source/Settings/FileTypeSettings.php | 58 ++++++++ .../Shipment/GetSmartReturnLabel.php | 139 ++++++++++++++++++ Setup/V1123/Schema/InstallIsSmartReturn.php | 56 +++++++ .../Schema/InstallSmartReturnBarcode.php | 57 +++++++ .../V1123/Schema/InstallSmartReturnLabel.php | 57 +++++++ view/frontend/web/js/view/form/fields.js | 15 +- 6 files changed, 376 insertions(+), 6 deletions(-) create mode 100644 Config/Source/Settings/FileTypeSettings.php create mode 100644 Controller/Adminhtml/Shipment/GetSmartReturnLabel.php create mode 100644 Setup/V1123/Schema/InstallIsSmartReturn.php create mode 100644 Setup/V1123/Schema/InstallSmartReturnBarcode.php create mode 100644 Setup/V1123/Schema/InstallSmartReturnLabel.php diff --git a/Config/Source/Settings/FileTypeSettings.php b/Config/Source/Settings/FileTypeSettings.php new file mode 100644 index 000000000..2b7088ad9 --- /dev/null +++ b/Config/Source/Settings/FileTypeSettings.php @@ -0,0 +1,58 @@ + static::PDF, 'label' => __('PDF')], + ['value' => static::ZPL200, 'label' => __('ZPL 200')], + ['value' => static::ZPL300, 'label' => __('ZPL 300')], + ['value' => static::ZPL600, 'label' => __('ZPL 600')], + ]; + // @codingStandardsIgnoreEnd + return $options; + } +} diff --git a/Controller/Adminhtml/Shipment/GetSmartReturnLabel.php b/Controller/Adminhtml/Shipment/GetSmartReturnLabel.php new file mode 100644 index 000000000..125e4233c --- /dev/null +++ b/Controller/Adminhtml/Shipment/GetSmartReturnLabel.php @@ -0,0 +1,139 @@ +shipmentRepository = $shipmentRepository; + } + + /** + * @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface|\Magento\Framework\Message\ManagerInterface + * @throws \Magento\Framework\Exception\InputException + * @throws \Magento\Framework\Exception\LocalizedException + * @throws \Magento\Framework\Exception\NoSuchEntityException + * @throws \Zend_Pdf_Exception + */ + public function execute() + { + $labels = $this->getLabels(); + + if (empty($labels)) { + $this->messageManager->addErrorMessage( + // @codingStandardsIgnoreLine + __('[POSTNL-0252] - There are no valid labels generated. Please check the logs for more information') + ); + + return $this->_redirect($this->_redirect->getRefererUrl()); + } + + return $this->getPdf->get($labels); + } + + /** + * @return \Magento\Sales\Api\Data\ShipmentInterface + * @throws \Magento\Framework\Exception\InputException + * @throws \Magento\Framework\Exception\NoSuchEntityException + */ + private function getShipment() + { + $shipmentId = $this->getRequest()->getParam('shipment_id'); + return $this->shipmentRepository->get($shipmentId); + } + + /** + * @return array|\Magento\Framework\Phrase|string|\TIG\PostNL\Api\Data\ShipmentLabelInterface + * @throws \Magento\Framework\Exception\InputException + * @throws \Magento\Framework\Exception\LocalizedException + * @throws \Magento\Framework\Exception\NoSuchEntityException + */ + private function getLabels() + { + $shipment = $this->getShipment(); + $shippingAddress = $shipment->getShippingAddress(); + + try { + $this->barcodeHandler->prepareShipment($shipment->getId(), $shippingAddress->getCountryId(), true); + } catch (LocalizedException $exception) { + $this->messageManager->addErrorMessage( + __('[POSTNL-0070] - Unable to generate barcode for shipment #%1', $shipment->getIncrementId()) + ); + return []; + } + + $labels = $this->getLabels->get($shipment->getId(), false); + + return $labels; + } +} diff --git a/Setup/V1123/Schema/InstallIsSmartReturn.php b/Setup/V1123/Schema/InstallIsSmartReturn.php new file mode 100644 index 000000000..b44d0f949 --- /dev/null +++ b/Setup/V1123/Schema/InstallIsSmartReturn.php @@ -0,0 +1,56 @@ + \Magento\Framework\DB\Ddl\Table::TYPE_BOOLEAN, + 'default' => 0, + 'nullable' => false, + 'comment' => 'Is Smart Return' + ]; + } +} diff --git a/Setup/V1123/Schema/InstallSmartReturnBarcode.php b/Setup/V1123/Schema/InstallSmartReturnBarcode.php new file mode 100644 index 000000000..bf5e39854 --- /dev/null +++ b/Setup/V1123/Schema/InstallSmartReturnBarcode.php @@ -0,0 +1,57 @@ + \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 'length' => 32, + 'nullable' => true, + 'default' => null, + 'comment' => 'Smart Return Barcode' + ]; + } +} diff --git a/Setup/V1123/Schema/InstallSmartReturnLabel.php b/Setup/V1123/Schema/InstallSmartReturnLabel.php new file mode 100644 index 000000000..84159cc4e --- /dev/null +++ b/Setup/V1123/Schema/InstallSmartReturnLabel.php @@ -0,0 +1,57 @@ + \Magento\Framework\DB\Ddl\Table::TYPE_BOOLEAN, + 'nullable' => false, + 'default' => 0, + 'comment' => 'Smart Return Label', + 'after' => 'return_label', + ]; + } +} diff --git a/view/frontend/web/js/view/form/fields.js b/view/frontend/web/js/view/form/fields.js index 864fa0e12..d15f4c8b8 100644 --- a/view/frontend/web/js/view/form/fields.js +++ b/view/frontend/web/js/view/form/fields.js @@ -193,18 +193,20 @@ define([ var self = this; var postcode; var housenumber; - + var country; var postcodeRegex = /^[1-9][0-9]{3} ?(?!sa|sd|ss)[a-z]{2}$/i; // Wait for the form to load, once loaded get the values of housenumber and postcode var fields = [ this.parentName + '.postcode-field-group.field-group.housenumber', - this.parentName + '.postcode-field-group.field-group.postcode' + this.parentName + '.postcode-field-group.field-group.postcode', + this.parentName + '.country_id' ]; - Registry.get(fields, function (housenumberElement, postcodeElement) { + Registry.get(fields, function (housenumberElement, postcodeElement, countryElement) { housenumber = housenumberElement.value(); postcode = postcodeElement.value(); + country = countryElement.value(); }); if (!postcode || !housenumber) { @@ -212,7 +214,7 @@ define([ } if ($.isNumeric(housenumber) && postcodeRegex.test(postcode)) { - return [housenumber, postcode]; + return [housenumber, postcode, country]; } if (self.request !== undefined || !postcodeRegex.test(postcode)) { @@ -235,7 +237,8 @@ define([ url: window.checkoutConfig.shipping.postnl.urls.address_postcode, data: { housenumber: formData[0], - postcode: formData[1] + postcode: formData[1], + country: formData[2] }, }).done(function (data) { self.handleResponse(data); @@ -322,7 +325,7 @@ define([ housenumberElement.value(''); additionElement.value(''); } - + // Next line is for initial load, before field is found in jQuery postcodeElement.additionalClasses['tig-postnl-full-width'] = (value !== 'NL'); From aabd654dffed9f9005464090df0979f40b25dbc7 Mon Sep 17 00:00:00 2001 From: Kevin van Dijk Date: Fri, 19 Aug 2022 14:10:32 +0200 Subject: [PATCH 06/72] POSTNLM2-1334 add smart return to shipment page --- Api/ShipmentManagementInterface.php | 3 +- Controller/Adminhtml/Order/Email.php | 136 ++++++++++++++++++ .../Shipment/GetSmartReturnLabel.php | 71 +++++---- Model/Mail/Template/TransportBuilder.php | 106 ++++++++++++++ Service/Api/ShipmentManagement.php | 4 +- .../Api/ShipmentManagement/GenerateLabel.php | 6 +- Service/Handler/BarcodeHandler.php | 4 - Service/Shipment/Labelling/GetLabels.php | 12 +- etc/di.xml | 1 + .../sales_order_shipment_grid.xml | 9 ++ 10 files changed, 300 insertions(+), 52 deletions(-) create mode 100644 Controller/Adminhtml/Order/Email.php create mode 100644 Model/Mail/Template/TransportBuilder.php diff --git a/Api/ShipmentManagementInterface.php b/Api/ShipmentManagementInterface.php index 14ed7c3cb..9d04302b6 100644 --- a/Api/ShipmentManagementInterface.php +++ b/Api/ShipmentManagementInterface.php @@ -57,11 +57,12 @@ public function cancelConfirm($shipmentId); * Generate a label for a PostNL shipment. * * @param int $shipmentId + * @param bool $smartReturns * * @api * @return string */ - public function generateLabel($shipmentId); + public function generateLabel($shipmentId, $smartReturns); /** * Create a PostNL shipment diff --git a/Controller/Adminhtml/Order/Email.php b/Controller/Adminhtml/Order/Email.php new file mode 100644 index 000000000..6b520bee6 --- /dev/null +++ b/Controller/Adminhtml/Order/Email.php @@ -0,0 +1,136 @@ +scopeConfig = $scopeConfig; + $this->storeManager = $storeManager; + $this->transportBuilder = $transportBuilder; + $this->logger = $logger; + $this->state = $state; + } + + public function sendEmail($shipment, $labels) + { + $shippingAddress = $shipment->getShippingAddress(); + $templateId = '7'; + $fromEmail = $this->scopeConfig->getValue('trans_email/ident_sales/email'); + $fromName = $this->scopeConfig->getValue('trans_email/ident_sales/name'); + $toEmail = $shippingAddress->getEmail(); + $fileContent = $this->getLabel($labels); + $fileName = 'test'; + + try { + $templateVars = [ + 'name' => $shippingAddress->getName(), + 'email' => $toEmail + ]; + + $storeId = $this->storeManager->getStore()->getId(); + + $from = ['email' => $fromEmail, 'name' => $fromName]; + $this->state->suspend(); + + $templateOptions = [ + 'area' => Area::AREA_FRONTEND, + 'store' => $storeId + ]; + $transport = $this->transportBuilder->setTemplateIdentifier($templateId) + ->setTemplateOptions($templateOptions) + ->setTemplateVars($templateVars) + ->setFrom($from) + ->addTo($toEmail) + ->getTransport(); + $attachmentPart = $this->transportBuilder->addAttachment($fileContent, $fileName); + $transport->getMessage()->getBody()->addPart($attachmentPart); + $transport->sendMessage(); + $this->state->resume(); + } catch (\Exception $e) { + $this->logger->critical($e->getMessage()); + } + } + + /** + * @param $labels + * + * @return mixed + */ + public function getLabel($labels) { + foreach ($labels as $key => $label) { + if ($label->getReturnLabel() === '1') { + $returnLabels[$key] = $label->getEntityId(); + } + } + $key = array_keys($returnLabels,max($returnLabels))[0]; + + return $labels[$key]->getLabel(); + } +} diff --git a/Controller/Adminhtml/Shipment/GetSmartReturnLabel.php b/Controller/Adminhtml/Shipment/GetSmartReturnLabel.php index 125e4233c..c73ff5f91 100644 --- a/Controller/Adminhtml/Shipment/GetSmartReturnLabel.php +++ b/Controller/Adminhtml/Shipment/GetSmartReturnLabel.php @@ -31,10 +31,11 @@ */ namespace TIG\PostNL\Controller\Adminhtml\Shipment; -use Magento\Framework\Exception\LocalizedException; use TIG\PostNL\Controller\Adminhtml\LabelAbstract; use Magento\Backend\App\Action\Context; use Magento\Sales\Model\Order\ShipmentRepository; +use TIG\PostNL\Controller\Adminhtml\Order\Email; +use TIG\PostNL\Service\Api\ShipmentManagement; use TIG\PostNL\Service\Shipment\Labelling\GetLabels; use TIG\PostNL\Controller\Adminhtml\PdfDownload as GetPdf; use TIG\PostNL\Helper\Tracking\Track; @@ -46,16 +47,24 @@ class GetSmartReturnLabel extends LabelAbstract /** @var ShipmentRepository */ private $shipmentRepository; + /** @var Email */ + private $email; + + /** @var ShipmentManagement */ + private $shipmentManagement; + /** - * PrintShippingLabel constructor. + * GetSmartReturnLabel constructor. * - * @param Context $context - * @param GetLabels $getLabels - * @param GetPdf $getPdf - * @param ShipmentRepository $shipmentRepository - * @param Track $track - * @param BarcodeHandler $barcodeHandler - * @param GetPackingslip $getPackingSlip + * @param Context $context + * @param GetLabels $getLabels + * @param GetPdf $getPdf + * @param ShipmentRepository $shipmentRepository + * @param Track $track + * @param BarcodeHandler $barcodeHandler + * @param GetPackingslip $getPackingSlip + * @param Email $email + * @param ShipmentManagement $shipmentManagement */ public function __construct( Context $context, @@ -64,7 +73,9 @@ public function __construct( ShipmentRepository $shipmentRepository, Track $track, BarcodeHandler $barcodeHandler, - GetPackingslip $getPackingSlip + GetPackingslip $getPackingSlip, + Email $email, + ShipmentManagement $shipmentManagement ) { parent::__construct( $context, @@ -76,18 +87,21 @@ public function __construct( ); $this->shipmentRepository = $shipmentRepository; + $this->email = $email; + $this->shipmentManagement = $shipmentManagement; } /** - * @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface|\Magento\Framework\Message\ManagerInterface + * @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface * @throws \Magento\Framework\Exception\InputException * @throws \Magento\Framework\Exception\LocalizedException * @throws \Magento\Framework\Exception\NoSuchEntityException - * @throws \Zend_Pdf_Exception */ public function execute() { - $labels = $this->getLabels(); + $magentoShipment = $this->getShipment(); + $this->shipmentManagement->generateLabel($magentoShipment->getId(), true); + $labels = $this->getLabels->get($magentoShipment->getId()); if (empty($labels)) { $this->messageManager->addErrorMessage( @@ -98,7 +112,11 @@ public function execute() return $this->_redirect($this->_redirect->getRefererUrl()); } - return $this->getPdf->get($labels); + // send email + $this->email->sendEmail($magentoShipment, $labels); + $this->messageManager->addSuccessMessage(__('Succesfully send out all Smart Return labels')); + + return $this->_redirect($this->_redirect->getRefererUrl()); } /** @@ -111,29 +129,4 @@ private function getShipment() $shipmentId = $this->getRequest()->getParam('shipment_id'); return $this->shipmentRepository->get($shipmentId); } - - /** - * @return array|\Magento\Framework\Phrase|string|\TIG\PostNL\Api\Data\ShipmentLabelInterface - * @throws \Magento\Framework\Exception\InputException - * @throws \Magento\Framework\Exception\LocalizedException - * @throws \Magento\Framework\Exception\NoSuchEntityException - */ - private function getLabels() - { - $shipment = $this->getShipment(); - $shippingAddress = $shipment->getShippingAddress(); - - try { - $this->barcodeHandler->prepareShipment($shipment->getId(), $shippingAddress->getCountryId(), true); - } catch (LocalizedException $exception) { - $this->messageManager->addErrorMessage( - __('[POSTNL-0070] - Unable to generate barcode for shipment #%1', $shipment->getIncrementId()) - ); - return []; - } - - $labels = $this->getLabels->get($shipment->getId(), false); - - return $labels; - } } diff --git a/Model/Mail/Template/TransportBuilder.php b/Model/Mail/Template/TransportBuilder.php new file mode 100644 index 000000000..5af54660c --- /dev/null +++ b/Model/Mail/Template/TransportBuilder.php @@ -0,0 +1,106 @@ +part = $part; + } + + public function addAttachment($content, $fileName) + { + $attachmentPart = new Part($content); + $attachmentPart->setType('application/pdf') + ->setFileName($fileName) + ->setDisposition(Mime::DISPOSITION_ATTACHMENT) + ->setEncoding(Mime::ENCODING_BASE64) + ->setCharset('utf-8'); + + return $attachmentPart; + } +} diff --git a/Service/Api/ShipmentManagement.php b/Service/Api/ShipmentManagement.php index 46793275b..e61304464 100644 --- a/Service/Api/ShipmentManagement.php +++ b/Service/Api/ShipmentManagement.php @@ -133,10 +133,10 @@ public function cancelConfirm($shipmentId) * @api * @return string */ - public function generateLabel($shipmentId) + public function generateLabel($shipmentId, $smartReturns) { try { - $result = $this->generateLabel->generate($shipmentId); + $result = $this->generateLabel->generate($shipmentId, $smartReturns); } catch (LocalizedException $exception) { // @codingStandardsIgnoreLine $this->logger->notice(__('Could not generate label for shipment #' . $shipmentId)); diff --git a/Service/Api/ShipmentManagement/GenerateLabel.php b/Service/Api/ShipmentManagement/GenerateLabel.php index f0eec7eba..3e2f25fe1 100644 --- a/Service/Api/ShipmentManagement/GenerateLabel.php +++ b/Service/Api/ShipmentManagement/GenerateLabel.php @@ -69,7 +69,7 @@ public function __construct( * @throws LocalizedException * @throws NoSuchEntityException */ - public function generate($shipmentId) + public function generate($shipmentId, $smartReturns = false) { $postnlShipment = $this->shipmentRepository->getByShipmentId($shipmentId); @@ -77,8 +77,8 @@ public function generate($shipmentId) $shipment = $postnlShipment->getShipment(); $shippingAddress = $shipment->getShippingAddress(); - $this->barcodeHandler->prepareShipment($shipment->getId(), $shippingAddress->getCountryId(), false); - $labels = $this->getLabels->get($shipment->getId(), false); + $this->barcodeHandler->prepareShipment($shipment->getId(), $shippingAddress->getCountryId(), $smartReturns); + $labels = $this->getLabels->get($shipment->getId(), false, true); if (empty($labels)) { return false; diff --git a/Service/Handler/BarcodeHandler.php b/Service/Handler/BarcodeHandler.php index e9281c4c7..319c808d1 100644 --- a/Service/Handler/BarcodeHandler.php +++ b/Service/Handler/BarcodeHandler.php @@ -295,10 +295,6 @@ private function validateShipment($magentoShipmentId, $countryId) return true; } - if ($shipment->getIsSmartReturn()) { - return true; - } - if ($isPrepared) { return false; } diff --git a/Service/Shipment/Labelling/GetLabels.php b/Service/Shipment/Labelling/GetLabels.php index bef699a2b..9d302969d 100644 --- a/Service/Shipment/Labelling/GetLabels.php +++ b/Service/Shipment/Labelling/GetLabels.php @@ -96,11 +96,12 @@ public function __construct( /** * @param $shipmentId * @param bool $confirm + * @param bool $smartReturn * * @return array|\Magento\Framework\Phrase|string|\TIG\PostNL\Api\Data\ShipmentLabelInterface * @throws \Magento\Framework\Exception\LocalizedException */ - public function get($shipmentId, $confirm = true) + public function get($shipmentId, $confirm = true, $smartReturn = false) { $shipment = $this->shipmentRepository->getByShipmentId($shipmentId); @@ -109,7 +110,7 @@ public function get($shipmentId, $confirm = true) } $this->labelValidator->validateProduct($shipment); - $labels = $this->getLabels($shipment, $confirm); + $labels = $this->getLabels($shipment, $confirm, $smartReturn); $labels = $this->labelValidator->validate($labels); $errors = $this->labelValidator->getErrors(); @@ -128,9 +129,14 @@ public function get($shipmentId, $confirm = true) * @return \Magento\Framework\Phrase|string|\TIG\PostNL\Api\Data\ShipmentLabelInterface * @throws \Magento\Framework\Exception\LocalizedException */ - private function getLabels(ShipmentInterface $shipment, $confirm) + private function getLabels(ShipmentInterface $shipment, $confirm, $smartReturn) { $labels = $this->shipmentLabelRepository->getByShipment($shipment); + + if ($smartReturn) { + $labels = null; + } + if ($labels && $confirm) { $this->confirmLabel->confirm($shipment); diff --git a/etc/di.xml b/etc/di.xml index a7f0fb217..ed88b6138 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -43,6 +43,7 @@ + diff --git a/view/adminhtml/ui_component/sales_order_shipment_grid.xml b/view/adminhtml/ui_component/sales_order_shipment_grid.xml index 4fc944176..7e1d2c67d 100644 --- a/view/adminhtml/ui_component/sales_order_shipment_grid.xml +++ b/view/adminhtml/ui_component/sales_order_shipment_grid.xml @@ -71,6 +71,15 @@ + + + + postnl_mass_send_smart_return_label + PostNL - Send Smart Return label + + + + From 7b612413b1048236289450791267095b8dea4bf6 Mon Sep 17 00:00:00 2001 From: Kevin van Dijk Date: Fri, 19 Aug 2022 14:11:15 +0200 Subject: [PATCH 07/72] add mass action --- .../Shipment/MassGetSmartReturnLabel.php | 145 ++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 Controller/Adminhtml/Shipment/MassGetSmartReturnLabel.php diff --git a/Controller/Adminhtml/Shipment/MassGetSmartReturnLabel.php b/Controller/Adminhtml/Shipment/MassGetSmartReturnLabel.php new file mode 100644 index 000000000..71c3d3097 --- /dev/null +++ b/Controller/Adminhtml/Shipment/MassGetSmartReturnLabel.php @@ -0,0 +1,145 @@ +email = $email; + $this->shipmentManagement = $shipmentManagement; + $this->collectionFactory = $collectionFactory; + $this->filter = $filter; + } + + /** + * @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface + * @throws \Magento\Framework\Exception\InputException + * @throws \Magento\Framework\Exception\LocalizedException + * @throws \Magento\Framework\Exception\NoSuchEntityException + */ + public function execute() + { + $magentoShipments = $this->getShipment(); + + foreach ($magentoShipments as $magentoShipment) { + $this->shipmentManagement->generateLabel($magentoShipment->getId(), true); + $labels = $this->getLabels->get($magentoShipment->getId()); + + if (empty($labels)) { + $this->messageManager->addErrorMessage( + // @codingStandardsIgnoreLine + __('[POSTNL-0252] - There are no valid labels generated. Please check the logs for more information') + ); + + return $this->_redirect($this->_redirect->getRefererUrl()); + } + + // send email + $this->email->sendEmail($magentoShipment, $labels); + } + + $this->messageManager->addSuccessMessage(__('Succesfully send out all Smart Return labels')); + + return $this->_redirect($this->_redirect->getRefererUrl()); + } + + /** + * @return \Magento\Framework\Data\Collection\AbstractDb + * @throws \Magento\Framework\Exception\InputException + * @throws \Magento\Framework\Exception\NoSuchEntityException + */ + private function getShipment() + { + $collection = $this->collectionFactory->create(); + $collection = $this->filter->getCollection($collection); + + return $collection; + } +} From 045b7ee3a1af12a35c1c9495fd1a584690af98b0 Mon Sep 17 00:00:00 2001 From: Kevin van Dijk Date: Mon, 5 Sep 2022 14:58:43 +0200 Subject: [PATCH 08/72] POSTNLM2-1334 check if there are already labels generated. --- Api/Data/ShipmentInterface.php | 11 +++ .../Shipment/GetSmartReturnLabel.php | 69 +++++++++++++------ .../Shipment/MassGetSmartReturnLabel.php | 69 ++++++++++++------- Model/Shipment.php | 20 ++++++ Model/ShipmentLabel.php | 12 +++- Service/Handler/BarcodeHandler.php | 5 ++ 6 files changed, 138 insertions(+), 48 deletions(-) diff --git a/Api/Data/ShipmentInterface.php b/Api/Data/ShipmentInterface.php index 38f78c0c1..38556153a 100644 --- a/Api/Data/ShipmentInterface.php +++ b/Api/Data/ShipmentInterface.php @@ -382,4 +382,15 @@ public function getIsSmartReturn(); * @return \TIG\PostNL\Api\Data\OrderInterface */ public function getPostNLOrder(); + + /** + * @param string $value + * @return \TIG\PostNL\Api\Data\ShipmentInterface + */ + public function setSmartReturnBarcode($value); + + /** + * @return string + */ + public function getSmartReturnBarcode(); } diff --git a/Controller/Adminhtml/Shipment/GetSmartReturnLabel.php b/Controller/Adminhtml/Shipment/GetSmartReturnLabel.php index c73ff5f91..ba98f74d2 100644 --- a/Controller/Adminhtml/Shipment/GetSmartReturnLabel.php +++ b/Controller/Adminhtml/Shipment/GetSmartReturnLabel.php @@ -31,6 +31,8 @@ */ namespace TIG\PostNL\Controller\Adminhtml\Shipment; +use TIG\PostNL\Api\ShipmentLabelRepositoryInterface; +use TIG\PostNL\Api\ShipmentRepositoryInterface; use TIG\PostNL\Controller\Adminhtml\LabelAbstract; use Magento\Backend\App\Action\Context; use Magento\Sales\Model\Order\ShipmentRepository; @@ -53,29 +55,39 @@ class GetSmartReturnLabel extends LabelAbstract /** @var ShipmentManagement */ private $shipmentManagement; + /** @var ShipmentLabel */ + private $shipmentLabel; + + /** @var ShipmentRepositoryInterface */ + private $shipmentRepositoryInterface; + /** * GetSmartReturnLabel constructor. * - * @param Context $context - * @param GetLabels $getLabels - * @param GetPdf $getPdf - * @param ShipmentRepository $shipmentRepository - * @param Track $track - * @param BarcodeHandler $barcodeHandler - * @param GetPackingslip $getPackingSlip - * @param Email $email - * @param ShipmentManagement $shipmentManagement + * @param Context $context + * @param GetLabels $getLabels + * @param GetPdf $getPdf + * @param ShipmentRepository $shipmentRepository + * @param Track $track + * @param BarcodeHandler $barcodeHandler + * @param GetPackingslip $getPackingSlip + * @param Email $email + * @param ShipmentManagement $shipmentManagement + * @param ShipmentLabelRepositoryInterface $shipmentLabel + * @param ShipmentRepositoryInterface $shipmentRepositoryInterface */ public function __construct( - Context $context, - GetLabels $getLabels, - GetPdf $getPdf, - ShipmentRepository $shipmentRepository, - Track $track, - BarcodeHandler $barcodeHandler, - GetPackingslip $getPackingSlip, - Email $email, - ShipmentManagement $shipmentManagement + Context $context, + GetLabels $getLabels, + GetPdf $getPdf, + ShipmentRepository $shipmentRepository, + Track $track, + BarcodeHandler $barcodeHandler, + GetPackingslip $getPackingSlip, + Email $email, + ShipmentManagement $shipmentManagement, + ShipmentLabelRepositoryInterface $shipmentLabel, + ShipmentRepositoryInterface $shipmentRepositoryInterface ) { parent::__construct( $context, @@ -86,9 +98,11 @@ public function __construct( $track ); - $this->shipmentRepository = $shipmentRepository; - $this->email = $email; - $this->shipmentManagement = $shipmentManagement; + $this->shipmentRepository = $shipmentRepository; + $this->email = $email; + $this->shipmentManagement = $shipmentManagement; + $this->shipmentLabel = $shipmentLabel; + $this->shipmentRepositoryInterface = $shipmentRepositoryInterface; } /** @@ -100,6 +114,18 @@ public function __construct( public function execute() { $magentoShipment = $this->getShipment(); + + //Check if there are already labels generated for this shipment. + $postnlShipment = $this->shipmentRepositoryInterface->getByShipmentId($magentoShipment->getId()); + $labels = $this->shipmentLabel->getByShipmentId($postnlShipment->getEntityId()); + + if ($labels) { + $this->email->sendEmail($magentoShipment, $labels); + $this->messageManager->addSuccessMessage(__('Succesfully send out all Smart Return labels')); + + return $this->_redirect($this->_redirect->getRefererUrl()); + } + $this->shipmentManagement->generateLabel($magentoShipment->getId(), true); $labels = $this->getLabels->get($magentoShipment->getId()); @@ -112,7 +138,6 @@ public function execute() return $this->_redirect($this->_redirect->getRefererUrl()); } - // send email $this->email->sendEmail($magentoShipment, $labels); $this->messageManager->addSuccessMessage(__('Succesfully send out all Smart Return labels')); diff --git a/Controller/Adminhtml/Shipment/MassGetSmartReturnLabel.php b/Controller/Adminhtml/Shipment/MassGetSmartReturnLabel.php index 71c3d3097..91a566bfe 100644 --- a/Controller/Adminhtml/Shipment/MassGetSmartReturnLabel.php +++ b/Controller/Adminhtml/Shipment/MassGetSmartReturnLabel.php @@ -31,6 +31,8 @@ */ namespace TIG\PostNL\Controller\Adminhtml\Shipment; +use TIG\PostNL\Api\ShipmentLabelRepositoryInterface; +use TIG\PostNL\Api\ShipmentRepositoryInterface; use TIG\PostNL\Controller\Adminhtml\LabelAbstract; use Magento\Backend\App\Action\Context; use TIG\PostNL\Controller\Adminhtml\Order\Email; @@ -57,31 +59,41 @@ class MassGetSmartReturnLabel extends LabelAbstract /** @var Filter */ private $filter; + /** @var ShipmentLabelRepositoryInterface */ + private $shipmentLabel; + + /** @var ShipmentRepositoryInterface */ + private $shipmentRepository; + /** * GetSmartReturnLabel constructor. * - * @param Context $context - * @param GetLabels $getLabels - * @param GetPdf $getPdf - * @param Track $track - * @param BarcodeHandler $barcodeHandler - * @param GetPackingslip $getPackingSlip - * @param Email $email - * @param ShipmentManagement $shipmentManagement - * @param ShipmentCollectionFactory $collectionFactory - * @param Filter $filter + * @param Context $context + * @param GetLabels $getLabels + * @param GetPdf $getPdf + * @param Track $track + * @param BarcodeHandler $barcodeHandler + * @param GetPackingslip $getPackingSlip + * @param Email $email + * @param ShipmentManagement $shipmentManagement + * @param ShipmentCollectionFactory $collectionFactory + * @param Filter $filter + * @param ShipmentLabelRepositoryInterface $shipmentLabel + * @param ShipmentRepositoryInterface $shipmentRepository */ public function __construct( - Context $context, - GetLabels $getLabels, - GetPdf $getPdf, - Track $track, - BarcodeHandler $barcodeHandler, - GetPackingslip $getPackingSlip, - Email $email, - ShipmentManagement $shipmentManagement, - ShipmentCollectionFactory $collectionFactory, - Filter $filter + Context $context, + GetLabels $getLabels, + GetPdf $getPdf, + Track $track, + BarcodeHandler $barcodeHandler, + GetPackingslip $getPackingSlip, + Email $email, + ShipmentManagement $shipmentManagement, + ShipmentCollectionFactory $collectionFactory, + Filter $filter, + ShipmentLabelRepositoryInterface $shipmentLabel, + ShipmentRepositoryInterface $shipmentRepository ) { parent::__construct( $context, @@ -94,8 +106,10 @@ public function __construct( $this->email = $email; $this->shipmentManagement = $shipmentManagement; - $this->collectionFactory = $collectionFactory; - $this->filter = $filter; + $this->collectionFactory = $collectionFactory; + $this->filter = $filter; + $this->shipmentLabel = $shipmentLabel; + $this->shipmentRepository = $shipmentRepository; } /** @@ -109,6 +123,16 @@ public function execute() $magentoShipments = $this->getShipment(); foreach ($magentoShipments as $magentoShipment) { + //Check if there are already labels generated for this shipment. + $postnlShipment = $this->shipmentRepository->getByShipmentId($magentoShipment->getId()); + $labels = $this->shipmentLabel->getByShipmentId($postnlShipment->getEntityId()); + + if ($labels) { + $this->email->sendEmail($magentoShipment, $labels); + $labels = null; + continue; + } + $this->shipmentManagement->generateLabel($magentoShipment->getId(), true); $labels = $this->getLabels->get($magentoShipment->getId()); @@ -121,7 +145,6 @@ public function execute() return $this->_redirect($this->_redirect->getRefererUrl()); } - // send email $this->email->sendEmail($magentoShipment, $labels); } diff --git a/Model/Shipment.php b/Model/Shipment.php index db301d81c..e0a2dbfd3 100644 --- a/Model/Shipment.php +++ b/Model/Shipment.php @@ -97,6 +97,8 @@ class Shipment extends AbstractModel implements ShipmentInterface const FIELD_IS_SMART_RETURN = 'is_smart_return'; + const FIELD_SMART_RETURN_BARCODE = 'smart_return_barcode'; + /** * @var string */ @@ -980,4 +982,22 @@ public function getIsSmartReturn() { return $this->getData(static::FIELD_IS_SMART_RETURN); } + + /** + * @param string + * + * @return $this + */ + public function setSmartReturnBarcode($value) + { + return $this->setData(static::FIELD_SMART_RETURN_BARCODE, $value); + } + + /** + * @return null|string + */ + public function getSmartReturnBarcode() + { + return $this->getData(static::FIELD_SMART_RETURN_BARCODE); + } } diff --git a/Model/ShipmentLabel.php b/Model/ShipmentLabel.php index 4d2733c51..0c762b8e1 100644 --- a/Model/ShipmentLabel.php +++ b/Model/ShipmentLabel.php @@ -55,11 +55,17 @@ class ShipmentLabel extends MagentoModel implements ShipmentLabelInterface // @codingStandardsIgnoreLine protected $_eventPrefix = 'tig_postnl_shipment_label'; - /** - * @var ShipmentRepositoryInterface - */ + /** @var ShipmentRepositoryInterface */ private $shipmentRepository; + /** + * @param Context $context + * @param Registry $registry + * @param ShipmentRepositoryInterface $shipmentRepository + * @param AbstractResource|null $resource + * @param AbstractDb|null $resourceCollection + * @param array $data + */ public function __construct( Context $context, Registry $registry, diff --git a/Service/Handler/BarcodeHandler.php b/Service/Handler/BarcodeHandler.php index 319c808d1..d4a98ecc7 100644 --- a/Service/Handler/BarcodeHandler.php +++ b/Service/Handler/BarcodeHandler.php @@ -199,6 +199,11 @@ public function addReturnBarcodes(ShipmentInterface $shipment) for ($count = 1; $count <= $parcelCount; $count++) { $returnBarcode = $this->generate($shipment, $isReturnBarcode); $this->createBarcode($shipment->getId(), $count, $returnBarcode, $isReturnBarcode); + + if ($shipment->getIsSmartReturn()) { + $shipment->setSmartReturnBarcode($returnBarcode); + } + $shipment->setReturnBarcode($returnBarcode); $this->shipmentRepository->save($shipment); } From 54b0c01de5d814ae5f5020fecb0574879f1c9803 Mon Sep 17 00:00:00 2001 From: Michiel Vonk Date: Wed, 7 Sep 2022 13:07:25 +0200 Subject: [PATCH 09/72] Added Attachment support --- Controller/Adminhtml/Order/Email.php | 5 +- Model/Mail/Template/TransportBuilder.php | 262 ++++++++++++++++++++--- 2 files changed, 230 insertions(+), 37 deletions(-) diff --git a/Controller/Adminhtml/Order/Email.php b/Controller/Adminhtml/Order/Email.php index 6b520bee6..2344f3d52 100644 --- a/Controller/Adminhtml/Order/Email.php +++ b/Controller/Adminhtml/Order/Email.php @@ -81,7 +81,7 @@ public function __construct( public function sendEmail($shipment, $labels) { $shippingAddress = $shipment->getShippingAddress(); - $templateId = '7'; + $templateId = '1'; $fromEmail = $this->scopeConfig->getValue('trans_email/ident_sales/email'); $fromName = $this->scopeConfig->getValue('trans_email/ident_sales/name'); $toEmail = $shippingAddress->getEmail(); @@ -107,10 +107,9 @@ public function sendEmail($shipment, $labels) ->setTemplateOptions($templateOptions) ->setTemplateVars($templateVars) ->setFrom($from) + ->addAttachment($fileContent, $fileName, 'text/pdf') ->addTo($toEmail) ->getTransport(); - $attachmentPart = $this->transportBuilder->addAttachment($fileContent, $fileName); - $transport->getMessage()->getBody()->addPart($attachmentPart); $transport->sendMessage(); $this->state->resume(); } catch (\Exception $e) { diff --git a/Model/Mail/Template/TransportBuilder.php b/Model/Mail/Template/TransportBuilder.php index 5af54660c..3e962ecde 100644 --- a/Model/Mail/Template/TransportBuilder.php +++ b/Model/Mail/Template/TransportBuilder.php @@ -29,53 +29,86 @@ * @copyright Copyright (c) Total Internet Group B.V. https://tig.nl/copyright * @license http://creativecommons.org/licenses/by-nc-nd/3.0/nl/deed.en_US */ + namespace TIG\PostNL\Model\Mail\Template; -use Laminas\Mime\Mime; -use Laminas\Mime\Part; +use Magento\Framework\App\TemplateTypesInterface; +use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Mail\AddressConverter; use Magento\Framework\Mail\EmailMessageInterfaceFactory; use Magento\Framework\Mail\MessageInterface; use Magento\Framework\Mail\MessageInterfaceFactory; +use Magento\Framework\Mail\MimeInterface; use Magento\Framework\Mail\MimeMessageInterfaceFactory; -use Magento\Framework\Mail\MimePart; use Magento\Framework\Mail\MimePartInterfaceFactory; use Magento\Framework\Mail\Template\FactoryInterface; use Magento\Framework\Mail\Template\SenderResolverInterface; use Magento\Framework\Mail\TransportInterfaceFactory; use Magento\Framework\ObjectManagerInterface; +use Magento\Framework\Phrase; +use Laminas\Mime\Mime; +use Laminas\Mime\Part; class TransportBuilder extends \Magento\Framework\Mail\Template\TransportBuilder { - /** @var Part */ - private $part; + /** + * @var string + */ + protected $templateIdentifier; /** - * @param FactoryInterface $templateFactory - * @param MessageInterface $message - * @param SenderResolverInterface $senderResolver - * @param ObjectManagerInterface $objectManager - * @param TransportInterfaceFactory $mailTransportFactory - * @param MessageInterfaceFactory|null $messageFactory - * @param EmailMessageInterfaceFactory|null $emailMessageInterfaceFactory - * @param MimeMessageInterfaceFactory|null $mimeMessageInterfaceFactory - * @param MimePartInterfaceFactory|null $mimePartInterfaceFactory - * @param AddressConverter|null $addressConverter - * @param Part $part + * @var $string */ + protected $templateModel; + + protected $templateVars; + + protected $templateOptions; + + protected $transport; + + protected $templateFactory; + + protected $objectManager; + + protected $message; + + protected $_senderResolver; + + protected $mailTransportFactory; + + private $messageData = []; + + protected $attachments = []; + + private $addressConverter; + + private $mimePartInterfaceFactory; + + private $mimeMessageInterfaceFactory; + + private $emailMessageInterfaceFactory; + public function __construct( - FactoryInterface $templateFactory, - MessageInterface $message, - SenderResolverInterface $senderResolver, - ObjectManagerInterface $objectManager, - TransportInterfaceFactory $mailTransportFactory, - MessageInterfaceFactory $messageFactory = null, - EmailMessageInterfaceFactory $emailMessageInterfaceFactory = null, - MimeMessageInterfaceFactory $mimeMessageInterfaceFactory = null, - MimePartInterfaceFactory $mimePartInterfaceFactory = null, - AddressConverter $addressConverter = null, - Part $part + FactoryInterface $templateFactory, + MessageInterface $message, + SenderResolverInterface $senderResolver, + ObjectManagerInterface $objectManager, + TransportInterfaceFactory $mailTransportFactory, + MessageInterfaceFactory $messageFactory, + EmailMessageInterfaceFactory $emailMessageInterfaceFactory, + MimeMessageInterfaceFactory $mimeMessageInterfaceFactory, + MimePartInterfaceFactory $mimePartInterfaceFactory, + AddressConverter $addressConverter ) { + $this->templateFactory = $templateFactory; + $this->objectManager = $objectManager; + $this->_senderResolver = $senderResolver; + $this->mailTransportFactory = $mailTransportFactory; + $this->addressConverter = $addressConverter; + $this->mimePartInterfaceFactory = $mimePartInterfaceFactory; + $this->mimeMessageInterfaceFactory = $mimeMessageInterfaceFactory; + parent::__construct( $templateFactory, $message, @@ -88,19 +121,180 @@ public function __construct( $mimePartInterfaceFactory, $addressConverter ); + $this->emailMessageInterfaceFactory = $emailMessageInterfaceFactory; + } + + /** @inheritDoc */ + public function addCc($address, $name = '') + { + $this->addAddressByType('cc', $address, $name); + + return $this; + } + + /** @inheritDoc */ + public function addTo($address, $name = '') + { + $this->addAddressByType('to', $address, $name); + + return $this; + } + + /** @inheritDoc */ + public function addBcc($address) + { + $this->addAddressByType('bcc', $address); + + return $this; + } + + /** @inheritDoc */ + public function setReplyTo($email, $name = null) + { + $this->addAddressByType('replyTo', $email, $name); + + return $this; + } + + /** @inheritDoc */ + public function setFrom($from) + { + return $this->setFromByScope($from); + } + + /** @inheritDoc */ + public function setFromByScope($from, $scopeId = null) + { + $result = $this->_senderResolver->resolve($from, $scopeId); + $this->addAddressByType('from', $result['email'], $result['name']); + + return $this; + } + + /** @inheritDoc */ + public function setTemplateIdentifier($templateIdentifier) + { + $this->templateIdentifier = $templateIdentifier; + + return $this; + } + + /** @inheritDoc */ + public function setTemplateModel($templateModel) + { + $this->templateModel = $templateModel; + + return $this; + } + + /** @inheritDoc */ + public function setTemplateVars($templateVars) + { + $this->templateVars = $templateVars; + + return $this; + } + + /** @inheritDoc */ + public function setTemplateOptions($templateOptions) + { + $this->templateOptions = $templateOptions; + + return $this; + } + + /** @inheritDoc */ + public function getTransport() + { + try { + $this->prepareMessage(); + $mailTransport = $this->mailTransportFactory->create(['message' => clone $this->message]); + } finally { + $this->reset(); + } + + return $mailTransport; + } + + /** @inheritDoc */ + protected function reset() + { + $this->messageData = []; + $this->templateIdentifier = null; + $this->templateVars = null; + $this->templateOptions = null; + + return $this; + } + + /** @inheritDoc */ + protected function getTemplate() + { + return $this->templateFactory->get($this->templateIdentifier, $this->templateModel) + ->setVars($this->templateVars) + ->setOptions($this->templateOptions); + } + + /** @inheritDoc */ + protected function prepareMessage() + { + $template = $this->getTemplate(); + $content = $template->processTemplate(); + switch ($template->getType()) { + case TemplateTypesInterface::TYPE_TEXT: + $part['type'] = MimeInterface::TYPE_TEXT; + break; + + case TemplateTypesInterface::TYPE_HTML: + $part['type'] = MimeInterface::TYPE_HTML; + break; + + default: + throw new LocalizedException( + new Phrase('Unknown template type') + ); + } + $mimePart = $this->mimePartInterfaceFactory->create(['content' => $content]); + $parts = count($this->attachments) ? array_merge([$mimePart], $this->attachments) : [$mimePart]; + $this->messageData['body'] = $this->mimeMessageInterfaceFactory->create( + ['parts' => $parts] + ); + + $this->messageData['subject'] = html_entity_decode( + (string) $template->getSubject(), + ENT_QUOTES + ); + $this->message = $this->emailMessageInterfaceFactory->create($this->messageData); + + return $this; + } + + private function addAddressByType($addressType, $email, $name = null): void + { + if (is_string($email)) { + $this->messageData[$addressType][] = $this->addressConverter->convert($email, $name); - $this->part = $part; + return; + } + $convertedAddressArray = $this->addressConverter->convertMany($email); + if (isset($this->messageData[$addressType])) { + $this->messageData[$addressType] = array_merge( + $this->messageData[$addressType], + $convertedAddressArray + ); + } } - public function addAttachment($content, $fileName) + public function addAttachment($content, $fileName, $fileType) { - $attachmentPart = new Part($content); - $attachmentPart->setType('application/pdf') + $attachmentPart = new Part(); + $attachmentPart->setContent($content) + ->setType($fileType) ->setFileName($fileName) ->setDisposition(Mime::DISPOSITION_ATTACHMENT) - ->setEncoding(Mime::ENCODING_BASE64) - ->setCharset('utf-8'); + ->setEncoding(Mime::ENCODING_BASE64); + $this->attachments[] = $attachmentPart; - return $attachmentPart; + return $this; } } From 91daea26ded920c0ab4b4ae0371d85b908390913 Mon Sep 17 00:00:00 2001 From: Michiel Vonk Date: Wed, 7 Sep 2022 13:32:34 +0200 Subject: [PATCH 10/72] Made TransportBuilder somewhat leaner --- Controller/Adminhtml/Order/Email.php | 8 +- Model/Mail/Template/TransportBuilder.php | 277 ++++------------------- 2 files changed, 50 insertions(+), 235 deletions(-) diff --git a/Controller/Adminhtml/Order/Email.php b/Controller/Adminhtml/Order/Email.php index 2344f3d52..5d10bd24b 100644 --- a/Controller/Adminhtml/Order/Email.php +++ b/Controller/Adminhtml/Order/Email.php @@ -86,7 +86,7 @@ public function sendEmail($shipment, $labels) $fromName = $this->scopeConfig->getValue('trans_email/ident_sales/name'); $toEmail = $shippingAddress->getEmail(); $fileContent = $this->getLabel($labels); - $fileName = 'test'; + $fileName = 'test.pdf'; try { $templateVars = [ @@ -107,7 +107,11 @@ public function sendEmail($shipment, $labels) ->setTemplateOptions($templateOptions) ->setTemplateVars($templateVars) ->setFrom($from) - ->addAttachment($fileContent, $fileName, 'text/pdf') + ->addAttachment( + base64_decode($fileContent), + 'text/pdf', + \Laminas\Mime\Mime::DISPOSITION_ATTACHMENT, \Laminas\Mime\Mime::ENCODING_BASE64, + 'test.pdf') ->addTo($toEmail) ->getTransport(); $transport->sendMessage(); diff --git a/Model/Mail/Template/TransportBuilder.php b/Model/Mail/Template/TransportBuilder.php index 3e962ecde..1130ab61f 100644 --- a/Model/Mail/Template/TransportBuilder.php +++ b/Model/Mail/Template/TransportBuilder.php @@ -32,269 +32,80 @@ namespace TIG\PostNL\Model\Mail\Template; -use Magento\Framework\App\TemplateTypesInterface; -use Magento\Framework\Exception\LocalizedException; -use Magento\Framework\Mail\AddressConverter; -use Magento\Framework\Mail\EmailMessageInterfaceFactory; -use Magento\Framework\Mail\MessageInterface; -use Magento\Framework\Mail\MessageInterfaceFactory; -use Magento\Framework\Mail\MimeInterface; -use Magento\Framework\Mail\MimeMessageInterfaceFactory; -use Magento\Framework\Mail\MimePartInterfaceFactory; -use Magento\Framework\Mail\Template\FactoryInterface; -use Magento\Framework\Mail\Template\SenderResolverInterface; -use Magento\Framework\Mail\TransportInterfaceFactory; -use Magento\Framework\ObjectManagerInterface; -use Magento\Framework\Phrase; use Laminas\Mime\Mime; use Laminas\Mime\Part; +use Laminas\Mime\Message as MimeMessage; +use Magento\Framework\Mail\MessageInterface; + class TransportBuilder extends \Magento\Framework\Mail\Template\TransportBuilder { /** - * @var string - */ - protected $templateIdentifier; - - /** - * @var $string + * @var array */ - protected $templateModel; - - protected $templateVars; - - protected $templateOptions; - - protected $transport; - - protected $templateFactory; - - protected $objectManager; - - protected $message; - - protected $_senderResolver; - - protected $mailTransportFactory; - - private $messageData = []; - - protected $attachments = []; - - private $addressConverter; - - private $mimePartInterfaceFactory; - - private $mimeMessageInterfaceFactory; - - private $emailMessageInterfaceFactory; - - public function __construct( - FactoryInterface $templateFactory, - MessageInterface $message, - SenderResolverInterface $senderResolver, - ObjectManagerInterface $objectManager, - TransportInterfaceFactory $mailTransportFactory, - MessageInterfaceFactory $messageFactory, - EmailMessageInterfaceFactory $emailMessageInterfaceFactory, - MimeMessageInterfaceFactory $mimeMessageInterfaceFactory, - MimePartInterfaceFactory $mimePartInterfaceFactory, - AddressConverter $addressConverter - ) { - $this->templateFactory = $templateFactory; - $this->objectManager = $objectManager; - $this->_senderResolver = $senderResolver; - $this->mailTransportFactory = $mailTransportFactory; - $this->addressConverter = $addressConverter; - $this->mimePartInterfaceFactory = $mimePartInterfaceFactory; - $this->mimeMessageInterfaceFactory = $mimeMessageInterfaceFactory; + private $parts = []; - parent::__construct( - $templateFactory, - $message, - $senderResolver, - $objectManager, - $mailTransportFactory, - $messageFactory, - $emailMessageInterfaceFactory, - $mimeMessageInterfaceFactory, - $mimePartInterfaceFactory, - $addressConverter - ); - $this->emailMessageInterfaceFactory = $emailMessageInterfaceFactory; - } - - /** @inheritDoc */ - public function addCc($address, $name = '') - { - $this->addAddressByType('cc', $address, $name); - - return $this; - } - - /** @inheritDoc */ - public function addTo($address, $name = '') - { - $this->addAddressByType('to', $address, $name); - - return $this; - } - - /** @inheritDoc */ - public function addBcc($address) - { - $this->addAddressByType('bcc', $address); - - return $this; - } - - /** @inheritDoc */ - public function setReplyTo($email, $name = null) - { - $this->addAddressByType('replyTo', $email, $name); - - return $this; - } - - /** @inheritDoc */ - public function setFrom($from) + protected function prepareMessage() { - return $this->setFromByScope($from); - } + parent::prepareMessage(); - /** @inheritDoc */ - public function setFromByScope($from, $scopeId = null) - { - $result = $this->_senderResolver->resolve($from, $scopeId); - $this->addAddressByType('from', $result['email'], $result['name']); + $mimeMessage = $this->getMimeMessage($this->message); - return $this; - } - - /** @inheritDoc */ - public function setTemplateIdentifier($templateIdentifier) - { - $this->templateIdentifier = $templateIdentifier; - - return $this; - } + foreach ($this->parts as $part) { + $mimeMessage->addPart($part); + } - /** @inheritDoc */ - public function setTemplateModel($templateModel) - { - $this->templateModel = $templateModel; + $this->message->setBody($mimeMessage); return $this; } - /** @inheritDoc */ - public function setTemplateVars($templateVars) - { - $this->templateVars = $templateVars; - + public function addAttachment( + $body, + $mimeType = Mime::TYPE_OCTETSTREAM, + $disposition = Mime::DISPOSITION_ATTACHMENT, + $encoding = Mime::ENCODING_BASE64, + $filename = null + ) { + $this->parts[] = $this->createMimePart($body, $mimeType, $disposition, $encoding, $filename); return $this; } - /** @inheritDoc */ - public function setTemplateOptions($templateOptions) - { - $this->templateOptions = $templateOptions; - - return $this; - } + private function createMimePart( + $content, + $type = Mime::TYPE_OCTETSTREAM, + $disposition = Mime::DISPOSITION_ATTACHMENT, + $encoding = Mime::ENCODING_BASE64, + $filename = null + ) { + $mimePart = new Part($content); + $mimePart->setType($type); + $mimePart->setDisposition($disposition); + $mimePart->setEncoding($encoding); - /** @inheritDoc */ - public function getTransport() - { - try { - $this->prepareMessage(); - $mailTransport = $this->mailTransportFactory->create(['message' => clone $this->message]); - } finally { - $this->reset(); + if ($filename) { + $mimePart->setFileName($filename); } - return $mailTransport; - } - - /** @inheritDoc */ - protected function reset() - { - $this->messageData = []; - $this->templateIdentifier = null; - $this->templateVars = null; - $this->templateOptions = null; - - return $this; + return $mimePart; } - /** @inheritDoc */ - protected function getTemplate() + private function getMimeMessage(MessageInterface $message) { - return $this->templateFactory->get($this->templateIdentifier, $this->templateModel) - ->setVars($this->templateVars) - ->setOptions($this->templateOptions); - } - - /** @inheritDoc */ - protected function prepareMessage() - { - $template = $this->getTemplate(); - $content = $template->processTemplate(); - switch ($template->getType()) { - case TemplateTypesInterface::TYPE_TEXT: - $part['type'] = MimeInterface::TYPE_TEXT; - break; - - case TemplateTypesInterface::TYPE_HTML: - $part['type'] = MimeInterface::TYPE_HTML; - break; + $body = $message->getBody(); - default: - throw new LocalizedException( - new Phrase('Unknown template type') - ); + if ($body instanceof MimeMessage) { + return $body; } - $mimePart = $this->mimePartInterfaceFactory->create(['content' => $content]); - $parts = count($this->attachments) ? array_merge([$mimePart], $this->attachments) : [$mimePart]; - $this->messageData['body'] = $this->mimeMessageInterfaceFactory->create( - ['parts' => $parts] - ); - $this->messageData['subject'] = html_entity_decode( - (string) $template->getSubject(), - ENT_QUOTES - ); - $this->message = $this->emailMessageInterfaceFactory->create($this->messageData); + /** @var MimeMessage $mimeMessage */ + $mimeMessage = new MimeMessage(); - return $this; - } - - private function addAddressByType($addressType, $email, $name = null): void - { - if (is_string($email)) { - $this->messageData[$addressType][] = $this->addressConverter->convert($email, $name); - - return; + if ($body) { + $mimePart = $this->createMimePart((string)$body, Mime::TYPE_TEXT, Mime::DISPOSITION_INLINE); + $mimeMessage->setParts([$mimePart]); } - $convertedAddressArray = $this->addressConverter->convertMany($email); - if (isset($this->messageData[$addressType])) { - $this->messageData[$addressType] = array_merge( - $this->messageData[$addressType], - $convertedAddressArray - ); - } - } - public function addAttachment($content, $fileName, $fileType) - { - $attachmentPart = new Part(); - $attachmentPart->setContent($content) - ->setType($fileType) - ->setFileName($fileName) - ->setDisposition(Mime::DISPOSITION_ATTACHMENT) - ->setEncoding(Mime::ENCODING_BASE64); - $this->attachments[] = $attachmentPart; - - return $this; + return $mimeMessage; } } From 3763388dc269a4eb3d26eb145f24b00f3eab0a27 Mon Sep 17 00:00:00 2001 From: Kevin van Dijk Date: Mon, 12 Sep 2022 17:08:38 +0200 Subject: [PATCH 11/72] smart returns refactor --- Controller/Adminhtml/Order/Email.php | 9 +++++---- .../Adminhtml/Shipment/GetSmartReturnLabel.php | 9 +++++++++ .../Adminhtml/Shipment/MassGetSmartReturnLabel.php | 12 +++++++++++- etc/email_templates.xml | 1 + 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/Controller/Adminhtml/Order/Email.php b/Controller/Adminhtml/Order/Email.php index 5d10bd24b..ac259ad27 100644 --- a/Controller/Adminhtml/Order/Email.php +++ b/Controller/Adminhtml/Order/Email.php @@ -32,6 +32,7 @@ namespace TIG\PostNL\Controller\Adminhtml\Order; +use Laminas\Mime\Mime; use Magento\Framework\App\Area; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\Mail\Template\TransportBuilder; @@ -81,12 +82,12 @@ public function __construct( public function sendEmail($shipment, $labels) { $shippingAddress = $shipment->getShippingAddress(); - $templateId = '1'; + $templateId = 'tig_postnl_smart_returns'; + $fileName = 'smart_return_label.pdf'; $fromEmail = $this->scopeConfig->getValue('trans_email/ident_sales/email'); $fromName = $this->scopeConfig->getValue('trans_email/ident_sales/name'); $toEmail = $shippingAddress->getEmail(); $fileContent = $this->getLabel($labels); - $fileName = 'test.pdf'; try { $templateVars = [ @@ -110,8 +111,8 @@ public function sendEmail($shipment, $labels) ->addAttachment( base64_decode($fileContent), 'text/pdf', - \Laminas\Mime\Mime::DISPOSITION_ATTACHMENT, \Laminas\Mime\Mime::ENCODING_BASE64, - 'test.pdf') + Mime::DISPOSITION_ATTACHMENT, Mime::ENCODING_BASE64, + $fileName) ->addTo($toEmail) ->getTransport(); $transport->sendMessage(); diff --git a/Controller/Adminhtml/Shipment/GetSmartReturnLabel.php b/Controller/Adminhtml/Shipment/GetSmartReturnLabel.php index ba98f74d2..047f3ed8f 100644 --- a/Controller/Adminhtml/Shipment/GetSmartReturnLabel.php +++ b/Controller/Adminhtml/Shipment/GetSmartReturnLabel.php @@ -118,6 +118,15 @@ public function execute() //Check if there are already labels generated for this shipment. $postnlShipment = $this->shipmentRepositoryInterface->getByShipmentId($magentoShipment->getId()); $labels = $this->shipmentLabel->getByShipmentId($postnlShipment->getEntityId()); + $isSmartReturn = $postnlShipment->getIsSmartReturn(); + + if (!$isSmartReturn) { + $this->messageManager->addErrorMessage( + __('Shipment with id: ' . $magentoShipment->getId() . ' is not a Smart Return shipment') + ); + + return $this->_redirect($this->_redirect->getRefererUrl()); + } if ($labels) { $this->email->sendEmail($magentoShipment, $labels); diff --git a/Controller/Adminhtml/Shipment/MassGetSmartReturnLabel.php b/Controller/Adminhtml/Shipment/MassGetSmartReturnLabel.php index 91a566bfe..91a384446 100644 --- a/Controller/Adminhtml/Shipment/MassGetSmartReturnLabel.php +++ b/Controller/Adminhtml/Shipment/MassGetSmartReturnLabel.php @@ -125,9 +125,19 @@ public function execute() foreach ($magentoShipments as $magentoShipment) { //Check if there are already labels generated for this shipment. $postnlShipment = $this->shipmentRepository->getByShipmentId($magentoShipment->getId()); + $isSmartReturn = $postnlShipment->getIsSmartReturn(); + + if (!$isSmartReturn) { + $this->messageManager->addErrorMessage( + __('Shipment with id: ' . $magentoShipment->getId() . ' is not a Smart Return shipment') + ); + + return $this->_redirect($this->_redirect->getRefererUrl()); + } + $labels = $this->shipmentLabel->getByShipmentId($postnlShipment->getEntityId()); - if ($labels) { + if ($labels && $isSmartReturn) { $this->email->sendEmail($magentoShipment, $labels); $labels = null; continue; diff --git a/etc/email_templates.xml b/etc/email_templates.xml index b5e4a724b..8bb248f2c 100644 --- a/etc/email_templates.xml +++ b/etc/email_templates.xml @@ -33,4 +33,5 @@ -->