From 5b16123d552e396bc8dc2fab26102cf3ac01326b Mon Sep 17 00:00:00 2001 From: Fabrizio Balliano Date: Thu, 29 Dec 2022 11:26:25 +0100 Subject: [PATCH 1/3] Rewrote Mage_Reports_Model_Resource_Review_Product_Collection query for a correct use of Zend_Db_Expr --- .../Model/Resource/Review/Product/Collection.php | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/app/code/core/Mage/Reports/Model/Resource/Review/Product/Collection.php b/app/code/core/Mage/Reports/Model/Resource/Review/Product/Collection.php index 54700755dd9..7bb19c5b0bf 100644 --- a/app/code/core/Mage/Reports/Model/Resource/Review/Product/Collection.php +++ b/app/code/core/Mage/Reports/Model/Resource/Review/Product/Collection.php @@ -56,7 +56,7 @@ public function joinReview() 'e.entity_id = r.entity_pk_value', [ 'review_cnt' => new Zend_Db_Expr(sprintf('(%s)', $subSelect)), - 'last_created' => 'MAX(r.created_at)', + 'last_created' => new Zend_Db_Expr('MAX(r.created_at)'), ] ) ->group('e.entity_id'); @@ -66,22 +66,18 @@ public function joinReview() $this->getConnection()->quoteInto('table_rating.store_id > ?', 0) ]; - /** - * @var array $groupByCondition of group by fields - */ - $groupByCondition = $this->getSelect()->getPart(Zend_Db_Select::GROUP); $percentField = $this->getConnection()->quoteIdentifier('table_rating.percent'); - $sumPercentField = $helper->prepareColumn("SUM({$percentField})", $groupByCondition); - $sumPercentApproved = $helper->prepareColumn('SUM(table_rating.percent_approved)', $groupByCondition); - $countRatingId = $helper->prepareColumn('COUNT(table_rating.rating_id)', $groupByCondition); + $sumPercentField = "SUM({$percentField})"; + $sumPercentApproved = 'SUM(table_rating.percent_approved)'; + $countRatingId = 'COUNT(table_rating.rating_id)'; $this->getSelect() ->joinLeft( ['table_rating' => $this->getTable('rating/rating_vote_aggregated')], implode(' AND ', $joinCondition), [ - 'avg_rating' => sprintf('%s/%s', $sumPercentField, $countRatingId), - 'avg_rating_approved' => sprintf('%s/%s', $sumPercentApproved, $countRatingId), + 'avg_rating' => new Zend_Db_Expr("$sumPercentField / $countRatingId"), + 'avg_rating_approved' => new Zend_Db_Expr("$sumPercentApproved / $countRatingId"), ] ); From 514505280ff1833632f613396b9ba1292b9ae546 Mon Sep 17 00:00:00 2001 From: Fabrizio Balliano Date: Thu, 29 Dec 2022 17:24:27 +0100 Subject: [PATCH 2/3] Fixed Mage_Reports_Model_Resource_Order_Collection --- app/Mage.php | 8 ++++ .../Model/Resource/Order/Collection.php | 48 +++++++++---------- 2 files changed, 32 insertions(+), 24 deletions(-) diff --git a/app/Mage.php b/app/Mage.php index 65cfa51725b..bb13c2b1c40 100644 --- a/app/Mage.php +++ b/app/Mage.php @@ -48,6 +48,14 @@ Varien_Autoload::register(); +/** AUTOLOADER PATCH **/ +if (file_exists($autoloaderPath = BP . DS . 'vendor/autoload.php') || + file_exists($autoloaderPath = BP . DS . '../vendor/autoload.php') +) { + require $autoloaderPath; +} +/** AUTOLOADER PATCH **/ + /* Support additional includes, such as composer's vendor/autoload.php files */ foreach (glob(BP . DS . 'app' . DS . 'etc' . DS . 'includes' . DS . '*.php') as $path) { include_once $path; diff --git a/app/code/core/Mage/Reports/Model/Resource/Order/Collection.php b/app/code/core/Mage/Reports/Model/Resource/Order/Collection.php index badb6be8877..6b03b957bf8 100644 --- a/app/code/core/Mage/Reports/Model/Resource/Order/Collection.php +++ b/app/code/core/Mage/Reports/Model/Resource/Order/Collection.php @@ -168,7 +168,7 @@ protected function _prepareSummaryLive($range, $customStart, $customEnd, $isFilt $this->getSelect() ->columns([ - 'quantity' => 'COUNT(main_table.entity_id)', + 'quantity' => new Zend_Db_Expr('COUNT(main_table.entity_id)'), 'range' => $tzRangeOffsetExpression, ]) ->where('main_table.state NOT IN (?)', [ @@ -203,8 +203,8 @@ protected function _prepareSummaryAggregated($range, $customStart, $customEnd) $rangePeriod2 = str_replace($tableName, "MIN($tableName)", $rangePeriod); $this->getSelect()->columns([ - 'revenue' => 'SUM(main_table.total_revenue_amount)', - 'quantity' => 'SUM(main_table.orders_count)', + 'revenue' => new Zend_Db_Expr('SUM(main_table.total_revenue_amount)'), + 'quantity' => new Zend_Db_Expr('SUM(main_table.orders_count)'), 'range' => $rangePeriod2, ]) ->order('range') @@ -512,7 +512,7 @@ public function calculateSales($isFilter = 0) 0 ); $this->getSelect()->columns([ - 'lifetime' => 'SUM(main_table.total_revenue_amount)', + 'lifetime' => new Zend_Db_Expr('SUM(main_table.total_revenue_amount)'), 'average' => $averageExpr ]); @@ -535,8 +535,8 @@ public function calculateSales($isFilter = 0) $this->getSelect() ->columns([ - 'lifetime' => "SUM({$expr})", - 'average' => "AVG({$expr})" + 'lifetime' => new Zend_Db_Expr("SUM({$expr})"), + 'average' => new Zend_Db_Expr("AVG({$expr})") ]) ->where('main_table.status NOT IN(?)', $statuses) ->where('main_table.state NOT IN(?)', [ @@ -584,31 +584,31 @@ public function setStoreIds($storeIds) $baseTotalInvocedCost = $adapter->getIfNullSql('main_table.base_total_invoiced_cost', 0); if ($storeIds) { $this->getSelect()->columns([ - 'subtotal' => 'SUM(main_table.base_subtotal)', - 'tax' => 'SUM(main_table.base_tax_amount)', - 'shipping' => 'SUM(main_table.base_shipping_amount)', - 'discount' => 'SUM(main_table.base_discount_amount)', - 'total' => 'SUM(main_table.base_grand_total)', - 'invoiced' => 'SUM(main_table.base_total_paid)', - 'refunded' => 'SUM(main_table.base_total_refunded)', - 'profit' => "SUM($baseSubtotalInvoiced) " + 'subtotal' => new Zend_Db_Expr('SUM(main_table.base_subtotal)'), + 'tax' => new Zend_Db_Expr('SUM(main_table.base_tax_amount)'), + 'shipping' => new Zend_Db_Expr('SUM(main_table.base_shipping_amount)'), + 'discount' => new Zend_Db_Expr('SUM(main_table.base_discount_amount)'), + 'total' => new Zend_Db_Expr('SUM(main_table.base_grand_total)'), + 'invoiced' => new Zend_Db_Expr('SUM(main_table.base_total_paid)'), + 'refunded' => new Zend_Db_Expr('SUM(main_table.base_total_refunded)'), + 'profit' => new Zend_Db_Expr("SUM($baseSubtotalInvoiced) " . "+ SUM({$baseDiscountRefunded}) - SUM({$baseSubtotalRefunded}) " - . "- SUM({$baseDiscountInvoiced}) - SUM({$baseTotalInvocedCost})" + . "- SUM({$baseDiscountInvoiced}) - SUM({$baseTotalInvocedCost})") ]); } else { $this->getSelect()->columns([ - 'subtotal' => 'SUM(main_table.base_subtotal * main_table.base_to_global_rate)', - 'tax' => 'SUM(main_table.base_tax_amount * main_table.base_to_global_rate)', - 'shipping' => 'SUM(main_table.base_shipping_amount * main_table.base_to_global_rate)', - 'discount' => 'SUM(main_table.base_discount_amount * main_table.base_to_global_rate)', - 'total' => 'SUM(main_table.base_grand_total * main_table.base_to_global_rate)', - 'invoiced' => 'SUM(main_table.base_total_paid * main_table.base_to_global_rate)', - 'refunded' => 'SUM(main_table.base_total_refunded * main_table.base_to_global_rate)', - 'profit' => "SUM({$baseSubtotalInvoiced} * main_table.base_to_global_rate) " + 'subtotal' => new Zend_Db_Expr('SUM(main_table.base_subtotal * main_table.base_to_global_rate)'), + 'tax' => new Zend_Db_Expr('SUM(main_table.base_tax_amount * main_table.base_to_global_rate)'), + 'shipping' => new Zend_Db_Expr('SUM(main_table.base_shipping_amount * main_table.base_to_global_rate)'), + 'discount' => new Zend_Db_Expr('SUM(main_table.base_discount_amount * main_table.base_to_global_rate)'), + 'total' => new Zend_Db_Expr('SUM(main_table.base_grand_total * main_table.base_to_global_rate)'), + 'invoiced' => new Zend_Db_Expr('SUM(main_table.base_total_paid * main_table.base_to_global_rate)'), + 'refunded' => new Zend_Db_Expr('SUM(main_table.base_total_refunded * main_table.base_to_global_rate)'), + 'profit' => new Zend_Db_Expr("SUM({$baseSubtotalInvoiced} * main_table.base_to_global_rate) " . "+ SUM({$baseDiscountRefunded} * main_table.base_to_global_rate) " . "- SUM({$baseSubtotalRefunded} * main_table.base_to_global_rate) " . "- SUM({$baseDiscountInvoiced} * main_table.base_to_global_rate) " - . "- SUM({$baseTotalInvocedCost} * main_table.base_to_global_rate)" + . "- SUM({$baseTotalInvocedCost} * main_table.base_to_global_rate)") ]); } From 517280b1a783d14f78926aabb0c8c53aeacc3842 Mon Sep 17 00:00:00 2001 From: Fabrizio Balliano Date: Thu, 29 Dec 2022 17:32:02 +0100 Subject: [PATCH 3/3] shouldnt be here --- app/Mage.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/app/Mage.php b/app/Mage.php index bb13c2b1c40..65cfa51725b 100644 --- a/app/Mage.php +++ b/app/Mage.php @@ -48,14 +48,6 @@ Varien_Autoload::register(); -/** AUTOLOADER PATCH **/ -if (file_exists($autoloaderPath = BP . DS . 'vendor/autoload.php') || - file_exists($autoloaderPath = BP . DS . '../vendor/autoload.php') -) { - require $autoloaderPath; -} -/** AUTOLOADER PATCH **/ - /* Support additional includes, such as composer's vendor/autoload.php files */ foreach (glob(BP . DS . 'app' . DS . 'etc' . DS . 'includes' . DS . '*.php') as $path) { include_once $path;