From 1f2e563385477085807297f19f7e4555d53774ec Mon Sep 17 00:00:00 2001 From: Rodrigo Sanders Date: Mon, 16 Oct 2023 16:46:45 -0300 Subject: [PATCH] Altura, Largura e Comprimento MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. [](#fix) * https://github.com/eloom/module-correios-frete/issues/5 2. [](#add) * Mapeamento dos atributos Altura, Largura e Comprimento de forma dinâmica pelo Admin. --- CHANGELOG.md | 9 ++ .../Adminhtml/Config/Source/Attributes.php | 48 +++++++++++ .../Config/Source/FormatoEmbalagem.php | 2 +- .../Adminhtml/Config/Source/PesoEncomenda.php | 2 +- .../Adminhtml/Config/Source/Servicos.php | 2 +- .../Adminhtml/Config/Source/TaxaExtra.php | 2 +- src/module-correios-frete/Helper/Data.php | 2 +- src/module-correios-frete/Model/Carrier.php | 83 ++++++++++++++----- .../Setup/InstallData.php | 2 +- .../Setup/InstallSchema.php | 2 +- .../etc/adminhtml/system.xml | 17 ++++ src/module-correios-frete/etc/config.xml | 6 +- src/module-correios-frete/etc/module.xml | 2 +- 13 files changed, 148 insertions(+), 31 deletions(-) create mode 100644 src/module-correios-frete/Block/Adminhtml/Config/Source/Attributes.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 4200525..22132eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,12 @@ +# 2.0.2 +## 16/10/2023 + +1. [](#fix) + * https://github.com/eloom/module-correios-frete/issues/5 + +2. [](#add) + * Mapeamento dos atributos Altura, Largura e Comprimento de forma dinâmica pelo Admin. + # 2.0.1 ## 21/09/2023 diff --git a/src/module-correios-frete/Block/Adminhtml/Config/Source/Attributes.php b/src/module-correios-frete/Block/Adminhtml/Config/Source/Attributes.php new file mode 100644 index 0000000..fe81771 --- /dev/null +++ b/src/module-correios-frete/Block/Adminhtml/Config/Source/Attributes.php @@ -0,0 +1,48 @@ +attributeFactory = $attributeFactory; + } + + public function toOptionArray() { + $attributes = $this->attributeFactory->getCollection(); + + $options = []; + $options[] = ['value' => '', 'label' => 'Selecione']; + + foreach ($attributes as $attribute) { + $front = $attribute->getFrontendLabel(); + + if (!empty($front)) { + $options[] = ['value' => $attribute->getAttributecode(), 'label' => $attribute->getAttributecode()]; + } else { + $options[] = ['value' => $attribute->getAttributecode(), 'label' => $attribute->getAttributecode()]; + } + } + + sort($options); + + return $options; + } +} \ No newline at end of file diff --git a/src/module-correios-frete/Block/Adminhtml/Config/Source/FormatoEmbalagem.php b/src/module-correios-frete/Block/Adminhtml/Config/Source/FormatoEmbalagem.php index 1e6b4ad..b0591dd 100644 --- a/src/module-correios-frete/Block/Adminhtml/Config/Source/FormatoEmbalagem.php +++ b/src/module-correios-frete/Block/Adminhtml/Config/Source/FormatoEmbalagem.php @@ -6,7 +6,7 @@ * @category elOOm * @package Modulo Frete com Correios * @copyright Copyright (c) 2023 elOOm (https://eloom.tech) -* @version 2.0.1 +* @version 2.0.2 * @license https://opensource.org/licenses/OSL-3.0 * @license https://opensource.org/licenses/AFL-3.0 * diff --git a/src/module-correios-frete/Block/Adminhtml/Config/Source/PesoEncomenda.php b/src/module-correios-frete/Block/Adminhtml/Config/Source/PesoEncomenda.php index de65790..88e1abc 100644 --- a/src/module-correios-frete/Block/Adminhtml/Config/Source/PesoEncomenda.php +++ b/src/module-correios-frete/Block/Adminhtml/Config/Source/PesoEncomenda.php @@ -6,7 +6,7 @@ * @category elOOm * @package Modulo Frete com Correios * @copyright Copyright (c) 2023 elOOm (https://eloom.tech) -* @version 2.0.1 +* @version 2.0.2 * @license https://opensource.org/licenses/OSL-3.0 * @license https://opensource.org/licenses/AFL-3.0 * diff --git a/src/module-correios-frete/Block/Adminhtml/Config/Source/Servicos.php b/src/module-correios-frete/Block/Adminhtml/Config/Source/Servicos.php index 65aa2c3..43aa4fc 100644 --- a/src/module-correios-frete/Block/Adminhtml/Config/Source/Servicos.php +++ b/src/module-correios-frete/Block/Adminhtml/Config/Source/Servicos.php @@ -6,7 +6,7 @@ * @category elOOm * @package Modulo Frete com Correios * @copyright Copyright (c) 2023 elOOm (https://eloom.tech) -* @version 2.0.1 +* @version 2.0.2 * @license https://opensource.org/licenses/OSL-3.0 * @license https://opensource.org/licenses/AFL-3.0 * diff --git a/src/module-correios-frete/Block/Adminhtml/Config/Source/TaxaExtra.php b/src/module-correios-frete/Block/Adminhtml/Config/Source/TaxaExtra.php index fbe6eb7..8306694 100644 --- a/src/module-correios-frete/Block/Adminhtml/Config/Source/TaxaExtra.php +++ b/src/module-correios-frete/Block/Adminhtml/Config/Source/TaxaExtra.php @@ -6,7 +6,7 @@ * @category elOOm * @package Modulo Frete com Correios * @copyright Copyright (c) 2023 elOOm (https://eloom.tech) -* @version 2.0.1 +* @version 2.0.2 * @license https://opensource.org/licenses/OSL-3.0 * @license https://opensource.org/licenses/AFL-3.0 * diff --git a/src/module-correios-frete/Helper/Data.php b/src/module-correios-frete/Helper/Data.php index cfbdc1a..542e1d8 100644 --- a/src/module-correios-frete/Helper/Data.php +++ b/src/module-correios-frete/Helper/Data.php @@ -6,7 +6,7 @@ * @category elOOm * @package Modulo Frete com Correios * @copyright Copyright (c) 2023 elOOm (https://eloom.tech) -* @version 2.0.1 +* @version 2.0.2 * @license https://opensource.org/licenses/OSL-3.0 * @license https://opensource.org/licenses/AFL-3.0 * diff --git a/src/module-correios-frete/Model/Carrier.php b/src/module-correios-frete/Model/Carrier.php index 4231d21..d6403de 100644 --- a/src/module-correios-frete/Model/Carrier.php +++ b/src/module-correios-frete/Model/Carrier.php @@ -6,7 +6,7 @@ * @category elOOm * @package Modulo Frete com Correios * @copyright Copyright (c) 2023 elOOm (https://eloom.tech) -* @version 2.0.1 +* @version 2.0.2 * @license https://opensource.org/licenses/OSL-3.0 * @license https://opensource.org/licenses/AFL-3.0 * @@ -19,7 +19,7 @@ use Eloom\SdkCorreios\Errors; use Eloom\SdkCorreios\Endpoints\Rastro; use Eloom\SdkCorreios\Exceptions\UnauthorizedException; - +use Eloom\SdkCorreios\Exceptions\CorreiosException; use Magento\Catalog\Model\Product; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Quote\Model\Quote\Address\RateRequest; @@ -40,9 +40,9 @@ class Carrier extends AbstractCarrierOnline implements CarrierInterface { private $fromZip = null; private $toZip = null; private $hasFreeMethod = false; - private $nVlComprimento = 0; - private $nVlAltura = 0; - private $nVlLargura = 0; + private $totalLength = 0; + private $totalHeight = 0; + private $totalWidth = 0; /** * Rate result data @@ -145,39 +145,67 @@ private function check(RateRequest $request) { return false; } + $weightAttr = $this->getConfigData('weight'); + + $widthAttr = $this->getConfigData('width'); + $heightAttr = $this->getConfigData('height'); + $lengthAttr = $this->getConfigData('length'); + + $defaultHeight = $this->getConfigData('default_height'); + $defaultWidth = $this->getConfigData('default_width'); + $defaultLength = $this->getConfigData('default_length'); + $price = 0; $weight = 0; - if ($request->getPackageValue() > 0 && $request->getPackageWeight() > 0) { - $price = $request->getPackageValue(); - $weight = $request->getPackageWeight(); - } else if ($request->getAllItems()) { + + $width = 0; + $height = 0; + $length = 0; + + if ($request->getAllItems()) { $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); foreach ($request->getAllItems() as $item) { if ($item->getProduct()->isVirtual()) { continue; } - if ($item->getHasChildren() && $item->isShipSeparately()) { + if ($item->getHasChildren()) { foreach ($item->getChildren() as $child) { - if ($child->getFreeShipping() && !$child->getProduct()->isVirtual()) { - $product = $objectManager->create(Product::class)->load($child->getProductId()); - - $price += (float)(!is_null($product->getData('special_price')) ? $product->getData('special_price') : $product->getData('price')); - $weight += (float)$product->getData('weight'); + if (!$child->getProduct()->isVirtual()) { + $product = $objectManager->create('Magento\Catalog\Model\Product')->load($child->getProductId()); + + $price += ($item->getPrice() - $item->getDiscountAmount()); + $parentIds = $objectManager->create('Magento\GroupedProduct\Model\Product\Type\Grouped')->getParentIdsByChild($product->getId()); + if (!$parentIds) { + $parentIds = $objectManager->create('Magento\ConfigurableProduct\Model\Product\Type\Configurable')->getParentIdsByChild($product->getId()); + + if ($parentIds) { + $parentProd = $objectManager->create('Magento\Catalog\Model\Product')->load($parentIds[0]); + + $weight += (float)$parentProd->getData($weightAttr); + $width += ($parentProd->getData($widthAttr) > 0 ? $parentProd->getData($widthAttr) : $defaultWidth); + $height += ($parentProd->getData($heightAttr) > 0 ? $parentProd->getData($heightAttr) : $defaultHeight); + $length += ($parentProd->getData($lengthAttr) > 0 ? $parentProd->getData($lengthAttr) : $defaultLength); + } + } } } } else { $product = $objectManager->create(Product::class)->load($item->getProductId()); if ($product->getTypeId() == 'simple') { $price += (float)(!is_null($product->getData('special_price')) ? $product->getData('special_price') : $product->getData('price')); - $weight += (float)$product->getData('weight'); + + $weight += (float)$product->getData($weightAttr); + $width += ($product->getData($widthAttr) > 0 ? $product->getData($widthAttr) : $defaultWidth); + $height += ($product->getData($heightAttr) > 0 ? $product->getData($heightAttr) : $defaultHeight); + $length += ($product->getData($lengthAttr) > 0 ? $product->getData($lengthAttr) : $defaultLength); } } } } - $this->nVlAltura = $this->getConfigData('default_height'); - $this->nVlLargura = $this->getConfigData('default_width'); - $this->nVlComprimento = $this->getConfigData('default_length'); + $this->totalHeight = $height; + $this->totalWidth = $width; + $this->totalLength = $length; $this->hasFreeMethod = $request->getFreeShipping(); $this->_freeMethod = $this->getConfigData('servico_gratuito'); @@ -376,7 +404,6 @@ private function calcPrecoPrazo() { $prazoClient->withProduct($codigoServicos); } $prazos = $prazoClient->withCepOrigem($this->fromZip)->withCepDestino($this->toZip)->nacional(); - /** * Preço @@ -391,7 +418,7 @@ private function calcPrecoPrazo() { $precoClient->withProduct($codigoServicos); } $precoClient->withCepOrigem($this->fromZip)->withCepDestino($this->toZip); - $precoClient->withDiametro(0)->withAltura($this->nVlAltura)->withLargura($this->nVlLargura)->withComprimento($this->nVlComprimento); + $precoClient->withDiametro(0)->withAltura($this->totalHeight)->withLargura($this->totalWidth)->withComprimento($this->totalLength); $nVlPeso = 0; if ($this->volumeWeight > $this->getConfigData('volume_weight_min') && $this->volumeWeight > $this->packageWeight) { @@ -406,7 +433,19 @@ private function calcPrecoPrazo() { } $precoClient->withTpObjeto($this->getConfigData('cd_formato')); - $precos = $precoClient->nacional(); + try { + $precos = $precoClient->nacional(); + } catch (CorreiosException $exception) { + $this->logger->critical($exception->getMessage()); + + $rate = $this->_rateErrorFactory->create(); + $rate->setCarrier($this->_code); + $rate->setCarrierTitle($this->getConfigData('title')); + $rate->setErrorMessage($exception->getMessage()); + $this->getRateResult()->append($rate); + + return $this->getRateResult(); + } /** * Merge diff --git a/src/module-correios-frete/Setup/InstallData.php b/src/module-correios-frete/Setup/InstallData.php index 5a4588e..761cad6 100644 --- a/src/module-correios-frete/Setup/InstallData.php +++ b/src/module-correios-frete/Setup/InstallData.php @@ -6,7 +6,7 @@ * @category elOOm * @package Modulo Frete com Correios * @copyright Copyright (c) 2023 elOOm (https://eloom.tech) -* @version 2.0.1 +* @version 2.0.2 * @license https://opensource.org/licenses/OSL-3.0 * @license https://opensource.org/licenses/AFL-3.0 * diff --git a/src/module-correios-frete/Setup/InstallSchema.php b/src/module-correios-frete/Setup/InstallSchema.php index 31ff28f..0ff8b16 100644 --- a/src/module-correios-frete/Setup/InstallSchema.php +++ b/src/module-correios-frete/Setup/InstallSchema.php @@ -6,7 +6,7 @@ * @category elOOm * @package Modulo Frete com Correios * @copyright Copyright (c) 2023 elOOm (https://eloom.tech) -* @version 2.0.1 +* @version 2.0.2 * @license https://opensource.org/licenses/OSL-3.0 * @license https://opensource.org/licenses/AFL-3.0 * diff --git a/src/module-correios-frete/etc/adminhtml/system.xml b/src/module-correios-frete/etc/adminhtml/system.xml index f13d929..f9fd458 100644 --- a/src/module-correios-frete/etc/adminhtml/system.xml +++ b/src/module-correios-frete/etc/adminhtml/system.xml @@ -126,6 +126,23 @@ required-entry validate-digits + + + Magento\Config\Block\System\Config\Form\Field\Heading + + + + Eloom\CorreiosFrete\Block\Adminhtml\Config\Source\Attributes + + + + Eloom\CorreiosFrete\Block\Adminhtml\Config\Source\Attributes + + + + Eloom\CorreiosFrete\Block\Adminhtml\Config\Source\Attributes + + Magento\Config\Model\Config\Source\Yesno diff --git a/src/module-correios-frete/etc/config.xml b/src/module-correios-frete/etc/config.xml index 3e2e396..1e79cf4 100644 --- a/src/module-correios-frete/etc/config.xml +++ b/src/module-correios-frete/etc/config.xml @@ -15,14 +15,18 @@ 0 0 0 + 11 2 16 - 11 1 0 0 0 + volume_largura + volume_altura + volume_comprimento + weight 5 15 diff --git a/src/module-correios-frete/etc/module.xml b/src/module-correios-frete/etc/module.xml index f4ad685..7d87b3d 100644 --- a/src/module-correios-frete/etc/module.xml +++ b/src/module-correios-frete/etc/module.xml @@ -1,6 +1,6 @@ - +