/** * Update rating position * * @param string $aggregation One of \Magento\Sales\Model\Resource\Report\Bestsellers::AGGREGATION_XXX constants * @param array $aggregationAliases * @param string $mainTable * @param string $aggregationTable * @return $this */ public function getBestsellersReportUpdateRatingPos($aggregation, $aggregationAliases, $mainTable, $aggregationTable) { if ($aggregation == $aggregationAliases['monthly']) { $this->_reportsResourceHelper->updateReportRatingPos('month', 'qty_ordered', $mainTable, $aggregationTable); } elseif ($aggregation == $aggregationAliases['yearly']) { $this->_reportsResourceHelper->updateReportRatingPos('year', 'qty_ordered', $mainTable, $aggregationTable); } else { $this->_reportsResourceHelper->updateReportRatingPos('day', 'qty_ordered', $mainTable, $aggregationTable); } return $this; }
/** * Update rating position * * @param string $aggregation One of \Magento\Sales\Model\Resource\Report\Bestsellers::AGGREGATION_XXX constants * @param array $aggregationAliases * @param string $mainTable * @param string $aggregationTable * @return $this */ public function getBestsellersReportUpdateRatingPos($aggregation, $aggregationAliases, $mainTable, $aggregationTable) { $adapter = $this->_resource->getConnection('sales_write'); if ($aggregation == $aggregationAliases['monthly']) { $this->_reportsResourceHelper->updateReportRatingPos($adapter, 'month', 'qty_ordered', $mainTable, $aggregationTable); } elseif ($aggregation == $aggregationAliases['yearly']) { $this->_reportsResourceHelper->updateReportRatingPos($adapter, 'year', 'qty_ordered', $mainTable, $aggregationTable); } else { $this->_reportsResourceHelper->updateReportRatingPos($adapter, 'day', 'qty_ordered', $mainTable, $aggregationTable); } return $this; }
/** * @param string $type * @param array $result * @dataProvider typesDataProvider * @return void */ public function testUpdateReportRatingPos($type, $result) { $mainTable = 'mainTable'; $column = 'column'; $aggregationTable = 'aggregationTable'; $selectMock = $this->getMockBuilder('Magento\\Framework\\DB\\Select')->disableOriginalConstructor()->getMock(); $selectMock->expects($this->any())->method('from')->willReturnSelf(); $selectMock->expects($this->once())->method('group')->willReturnSelf(); $selectMock->expects($this->once())->method('order')->willReturnSelf(); $selectMock->expects($this->once())->method('insertFromSelect')->with($aggregationTable, $result)->willReturnSelf(); $this->connectionMock->expects($this->any())->method('select')->willReturn($selectMock); $this->helper->updateReportRatingPos($this->connectionMock, $type, $column, $mainTable, $aggregationTable); }
/** * Aggregate products view data * * @param null|mixed $from * @param null|mixed $to * @return $this */ public function aggregate($from = null, $to = null) { $mainTable = $this->getMainTable(); $adapter = $this->_getWriteAdapter(); // 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); if ($from !== null || $to !== null) { $subSelect = $this->_getTableDateRangeSelect($this->getTable('report_event'), 'logged_at', 'logged_at', $from, $to); } else { $subSelect = null; } $this->_clearTableByDateRange($mainTable, $from, $to, $subSelect); // convert dates from UTC to current admin timezone $periodExpr = $adapter->getDatePartSql($this->getStoreTZOffsetQuery(array('source_table' => $this->getTable('report_event')), 'source_table.logged_at', $from, $to)); $select = $adapter->select(); $select->group(array($periodExpr, 'source_table.store_id', 'source_table.object_id')); $viewsNumExpr = new \Zend_Db_Expr('COUNT(source_table.event_id)'); $columns = array('period' => $periodExpr, 'store_id' => 'source_table.store_id', 'product_id' => 'source_table.object_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('MIN(%s)', $adapter->getIfNullSql($adapter->getIfNullSql('product_price.value', 'product_default_price.value'), 0))), 'views_num' => $viewsNumExpr); $select->from(array('source_table' => $this->getTable('report_event')), $columns)->where('source_table.event_type_id = ?', \Magento\Reports\Model\Event::EVENT_PRODUCT_VIEW); $select->joinInner(array('product' => $this->getTable('catalog_product_entity')), 'product.entity_id = source_table.object_id', array()); // join product attributes Name & Price $nameAttribute = $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.attribute_id = ?', $nameAttribute->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.attribute_id = ?', $nameAttribute->getAttributeId())); $joinProductName = implode(' AND ', $joinProductName); $select->joinLeft(array('product_name' => $nameAttribute->getBackend()->getTable()), $joinExprProductName, array())->joinLeft(array('product_default_name' => $nameAttribute->getBackend()->getTable()), $joinProductName, array()); $priceAttribute = $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.attribute_id = ?', $priceAttribute->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.attribute_id = ?', $priceAttribute->getAttributeId())); $joinProductPrice = implode(' AND ', $joinProductPrice); $select->joinLeft(array('product_price' => $priceAttribute->getBackend()->getTable()), $joinExprProductPrice, array())->joinLeft(array('product_default_price' => $priceAttribute->getBackend()->getTable()), $joinProductPrice, array()); $havingPart = array($adapter->prepareSqlCondition($viewsNumExpr, array('gt' => 0))); if (null !== $subSelect) { $subSelectHavingPart = $this->_makeConditionFromDateRangeSelect($subSelect, 'period'); if ($subSelectHavingPart) { $havingPart[] = '(' . $subSelectHavingPart . ')'; } } $select->having(implode(' AND ', $havingPart)); $select->useStraightJoin(); $insertQuery = $select->insertFromSelect($this->getMainTable(), array_keys($columns)); $adapter->query($insertQuery); $this->_resourceHelper->updateReportRatingPos('day', 'views_num', $mainTable, $this->getTable(self::AGGREGATION_DAILY)); $this->_resourceHelper->updateReportRatingPos('month', 'views_num', $mainTable, $this->getTable(self::AGGREGATION_MONTHLY)); $this->_resourceHelper->updateReportRatingPos('year', 'views_num', $mainTable, $this->getTable(self::AGGREGATION_YEARLY)); $this->_setFlagData(\Magento\Reports\Model\Flag::REPORT_PRODUCT_VIEWED_FLAG_CODE); return $this; }
/** * @param mixed $from * @param mixed $to * @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $truncateCount * @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $deleteCount * @dataProvider intervalsDataProvider * @return void */ public function testAggregate($from, $to, $truncateCount, $deleteCount) { $this->connectionMock->expects($truncateCount)->method('truncateTable'); $this->connectionMock->expects($deleteCount)->method('delete'); $this->helperMock->expects($this->at(0))->method('updateReportRatingPos')->with($this->connectionMock, 'day', 'views_num', 'report_viewed_product_aggregated_daily', 'report_viewed_product_aggregated_daily')->willReturnSelf(); $this->helperMock->expects($this->at(1))->method('updateReportRatingPos')->with($this->connectionMock, 'month', 'views_num', 'report_viewed_product_aggregated_daily', 'report_viewed_product_aggregated_monthly')->willReturnSelf(); $this->helperMock->expects($this->at(2))->method('updateReportRatingPos')->with($this->connectionMock, 'year', 'views_num', 'report_viewed_product_aggregated_daily', 'report_viewed_product_aggregated_yearly')->willReturnSelf(); $this->flagMock->expects($this->once())->method('unsetData')->willReturnSelf(); $this->flagMock->expects($this->once())->method('loadSelf')->willReturnSelf(); $this->flagMock->expects($this->never())->method('setFlagData')->willReturnSelf(); $this->flagMock->expects($this->once())->method('setLastUpdate')->willReturnSelf(); $this->flagMock->expects($this->once())->method('save')->willReturnSelf(); $this->flagMock->expects($this->once())->method('setReportFlagCode')->with(\Magento\Reports\Model\Flag::REPORT_PRODUCT_VIEWED_FLAG_CODE)->willReturnSelf(); $this->viewed->aggregate($from, $to); }
/** * Add information about product ids to visitor/customer * * @param \Magento\Framework\Object|\Magento\Reports\Model\Product\Index\AbstractIndex $object * @param array $productIds * @return $this */ public function registerIds(\Magento\Framework\Object $object, $productIds) { $row = ['visitor_id' => $object->getVisitorId(), 'customer_id' => $object->getCustomerId(), 'store_id' => $object->getStoreId()]; $addedAt = (new \DateTime())->getTimestamp(); $data = []; foreach ($productIds as $productId) { $productId = (int) $productId; if ($productId) { $row['product_id'] = $productId; $row['added_at'] = $this->dateTime->formatDate($addedAt); $data[] = $row; } $addedAt -= $addedAt > 0 ? 1 : 0; } $matchFields = ['product_id', 'store_id']; foreach ($data as $row) { $this->_resourceHelper->mergeVisitorProductIndex($this->getMainTable(), $row, $matchFields); } return $this; }