From a516e1add02c13985b4dd87ea4980882325de9c9 Mon Sep 17 00:00:00 2001 From: "flavien.chantelot" Date: Wed, 8 Jan 2014 12:19:21 +0100 Subject: [PATCH 1/3] Optimization on Weee tax calculation Just load the needed weee attributs and avoid a sort. The difference is really big when you have a lot of attributs --- app/code/Magento/Weee/Model/Tax.php | 86 +++++++++++++++-------------- 1 file changed, 45 insertions(+), 41 deletions(-) diff --git a/app/code/Magento/Weee/Model/Tax.php b/app/code/Magento/Weee/Model/Tax.php index afcb6c630342e..4563598dcab96 100644 --- a/app/code/Magento/Weee/Model/Tax.php +++ b/app/code/Magento/Weee/Model/Tax.php @@ -47,6 +47,7 @@ class Tax extends \Magento\Core\Model\AbstractModel protected $_allAttributes = null; protected $_productDiscounts = array(); + protected $_weeeAttributes = null; /** * Weee data @@ -209,54 +210,57 @@ public function getProductWeeeAttributes( $discountPercent = $this->_getDiscountPercentForProduct($product); } - $productAttributes = $product->getTypeInstance()->getSetAttributes($product); - foreach ($productAttributes as $code => $attribute) { - if (in_array($code, $allWeee)) { + if($this->_weeeAttributes === null) { + $this->_weeeAttributes = $this->_attributeFactory->create() + ->getResourceCollection() + ->addFieldToFilter('attribute_code', array('in' => $allWeee)) + ; + } - $attributeSelect = $this->getResource()->getReadConnection()->select(); - $attributeSelect - ->from($this->getResource()->getTable('weee_tax'), 'value') - ->where('attribute_id = ?', (int)$attribute->getId()) - ->where('website_id IN(?)', array($websiteId, 0)) - ->where('country = ?', $rateRequest->getCountryId()) - ->where('state IN(?)', array($rateRequest->getRegionId(), '*')) - ->where('entity_id = ?', (int)$product->getId()) - ->limit(1); + foreach ($this->_weeeAttributes as $attribute) { + $attributeSelect = $this->getResource()->getReadConnection()->select(); + $attributeSelect + ->from($this->getResource()->getTable('weee_tax'), 'value') + ->where('attribute_id = ?', (int)$attribute->getId()) + ->where('website_id IN(?)', array($websiteId, 0)) + ->where('country = ?', $rateRequest->getCountryId()) + ->where('state IN(?)', array($rateRequest->getRegionId(), '*')) + ->where('entity_id = ?', (int)$product->getId()) + ->limit(1); - $order = array('state ' . \Magento\DB\Select::SQL_DESC, 'website_id ' . \Magento\DB\Select::SQL_DESC); - $attributeSelect->order($order); + $order = array('state ' . \Magento\DB\Select::SQL_DESC, 'website_id ' . \Magento\DB\Select::SQL_DESC); + $attributeSelect->order($order); - $value = $this->getResource()->getReadConnection()->fetchOne($attributeSelect); - if ($value) { - if ($discountPercent) { - $value = $this->_storeManager->getStore()->roundPrice($value-($value*$discountPercent/100)); - } + $value = $this->getResource()->getReadConnection()->fetchOne($attributeSelect); + if ($value) { + if ($discountPercent) { + $value = $this->_storeManager->getStore()->roundPrice($value-($value*$discountPercent/100)); + } - $taxAmount = $amount = 0; - $amount = $value; - if ($calculateTax && $this->_weeeData->isTaxable($store)) { - /** @var \Magento\Tax\Model\Calculation $calculator */ - $defaultPercent = $this->_calculationFactory->create() - ->getRate( - $defaultRateRequest->setProductClassId($product->getTaxClassId()) - ); - $currentPercent = $product->getTaxPercent(); - if ($this->_taxData->priceIncludesTax($store)) { - $taxAmount = $this->_storeManager->getStore() - ->roundPrice($value/(100+$defaultPercent)*$currentPercent); - } else { - $taxAmount = $this->_storeManager->getStore()->roundPrice($value*$defaultPercent/100); - } + $taxAmount = $amount = 0; + $amount = $value; + if ($calculateTax && $this->_weeeData->isTaxable($store)) { + /** @var \Magento\Tax\Model\Calculation $calculator */ + $defaultPercent = $this->_calculationFactory->create() + ->getRate( + $defaultRateRequest->setProductClassId($product->getTaxClassId()) + ); + $currentPercent = $product->getTaxPercent(); + if ($this->_taxData->priceIncludesTax($store)) { + $taxAmount = $this->_storeManager->getStore() + ->roundPrice($value/(100+$defaultPercent)*$currentPercent); + } else { + $taxAmount = $this->_storeManager->getStore()->roundPrice($value*$defaultPercent/100); } + } - $one = new \Magento\Object(); - $one->setName(__($attribute->getFrontend()->getLabel())) - ->setAmount($amount) - ->setTaxAmount($taxAmount) - ->setCode($attribute->getAttributeCode()); + $one = new \Magento\Object(); + $one->setName(__($attribute->getFrontend()->getLabel())) + ->setAmount($amount) + ->setTaxAmount($taxAmount) + ->setCode($attribute->getAttributeCode()); - $result[] = $one; - } + $result[] = $one; } } return $result; From b900007f8fa92f71c705791097656eb3c08f83c2 Mon Sep 17 00:00:00 2001 From: "flavien.chantelot" Date: Tue, 28 Oct 2014 18:22:41 +0100 Subject: [PATCH 2/3] Fix for issue #716. Wrong mimetype returned by magento image library --- .../Magento/Framework/Image/Adapter/AbstractAdapter.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/internal/Magento/Framework/Image/Adapter/AbstractAdapter.php b/lib/internal/Magento/Framework/Image/Adapter/AbstractAdapter.php index 2a7407a8dc5b8..41e9a24aa3eff 100644 --- a/lib/internal/Magento/Framework/Image/Adapter/AbstractAdapter.php +++ b/lib/internal/Magento/Framework/Image/Adapter/AbstractAdapter.php @@ -291,12 +291,12 @@ public function __construct(\Magento\Framework\Filesystem $filesystem, array $da * Assign image width, height, fileType and fileMimeType to object properties * using getimagesize function * - * @return int|null + * @return string */ public function getMimeType() { - if ($this->_fileType) { - return $this->_fileType; + if ($this->_fileMimeType) { + return $this->_fileMimeType; } else { list($this->_imageSrcWidth, $this->_imageSrcHeight, $this->_fileType, ) = getimagesize($this->_fileName); $this->_fileMimeType = image_type_to_mime_type($this->_fileType); From fc17fac31c8f2045251e2691684b23a5d0037b8a Mon Sep 17 00:00:00 2001 From: "flavien.chantelot" Date: Tue, 28 Oct 2014 18:33:44 +0100 Subject: [PATCH 3/3] Set back the right file --- app/code/Magento/Weee/Model/Tax.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Weee/Model/Tax.php b/app/code/Magento/Weee/Model/Tax.php index 745d45b52f52c..664502c723977 100644 --- a/app/code/Magento/Weee/Model/Tax.php +++ b/app/code/Magento/Weee/Model/Tax.php @@ -396,4 +396,4 @@ public function updateProductsDiscountPercent($products) $this->getResource()->updateProductsDiscountPercent($products); return $this; } -} \ No newline at end of file +}