1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: class Mage_Sales_Model_Entity_Sale_Collection extends Varien_Object implements IteratorAggregate
29: {
30:
31: 32: 33: 34: 35:
36: protected $_read;
37:
38: protected $_items = array();
39:
40: protected $_totals = array('lifetime' => 0, 'num_orders' => 0);
41:
42: 43: 44: 45: 46:
47: protected $_entity;
48:
49: 50: 51: 52: 53:
54: protected $_select;
55:
56: 57: 58: 59: 60:
61: protected $_customer;
62:
63: public function __construct()
64: {
65: $this->_entity = Mage::getModel('sales_entity/order');
66: $this->_read = $this->_entity->getReadConnection();
67: }
68:
69: public function setCustomerFilter(Mage_Customer_Model_Customer $customer)
70: {
71: $this->_customer = $customer;
72: return $this;
73: }
74:
75: public function load($printQuery = false, $logQuery = false)
76: {
77: $this->_select = $this->_read->select();
78: $entityTable= $this->getEntity()->getEntityTable();
79: $paidTable = $this->getAttribute('grand_total')->getBackend()->getTable();
80: $idField = $this->getEntity()->getIdFieldName();
81: $this->getSelect()
82: ->from(array('sales' => $entityTable),
83: array(
84: 'store_id',
85: 'lifetime' => 'sum(sales.base_grand_total)',
86: 'avgsale' => 'avg(sales.base_grand_total)',
87: 'num_orders'=> 'count(sales.base_grand_total)'
88: )
89: )
90: ->where('sales.entity_type_id=?', $this->getEntity()->getTypeId())
91: ->group('sales.store_id')
92: ;
93: if ($this->_customer instanceof Mage_Customer_Model_Customer) {
94: $this->getSelect()
95: ->where('sales.customer_id=?', $this->_customer->getId());
96: }
97:
98: $this->printLogQuery($printQuery, $logQuery);
99: try {
100: $values = $this->_read->fetchAll($this->getSelect()->__toString());
101: } catch (Exception $e) {
102: $this->printLogQuery(true, true, $this->getSelect()->__toString());
103: throw $e;
104: }
105: $stores = Mage::getResourceModel('core/store_collection')->setWithoutDefaultFilter()->load()->toOptionHash();
106: if (! empty($values)) {
107: foreach ($values as $v) {
108: $obj = new Varien_Object($v);
109: $storeName = isset($stores[$obj->getStoreId()]) ? $stores[$obj->getStoreId()] : null;
110:
111: $this->_items[ $v['store_id'] ] = $obj;
112: $this->_items[ $v['store_id'] ]->setStoreName($storeName);
113: $this->_items[ $v['store_id'] ]->setAvgNormalized($obj->getAvgsale() * $obj->getNumOrders());
114: foreach ($this->_totals as $key => $value) {
115: $this->_totals[$key] += $obj->getData($key);
116: }
117: }
118: if ($this->_totals['num_orders']) {
119: $this->_totals['avgsale'] = $this->_totals['lifetime'] / $this->_totals['num_orders'];
120: }
121: }
122:
123: return $this;
124: }
125:
126: 127: 128: 129: 130: 131: 132:
133: public function printLogQuery($printQuery = false, $logQuery = false, $sql = null) {
134: if ($printQuery) {
135: echo is_null($sql) ? $this->getSelect()->__toString() : $sql;
136: }
137:
138: if ($logQuery){
139: Mage::log(is_null($sql) ? $this->getSelect()->__toString() : $sql);
140: }
141: return $this;
142: }
143:
144: 145: 146: 147: 148:
149: public function getSelect()
150: {
151: return $this->_select;
152: }
153:
154: 155: 156: 157: 158:
159: public function getAttribute($attr)
160: {
161: return $this->_entity->getAttribute($attr);
162: }
163:
164: 165: 166: 167: 168:
169: public function getEntity()
170: {
171: return $this->_entity;
172: }
173:
174: 175: 176: 177: 178:
179: public function getIterator()
180: {
181: return new ArrayIterator($this->_items);
182: }
183:
184: 185: 186: 187: 188:
189: public function getItems()
190: {
191: return $this->_items;
192: }
193:
194: 195: 196: 197: 198:
199: public function getTotals()
200: {
201: return new Varien_Object($this->_totals);
202: }
203:
204: }
205: