PageRenderTime 58ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/magento/app/code/core/Mage/Sales/Model/Entity/Sale/Collection.php

https://bitbucket.org/jit_bec/shopifine
PHP | 204 lines | 100 code | 23 blank | 81 comment | 5 complexity | f3639c53f9aa63771383387d54cb019f MD5 | raw file
Possible License(s): LGPL-3.0
  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_Sales
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. class Mage_Sales_Model_Entity_Sale_Collection extends Varien_Object implements IteratorAggregate
  27. {
  28. /**
  29. * Read connection
  30. *
  31. * @var Zend_Db_Adapter_Abstract
  32. */
  33. protected $_read;
  34. protected $_items = array();
  35. protected $_totals = array('lifetime' => 0, 'num_orders' => 0);
  36. /**
  37. * Entity attribute
  38. *
  39. * @var Mage_Eav_Model_Entity_Abstract
  40. */
  41. protected $_entity;
  42. /**
  43. * Collection's Zend_Db_Select object
  44. *
  45. * @var Zend_Db_Select
  46. */
  47. protected $_select;
  48. /**
  49. * Enter description here...
  50. *
  51. * @var Mage_Customer_Model_Customer
  52. */
  53. protected $_customer;
  54. public function __construct()
  55. {
  56. $this->_entity = Mage::getModel('sales_entity/order');
  57. $this->_read = $this->_entity->getReadConnection();
  58. }
  59. public function setCustomerFilter(Mage_Customer_Model_Customer $customer)
  60. {
  61. $this->_customer = $customer;
  62. return $this;
  63. }
  64. public function load($printQuery = false, $logQuery = false)
  65. {
  66. $this->_select = $this->_read->select();
  67. $entityTable= $this->getEntity()->getEntityTable();
  68. $paidTable = $this->getAttribute('grand_total')->getBackend()->getTable();
  69. $idField = $this->getEntity()->getIdFieldName();
  70. $this->getSelect()
  71. ->from(array('sales' => $entityTable),
  72. array(
  73. 'store_id',
  74. 'lifetime' => 'sum(sales.base_grand_total)',
  75. 'avgsale' => 'avg(sales.base_grand_total)',
  76. 'num_orders'=> 'count(sales.base_grand_total)'
  77. )
  78. )
  79. ->where('sales.entity_type_id=?', $this->getEntity()->getTypeId())
  80. ->group('sales.store_id')
  81. ;
  82. if ($this->_customer instanceof Mage_Customer_Model_Customer) {
  83. $this->getSelect()
  84. ->where('sales.customer_id=?', $this->_customer->getId());
  85. }
  86. $this->printLogQuery($printQuery, $logQuery);
  87. try {
  88. $values = $this->_read->fetchAll($this->getSelect()->__toString());
  89. } catch (Exception $e) {
  90. $this->printLogQuery(true, true, $this->getSelect()->__toString());
  91. throw $e;
  92. }
  93. $stores = Mage::getResourceModel('core/store_collection')->setWithoutDefaultFilter()->load()->toOptionHash();
  94. if (! empty($values)) {
  95. foreach ($values as $v) {
  96. $obj = new Varien_Object($v);
  97. $storeName = isset($stores[$obj->getStoreId()]) ? $stores[$obj->getStoreId()] : null;
  98. $this->_items[ $v['store_id'] ] = $obj;
  99. $this->_items[ $v['store_id'] ]->setStoreName($storeName);
  100. $this->_items[ $v['store_id'] ]->setAvgNormalized($obj->getAvgsale() * $obj->getNumOrders());
  101. foreach ($this->_totals as $key => $value) {
  102. $this->_totals[$key] += $obj->getData($key);
  103. }
  104. }
  105. if ($this->_totals['num_orders']) {
  106. $this->_totals['avgsale'] = $this->_totals['lifetime'] / $this->_totals['num_orders'];
  107. }
  108. }
  109. return $this;
  110. }
  111. /**
  112. * Print and/or log query
  113. *
  114. * @param boolean $printQuery
  115. * @param boolean $logQuery
  116. * @return Mage_Sales_Model_Entity_Order_Attribute_Collection_Paid
  117. */
  118. public function printLogQuery($printQuery = false, $logQuery = false, $sql = null) {
  119. if ($printQuery) {
  120. echo is_null($sql) ? $this->getSelect()->__toString() : $sql;
  121. }
  122. if ($logQuery){
  123. Mage::log(is_null($sql) ? $this->getSelect()->__toString() : $sql);
  124. }
  125. return $this;
  126. }
  127. /**
  128. * Get zend db select instance
  129. *
  130. * @return Zend_Db_Select
  131. */
  132. public function getSelect()
  133. {
  134. return $this->_select;
  135. }
  136. /**
  137. * Enter description here...
  138. *
  139. * @return Mage_Eav_Model_Entity_Attribute_Abstract
  140. */
  141. public function getAttribute($attr)
  142. {
  143. return $this->_entity->getAttribute($attr);
  144. }
  145. /**
  146. * Enter description here...
  147. *
  148. * @return Mage_Eav_Model_Entity_Abstract
  149. */
  150. public function getEntity()
  151. {
  152. return $this->_entity;
  153. }
  154. /**
  155. * Enter description here...
  156. *
  157. * @return ArrayIterator
  158. */
  159. public function getIterator()
  160. {
  161. return new ArrayIterator($this->_items);
  162. }
  163. /**
  164. * Enter description here...
  165. *
  166. * @return array
  167. */
  168. public function getItems()
  169. {
  170. return $this->_items;
  171. }
  172. /**
  173. * Enter description here...
  174. *
  175. * @return Varien_Object
  176. */
  177. public function getTotals()
  178. {
  179. return new Varien_Object($this->_totals);
  180. }
  181. }