Example #1
0
 /**
  * @param \Magento\Core\Model\EntityFactory $entityFactory
  * @param \Magento\Framework\Logger $logger
  * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
  * @param \Magento\Framework\Event\ManagerInterface $eventManager
  * @param \Magento\Eav\Model\Config $eavConfig
  * @param \Magento\Framework\App\Resource $resource
  * @param \Magento\Eav\Model\EntityFactory $eavEntityFactory
  * @param \Magento\Catalog\Model\Resource\Helper $resourceHelper
  * @param \Magento\Framework\Validator\UniversalFactory $universalFactory
  * @param \Magento\Framework\StoreManagerInterface $storeManager
  * @param \Magento\Framework\Module\Manager $moduleManager
  * @param \Magento\Catalog\Model\Indexer\Product\Flat\State $catalogProductFlatState
  * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  * @param \Magento\Catalog\Model\Product\OptionFactory $productOptionFactory
  * @param \Magento\Catalog\Model\Resource\Url $catalogUrl
  * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
  * @param \Magento\Customer\Model\Session $customerSession
  * @param \Magento\Framework\Stdlib\DateTime $dateTime
  * @param \Magento\Catalog\Model\Resource\Product $product
  * @param \Magento\Reports\Model\Event\TypeFactory $eventTypeFactory
  * @param \Magento\Catalog\Model\Product\Type $productType
  * @param mixed $connection
  * 
  * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  */
 public function __construct(\Magento\Core\Model\EntityFactory $entityFactory, \Magento\Framework\Logger $logger, \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Eav\Model\Config $eavConfig, \Magento\Framework\App\Resource $resource, \Magento\Eav\Model\EntityFactory $eavEntityFactory, \Magento\Catalog\Model\Resource\Helper $resourceHelper, \Magento\Framework\Validator\UniversalFactory $universalFactory, \Magento\Framework\StoreManagerInterface $storeManager, \Magento\Framework\Module\Manager $moduleManager, \Magento\Catalog\Model\Indexer\Product\Flat\State $catalogProductFlatState, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Catalog\Model\Product\OptionFactory $productOptionFactory, \Magento\Catalog\Model\Resource\Url $catalogUrl, \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate, \Magento\Customer\Model\Session $customerSession, \Magento\Framework\Stdlib\DateTime $dateTime, \Magento\Catalog\Model\Resource\Product $product, \Magento\Reports\Model\Event\TypeFactory $eventTypeFactory, \Magento\Catalog\Model\Product\Type $productType, $connection = null)
 {
     $this->setProductEntityId($product->getEntityIdField());
     $this->setProductEntityTableName($product->getEntityTable());
     $this->setProductEntityTypeId($product->getTypeId());
     parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $eavConfig, $resource, $eavEntityFactory, $resourceHelper, $universalFactory, $storeManager, $moduleManager, $catalogProductFlatState, $scopeConfig, $productOptionFactory, $catalogUrl, $localeDate, $customerSession, $dateTime, $connection);
     $this->_eventTypeFactory = $eventTypeFactory;
     $this->_productType = $productType;
 }
Example #2
0
 /**
  * Aggregate Orders data by order created at
  *
  * @param string|int|\Zend_Date|array|null $from
  * @param string|int|\Zend_Date|array|null $to
  * @return $this
  * @throws \Exception
  */
 public function aggregate($from = null, $to = null)
 {
     // convert input dates to UTC to be comparable with DATETIME fields in DB
     $from = $this->_dateToUtc($from);
     $to = $this->_dateToUtc($to);
     $this->_checkDates($from, $to);
     $adapter = $this->_getWriteAdapter();
     //$this->_getWriteAdapter()->beginTransaction();
     try {
         if ($from !== null || $to !== null) {
             $subSelect = $this->_getTableDateRangeSelect($this->getTable('sales_flat_order'), 'created_at', 'updated_at', $from, $to);
         } else {
             $subSelect = null;
         }
         $this->_clearTableByDateRange($this->getMainTable(), $from, $to, $subSelect);
         // convert dates from UTC to current admin timezone
         $periodExpr = $adapter->getDatePartSql($this->getStoreTZOffsetQuery(array('source_table' => $this->getTable('sales_flat_order')), 'source_table.created_at', $from, $to));
         $select = $adapter->select();
         $select->group(array($periodExpr, 'source_table.store_id', 'order_item.product_id'));
         $columns = array('period' => $periodExpr, 'store_id' => 'source_table.store_id', 'product_id' => 'order_item.product_id', 'product_name' => new \Zend_Db_Expr(sprintf('MIN(%s)', $adapter->getIfNullSql('product_name.value', 'product_default_name.value'))), 'product_price' => new \Zend_Db_Expr(sprintf('%s * %s', new \Zend_Db_Expr(sprintf('MIN(%s)', $adapter->getIfNullSql($adapter->getIfNullSql('product_price.value', 'product_default_price.value'), 0))), new \Zend_Db_Expr(sprintf('MIN(%s)', $adapter->getIfNullSql('source_table.base_to_global_rate', '0'))))), 'qty_ordered' => new \Zend_Db_Expr('SUM(order_item.qty_ordered)'));
         $select->from(array('source_table' => $this->getTable('sales_flat_order')), $columns)->joinInner(array('order_item' => $this->getTable('sales_flat_order_item')), 'order_item.order_id = source_table.entity_id', array())->where('source_table.state != ?', \Magento\Sales\Model\Order::STATE_CANCELED);
         $joinExpr = array('product.entity_id = order_item.product_id', $adapter->quoteInto('product.entity_type_id = ?', $this->_productResource->getTypeId()), $adapter->quoteInto('product.type_id NOT IN(?)', $this->ignoredProductTypes));
         $joinExpr = implode(' AND ', $joinExpr);
         $select->joinInner(array('product' => $this->getTable('catalog_product_entity')), $joinExpr, array());
         // join product attributes Name & Price
         $attr = $this->_productResource->getAttribute('name');
         $joinExprProductName = array('product_name.entity_id = product.entity_id', 'product_name.store_id = source_table.store_id', $adapter->quoteInto('product_name.entity_type_id = ?', $this->_productResource->getTypeId()), $adapter->quoteInto('product_name.attribute_id = ?', $attr->getAttributeId()));
         $joinExprProductName = implode(' AND ', $joinExprProductName);
         $joinProductName = array('product_default_name.entity_id = product.entity_id', 'product_default_name.store_id = 0', $adapter->quoteInto('product_default_name.entity_type_id = ?', $this->_productResource->getTypeId()), $adapter->quoteInto('product_default_name.attribute_id = ?', $attr->getAttributeId()));
         $joinProductName = implode(' AND ', $joinProductName);
         $select->joinLeft(array('product_name' => $attr->getBackend()->getTable()), $joinExprProductName, array())->joinLeft(array('product_default_name' => $attr->getBackend()->getTable()), $joinProductName, array());
         $attr = $this->_productResource->getAttribute('price');
         $joinExprProductPrice = array('product_price.entity_id = product.entity_id', 'product_price.store_id = source_table.store_id', $adapter->quoteInto('product_price.entity_type_id = ?', $this->_productResource->getTypeId()), $adapter->quoteInto('product_price.attribute_id = ?', $attr->getAttributeId()));
         $joinExprProductPrice = implode(' AND ', $joinExprProductPrice);
         $joinProductPrice = array('product_default_price.entity_id = product.entity_id', 'product_default_price.store_id = 0', $adapter->quoteInto('product_default_price.entity_type_id = ?', $this->_productResource->getTypeId()), $adapter->quoteInto('product_default_price.attribute_id = ?', $attr->getAttributeId()));
         $joinProductPrice = implode(' AND ', $joinProductPrice);
         $select->joinLeft(array('product_price' => $attr->getBackend()->getTable()), $joinExprProductPrice, array())->joinLeft(array('product_default_price' => $attr->getBackend()->getTable()), $joinProductPrice, array());
         if ($subSelect !== null) {
             $select->having($this->_makeConditionFromDateRangeSelect($subSelect, 'period'));
         }
         $select->useStraightJoin();
         // important!
         $insertQuery = $select->insertFromSelect($this->getMainTable(), array_keys($columns));
         $adapter->query($insertQuery);
         $columns = array('period' => 'period', 'store_id' => new \Zend_Db_Expr(\Magento\Store\Model\Store::DEFAULT_STORE_ID), 'product_id' => 'product_id', 'product_name' => new \Zend_Db_Expr('MIN(product_name)'), 'product_price' => new \Zend_Db_Expr('MIN(product_price)'), 'qty_ordered' => new \Zend_Db_Expr('SUM(qty_ordered)'));
         $select->reset();
         $select->from($this->getMainTable(), $columns)->where('store_id <> ?', \Magento\Store\Model\Store::DEFAULT_STORE_ID);
         if ($subSelect !== null) {
             $select->where($this->_makeConditionFromDateRangeSelect($subSelect, 'period'));
         }
         $select->group(array('period', 'product_id'));
         $insertQuery = $select->insertFromSelect($this->getMainTable(), array_keys($columns));
         $adapter->query($insertQuery);
         // update rating
         $this->_updateRatingPos(self::AGGREGATION_DAILY);
         $this->_updateRatingPos(self::AGGREGATION_MONTHLY);
         $this->_updateRatingPos(self::AGGREGATION_YEARLY);
         $this->_setFlagData(\Magento\Reports\Model\Flag::REPORT_BESTSELLERS_FLAG_CODE);
     } catch (\Exception $e) {
         throw $e;
     }
     return $this;
 }