Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrote Mage_Reports_Model_Resource_Review_Product_Collection/Mage_Reports_Model_Resource_Order_Collection queries for a correct use of Zend_Db_Expr #2864

Merged
merged 3 commits into from
Dec 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions app/code/core/Mage/Reports/Model/Resource/Order/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 (?)', [
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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
]);

Expand All @@ -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(?)', [
Expand Down Expand Up @@ -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)")
addison74 marked this conversation as resolved.
Show resolved Hide resolved
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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"),
]
);

Expand Down