Skip to content

Commit 0ca2917

Browse files
authored
Rewrote Mage_Reports_Model_Resource_Review_Product_Collection/Mage_Reports_Model_Resource_Order_Collection queries for a correct use of Zend_Db_Expr (#2864)
* Rewrote Mage_Reports_Model_Resource_Review_Product_Collection query for a correct use of Zend_Db_Expr * Fixed Mage_Reports_Model_Resource_Order_Collection * shouldnt be here
1 parent 583113d commit 0ca2917

File tree

2 files changed

+30
-34
lines changed

2 files changed

+30
-34
lines changed

app/code/core/Mage/Reports/Model/Resource/Order/Collection.php

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ protected function _prepareSummaryLive($range, $customStart, $customEnd, $isFilt
168168

169169
$this->getSelect()
170170
->columns([
171-
'quantity' => 'COUNT(main_table.entity_id)',
171+
'quantity' => new Zend_Db_Expr('COUNT(main_table.entity_id)'),
172172
'range' => $tzRangeOffsetExpression,
173173
])
174174
->where('main_table.state NOT IN (?)', [
@@ -203,8 +203,8 @@ protected function _prepareSummaryAggregated($range, $customStart, $customEnd)
203203
$rangePeriod2 = str_replace($tableName, "MIN($tableName)", $rangePeriod);
204204

205205
$this->getSelect()->columns([
206-
'revenue' => 'SUM(main_table.total_revenue_amount)',
207-
'quantity' => 'SUM(main_table.orders_count)',
206+
'revenue' => new Zend_Db_Expr('SUM(main_table.total_revenue_amount)'),
207+
'quantity' => new Zend_Db_Expr('SUM(main_table.orders_count)'),
208208
'range' => $rangePeriod2,
209209
])
210210
->order('range')
@@ -512,7 +512,7 @@ public function calculateSales($isFilter = 0)
512512
0
513513
);
514514
$this->getSelect()->columns([
515-
'lifetime' => 'SUM(main_table.total_revenue_amount)',
515+
'lifetime' => new Zend_Db_Expr('SUM(main_table.total_revenue_amount)'),
516516
'average' => $averageExpr
517517
]);
518518

@@ -535,8 +535,8 @@ public function calculateSales($isFilter = 0)
535535

536536
$this->getSelect()
537537
->columns([
538-
'lifetime' => "SUM({$expr})",
539-
'average' => "AVG({$expr})"
538+
'lifetime' => new Zend_Db_Expr("SUM({$expr})"),
539+
'average' => new Zend_Db_Expr("AVG({$expr})")
540540
])
541541
->where('main_table.status NOT IN(?)', $statuses)
542542
->where('main_table.state NOT IN(?)', [
@@ -584,31 +584,31 @@ public function setStoreIds($storeIds)
584584
$baseTotalInvocedCost = $adapter->getIfNullSql('main_table.base_total_invoiced_cost', 0);
585585
if ($storeIds) {
586586
$this->getSelect()->columns([
587-
'subtotal' => 'SUM(main_table.base_subtotal)',
588-
'tax' => 'SUM(main_table.base_tax_amount)',
589-
'shipping' => 'SUM(main_table.base_shipping_amount)',
590-
'discount' => 'SUM(main_table.base_discount_amount)',
591-
'total' => 'SUM(main_table.base_grand_total)',
592-
'invoiced' => 'SUM(main_table.base_total_paid)',
593-
'refunded' => 'SUM(main_table.base_total_refunded)',
594-
'profit' => "SUM($baseSubtotalInvoiced) "
587+
'subtotal' => new Zend_Db_Expr('SUM(main_table.base_subtotal)'),
588+
'tax' => new Zend_Db_Expr('SUM(main_table.base_tax_amount)'),
589+
'shipping' => new Zend_Db_Expr('SUM(main_table.base_shipping_amount)'),
590+
'discount' => new Zend_Db_Expr('SUM(main_table.base_discount_amount)'),
591+
'total' => new Zend_Db_Expr('SUM(main_table.base_grand_total)'),
592+
'invoiced' => new Zend_Db_Expr('SUM(main_table.base_total_paid)'),
593+
'refunded' => new Zend_Db_Expr('SUM(main_table.base_total_refunded)'),
594+
'profit' => new Zend_Db_Expr("SUM($baseSubtotalInvoiced) "
595595
. "+ SUM({$baseDiscountRefunded}) - SUM({$baseSubtotalRefunded}) "
596-
. "- SUM({$baseDiscountInvoiced}) - SUM({$baseTotalInvocedCost})"
596+
. "- SUM({$baseDiscountInvoiced}) - SUM({$baseTotalInvocedCost})")
597597
]);
598598
} else {
599599
$this->getSelect()->columns([
600-
'subtotal' => 'SUM(main_table.base_subtotal * main_table.base_to_global_rate)',
601-
'tax' => 'SUM(main_table.base_tax_amount * main_table.base_to_global_rate)',
602-
'shipping' => 'SUM(main_table.base_shipping_amount * main_table.base_to_global_rate)',
603-
'discount' => 'SUM(main_table.base_discount_amount * main_table.base_to_global_rate)',
604-
'total' => 'SUM(main_table.base_grand_total * main_table.base_to_global_rate)',
605-
'invoiced' => 'SUM(main_table.base_total_paid * main_table.base_to_global_rate)',
606-
'refunded' => 'SUM(main_table.base_total_refunded * main_table.base_to_global_rate)',
607-
'profit' => "SUM({$baseSubtotalInvoiced} * main_table.base_to_global_rate) "
600+
'subtotal' => new Zend_Db_Expr('SUM(main_table.base_subtotal * main_table.base_to_global_rate)'),
601+
'tax' => new Zend_Db_Expr('SUM(main_table.base_tax_amount * main_table.base_to_global_rate)'),
602+
'shipping' => new Zend_Db_Expr('SUM(main_table.base_shipping_amount * main_table.base_to_global_rate)'),
603+
'discount' => new Zend_Db_Expr('SUM(main_table.base_discount_amount * main_table.base_to_global_rate)'),
604+
'total' => new Zend_Db_Expr('SUM(main_table.base_grand_total * main_table.base_to_global_rate)'),
605+
'invoiced' => new Zend_Db_Expr('SUM(main_table.base_total_paid * main_table.base_to_global_rate)'),
606+
'refunded' => new Zend_Db_Expr('SUM(main_table.base_total_refunded * main_table.base_to_global_rate)'),
607+
'profit' => new Zend_Db_Expr("SUM({$baseSubtotalInvoiced} * main_table.base_to_global_rate) "
608608
. "+ SUM({$baseDiscountRefunded} * main_table.base_to_global_rate) "
609609
. "- SUM({$baseSubtotalRefunded} * main_table.base_to_global_rate) "
610610
. "- SUM({$baseDiscountInvoiced} * main_table.base_to_global_rate) "
611-
. "- SUM({$baseTotalInvocedCost} * main_table.base_to_global_rate)"
611+
. "- SUM({$baseTotalInvocedCost} * main_table.base_to_global_rate)")
612612
]);
613613
}
614614

app/code/core/Mage/Reports/Model/Resource/Review/Product/Collection.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function joinReview()
5656
'e.entity_id = r.entity_pk_value',
5757
[
5858
'review_cnt' => new Zend_Db_Expr(sprintf('(%s)', $subSelect)),
59-
'last_created' => 'MAX(r.created_at)',
59+
'last_created' => new Zend_Db_Expr('MAX(r.created_at)'),
6060
]
6161
)
6262
->group('e.entity_id');
@@ -66,22 +66,18 @@ public function joinReview()
6666
$this->getConnection()->quoteInto('table_rating.store_id > ?', 0)
6767
];
6868

69-
/**
70-
* @var array $groupByCondition of group by fields
71-
*/
72-
$groupByCondition = $this->getSelect()->getPart(Zend_Db_Select::GROUP);
7369
$percentField = $this->getConnection()->quoteIdentifier('table_rating.percent');
74-
$sumPercentField = $helper->prepareColumn("SUM({$percentField})", $groupByCondition);
75-
$sumPercentApproved = $helper->prepareColumn('SUM(table_rating.percent_approved)', $groupByCondition);
76-
$countRatingId = $helper->prepareColumn('COUNT(table_rating.rating_id)', $groupByCondition);
70+
$sumPercentField = "SUM({$percentField})";
71+
$sumPercentApproved = 'SUM(table_rating.percent_approved)';
72+
$countRatingId = 'COUNT(table_rating.rating_id)';
7773

7874
$this->getSelect()
7975
->joinLeft(
8076
['table_rating' => $this->getTable('rating/rating_vote_aggregated')],
8177
implode(' AND ', $joinCondition),
8278
[
83-
'avg_rating' => sprintf('%s/%s', $sumPercentField, $countRatingId),
84-
'avg_rating_approved' => sprintf('%s/%s', $sumPercentApproved, $countRatingId),
79+
'avg_rating' => new Zend_Db_Expr("$sumPercentField / $countRatingId"),
80+
'avg_rating_approved' => new Zend_Db_Expr("$sumPercentApproved / $countRatingId"),
8581
]
8682
);
8783

0 commit comments

Comments
 (0)