/** * Update rating position * * @param string $aggregation One of BestsellersReport::AGGREGATION_XXX constants * @param array $aggregationAliases * @param string $mainTable * @param string $aggregationTable * @return $this */ public function getBestsellersReportUpdateRatingPos($aggregation, $aggregationAliases, $mainTable, $aggregationTable) { $connection = $this->_resource->getConnection('sales'); if ($aggregation == $aggregationAliases['monthly']) { $this->_reportsResourceHelper->updateReportRatingPos($connection, 'month', 'qty_ordered', $mainTable, $aggregationTable); } elseif ($aggregation == $aggregationAliases['yearly']) { $this->_reportsResourceHelper->updateReportRatingPos($connection, 'year', 'qty_ordered', $mainTable, $aggregationTable); } else { $this->_reportsResourceHelper->updateReportRatingPos($connection, '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); }
/** * Add information about product ids to visitor/customer * * @param \Magento\Framework\DataObject|\Magento\Reports\Model\Product\Index\AbstractIndex $object * @param int[] $productIds * @return $this */ public function registerIds(\Magento\Framework\DataObject $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; }
/** * @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); }
/** * Aggregate products view data * * @param null|mixed $from * @param null|mixed $to * @return $this * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function aggregate($from = null, $to = null) { $mainTable = $this->getMainTable(); $connection = $this->getConnection(); 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 to current admin timezone $periodExpr = $connection->getDatePartSql($this->getStoreTZOffsetQuery(['source_table' => $this->getTable('report_event')], 'source_table.logged_at', $from, $to)); $select = $connection->select(); $select->group([$periodExpr, 'source_table.store_id', 'source_table.object_id']); $viewsNumExpr = new \Zend_Db_Expr('COUNT(source_table.event_id)'); $columns = ['period' => $periodExpr, 'store_id' => 'source_table.store_id', 'product_id' => 'source_table.object_id', 'product_name' => new \Zend_Db_Expr(sprintf('MIN(%s)', $connection->getIfNullSql('product_name.value', 'product_default_name.value'))), 'product_price' => new \Zend_Db_Expr(sprintf('MIN(%s)', $connection->getIfNullSql($connection->getIfNullSql('product_price.value', 'product_default_price.value'), 0))), 'views_num' => $viewsNumExpr]; $select->from(['source_table' => $this->getTable('report_event')], $columns)->where('source_table.event_type_id = ?', \Magento\Reports\Model\Event::EVENT_PRODUCT_VIEW); $productLinkField = $this->_productResource->getLinkField(); $select->joinInner(['product' => $this->getTable('catalog_product_entity')], "product.{$productLinkField} = source_table.object_id", []); // join product attributes Name & Price $nameAttribute = $this->_productResource->getAttribute('name'); $joinExprProductName = ["product_name.{$productLinkField} = product.{$productLinkField}", 'product_name.store_id = source_table.store_id', $connection->quoteInto('product_name.attribute_id = ?', $nameAttribute->getAttributeId())]; $joinExprProductName = implode(' AND ', $joinExprProductName); $joinProductName = ["product_default_name.{$productLinkField} = product.{$productLinkField}", 'product_default_name.store_id = 0', $connection->quoteInto('product_default_name.attribute_id = ?', $nameAttribute->getAttributeId())]; $joinProductName = implode(' AND ', $joinProductName); $select->joinLeft(['product_name' => $nameAttribute->getBackend()->getTable()], $joinExprProductName, [])->joinLeft(['product_default_name' => $nameAttribute->getBackend()->getTable()], $joinProductName, []); $priceAttribute = $this->_productResource->getAttribute('price'); $joinExprProductPrice = ["product_price.{$productLinkField} = product.{$productLinkField}", 'product_price.store_id = source_table.store_id', $connection->quoteInto('product_price.attribute_id = ?', $priceAttribute->getAttributeId())]; $joinExprProductPrice = implode(' AND ', $joinExprProductPrice); $joinProductPrice = ["product_default_price.{$productLinkField} = product.{$productLinkField}", 'product_default_price.store_id = 0', $connection->quoteInto('product_default_price.attribute_id = ?', $priceAttribute->getAttributeId())]; $joinProductPrice = implode(' AND ', $joinProductPrice); $select->joinLeft(['product_price' => $priceAttribute->getBackend()->getTable()], $joinExprProductPrice, [])->joinLeft(['product_default_price' => $priceAttribute->getBackend()->getTable()], $joinProductPrice, []); $havingPart = [$connection->prepareSqlCondition($viewsNumExpr, ['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)); $connection->query($insertQuery); $this->_resourceHelper->updateReportRatingPos($connection, 'day', 'views_num', $mainTable, $this->getTable(self::AGGREGATION_DAILY)); $this->_resourceHelper->updateReportRatingPos($connection, 'month', 'views_num', $mainTable, $this->getTable(self::AGGREGATION_MONTHLY)); $this->_resourceHelper->updateReportRatingPos($connection, 'year', 'views_num', $mainTable, $this->getTable(self::AGGREGATION_YEARLY)); $this->_setFlagData(\Magento\Reports\Model\Flag::REPORT_PRODUCT_VIEWED_FLAG_CODE); return $this; }
/** * Add information about product ids to visitor/customer * * @param \Magento\Framework\DataObject|\Magento\Reports\Model\Product\Index\AbstractIndex $object * @param int[] $productIds * @return $this */ public function registerIds(\Magento\Framework\DataObject $object, $productIds) { $row = ['visitor_id' => $object->getVisitorId(), 'customer_id' => $object->getCustomerId(), 'store_id' => $object->getStoreId()]; $data = []; foreach ($productIds as $productId) { $productId = (int) $productId; if ($productId) { $row['product_id'] = $productId; $data[] = $row; } } $matchFields = ['product_id', 'store_id']; foreach ($data as $row) { $this->_resourceHelper->mergeVisitorProductIndex($this->getMainTable(), $row, $matchFields); } return $this; }