Mage_Reports_Model_Mysql4_Customer_Collection Class Reference

Inheritance diagram for Mage_Reports_Model_Mysql4_Customer_Collection:

Mage_Customer_Model_Entity_Customer_Collection Mage_Eav_Model_Entity_Collection_Abstract Varien_Data_Collection_Db Varien_Data_Collection Mage_Reports_Model_Mysql4_Accounts_Collection

List of all members.

Public Member Functions

 addCartInfo ()
 addCustomerName ()
 joinOrders ($from= '', $to= '')
 addOrdersCount ()
 addSumAvgTotals ($storeId=0)
 orderByTotalAmount ($dir= 'desc')
 orderByCustomerRegistration ($dir= 'desc')
 getSelectCountSql ()

Protected Attributes

 $_customerIdTableName
 $_customerIdFieldName
 $_orderEntityTableName
 $_orderEntityFieldName


Detailed Description

Definition at line 35 of file Collection.php.


Member Function Documentation

addCartInfo (  ) 

Definition at line 43 of file Collection.php.

00044     {
00045         foreach ($this->getItems() as $item)
00046         {
00047             $quote = Mage::getModel('sales/quote')->loadByCustomer($item->getId());
00048 
00049             if (is_object($quote))
00050             {
00051                 $totals = $quote->getTotals();
00052                 $item->setTotal($totals['subtotal']->getValue());
00053                 $quote_items = Mage::getResourceModel('sales/quote_item_collection')->setQuoteFilter($quote->getId());
00054                 $quote_items->load();
00055                 $item->setItems($quote_items->count());
00056             } else {
00057                 $item->remove();
00058             }
00059 
00060         }
00061         return $this;
00062     }

addCustomerName (  ) 

Definition at line 64 of file Collection.php.

00065     {
00066         $this->addNameToSelect();
00067         return $this;
00068     }

addOrdersCount (  ) 

Definition at line 97 of file Collection.php.

00098     {
00099         $order = Mage::getResourceSingleton('sales/order');
00100         /* @var $order Mage_Sales_Model_Entity_Order */
00101         $stateAttr = $order->getAttribute('state');
00102         $_joinCondition = "{$this->_customerIdTableName}.entity_id=order_state.entity_id";
00103         $_joinCondition .= $this->getConnection()->quoteInto(' AND order_state.attribute_id=? ', $stateAttr->getId());
00104         $_joinCondition .= $this->getConnection()->quoteInto(' AND order_state.value<>? ', Mage_Sales_Model_Order::STATE_CANCELED);
00105 
00106         $this->getSelect()
00107             ->from('', array("orders_count" => "COUNT(order_state.entity_id)"))
00108             ->joinLeft(
00109                 array('order_state' => $stateAttr->getBackend()->getTable()),
00110                 $_joinCondition,
00111                 array())
00112             ->group("e.entity_id");
00113 
00114         return $this;
00115     }

addSumAvgTotals ( storeId = 0  ) 

Order summary info for each customer such as orders_count, orders_avg_amount, orders_total_amount

Join subtotal attribute

Join store_to_base_rate attribute

calculate average and total amount

calculate average and total amount

Definition at line 121 of file Collection.php.

00122     {
00123         /**
00124          * Join subtotal attribute
00125          */
00126         $order = Mage::getResourceSingleton('sales/order');
00127         /* @var $order Mage_Sales_Model_Entity_Order */
00128 
00129         if ($storeId == 0) {
00130             /**
00131              * Join store_to_base_rate attribute
00132              */
00133             $attr = $order->getAttribute('base_to_global_rate');
00134             /* @var $attr Mage_Eav_Model_Entity_Attribute_Abstract */
00135             $attrId = $attr->getAttributeId();
00136             $baseToGlobalRateTableName = $attr->getBackend()->getTable();
00137             $baseToGlobalRateFieldName = $attr->getBackend()->isStatic() ? 'base_to_global_rate' : 'value';
00138 
00139             $this->getSelect()
00140                 ->joinLeft(array('_b2gr_'.$baseToGlobalRateTableName => $baseToGlobalRateTableName),
00141                     "_b2gr_{$baseToGlobalRateTableName}.entity_id={$this->_customerIdTableName}.entity_id AND ".
00142                     "_b2gr_{$baseToGlobalRateTableName}.attribute_id={$attrId}", array());
00143 
00144             /**
00145              * calculate average and total amount
00146              */
00147             $expr = "({$this->_customerIdTableName}.base_subtotal-IFNULL({$this->_customerIdTableName}.base_subtotal_canceled,0)-IFNULL({$this->_customerIdTableName}.base_subtotal_refunded,0))*_b2gr_{$baseToGlobalRateTableName}.{$baseToGlobalRateFieldName}";
00148 
00149         } else {
00150 
00151             /**
00152              * calculate average and total amount
00153              */
00154             $expr = "{$this->_customerIdTableName}.base_subtotal-IFNULL({$this->_customerIdTableName}.base_subtotal_canceled,0)-IFNULL({$this->_customerIdTableName}.base_subtotal_refunded,0)";
00155         }
00156 
00157         $this->getSelect()
00158             ->from('', array("orders_avg_amount" => "AVG({$expr})"))
00159             ->from('', array("orders_sum_amount" => "SUM({$expr})"));
00160 
00161         return $this;
00162     }

getSelectCountSql (  ) 

Get SQL for get record count

Returns:
Varien_Db_Select

Reimplemented from Mage_Customer_Model_Entity_Customer_Collection.

Definition at line 177 of file Collection.php.

00178     {
00179         $countSelect = clone $this->getSelect();
00180         $countSelect->reset(Zend_Db_Select::ORDER);
00181         $countSelect->reset(Zend_Db_Select::LIMIT_COUNT);
00182         $countSelect->reset(Zend_Db_Select::LIMIT_OFFSET);
00183         $countSelect->reset(Zend_Db_Select::COLUMNS);
00184         $countSelect->reset(Zend_Db_Select::GROUP);
00185         $countSelect->reset(Zend_Db_Select::HAVING);
00186         $countSelect->from("", "count(DISTINCT e.entity_id)");
00187         $sql = $countSelect->__toString();
00188         return $sql;
00189     }

joinOrders ( from = '',
to = '' 
)

Order for each customer

Definition at line 73 of file Collection.php.

00074     {
00075         $order = Mage::getResourceSingleton('sales/order');
00076         /* @var $order Mage_Sales_Model_Entity_Order */
00077         $attr = $order->getAttribute('customer_id');
00078         /* @var $attr Mage_Eav_Model_Entity_Attribute_Abstract */
00079         $attrId = $attr->getAttributeId();
00080         $this->_customerIdTableName = $attr->getBackend()->getTable();
00081         $this->_customerIdFieldName = $attr->getBackend()->isStatic() ? 'customer_id' : 'value';
00082 
00083         if ($from != '' && $to != '') {
00084             $dateFilter = " and {$this->_customerIdTableName}.created_at BETWEEN '{$from}' AND '{$to}'";
00085         } else {
00086             $dateFilter = '';
00087         }
00088 
00089         $this->getSelect()
00090             ->joinLeft($this->_customerIdTableName,
00091                 "{$this->_customerIdTableName}.{$this->_customerIdFieldName}=e.entity_id".$dateFilter,
00092             array());
00093 
00094         return $this;
00095     }

orderByCustomerRegistration ( dir = 'desc'  ) 

Definition at line 171 of file Collection.php.

00172     {
00173         $this->addAttributeToSort('entity_id', $dir);
00174         return $this;
00175     }

orderByTotalAmount ( dir = 'desc'  ) 

Definition at line 164 of file Collection.php.

00165     {
00166         $this->getSelect()
00167             ->order("orders_sum_amount {$dir}");
00168         return $this;
00169     }


Member Data Documentation

$_customerIdFieldName [protected]

Definition at line 39 of file Collection.php.

$_customerIdTableName [protected]

Definition at line 38 of file Collection.php.

$_orderEntityFieldName [protected]

Definition at line 41 of file Collection.php.

$_orderEntityTableName [protected]

Definition at line 40 of file Collection.php.


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:24:36 2009 for Magento by  doxygen 1.5.8