PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/app/code/core/Mage/Reports/Model/Mysql4/Product/Collection.php

https://github.com/ticean/magento-mirror
PHP | 319 lines | 222 code | 42 blank | 55 comment | 25 complexity | 038fd7a4b96f4cc6c6c47246e67e5064 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Reports
  23. * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Products Report collection
  28. *
  29. * @category Mage
  30. * @package Mage_Reports
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Reports_Model_Mysql4_Product_Collection extends Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
  34. {
  35. const SELECT_COUNT_SQL_TYPE_CART = 1;
  36. protected $_productEntityId;
  37. protected $_productEntityTableName;
  38. protected $_productEntityTypeId;
  39. protected $_selectCountSqlType = 0;
  40. /**
  41. * Set Type for COUNT SQL Select
  42. *
  43. * @param int $type
  44. * @return Mage_Reports_Model_Mysql4_Product_Collection
  45. */
  46. public function setSelectCountSqlType($type)
  47. {
  48. $this->_selectCountSqlType = $type;
  49. return $this;
  50. }
  51. public function setProductEntityId($value)
  52. {
  53. $this->_productEntityId = $value;
  54. return $this;
  55. }
  56. public function getProductEntityId()
  57. {
  58. return $this->_productEntityId;
  59. }
  60. public function setProductEntityTableName($value)
  61. {
  62. $this->_productEntityTableName = $value;
  63. return $this;
  64. }
  65. public function getProductEntityTableName()
  66. {
  67. return $this->_productEntityTableName;
  68. }
  69. public function setProductEntityTypeId($value)
  70. {
  71. $this->_productEntityTypeId = $value;
  72. return $this;
  73. }
  74. public function getProductEntityTypeId()
  75. {
  76. return $this->_productEntityTypeId;
  77. }
  78. public function __construct()
  79. {
  80. $product = Mage::getResourceSingleton('catalog/product');
  81. /* @var $product Mage_Catalog_Model_Entity_Product */
  82. $this->setProductEntityId($product->getEntityIdField());
  83. $this->setProductEntityTableName($product->getEntityTable());
  84. $this->setProductEntityTypeId($product->getTypeId());
  85. parent::__construct();
  86. }
  87. protected function _joinFields()
  88. {
  89. $this->_totals = new Varien_Object();
  90. $this->addAttributeToSelect('entity_id')
  91. ->addAttributeToSelect('name')
  92. ->addAttributeToSelect('price');
  93. /*$this->getSelect()->columns(array(
  94. 'viewed' => 'CONCAT("","")',
  95. 'added' => 'CONCAT("","")',
  96. 'purchased' => 'CONCAT("","")',
  97. 'fulfilled' => 'CONCAT("","")',
  98. 'revenue' => 'CONCAT("","")',
  99. ));*/
  100. }
  101. public function getSelectCountSql()
  102. {
  103. if ($this->_selectCountSqlType == self::SELECT_COUNT_SQL_TYPE_CART) {
  104. $countSelect = clone $this->getSelect();
  105. $countSelect->reset()
  106. ->from(
  107. array('quote_item_table' => $this->getTable('sales/quote_item')),
  108. 'COUNT(DISTINCT quote_item_table.product_id)'
  109. )
  110. ->join(
  111. array('quote_table' => $this->getTable('sales/quote')),
  112. 'quote_table.entity_id = quote_item_table.quote_id AND quote_table.is_active = 1',
  113. array()
  114. );
  115. return $countSelect->__toString();
  116. }
  117. $countSelect = clone $this->getSelect();
  118. $countSelect->reset(Zend_Db_Select::ORDER);
  119. $countSelect->reset(Zend_Db_Select::LIMIT_COUNT);
  120. $countSelect->reset(Zend_Db_Select::LIMIT_OFFSET);
  121. $countSelect->reset(Zend_Db_Select::COLUMNS);
  122. $countSelect->reset(Zend_Db_Select::GROUP);
  123. $countSelect->reset(Zend_Db_Select::HAVING);
  124. $countSelect->columns("count(DISTINCT e.entity_id)");
  125. $sql = $countSelect->__toString();
  126. return $sql;
  127. }
  128. public function addCartsCount()
  129. {
  130. $countSelect = clone $this->getSelect();
  131. $countSelect->reset();
  132. $countSelect->from(array("quote_items" => $this->getTable('sales/quote_item')), "count(*)")
  133. ->join(array('quotes' => $this->getTable('sales/quote')),
  134. 'quotes.entity_id = quote_items.quote_id AND quotes.is_active = 1',
  135. array())
  136. ->where("quote_items.product_id = e.entity_id");
  137. $this->getSelect()
  138. ->columns(array("carts" => "({$countSelect})"))
  139. ->group("e.{$this->getProductEntityId()}")
  140. ->having('carts > 0');
  141. return $this;
  142. }
  143. public function addOrdersCount($from = '', $to = '')
  144. {
  145. $this->getSelect()
  146. ->joinLeft(array("order_items" => $this->getTable('sales/order_item')),
  147. "order_items.product_id = e.{$this->getProductEntityId()}", array())
  148. ->columns(array("orders" => "count(`order_items2`.item_id)"))
  149. ->group("e.{$this->getProductEntityId()}");
  150. if ($from != '' && $to != '') {
  151. $dateFilter = " and order_items2.created_at BETWEEN '{$from}' AND '{$to}'";
  152. } else {
  153. $dateFilter = '';
  154. }
  155. $this->getSelect()
  156. ->joinLeft(array("order_items2" => $this->getTable('sales/order_item')),
  157. "order_items2.item_id = order_items.item_id".$dateFilter, array());
  158. return $this;
  159. }
  160. public function addOrderedQty($from = '', $to = '')
  161. {
  162. $qtyOrderedTableName = $this->getTable('sales/order_item');
  163. $qtyOrderedFieldName = 'qty_ordered';
  164. $productIdFieldName = 'product_id';
  165. $compositeTypeIds = Mage::getSingleton('catalog/product_type')->getCompositeTypes();
  166. $productTypes = $this->getConnection()->quoteInto(' AND (e.type_id NOT IN (?))', $compositeTypeIds);
  167. if ($from != '' && $to != '') {
  168. $dateFilter = " AND `order`.created_at BETWEEN '{$from}' AND '{$to}'";
  169. } else {
  170. $dateFilter = "";
  171. }
  172. $this->getSelect()->reset()->from(
  173. array('order_items' => $qtyOrderedTableName),
  174. array(
  175. 'ordered_qty' => "SUM(order_items.{$qtyOrderedFieldName})",
  176. 'order_items_name' => 'order_items.name'
  177. )
  178. );
  179. $_joinCondition = $this->getConnection()->quoteInto(
  180. 'order.entity_id = order_items.order_id AND order.state<>?', Mage_Sales_Model_Order::STATE_CANCELED
  181. );
  182. $_joinCondition .= $dateFilter;
  183. $this->getSelect()->joinInner(
  184. array('order' => $this->getTable('sales/order')),
  185. $_joinCondition,
  186. array()
  187. );
  188. $this->getSelect()
  189. ->joinLeft(array('e' => $this->getProductEntityTableName()),
  190. "e.entity_id = order_items.{$productIdFieldName}
  191. AND e.entity_type_id = {$this->getProductEntityTypeId()}{$productTypes}",
  192. array(
  193. 'entity_id' => 'order_items.product_id',
  194. 'entity_type_id' => 'e.entity_type_id',
  195. 'attribute_set_id' => 'e.attribute_set_id',
  196. 'type_id' => 'e.type_id',
  197. 'sku' => 'e.sku',
  198. 'has_options' => 'e.has_options',
  199. 'required_options' => 'e.required_options',
  200. 'created_at' => 'e.created_at',
  201. 'updated_at' => 'e.updated_at'
  202. )
  203. )
  204. ->group('order_items.product_id')
  205. ->having('ordered_qty > 0');
  206. return $this;
  207. }
  208. public function setOrder($attribute, $dir='desc')
  209. {
  210. switch ($attribute)
  211. {
  212. case 'carts':
  213. case 'orders':
  214. case 'ordered_qty':
  215. $this->getSelect()->order($attribute . ' ' . $dir);
  216. break;
  217. default:
  218. parent::setOrder($attribute, $dir);
  219. }
  220. return $this;
  221. }
  222. public function addViewsCount($from = '', $to = '')
  223. {
  224. /**
  225. * Getting event type id for catalog_product_view event
  226. */
  227. foreach (Mage::getModel('reports/event_type')->getCollection() as $eventType) {
  228. if ($eventType->getEventName() == 'catalog_product_view') {
  229. $productViewEvent = $eventType->getId();
  230. break;
  231. }
  232. }
  233. $this->getSelect()->reset()
  234. ->from(
  235. array('_table_views' => $this->getTable('reports/event')),
  236. array('views' => 'COUNT(_table_views.event_id)'))
  237. ->join(array('e' => $this->getProductEntityTableName()),
  238. "e.entity_id = _table_views.object_id AND e.entity_type_id = {$this->getProductEntityTypeId()}")
  239. ->where('_table_views.event_type_id = ?', $productViewEvent)
  240. ->group('e.entity_id')
  241. ->order('views desc')
  242. ->having('views > 0');
  243. if ($from != '' && $to != '') {
  244. $this->getSelect()
  245. ->where('logged_at >= ?', $from)
  246. ->where('logged_at <= ?', $to);
  247. }
  248. return $this;
  249. }
  250. /**
  251. * Add store restrictions to product collection
  252. *
  253. * @param array $storeIds
  254. * @param array $websiteIds
  255. * @return Mage_Reports_Model_Mysql4_Product_Collection
  256. */
  257. public function addStoreRestrictions($storeIds, $websiteIds)
  258. {
  259. if (!is_array($storeIds)) {
  260. $storeIds = array($storeIds);
  261. }
  262. if (!is_array($websiteIds)) {
  263. $websiteIds = array($websiteIds);
  264. }
  265. $filters = $this->_productLimitationFilters;
  266. if ($filters['store_id']) {
  267. if (!in_array($filters['store_id'], $storeIds)) {
  268. $this->addStoreFilter($filters['store_id']);
  269. } else {
  270. $this->addStoreFilter($this->getStoreId());
  271. }
  272. } else {
  273. $this->addWebsiteFilter($websiteIds);
  274. }
  275. return $this;
  276. }
  277. }