| Current Path : /home/rtorresani/www/vendor/magento/module-reports/Model/ResourceModel/Product/Sold/ |
| Current File : //home/rtorresani/www/vendor/magento/module-reports/Model/ResourceModel/Product/Sold/Collection.php |
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Report Sold Products collection
*
* @author Magento Core Team <core@magentocommerce.com>
*/
namespace Magento\Reports\Model\ResourceModel\Product\Sold;
use Magento\Framework\DB\Select;
use Zend_Db_Select_Exception;
/**
* Data collection.
*
* @SuppressWarnings(PHPMD.DepthOfInheritance)
* @api
* @since 100.0.2
*/
class Collection extends \Magento\Reports\Model\ResourceModel\Order\Collection
{
/**
* Set Date range to collection.
*
* @param string $from
* @param string $to
* @return $this
* @throws Zend_Db_Select_Exception
*/
public function setDateRange($from, $to)
{
$this->_reset()->addAttributeToSelect(
'*'
)->addOrderedQty(
$from,
$to
)->setOrder(
'ordered_qty',
self::SORT_ORDER_DESC
);
return $this;
}
/**
* Add ordered qty's
*
* @param string $from
* @param string $to
* @return $this
* @throws Zend_Db_Select_Exception
*/
public function addOrderedQty($from = '', $to = '')
{
$connection = $this->getConnection();
$orderTableAliasName = $connection->quoteIdentifier('order');
$orderJoinCondition = [
$orderTableAliasName . '.entity_id = order_items.order_id',
$connection->quoteInto("{$orderTableAliasName}.state <> ?", \Magento\Sales\Model\Order::STATE_CANCELED),
];
if ($from != '' && $to != '') {
$fieldName = $orderTableAliasName . '.created_at';
$orderJoinCondition[] = $this->prepareBetweenSql($fieldName, $from, $to);
}
$this->getSelect()->reset()->from(
['order_items' => $this->getTable('sales_order_item')],
[
'ordered_qty' => 'order_items.qty_ordered',
'order_items_name' => 'order_items.name',
'order_items_sku' => 'order_items.sku'
]
)->joinInner(
['order' => $this->getTable('sales_order')],
implode(' AND ', $orderJoinCondition),
[]
)->where(
'order_items.parent_item_id IS NULL'
)->having(
'order_items.qty_ordered > ?',
0
)->columns(
'SUM(order_items.qty_ordered) as ordered_qty'
)->group(
'order_items.sku'
);
return $this;
}
/**
* Set store filter to collection
*
* @param array $storeIds
* @return $this
*/
public function setStoreIds($storeIds)
{
if ($storeIds) {
$this->getSelect()->where('order_items.store_id IN (?)', (array)$storeIds);
}
return $this;
}
/**
* Set order
*
* @param string $attribute
* @param string $dir
* @return $this
*/
public function setOrder($attribute, $dir = self::SORT_ORDER_DESC)
{
if (in_array($attribute, ['orders', 'ordered_qty'])) {
$this->getSelect()->order($attribute . ' ' . $dir);
} else {
parent::setOrder($attribute, $dir);
}
return $this;
}
/**
* @inheritdoc
*
* @return Select
* @since 100.2.0
*/
public function getSelectCountSql()
{
$countSelect = clone parent::getSelectCountSql();
$countSelect->reset(Select::COLUMNS);
$countSelect->columns('COUNT(DISTINCT order_items.item_id)');
return $countSelect;
}
/**
* Prepare between sql
*
* @param string $fieldName Field name with table suffix ('created_at' or 'main_table.created_at')
* @param string $from
* @param string $to
* @return string Formatted sql string
*/
protected function prepareBetweenSql($fieldName, $from, $to)
{
return sprintf(
'(%s BETWEEN %s AND %s)',
$fieldName,
$this->getConnection()->quote($from),
$this->getConnection()->quote($to)
);
}
}