Example #1
0
 public function load($printQuery = false, $logQuery = false)
 {
     $this->_select = $this->_read->select();
     $entityTable = $this->getEntity()->getEntityTable();
     $paidTable = $this->getAttribute('grand_total')->getBackend()->getTable();
     $rateTable = $this->getAttribute('store_to_order_rate')->getBackend()->getTable();
     $idField = $this->getEntity()->getIdFieldName();
     $this->getSelect()->from(array('sales' => $entityTable), array('store_id'))->join(array('paid' => $paidTable), 'paid.' . $idField . '=sales.' . $idField . ' and paid.attribute_id=' . $this->getAttribute('grand_total')->getId(), array('lifetime' => 'sum(paid.value * rate.value)', 'avgsale' => 'avg(paid.value * rate.value)', 'num_orders' => 'count(paid.entity_id)'))->join(array('rate' => $rateTable), 'rate.' . $idField . '=sales.' . $idField . ' and rate.attribute_id=' . $this->getAttribute('store_to_order_rate')->getId(), array('value'))->where('sales.entity_type_id=?', $this->getEntity()->getTypeId())->group('sales.store_id');
     if ($this->_customer instanceof Mage_Customer_Model_Customer) {
         $this->getSelect()->join(array('customer' => $this->getAttribute('customer_id')->getBackend()->getTable()), 'customer.' . $idField . '=sales.' . $idField . ' and customer.attribute_id=' . $this->getAttribute('customer_id')->getId(), array('value'))->where('customer.value=?', $this->_customer->getId());
     }
     $this->printLogQuery($printQuery, $logQuery);
     try {
         $values = $this->_read->fetchAll($this->getSelect()->__toString());
     } catch (Exception $e) {
         $this->printLogQuery(true, true, $this->getSelect()->__toString());
         throw $e;
     }
     $stores = Mage::getResourceModel('core/store_collection')->setWithoutDefaultFilter()->load()->toOptionHash();
     if (!empty($values)) {
         foreach ($values as $v) {
             $obj = new Varien_Object($v);
             $storeName = isset($stores[$obj->getStoreId()]) ? $stores[$obj->getStoreId()] : null;
             $this->_items[$v['store_id']] = $obj;
             $this->_items[$v['store_id']]->setStoreName($storeName);
             $this->_items[$v['store_id']]->setAvgNormalized($obj->getAvgsale() * $obj->getNumOrders());
             foreach ($this->_totals as $key => $value) {
                 $this->_totals[$key] += $obj->getData($key);
             }
         }
         if ($this->_totals['num_orders']) {
             $this->_totals['avgsale'] = $this->_totals['lifetime'] / $this->_totals['num_orders'];
         }
     }
     return $this;
 }
Example #2
0
 /**
  * Load data
  *
  * @param boolean $printQuery
  * @param boolean $logQuery
  * @return Mage_Sales_Model_Mysql4_Sale_Collection
  */
 public function load($printQuery = false, $logQuery = false)
 {
     $this->_select = $this->_read->select();
     $this->getSelect()->from(array('sales' => $this->_resource->getMainTable()), array('store_id', 'lifetime' => 'sum(sales.base_grand_total)', 'base_lifetime' => 'sum(sales.base_grand_total * sales.base_to_global_rate)', 'avgsale' => 'avg(sales.base_grand_total)', 'base_avgsale' => 'avg(sales.base_grand_total * sales.base_to_global_rate)', 'num_orders' => 'count(sales.base_grand_total)'))->group('sales.store_id');
     if ($this->_customer instanceof Mage_Customer_Model_Customer) {
         $this->getSelect()->where('sales.customer_id=?', $this->_customer->getId());
     }
     if (!is_null($this->_orderStateValue)) {
         $this->getSelect()->where('state ' . $this->_orderStateCondition . ' (?)', $this->_orderStateValue);
     }
     Mage::dispatchEvent('sales_sale_collection_query_before', array('collection' => $this));
     $this->printLogQuery($printQuery, $logQuery);
     try {
         $values = $this->_read->fetchAll($this->getSelect()->__toString());
     } catch (Exception $e) {
         $this->printLogQuery(true, true, $this->getSelect()->__toString());
         throw $e;
     }
     $stores = Mage::getResourceModel('core/store_collection')->setWithoutDefaultFilter()->load()->toOptionHash();
     if (!empty($values)) {
         foreach ($values as $v) {
             $obj = new Varien_Object($v);
             $storeName = isset($stores[$obj->getStoreId()]) ? $stores[$obj->getStoreId()] : null;
             $this->_items[$v['store_id']] = $obj;
             $this->_items[$v['store_id']]->setStoreName($storeName);
             $this->_items[$v['store_id']]->setWebsiteId(Mage::app()->getStore($obj->getStoreId())->getWebsiteId());
             $this->_items[$v['store_id']]->setAvgNormalized($obj->getAvgsale() * $obj->getNumOrders());
             foreach ($this->_totals as $key => $value) {
                 $this->_totals[$key] += $obj->getData($key);
             }
         }
         if ($this->_totals['num_orders']) {
             $this->_totals['avgsale'] = $this->_totals['base_lifetime'] / $this->_totals['num_orders'];
         }
     }
     return $this;
 }