Skip to content

Commit 0857bad

Browse files
author
valdislav
committed
MAGETWO-35638: Pull request processing
2 parents bf26be5 + dd4636e commit 0857bad

File tree

4 files changed

+266
-86
lines changed

4 files changed

+266
-86
lines changed

app/code/Magento/Reports/Block/Adminhtml/Shopcart/Product/Grid.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,7 @@ protected function _construct()
5757
protected function _prepareCollection()
5858
{
5959
$collection = $this->_quotesFactory->create();
60-
if ($this->queryResolver->isSingleQuery()) {
61-
$collection->prepareForProductsInCarts();
62-
$collection->setSelectCountSqlType(
63-
\Magento\Reports\Model\Resource\Quote\Collection::SELECT_COUNT_SQL_TYPE_CART
64-
);
65-
} else {
66-
$collection->prepareActiveCartItems();
67-
}
60+
$collection->prepareActiveCartItems();
6861
$this->setCollection($collection);
6962
return parent::_prepareCollection();
7063
}
@@ -80,6 +73,7 @@ protected function _prepareColumns()
8073
'header' => __('ID'),
8174
'align' => 'right',
8275
'index' => 'entity_id',
76+
'sortable' => false,
8377
'header_css_class' => 'col-id',
8478
'column_css_class' => 'col-id'
8579
]
@@ -90,6 +84,7 @@ protected function _prepareColumns()
9084
[
9185
'header' => __('Product'),
9286
'index' => 'name',
87+
'sortable' => false,
9388
'header_css_class' => 'col-product',
9489
'column_css_class' => 'col-product'
9590
]
@@ -104,6 +99,7 @@ protected function _prepareColumns()
10499
'type' => 'currency',
105100
'currency_code' => $currencyCode,
106101
'index' => 'price',
102+
'sortable' => false,
107103
'renderer' => 'Magento\Reports\Block\Adminhtml\Grid\Column\Renderer\Currency',
108104
'rate' => $this->getRate($currencyCode),
109105
'header_css_class' => 'col-price',
@@ -117,6 +113,7 @@ protected function _prepareColumns()
117113
'header' => __('Carts'),
118114
'align' => 'right',
119115
'index' => 'carts',
116+
'sortable' => false,
120117
'header_css_class' => 'col-carts',
121118
'column_css_class' => 'col-carts'
122119
]
@@ -128,6 +125,7 @@ protected function _prepareColumns()
128125
'header' => __('Orders'),
129126
'align' => 'right',
130127
'index' => 'orders',
128+
'sortable' => false,
131129
'header_css_class' => 'col-qty',
132130
'column_css_class' => 'col-qty'
133131
]

app/code/Magento/Reports/Model/Resource/Quote/Collection.php

Lines changed: 105 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,17 @@ class Collection extends \Magento\Quote\Model\Resource\Quote\Collection
3939
/**
4040
* @var \Magento\Catalog\Model\Resource\Product\Collection
4141
*/
42-
protected $_productResource;
42+
protected $productResource;
4343

4444
/**
4545
* @var \Magento\Customer\Model\Resource\Customer
4646
*/
47-
protected $_customerResource;
47+
protected $customerResource;
48+
49+
/**
50+
* @var \Magento\Sales\Model\Resource\Order\Collection
51+
*/
52+
protected $orderResource;
4853

4954
/**
5055
* @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
@@ -53,6 +58,7 @@ class Collection extends \Magento\Quote\Model\Resource\Quote\Collection
5358
* @param \Magento\Framework\Event\ManagerInterface $eventManager
5459
* @param \Magento\Catalog\Model\Resource\Product\Collection $productResource
5560
* @param \Magento\Customer\Model\Resource\Customer $customerResource
61+
* @param \Magento\Sales\Model\Resource\Order\Collection $orderResource
5662
* @param null $connection
5763
* @param \Magento\Framework\Model\Resource\Db\AbstractDb $resource
5864
*/
@@ -63,12 +69,14 @@ public function __construct(
6369
\Magento\Framework\Event\ManagerInterface $eventManager,
6470
\Magento\Catalog\Model\Resource\Product\Collection $productResource,
6571
\Magento\Customer\Model\Resource\Customer $customerResource,
72+
\Magento\Sales\Model\Resource\Order\Collection $orderResource,
6673
$connection = null,
6774
\Magento\Framework\Model\Resource\Db\AbstractDb $resource = null
6875
) {
6976
parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
70-
$this->_productResource = $productResource;
71-
$this->_customerResource = $customerResource;
77+
$this->productResource = $productResource;
78+
$this->customerResource = $customerResource;
79+
$this->orderResource = $orderResource;
7280
}
7381

7482
/**
@@ -120,75 +128,47 @@ public function prepareForAbandonedReport($storeIds, $filter = null)
120128
/**
121129
* Prepare select query for products in carts report
122130
*
123-
* @return $this
131+
* @return \Magento\Framework\DB\Select
124132
*/
125-
public function prepareForProductsInCarts()
133+
public function prepareActiveCartItems()
126134
{
127-
$productAttrName = $this->_productResource->getAttribute('name');
128-
$productAttrNameId = (int)$productAttrName->getAttributeId();
129-
$productAttrNameTable = $productAttrName->getBackend()->getTable();
130-
$productAttrPrice = $this->_productResource->getAttribute('price');
131-
$productAttrPriceId = (int)$productAttrPrice->getAttributeId();
132-
$productAttrPriceTable = $productAttrPrice->getBackend()->getTable();
133-
134-
$this->getSelect()->useStraightJoin(
135-
true
136-
)->reset(
137-
\Zend_Db_Select::COLUMNS
138-
)->joinInner(
139-
['quote_items' => $this->getTable('quote_item')],
140-
'quote_items.quote_id = main_table.entity_id',
141-
null
142-
)->joinInner(
143-
['e' => $this->getTable('catalog_product_entity')],
144-
'e.entity_id = quote_items.product_id',
145-
null
146-
)->joinInner(
147-
['product_name' => $productAttrNameTable],
148-
'product_name.entity_id = e.entity_id'
149-
. ' AND product_name.attribute_id = ' . $productAttrNameId
150-
. ' AND product_name.store_id = ' . \Magento\Store\Model\Store::DEFAULT_STORE_ID,
151-
['name' => 'product_name.value']
152-
)->joinInner(
153-
['product_price' => $productAttrPriceTable],
154-
"product_price.entity_id = e.entity_id AND product_price.attribute_id = {$productAttrPriceId}",
155-
['price' => new \Zend_Db_Expr('product_price.value * main_table.base_to_global_rate')]
156-
)->joinLeft(
157-
['order_items' => new \Zend_Db_Expr(sprintf('(%s)', $this->getOrdersSubSelect()))],
158-
'order_items.product_id = e.entity_id',
159-
[]
160-
)->columns(
161-
'e.*'
162-
)->columns(
163-
['carts' => new \Zend_Db_Expr('COUNT(quote_items.item_id)')]
164-
)->columns(
165-
'order_items.orders'
166-
)->where(
167-
'main_table.is_active = ?',
168-
1
169-
)->group(
170-
'quote_items.product_id'
171-
);
135+
$quoteItemsSelect = $this->getSelect();
136+
$quoteItemsSelect->reset()
137+
->from(['main_table' => $this->getTable('quote')], '')
138+
->columns('quote_items.product_id')
139+
->columns(['carts' => new \Zend_Db_Expr('COUNT(quote_items.item_id)')])
140+
->columns('main_table.base_to_global_rate')
141+
->joinInner(
142+
['quote_items' => $this->getTable('quote_item')],
143+
'quote_items.quote_id = main_table.entity_id',
144+
null
145+
)->where(
146+
'main_table.is_active = ?',
147+
1
148+
)->group(
149+
'quote_items.product_id'
150+
);
172151

173-
return $this;
152+
return $quoteItemsSelect;
174153
}
175154

176155
/**
177-
* Orders quantity subselect
156+
* Orders quantity data
178157
*
179-
* @return \Magento\Framework\DB\Select
158+
* @param array $productIds
159+
* @return array
180160
*/
181-
protected function getOrdersSubSelect()
161+
protected function getOrdersData(array $productIds)
182162
{
183-
$ordersSubSelect = clone $this->getSelect();
163+
$ordersSubSelect = clone $this->orderResource->getSelect();
184164
$ordersSubSelect->reset()->from(
185165
['oi' => $this->getTable('sales_order_item')],
186166
['orders' => new \Zend_Db_Expr('COUNT(1)'), 'product_id']
187-
)->group(
167+
)->where('oi.product_id IN (?)', $productIds)->group(
188168
'oi.product_id'
189169
);
190170

191-
return $ordersSubSelect;
171+
return $this->orderResource->getConnection()->fetchAssoc($ordersSubSelect);
192172
}
193173

194174
/**
@@ -211,7 +191,7 @@ public function addStoreFilter($storeIds)
211191
*/
212192
public function addCustomerData($filter = null)
213193
{
214-
$customersSelect = $this->_customerResource->getReadConnection()->select();
194+
$customersSelect = $this->customerResource->getReadConnection()->select();
215195
$customersSelect->from(['customer' => 'customer_entity'], 'entity_id');
216196
if (isset($filter['customer_name'])) {
217197
$customersSelect = $this->getCustomerNames($customersSelect);
@@ -223,7 +203,7 @@ public function addCustomerData($filter = null)
223203
if (isset($filter['email'])) {
224204
$customersSelect->where('customer.email LIKE ?', '%' . $filter['email'] . '%');
225205
}
226-
$filteredCustomers = $this->_customerResource->getReadConnection()->fetchCol($customersSelect);
206+
$filteredCustomers = $this->customerResource->getReadConnection()->fetchCol($customersSelect);
227207
$this->getSelect()->where('main_table.customer_id IN (?)', $filteredCustomers);
228208
return $this;
229209
}
@@ -270,24 +250,14 @@ public function addSubtotal($storeIds = '', $filter = null)
270250
/**
271251
* Get select count sql
272252
*
273-
* @return string
253+
* @return \Magento\Framework\DB\Select
274254
*/
275255
public function getSelectCountSql()
276256
{
277-
$countSelect = clone $this->getSelect();
278-
$countSelect->reset(\Zend_Db_Select::ORDER);
279-
$countSelect->reset(\Zend_Db_Select::LIMIT_COUNT);
280-
$countSelect->reset(\Zend_Db_Select::LIMIT_OFFSET);
281-
$countSelect->reset(\Zend_Db_Select::COLUMNS);
282-
$countSelect->reset(\Zend_Db_Select::GROUP);
283-
$countSelect->resetJoinLeft();
284-
285-
if ($this->_selectCountSqlType == self::SELECT_COUNT_SQL_TYPE_CART) {
286-
$countSelect->columns("COUNT(DISTINCT e.entity_id)");
287-
} else {
288-
$countSelect->columns("COUNT(DISTINCT main_table.entity_id)");
289-
}
290-
257+
$countSelect = clone $this->prepareActiveCartItems();
258+
$countSelect->reset(\Zend_Db_Select::COLUMNS)
259+
->reset(\Zend_Db_Select::GROUP)
260+
->columns('COUNT(DISTINCT quote_items.product_id)');
291261
return $countSelect;
292262
}
293263

@@ -297,10 +267,10 @@ public function getSelectCountSql()
297267
*/
298268
protected function getCustomerNames($select)
299269
{
300-
$attrFirstname = $this->_customerResource->getAttribute('firstname');
270+
$attrFirstname = $this->customerResource->getAttribute('firstname');
301271
$attrFirstnameId = (int)$attrFirstname->getAttributeId();
302272
$attrFirstnameTableName = $attrFirstname->getBackend()->getTable();
303-
$attrLastname = $this->_customerResource->getAttribute('lastname');
273+
$attrLastname = $this->customerResource->getAttribute('lastname');
304274
$attrLastnameId = (int)$attrLastname->getAttributeId();
305275
$attrLastnameTableName = $attrLastname->getBackend()->getTable();
306276
$select->joinInner(
@@ -326,7 +296,7 @@ protected function getCustomerNames($select)
326296
*/
327297
public function resolveCustomerNames()
328298
{
329-
$select = $this->_customerResource->getReadConnection()->select();
299+
$select = $this->customerResource->getReadConnection()->select();
330300
$customerName = $select->getAdapter()->getConcatSql(['cust_fname.value', 'cust_lname.value'], ' ');
331301

332302
$select->from(
@@ -343,4 +313,61 @@ public function resolveCustomerNames()
343313
next($customersData);
344314
}
345315
}
316+
317+
/**
318+
* Separate query for product and order data
319+
*
320+
* @return array
321+
* @throws \Magento\Eav\Exception
322+
*/
323+
protected function getProductData()
324+
{
325+
$productConnection = $this->productResource->getConnection('read');
326+
$productAttrName = $this->productResource->getAttribute('name');
327+
$productAttrNameId = (int)$productAttrName->getAttributeId();
328+
$productAttrPrice = $this->productResource->getAttribute('price');
329+
$productAttrPriceId = (int)$productAttrPrice->getAttributeId();
330+
331+
$select = clone $this->productResource->getSelect();
332+
$select->reset();
333+
$select->from(
334+
['main_table' => $this->getTable('catalog_product_entity')]
335+
)->useStraightJoin(
336+
true
337+
)->joinInner(
338+
['product_name' => $productAttrName->getBackend()->getTable()],
339+
'product_name.entity_id = main_table.entity_id'
340+
. ' AND product_name.attribute_id = ' . $productAttrNameId
341+
. ' AND product_name.store_id = ' . \Magento\Store\Model\Store::DEFAULT_STORE_ID,
342+
['name' => 'product_name.value']
343+
)->joinInner(
344+
['product_price' => $productAttrPrice->getBackend()->getTable()],
345+
"product_price.entity_id = main_table.entity_id AND product_price.attribute_id = {$productAttrPriceId}",
346+
['price' => new \Zend_Db_Expr('product_price.value')]
347+
);
348+
$productData = $productConnection->fetchAssoc($select);
349+
return $productData;
350+
}
351+
352+
/**
353+
* Add data fetched from another database
354+
*
355+
* @return $this
356+
*/
357+
protected function _afterLoad()
358+
{
359+
parent::_afterLoad();
360+
$productData = $this->getProductData();
361+
$productIds = array_keys($productData);
362+
$orderData = $this->getOrdersData($productIds);
363+
$items = $this->getItems();
364+
foreach ($items as $item) {
365+
$item->setId($item->getProductId());
366+
$item->setPrice($productData[$item->getProductId()]['price'] * $item->getBaseToGlobalRate());
367+
$item->setName($productData[$item->getProductId()]['name']);
368+
$item->setOrders($orderData[$item->getProductId()]['orders']);
369+
}
370+
371+
return $this;
372+
}
346373
}

0 commit comments

Comments
 (0)