Skip to content

Commit bf26be5

Browse files
author
valdislav
committed
MAGETWO-35638: Pull request processing
2 parents 74b37e9 + 3e3a505 commit bf26be5

File tree

4 files changed

+82
-116
lines changed

4 files changed

+82
-116
lines changed

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

Lines changed: 10 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ class Collection extends \Magento\Catalog\Model\Resource\Product\Collection
5656
*/
5757
protected $_productType;
5858

59+
/**
60+
* @var \Magento\Quote\Model\Resource\Quote\Collection
61+
*/
62+
protected $quoteResource;
63+
5964
/**
6065
* @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
6166
* @param \Psr\Log\LoggerInterface $logger
@@ -79,6 +84,7 @@ class Collection extends \Magento\Catalog\Model\Resource\Product\Collection
7984
* @param \Magento\Catalog\Model\Resource\Product $product
8085
* @param \Magento\Reports\Model\Event\TypeFactory $eventTypeFactory
8186
* @param \Magento\Catalog\Model\Product\Type $productType
87+
* @param \Magento\Quote\Model\Resource\Quote\Collection $quoteResource
8288
* @param mixed $connection
8389
*
8490
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
@@ -106,6 +112,7 @@ public function __construct(
106112
\Magento\Catalog\Model\Resource\Product $product,
107113
\Magento\Reports\Model\Event\TypeFactory $eventTypeFactory,
108114
\Magento\Catalog\Model\Product\Type $productType,
115+
\Magento\Quote\Model\Resource\Quote\Collection $quoteResource,
109116
$connection = null
110117
) {
111118
$this->setProductEntityId($product->getEntityIdField());
@@ -135,6 +142,7 @@ public function __construct(
135142
);
136143
$this->_eventTypeFactory = $eventTypeFactory;
137144
$this->_productType = $productType;
145+
$this->quoteResource = $quoteResource;
138146
}
139147

140148
/**
@@ -239,7 +247,7 @@ protected function _joinFields()
239247
public function getSelectCountSql()
240248
{
241249
if ($this->_selectCountSqlType == self::SELECT_COUNT_SQL_TYPE_CART) {
242-
$countSelect = clone $this->getSelect();
250+
$countSelect = clone $this->quoteResource->getSelect();
243251
$countSelect->reset()->from(
244252
['quote_item_table' => $this->getTable('quote_item')],
245253
['COUNT(DISTINCT quote_item_table.product_id)']
@@ -263,102 +271,6 @@ public function getSelectCountSql()
263271
return $countSelect;
264272
}
265273

266-
/**
267-
* Add orders count
268-
*
269-
* @param string $from
270-
* @param string $to
271-
* @return $this
272-
*/
273-
public function addOrdersCount($from = '', $to = '')
274-
{
275-
$orderItemTableName = $this->getTable('sales_order_item');
276-
$productFieldName = sprintf('e.%s', $this->getProductEntityId());
277-
278-
$this->getSelect()->joinLeft(
279-
['order_items' => $orderItemTableName],
280-
"order_items.product_id = {$productFieldName}",
281-
[]
282-
)->columns(
283-
['orders' => 'COUNT(order_items2.item_id)']
284-
)->group(
285-
$productFieldName
286-
);
287-
288-
$dateFilter = ['order_items2.item_id = order_items.item_id'];
289-
if ($from != '' && $to != '') {
290-
$dateFilter[] = $this->_prepareBetweenSql('order_items2.created_at', $from, $to);
291-
}
292-
293-
$this->getSelect()->joinLeft(
294-
['order_items2' => $orderItemTableName],
295-
implode(' AND ', $dateFilter),
296-
[]
297-
);
298-
299-
return $this;
300-
}
301-
302-
/**
303-
* Add ordered qty's
304-
*
305-
* @param string $from
306-
* @param string $to
307-
* @return $this
308-
*/
309-
public function addOrderedQty($from = '', $to = '')
310-
{
311-
$adapter = $this->getConnection();
312-
$compositeTypeIds = $this->_productType->getCompositeTypes();
313-
$orderTableAliasName = $adapter->quoteIdentifier('order');
314-
315-
$orderJoinCondition = [
316-
$orderTableAliasName . '.entity_id = order_items.order_id',
317-
$adapter->quoteInto("{$orderTableAliasName}.state <> ?", \Magento\Sales\Model\Order::STATE_CANCELED),
318-
];
319-
320-
$productJoinCondition = [
321-
$adapter->quoteInto('(e.type_id NOT IN (?))', $compositeTypeIds),
322-
'e.entity_id = order_items.product_id',
323-
$adapter->quoteInto('e.attribute_set_id = ?', $this->getProductAttributeSetId()),
324-
];
325-
326-
if ($from != '' && $to != '') {
327-
$fieldName = $orderTableAliasName . '.created_at';
328-
$orderJoinCondition[] = $this->_prepareBetweenSql($fieldName, $from, $to);
329-
}
330-
331-
$this->getSelect()->reset()->from(
332-
['order_items' => $this->getTable('sales_order_item')],
333-
['ordered_qty' => 'SUM(order_items.qty_ordered)', 'order_items_name' => 'order_items.name']
334-
)->joinInner(
335-
['order' => $this->getTable('sales_order')],
336-
implode(' AND ', $orderJoinCondition),
337-
[]
338-
)->joinLeft(
339-
['e' => $this->getProductEntityTableName()],
340-
implode(' AND ', $productJoinCondition),
341-
[
342-
'entity_id' => 'order_items.product_id',
343-
'attribute_set_id' => 'e.attribute_set_id',
344-
'type_id' => 'e.type_id',
345-
'sku' => 'e.sku',
346-
'has_options' => 'e.has_options',
347-
'required_options' => 'e.required_options',
348-
'created_at' => 'e.created_at',
349-
'updated_at' => 'e.updated_at'
350-
]
351-
)->where(
352-
'parent_item_id IS NULL'
353-
)->group(
354-
'order_items.product_id'
355-
)->having(
356-
'SUM(order_items.qty_ordered) > ?',
357-
0
358-
);
359-
return $this;
360-
}
361-
362274
/**
363275
* Set order
364276
*
@@ -368,7 +280,7 @@ public function addOrderedQty($from = '', $to = '')
368280
*/
369281
public function setOrder($attribute, $dir = self::SORT_ORDER_DESC)
370282
{
371-
if (in_array($attribute, ['carts', 'orders', 'ordered_qty'])) {
283+
if (in_array($attribute, ['carts'])) {
372284
$this->getSelect()->order($attribute . ' ' . $dir);
373285
} else {
374286
parent::setOrder($attribute, $dir);

app/code/Magento/Reports/Model/Resource/Product/Lowstock/Collection.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class Collection extends \Magento\Reports\Model\Resource\Product\Collection
7171
* @param \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
7272
* @param \Magento\CatalogInventory\Api\StockConfigurationInterface $stockConfiguration
7373
* @param \Magento\CatalogInventory\Model\Resource\Stock\Item $itemResource
74+
* @param \Magento\Quote\Model\Resource\Quote\Collection $quoteResource
7475
* @param mixed $connection
7576
*
7677
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
@@ -101,6 +102,7 @@ public function __construct(
101102
\Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,
102103
\Magento\CatalogInventory\Api\StockConfigurationInterface $stockConfiguration,
103104
\Magento\CatalogInventory\Model\Resource\Stock\Item $itemResource,
105+
\Magento\Quote\Model\Resource\Quote\Collection $quoteResource,
104106
$connection = null
105107
) {
106108
parent::__construct(
@@ -126,6 +128,7 @@ public function __construct(
126128
$product,
127129
$eventTypeFactory,
128130
$productType,
131+
$quoteResource,
129132
$connection
130133
);
131134
$this->stockRegistry = $stockRegistry;

app/code/Magento/Reports/Model/Resource/Product/Sold/Collection.php

Lines changed: 67 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212
namespace Magento\Reports\Model\Resource\Product\Sold;
1313

14-
class Collection extends \Magento\Reports\Model\Resource\Product\Collection
14+
class Collection extends \Magento\Reports\Model\Resource\Order\Collection
1515
{
1616
/**
1717
* Set Date range to collection
@@ -34,6 +34,46 @@ public function setDateRange($from, $to)
3434
return $this;
3535
}
3636

37+
/**
38+
* Add ordered qty's
39+
*
40+
* @param string $from
41+
* @param string $to
42+
* @return $this
43+
*/
44+
public function addOrderedQty($from = '', $to = '')
45+
{
46+
$adapter = $this->getConnection();
47+
$orderTableAliasName = $adapter->quoteIdentifier('order');
48+
49+
$orderJoinCondition = [
50+
$orderTableAliasName . '.entity_id = order_items.order_id',
51+
$adapter->quoteInto("{$orderTableAliasName}.state <> ?", \Magento\Sales\Model\Order::STATE_CANCELED),
52+
];
53+
54+
if ($from != '' && $to != '') {
55+
$fieldName = $orderTableAliasName . '.created_at';
56+
$orderJoinCondition[] = $this->prepareBetweenSql($fieldName, $from, $to);
57+
}
58+
59+
$this->getSelect()->reset()->from(
60+
['order_items' => $this->getTable('sales_order_item')],
61+
['ordered_qty' => 'SUM(order_items.qty_ordered)', 'order_items_name' => 'order_items.name']
62+
)->joinInner(
63+
['order' => $this->getTable('sales_order')],
64+
implode(' AND ', $orderJoinCondition),
65+
[]
66+
)->where(
67+
'parent_item_id IS NULL'
68+
)->group(
69+
'order_items.product_id'
70+
)->having(
71+
'SUM(order_items.qty_ordered) > ?',
72+
0
73+
);
74+
return $this;
75+
}
76+
3777
/**
3878
* Set store filter to collection
3979
*
@@ -49,29 +89,38 @@ public function setStoreIds($storeIds)
4989
}
5090

5191
/**
52-
* Add website product limitation
92+
* Set order
5393
*
94+
* @param string $attribute
95+
* @param string $dir
5496
* @return $this
5597
*/
56-
protected function _productLimitationJoinWebsite()
98+
public function setOrder($attribute, $dir = self::SORT_ORDER_DESC)
5799
{
58-
$filters = $this->_productLimitationFilters;
59-
$conditions = ['product_website.product_id=e.entity_id'];
60-
if (isset($filters['website_ids'])) {
61-
$conditions[] = $this->getConnection()->quoteInto(
62-
'product_website.website_id IN(?)',
63-
$filters['website_ids']
64-
);
65-
66-
$subQuery = $this->getConnection()->select()->from(
67-
['product_website' => $this->getTable('catalog_product_website')],
68-
['product_website.product_id']
69-
)->where(
70-
join(' AND ', $conditions)
71-
);
72-
$this->getSelect()->where('e.entity_id IN( ' . $subQuery . ' )');
100+
if (in_array($attribute, ['orders', 'ordered_qty'])) {
101+
$this->getSelect()->order($attribute . ' ' . $dir);
102+
} else {
103+
parent::setOrder($attribute, $dir);
73104
}
74105

75106
return $this;
76107
}
108+
109+
/**
110+
* Prepare between sql
111+
*
112+
* @param string $fieldName Field name with table suffix ('created_at' or 'main_table.created_at')
113+
* @param string $from
114+
* @param string $to
115+
* @return string Formatted sql string
116+
*/
117+
protected function prepareBetweenSql($fieldName, $from, $to)
118+
{
119+
return sprintf(
120+
'(%s BETWEEN %s AND %s)',
121+
$fieldName,
122+
$this->getConnection()->quote($from),
123+
$this->getConnection()->quote($to)
124+
);
125+
}
77126
}

dev/tests/static/testsuite/Magento/Test/Legacy/_files/obsolete_methods.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2159,4 +2159,6 @@
21592159
['addOrdersCount', 'Magento\Reports\Model\Resource\Customer\Collection'],
21602160
['addSumAvgTotals', 'Magento\Reports\Model\Resource\Customer\Collection'],
21612161
['orderByTotalAmount', 'Magento\Reports\Model\Resource\Customer\Collection'],
2162+
['addOrdersCount', 'Magento\Reports\Model\Resource\Product\Collection'],
2163+
['addOrderedQty', 'Magento\Reports\Model\Resource\Product\Collection'],
21622164
];

0 commit comments

Comments
 (0)