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: 29: 30: 31: 32: 33: 34:
35: class Mage_Sales_Model_Resource_Sale_Collection extends Varien_Data_Collection_Db
36: {
37:
38: 39: 40: 41: 42:
43: protected $_totals = array(
44: 'lifetime' => 0, 'base_lifetime' => 0, 'base_avgsale' => 0, 'num_orders' => 0);
45:
46:
47: 48: 49: 50: 51:
52: protected $_customer;
53:
54: 55: 56: 57: 58:
59: protected $_state = null;
60:
61: 62: 63: 64: 65:
66: protected $_orderStateCondition = null;
67:
68: 69: 70: 71:
72: public function __construct()
73: {
74: $conn = Mage::getResourceSingleton('sales/order')->getReadConnection();
75: $this->setConnection($conn);
76: }
77:
78: 79: 80: 81: 82: 83:
84: public function setCustomerFilter(Mage_Customer_Model_Customer $customer)
85: {
86: $this->_customer = $customer;
87: return $this;
88: }
89:
90: 91: 92: 93: 94: 95:
96: public function addStoreFilter($storeIds)
97: {
98: return $this->addFieldToFilter('store_id', array('in' => $storeIds));
99: }
100:
101: 102: 103: 104: 105: 106: 107:
108: public function setOrderStateFilter($state, $exclude = false)
109: {
110: $this->_orderStateCondition = ($exclude) ? 'NOT IN' : 'IN';
111: $this->_orderStateValue = (!is_array($state)) ? array($state) : $state;
112: return $this;
113: }
114:
115:
116: 117: 118: 119: 120:
121: protected function _beforeLoad()
122: {
123: $this->getSelect()
124: ->from(
125: array('sales' => Mage::getResourceSingleton('sales/order')->getMainTable()),
126: array(
127: 'store_id',
128: 'lifetime' => new Zend_Db_Expr('SUM(sales.base_grand_total)'),
129: 'base_lifetime' => new Zend_Db_Expr('SUM(sales.base_grand_total * sales.base_to_global_rate)'),
130: 'avgsale' => new Zend_Db_Expr('AVG(sales.base_grand_total)'),
131: 'base_avgsale' => new Zend_Db_Expr('AVG(sales.base_grand_total * sales.base_to_global_rate)'),
132: 'num_orders' => new Zend_Db_Expr('COUNT(sales.base_grand_total)')
133: )
134: )
135: ->group('sales.store_id');
136:
137: if ($this->_customer instanceof Mage_Customer_Model_Customer) {
138: $this->addFieldToFilter('sales.customer_id', $this->_customer->getId());
139: }
140:
141: if (!is_null($this->_orderStateValue)) {
142: $condition = '';
143: switch ($this->_orderStateCondition) {
144: case 'IN' :
145: $condition = 'in';
146: break;
147: case 'NOT IN' :
148: $condition = 'nin';
149: break;
150: }
151: $this->addFieldToFilter('state', array($condition => $this->_orderStateValue));
152: }
153:
154: Mage::dispatchEvent('sales_sale_collection_query_before', array('collection' => $this));
155: return $this;
156: }
157:
158: 159: 160: 161: 162:
163: public function load($printQuery = false, $logQuery = false)
164: {
165: if ($this->isLoaded()) {
166: return $this;
167: }
168:
169: $this->_beforeLoad();
170:
171: $this->_renderFilters()
172: ->_renderOrders()
173: ->_renderLimit();
174:
175: $this->printLogQuery($printQuery, $logQuery);
176:
177: $data = $this->getData();
178: $this->resetData();
179:
180: $stores = Mage::getResourceModel('core/store_collection')
181: ->setWithoutDefaultFilter()
182: ->load()
183: ->toOptionHash();
184: $this->_items = array();
185: foreach ($data as $v) {
186: $storeObject = new Varien_Object($v);
187: $storeId = $v['store_id'];
188: $storeName = isset($stores[$storeId]) ? $stores[$storeId] : null;
189: $storeObject->setStoreName($storeName)
190: ->setWebsiteId(Mage::app()->getStore($storeId)->getWebsiteId())
191: ->setAvgNormalized($v['avgsale'] * $v['num_orders']);
192: $this->_items[$storeId] = $storeObject;
193: foreach ($this->_totals as $key => $value) {
194: $this->_totals[$key] += $storeObject->getData($key);
195: }
196: }
197:
198: if ($this->_totals['num_orders']) {
199: $this->_totals['avgsale'] = $this->_totals['base_lifetime'] / $this->_totals['num_orders'];
200: }
201:
202: $this->_setIsLoaded();
203: $this->_afterLoad();
204: return $this;
205: }
206:
207: 208: 209: 210: 211:
212: public function getTotals()
213: {
214: return new Varien_Object($this->_totals);
215: }
216: }
217: