![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/vendor/magento/module-reports/Model/ResourceModel/Product/Sold/ |
<?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ /** * Report Sold Products collection * * @author Magento Core Team <[email protected]> */ 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) ); } }