00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 class Mage_Sales_Model_Mysql4_Sale_Collection extends Varien_Object implements IteratorAggregate
00029 {
00030
00031
00032
00033
00034
00035
00036 protected $_read;
00037
00038 protected $_items = array();
00039
00040 protected $_totals = array('lifetime' => 0, 'num_orders' => 0);
00041
00042
00043
00044
00045
00046
00047 protected $_entity;
00048
00049
00050
00051
00052
00053
00054 protected $_select;
00055
00056
00057
00058
00059
00060
00061 protected $_customer;
00062
00063 public function __construct()
00064 {
00065 $this->_entity = Mage::getModel('sales_entity/order');
00066 $this->_read = $this->_entity->getReadConnection();
00067 }
00068
00069 public function setCustomerFilter(Mage_Customer_Model_Customer $customer)
00070 {
00071 $this->_customer = $customer;
00072 return $this;
00073 }
00074
00075 public function load($printQuery = false, $logQuery = false)
00076 {
00077 $this->_select = $this->_read->select();
00078 $entityTable= $this->getEntity()->getEntityTable();
00079 $paidTable = $this->getAttribute('grand_total')->getBackend()->getTable();
00080 $idField = $this->getEntity()->getIdFieldName();
00081 $this->getSelect()
00082 ->from(array('sales' => $entityTable),
00083 array(
00084 'store_id',
00085 'lifetime' => 'sum(sales.base_grand_total)',
00086 'avgsale' => 'avg(sales.base_grand_total)',
00087 'num_orders'=> 'count(sales.base_grand_total)'
00088 )
00089 )
00090 ->where('sales.entity_type_id=?', $this->getEntity()->getTypeId())
00091 ->group('sales.store_id')
00092 ;
00093 if ($this->_customer instanceof Mage_Customer_Model_Customer) {
00094 $this->getSelect()
00095 ->where('sales.customer_id=?', $this->_customer->getId());
00096 }
00097
00098 $this->printLogQuery($printQuery, $logQuery);
00099 try {
00100 $values = $this->_read->fetchAll($this->getSelect()->__toString());
00101 } catch (Exception $e) {
00102 $this->printLogQuery(true, true, $this->getSelect()->__toString());
00103 throw $e;
00104 }
00105 $stores = Mage::getResourceModel('core/store_collection')->setWithoutDefaultFilter()->load()->toOptionHash();
00106 if (! empty($values)) {
00107 foreach ($values as $v) {
00108 $obj = new Varien_Object($v);
00109 $storeName = isset($stores[$obj->getStoreId()]) ? $stores[$obj->getStoreId()] : null;
00110
00111 $this->_items[ $v['store_id'] ] = $obj;
00112 $this->_items[ $v['store_id'] ]->setStoreName($storeName);
00113 $this->_items[ $v['store_id'] ]->setAvgNormalized($obj->getAvgsale() * $obj->getNumOrders());
00114 foreach ($this->_totals as $key => $value) {
00115 $this->_totals[$key] += $obj->getData($key);
00116 }
00117 }
00118 if ($this->_totals['num_orders']) {
00119 $this->_totals['avgsale'] = $this->_totals['lifetime'] / $this->_totals['num_orders'];
00120 }
00121 }
00122
00123 return $this;
00124 }
00125
00126
00127
00128
00129
00130
00131
00132
00133 public function printLogQuery($printQuery = false, $logQuery = false, $sql = null) {
00134 if ($printQuery) {
00135 echo is_null($sql) ? $this->getSelect()->__toString() : $sql;
00136 }
00137
00138 if ($logQuery){
00139 Mage::log(is_null($sql) ? $this->getSelect()->__toString() : $sql);
00140 }
00141 return $this;
00142 }
00143
00144
00145
00146
00147
00148
00149 public function getSelect()
00150 {
00151 return $this->_select;
00152 }
00153
00154
00155
00156
00157
00158
00159 public function getAttribute($attr)
00160 {
00161 return $this->_entity->getAttribute($attr);
00162 }
00163
00164
00165
00166
00167
00168
00169 public function getEntity()
00170 {
00171 return $this->_entity;
00172 }
00173
00174
00175
00176
00177
00178
00179 public function getIterator()
00180 {
00181 return new ArrayIterator($this->_items);
00182 }
00183
00184
00185
00186
00187
00188
00189 public function getItems()
00190 {
00191 return $this->_items;
00192 }
00193
00194
00195
00196
00197
00198
00199 public function getTotals()
00200 {
00201 return new Varien_Object($this->_totals);
00202 }
00203
00204 }