From f90a7150d07fa21d98e3c8fb650048a5f9513611 Mon Sep 17 00:00:00 2001
From: Anton Kaplya <akaplya@ebay.com>
Date: Wed, 13 May 2015 21:34:13 +0300
Subject: [PATCH] MAGETWO-36882: Stabilizing branch

---
 .../Magento/Reports/Model/Resource/Helper.php |  8 +-
 .../Model/Resource/HelperInterface.php        | 13 +--
 .../Model/Resource/Report/Product/Viewed.php  | 26 ++++--
 .../Magento/Sales/Model/Resource/Helper.php   | 18 ++++-
 .../Model/Resource/Report/Bestsellers.php     | 79 +------------------
 app/code/Magento/Sales/etc/di.xml             | 10 +++
 6 files changed, 62 insertions(+), 92 deletions(-)

diff --git a/app/code/Magento/Reports/Model/Resource/Helper.php b/app/code/Magento/Reports/Model/Resource/Helper.php
index 08387fcd26d..28c78260fb2 100644
--- a/app/code/Magento/Reports/Model/Resource/Helper.php
+++ b/app/code/Magento/Reports/Model/Resource/Helper.php
@@ -39,15 +39,15 @@ class Helper extends \Magento\Framework\DB\Helper implements \Magento\Reports\Mo
     /**
      * Update rating position
      *
-     * @param string $type day|month|year
+     * @param string $adapter
+     * @param string $type
      * @param string $column
      * @param string $mainTable
-     * @param string $aggregationTable
+     * @param $aggregationTable
      * @return $this
      */
-    public function updateReportRatingPos($type, $column, $mainTable, $aggregationTable)
+    public function updateReportRatingPos($adapter, $type, $column, $mainTable, $aggregationTable)
     {
-        $adapter = $this->_getWriteAdapter();
         $periodSubSelect = $adapter->select();
         $ratingSubSelect = $adapter->select();
         $ratingSelect = $adapter->select();
diff --git a/app/code/Magento/Reports/Model/Resource/HelperInterface.php b/app/code/Magento/Reports/Model/Resource/HelperInterface.php
index efac474d6d5..9a836b23496 100644
--- a/app/code/Magento/Reports/Model/Resource/HelperInterface.php
+++ b/app/code/Magento/Reports/Model/Resource/HelperInterface.php
@@ -24,11 +24,12 @@ interface HelperInterface
     /**
      * Update rating position
      *
-     * @param string $type day|month|year
-     * @param string $column
-     * @param string $mainTable
-     * @param string $aggregationTable
-     * @return \Magento\Framework\DB\Helper\AbstractHelper
+     * @param $adapter
+     * @param $type
+     * @param $column
+     * @param $mainTable
+     * @param $aggregationTable
+     * @return mixed
      */
-    public function updateReportRatingPos($type, $column, $mainTable, $aggregationTable);
+    public function updateReportRatingPos($adapter, $type, $column, $mainTable, $aggregationTable);
 }
diff --git a/app/code/Magento/Reports/Model/Resource/Report/Product/Viewed.php b/app/code/Magento/Reports/Model/Resource/Report/Product/Viewed.php
index 9ebd26efbd7..74e6f7c80d6 100644
--- a/app/code/Magento/Reports/Model/Resource/Report/Product/Viewed.php
+++ b/app/code/Magento/Reports/Model/Resource/Report/Product/Viewed.php
@@ -222,12 +222,28 @@ class Viewed extends \Magento\Sales\Model\Resource\Report\AbstractReport
         $insertQuery = $select->insertFromSelect($this->getMainTable(), array_keys($columns));
         $adapter->query($insertQuery);
 
-        $this->_resourceHelper->updateReportRatingPos('day', 'views_num', $mainTable, $this->getTable(self::AGGREGATION_DAILY));
-        $this->_resourceHelper->updateReportRatingPos('month', 'views_num', $mainTable, $this->getTable(self::AGGREGATION_MONTHLY));
-        $this->_resourceHelper->updateReportRatingPos('year', 'views_num', $mainTable, $this->getTable(self::AGGREGATION_YEARLY));
-
+        $this->_resourceHelper->updateReportRatingPos(
+            $adapter,
+            'day',
+            'views_num',
+            $mainTable,
+            $this->getTable(self::AGGREGATION_DAILY)
+        );
+        $this->_resourceHelper->updateReportRatingPos(
+            $adapter,
+            'month',
+            'views_num',
+            $mainTable,
+            $this->getTable(self::AGGREGATION_MONTHLY)
+        );
+        $this->_resourceHelper->updateReportRatingPos(
+            $adapter,
+            'year',
+            'views_num',
+            $mainTable,
+            $this->getTable(self::AGGREGATION_YEARLY)
+        );
         $this->_setFlagData(\Magento\Reports\Model\Flag::REPORT_PRODUCT_VIEWED_FLAG_CODE);
-
         return $this;
     }
 }
diff --git a/app/code/Magento/Sales/Model/Resource/Helper.php b/app/code/Magento/Sales/Model/Resource/Helper.php
index 958b798694b..d5aa62d239d 100644
--- a/app/code/Magento/Sales/Model/Resource/Helper.php
+++ b/app/code/Magento/Sales/Model/Resource/Helper.php
@@ -44,17 +44,31 @@ class Helper extends \Magento\Framework\DB\Helper implements HelperInterface
         $mainTable,
         $aggregationTable
     ) {
+        $adapter = $this->_resource->getConnection('sales_write');
         if ($aggregation == $aggregationAliases['monthly']) {
             $this->_reportsResourceHelper->updateReportRatingPos(
+                $adapter,
                 'month',
                 'qty_ordered',
                 $mainTable,
                 $aggregationTable
             );
         } elseif ($aggregation == $aggregationAliases['yearly']) {
-            $this->_reportsResourceHelper->updateReportRatingPos('year', 'qty_ordered', $mainTable, $aggregationTable);
+            $this->_reportsResourceHelper->updateReportRatingPos(
+                $adapter,
+                'year',
+                'qty_ordered',
+                $mainTable,
+                $aggregationTable
+            );
         } else {
-            $this->_reportsResourceHelper->updateReportRatingPos('day', 'qty_ordered', $mainTable, $aggregationTable);
+            $this->_reportsResourceHelper->updateReportRatingPos(
+                $adapter,
+                'day',
+                'qty_ordered',
+                $mainTable,
+                $aggregationTable
+            );
         }
 
         return $this;
diff --git a/app/code/Magento/Sales/Model/Resource/Report/Bestsellers.php b/app/code/Magento/Sales/Model/Resource/Report/Bestsellers.php
index 43d3b691107..26926615896 100644
--- a/app/code/Magento/Sales/Model/Resource/Report/Bestsellers.php
+++ b/app/code/Magento/Sales/Model/Resource/Report/Bestsellers.php
@@ -134,26 +134,8 @@ class Bestsellers extends AbstractReport
                 'period' => $periodExpr,
                 'store_id' => 'source_table.store_id',
                 'product_id' => 'order_item.product_id',
-                'product_name' => new \Zend_Db_Expr(
-                    sprintf('MIN(%s)', $adapter->getIfNullSql('product_name.value', 'product_default_name.value'))
-                ),
-                'product_price' => new \Zend_Db_Expr(
-                    sprintf(
-                        '%s * %s',
-                        new \Zend_Db_Expr(
-                            sprintf(
-                                'MIN(%s)',
-                                $adapter->getIfNullSql(
-                                    $adapter->getIfNullSql('product_price.value', 'product_default_price.value'),
-                                    0
-                                )
-                            )
-                        ),
-                        new \Zend_Db_Expr(
-                            sprintf('MIN(%s)', $adapter->getIfNullSql('source_table.base_to_global_rate', '0'))
-                        )
-                    )
-                ),
+                'product_name' => new \Zend_Db_Expr('MIN(order_item.name)'),
+                'product_price' => new \Zend_Db_Expr('MIN(order_item.base_price) * MIN(source_table.base_to_global_rate)'),
                 'qty_ordered' => new \Zend_Db_Expr('SUM(order_item.qty_ordered)'),
             ];
 
@@ -167,61 +149,8 @@ class Bestsellers extends AbstractReport
             )->where(
                 'source_table.state != ?',
                 \Magento\Sales\Model\Order::STATE_CANCELED
-            );
-
-            $joinExpr = [
-                'product.entity_id = order_item.product_id',
-                $adapter->quoteInto('product.type_id NOT IN(?)', $this->ignoredProductTypes),
-            ];
-
-            $joinExpr = implode(' AND ', $joinExpr);
-            $select->joinInner(['product' => $this->getTable('catalog_product_entity')], $joinExpr, []);
-
-            // join product attributes Name & Price
-            $attr = $this->_productResource->getAttribute('name');
-            $joinExprProductName = [
-                'product_name.entity_id = product.entity_id',
-                'product_name.store_id = source_table.store_id',
-                $adapter->quoteInto('product_name.attribute_id = ?', $attr->getAttributeId()),
-            ];
-            $joinExprProductName = implode(' AND ', $joinExprProductName);
-            $joinProductName = [
-                'product_default_name.entity_id = product.entity_id',
-                'product_default_name.store_id = 0',
-                $adapter->quoteInto('product_default_name.attribute_id = ?', $attr->getAttributeId()),
-            ];
-            $joinProductName = implode(' AND ', $joinProductName);
-            $select->joinLeft(
-                ['product_name' => $attr->getBackend()->getTable()],
-                $joinExprProductName,
-                []
-            )->joinLeft(
-                ['product_default_name' => $attr->getBackend()->getTable()],
-                $joinProductName,
-                []
-            );
-            $attr = $this->_productResource->getAttribute('price');
-            $joinExprProductPrice = [
-                'product_price.entity_id = product.entity_id',
-                'product_price.store_id = source_table.store_id',
-                $adapter->quoteInto('product_price.attribute_id = ?', $attr->getAttributeId()),
-            ];
-            $joinExprProductPrice = implode(' AND ', $joinExprProductPrice);
-
-            $joinProductPrice = [
-                'product_default_price.entity_id = product.entity_id',
-                'product_default_price.store_id = 0',
-                $adapter->quoteInto('product_default_price.attribute_id = ?', $attr->getAttributeId()),
-            ];
-            $joinProductPrice = implode(' AND ', $joinProductPrice);
-            $select->joinLeft(
-                ['product_price' => $attr->getBackend()->getTable()],
-                $joinExprProductPrice,
-                []
-            )->joinLeft(
-                ['product_default_price' => $attr->getBackend()->getTable()],
-                $joinProductPrice,
-                []
+            )->where(
+                'order_item.product_type NOT IN(?)', $this->ignoredProductTypes
             );
 
             if ($subSelect !== null) {
diff --git a/app/code/Magento/Sales/etc/di.xml b/app/code/Magento/Sales/etc/di.xml
index 4c103a22dfe..eeee3d72fc7 100644
--- a/app/code/Magento/Sales/etc/di.xml
+++ b/app/code/Magento/Sales/etc/di.xml
@@ -205,4 +205,14 @@
             <argument name="resourcePrefix" xsi:type="string">sales</argument>
         </arguments>
     </type>
+    <type name="Magento\Sales\Model\Resource\Report\Order\Createdat">
+        <arguments>
+            <argument name="resourcePrefix" xsi:type="string">sales</argument>
+        </arguments>
+    </type>
+    <type name="Magento\Sales\Model\Resource\Report\Bestsellers">
+        <arguments>
+            <argument name="resourcePrefix" xsi:type="string">sales</argument>
+        </arguments>
+    </type>
 </config>
-- 
GitLab