PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Reports/Model/Resource/Quote/Collection.php

https://bitbucket.org/kdms/sh-magento
PHP | 295 lines | 179 code | 30 blank | 86 comment | 15 complexity | 7d61515b7ff675e86e170c84bf9e44f2 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento Enterprise Edition
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Magento Enterprise Edition License
  8. * that is bundled with this package in the file LICENSE_EE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://www.magentocommerce.com/license/enterprise-edition
  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) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://www.magentocommerce.com/license/enterprise-edition
  25. */
  26. /**
  27. * Reports quote collection
  28. *
  29. * @category Mage
  30. * @package Mage_Reports
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Reports_Model_Resource_Quote_Collection extends Mage_Sales_Model_Resource_Quote_Collection
  34. {
  35. const SELECT_COUNT_SQL_TYPE_CART = 1;
  36. protected $_selectCountSqlType = 0;
  37. /**
  38. * Join fields
  39. *
  40. * @var array
  41. */
  42. protected $_joinedFields = array();
  43. /**
  44. * Map
  45. *
  46. * @var array
  47. */
  48. protected $_map = array('fields' => array('store_id' => 'main_table.store_id'));
  49. /**
  50. * Set type for COUNT SQL select
  51. *
  52. * @param int $type
  53. * @return Mage_Reports_Model_Mysql4_Quote_Collection
  54. */
  55. public function setSelectCountSqlType($type)
  56. {
  57. $this->_selectCountSqlType = $type;
  58. return $this;
  59. }
  60. protected function _construct()
  61. {
  62. parent::_construct();
  63. /**
  64. * Allow to use analytic function
  65. */
  66. $this->_useAnalyticFunction = true;
  67. }
  68. /**
  69. * Prepare for abandoned report
  70. *
  71. * @param array $storeIds
  72. * @param string $filter
  73. * @return Mage_Reports_Model_Resource_Quote_Collection
  74. */
  75. public function prepareForAbandonedReport($storeIds, $filter = null)
  76. {
  77. $this->addFieldToFilter('items_count', array('neq' => '0'))
  78. ->addFieldToFilter('main_table.is_active', '1')
  79. ->addSubtotal($storeIds, $filter)
  80. ->addCustomerData($filter)
  81. ->setOrder('updated_at');
  82. if (is_array($storeIds) && !empty($storeIds)) {
  83. $this->addFieldToFilter('store_id', array('in' => $storeIds));
  84. }
  85. return $this;
  86. }
  87. /**
  88. * Prepare select query for products in carts report
  89. *
  90. * @return Mage_Reports_Model_Resource_Quote_Collection
  91. */
  92. public function prepareForProductsInCarts()
  93. {
  94. $productEntity = Mage::getResourceSingleton('catalog/product_collection');
  95. $productAttrName = $productEntity->getAttribute('name');
  96. $productAttrNameId = (int) $productAttrName->getAttributeId();
  97. $productAttrNameTable = $productAttrName->getBackend()->getTable();
  98. $productAttrPrice = $productEntity->getAttribute('price');
  99. $productAttrPriceId = (int) $productAttrPrice->getAttributeId();
  100. $productAttrPriceTable = $productAttrPrice->getBackend()->getTable();
  101. $ordersSubSelect = clone $this->getSelect();
  102. $ordersSubSelect->reset()
  103. ->from(
  104. array('oi' => $this->getTable('sales/order_item')),
  105. array(
  106. 'orders' => new Zend_Db_Expr('COUNT(1)'),
  107. 'product_id'))
  108. ->group('oi.product_id');
  109. $this->getSelect()
  110. ->useStraightJoin(true)
  111. ->reset(Zend_Db_Select::COLUMNS)
  112. ->joinInner(
  113. array('quote_items' => $this->getTable('sales/quote_item')),
  114. 'quote_items.quote_id = main_table.entity_id',
  115. null)
  116. ->joinInner(
  117. array('e' => $this->getTable('catalog/product')),
  118. 'e.entity_id = quote_items.product_id',
  119. null)
  120. ->joinInner(
  121. array('product_name' => $productAttrNameTable),
  122. "product_name.entity_id = e.entity_id AND product_name.attribute_id = {$productAttrNameId}",
  123. array('name'=>'product_name.value'))
  124. ->joinInner(
  125. array('product_price' => $productAttrPriceTable),
  126. "product_price.entity_id = e.entity_id AND product_price.attribute_id = {$productAttrPriceId}",
  127. array('price' => new Zend_Db_Expr('product_price.value * main_table.base_to_global_rate')))
  128. ->joinLeft(
  129. array('order_items' => new Zend_Db_Expr(sprintf('(%s)', $ordersSubSelect))),
  130. 'order_items.product_id = e.entity_id',
  131. array()
  132. )
  133. ->columns('e.*')
  134. ->columns(array('carts' => new Zend_Db_Expr('COUNT(quote_items.item_id)')))
  135. ->columns('order_items.orders')
  136. ->where('main_table.is_active = ?', 1)
  137. ->group('quote_items.product_id');
  138. return $this;
  139. }
  140. /**
  141. * Add store ids to filter
  142. *
  143. * @param array $storeIds
  144. * @return Mage_Reports_Model_Resource_Quote_Collection
  145. */
  146. public function addStoreFilter($storeIds)
  147. {
  148. $this->addFieldToFilter('store_id', array('in' => $storeIds));
  149. return $this;
  150. }
  151. /**
  152. * Add customer data
  153. *
  154. * @param unknown_type $filter
  155. * @return Mage_Reports_Model_Resource_Quote_Collection
  156. */
  157. public function addCustomerData($filter = null)
  158. {
  159. $customerEntity = Mage::getResourceSingleton('customer/customer');
  160. $attrFirstname = $customerEntity->getAttribute('firstname');
  161. $attrFirstnameId = (int) $attrFirstname->getAttributeId();
  162. $attrFirstnameTableName = $attrFirstname->getBackend()->getTable();
  163. $attrLastname = $customerEntity->getAttribute('lastname');
  164. $attrLastnameId = (int) $attrLastname->getAttributeId();
  165. $attrLastnameTableName = $attrLastname->getBackend()->getTable();
  166. $attrEmail = $customerEntity->getAttribute('email');
  167. $attrEmailTableName = $attrEmail->getBackend()->getTable();
  168. $adapter = $this->getSelect()->getAdapter();
  169. $customerName = $adapter->getConcatSql(array('cust_fname.value', 'cust_lname.value'), ' ');
  170. $this->getSelect()
  171. ->joinInner(
  172. array('cust_email' => $attrEmailTableName),
  173. 'cust_email.entity_id = main_table.customer_id',
  174. array('email' => 'cust_email.email')
  175. )
  176. ->joinInner(
  177. array('cust_fname' => $attrFirstnameTableName),
  178. implode(' AND ', array(
  179. 'cust_fname.entity_id = main_table.customer_id',
  180. $adapter->quoteInto('cust_fname.attribute_id = ?', (int)$attrFirstnameId),
  181. )),
  182. array('firstname' => 'cust_fname.value')
  183. )
  184. ->joinInner(
  185. array('cust_lname' => $attrLastnameTableName),
  186. implode(' AND ', array(
  187. 'cust_lname.entity_id = main_table.customer_id',
  188. $adapter->quoteInto('cust_lname.attribute_id = ?', (int)$attrLastnameId)
  189. )),
  190. array(
  191. 'lastname' => 'cust_lname.value',
  192. 'customer_name' => $customerName
  193. )
  194. );
  195. $this->_joinedFields['customer_name'] = $customerName;
  196. $this->_joinedFields['email'] = 'cust_email.email';
  197. if ($filter) {
  198. if (isset($filter['customer_name'])) {
  199. $likeExpr = '%' . $filter['customer_name'] . '%';
  200. $this->getSelect()->where($this->_joinedFields['customer_name'] . ' LIKE ?', $likeExpr);
  201. }
  202. if (isset($filter['email'])) {
  203. $likeExpr = '%' . $filter['email'] . '%';
  204. $this->getSelect()->where($this->_joinedFields['email'] . ' LIKE ?', $likeExpr);
  205. }
  206. }
  207. return $this;
  208. }
  209. /**
  210. * Add subtotals
  211. *
  212. * @param array $storeIds
  213. * @param array $filter
  214. * @return Mage_Reports_Model_Resource_Quote_Collection
  215. */
  216. public function addSubtotal($storeIds = '', $filter = null)
  217. {
  218. if (is_array($storeIds)) {
  219. $this->getSelect()->columns(array(
  220. 'subtotal' => '(main_table.base_subtotal_with_discount*main_table.base_to_global_rate)'
  221. ));
  222. $this->_joinedFields['subtotal'] =
  223. '(main_table.base_subtotal_with_discount*main_table.base_to_global_rate)';
  224. } else {
  225. $this->getSelect()->columns(array('subtotal' => 'main_table.base_subtotal_with_discount'));
  226. $this->_joinedFields['subtotal'] = 'main_table.base_subtotal_with_discount';
  227. }
  228. if ($filter && is_array($filter) && isset($filter['subtotal'])) {
  229. if (isset($filter['subtotal']['from'])) {
  230. $this->getSelect()->where(
  231. $this->_joinedFields['subtotal'] . ' >= ?',
  232. $filter['subtotal']['from'], Zend_Db::FLOAT_TYPE
  233. );
  234. }
  235. if (isset($filter['subtotal']['to'])) {
  236. $this->getSelect()->where(
  237. $this->_joinedFields['subtotal'] . ' <= ?',
  238. $filter['subtotal']['to'], Zend_Db::FLOAT_TYPE
  239. );
  240. }
  241. }
  242. return $this;
  243. }
  244. /**
  245. * Get select count sql
  246. *
  247. * @return unknown
  248. */
  249. public function getSelectCountSql()
  250. {
  251. $countSelect = clone $this->getSelect();
  252. $countSelect->reset(Zend_Db_Select::ORDER);
  253. $countSelect->reset(Zend_Db_Select::LIMIT_COUNT);
  254. $countSelect->reset(Zend_Db_Select::LIMIT_OFFSET);
  255. $countSelect->reset(Zend_Db_Select::COLUMNS);
  256. $countSelect->reset(Zend_Db_Select::GROUP);
  257. $countSelect->resetJoinLeft();
  258. if ($this->_selectCountSqlType == self::SELECT_COUNT_SQL_TYPE_CART) {
  259. $countSelect->columns("COUNT(DISTINCT e.entity_id)");
  260. } else {
  261. $countSelect->columns("COUNT(DISTINCT main_table.entity_id)");
  262. }
  263. return $countSelect;
  264. }
  265. }