Created
December 18, 2012 20:20
-
-
Save ecogswell/4331585 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
protected function _prepareCollection() | |
{ | |
$filters = $this->getRequest()->getParams(); | |
$start_date = (isset($filters['date_from'])) ? date("'Y-m-d'", strtotime($filters['date_from'])) : "'0000-00-00'"; | |
$end_date = (isset($filters['date_to'])) ? date("'Y-m-d'", strtotime($filters['date_to'])) : 'NOW()'; | |
$collection = Mage::getModel("sales/order")->getCollection(); | |
$resource = Mage::getSingleton('core/resource'); | |
$distributorTable = $resource->getTableName('PurinaPro_Groups/Distributor'); | |
$distributorLinkTable = $resource->getTableName('PurinaPro_Groups/Distributor_OrderLink'); | |
$months = $this->getMonths(); | |
$collection->getSelect() | |
->reset(Zend_Db_Select::COLUMNS) | |
->join(array("distributor_link"=>$distributorLinkTable),"main_table.entity_id = distributor_link.order_id",array()) | |
->join(array("distributor"=>$distributorTable),"distributor_link.distributor_id = distributor.id",array()) | |
->columns(array( | |
"customer_id" => "main_table.customer_id", | |
"firstname"=>"main_table.customer_firstname", | |
"lastname"=>"main_table.customer_lastname", | |
"distributor_name"=>"distributor.name", | |
"order_months_count" => "(select COUNT(distinct MONTH(created_at)) from sales_flat_order where customer_id=main_table.customer_id and created_at between $start_date and $end_date)", | |
"total_qty_ordered" => "(select sum(total_qty_ordered) from sales_flat_order where customer_id=main_table.customer_id and created_at between $start_date and $end_date)", | |
"total_grand_total" => "(select sum(base_grand_total) from sales_flat_order where customer_id=main_table.customer_id and created_at between $start_date and $end_date)", | |
)); | |
foreach($months as $key => $month){ | |
$collection->getSelect() | |
->columns(array( | |
$month."_count" => "sum(MONTH(main_table.created_at) = $key)", | |
$month."_total" => "(select sum(base_subtotal) from sales_flat_order where month(created_at) = $key and customer_id=main_table.customer_id and created_at between $start_date and $end_date)", | |
#$month."_grand_total" => "(select sum(base_grand_total) from sales_flat_order where month(created_at) = $key and customer_id=sfo.customer_id and created_at between $start_date and $end_date)", | |
)); | |
} | |
$collection->getSelect() | |
->where('main_table.customer_id IS NOT NULL') | |
->where("main_table.created_at between $start_date and $end_date") | |
->group('main_table.customer_id'); | |
$this->setCollection($collection); | |
$this->addExportType('*/*/exportCsv', Mage::helper('reports')->__('CSV')); | |
return parent::_prepareCollection(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment