Add total in magento custom module grid footer - magento

i have added sales order grid in my custom module.and also get footer total row in the grid. but the problem is when i add 'adult' column in the total it display nothing.for adult,child,total i m using renderer to get row data from order.so how to get total for adult,child,total at the footer.
i have used below code to display footer totals in grid.php
protected $_countTotals = true;
public function getTotals()
{
$totals = new Varien_Object();
$fields = array(
'base_grand_total' => 0,
'adult'=>0,
//actual column index, see _prepareColumns()
);
foreach ($this->getCollection() as $item) {
foreach($fields as $field=>$value){
$fields[$field]+=$item->getData($field);
}
}
//First column in the grid
$fields['increment_id'] = 'Totals';
$totals->setData($fields);
return $totals;
}
Thanks

Add this function to the grid.php
public function getReport($from, $to) {
if ($from == '') {
$from = $this->getFilter('report_from');
}
if ($to == '') {
$to = $this->getFilter('report_to');
}
$totalObj = Mage::getModel('reports/totals');
$totals = $totalObj->countTotals($this, $from, $to);
$this->setTotals($totals);
$this->addGrandTotals($totals);
return $this->getCollection()->getReport($from, $to);
}
and also in adding column add 'total' => 'sum' as below (I think you made this but once verify it properly):
$this->addColumn('item_id', array(
'header' => Mage::helper('mymodule')->__('Item ID'),
'align' => 'right',
'index' => 'item_id',
'type' => 'number',
'total' => 'sum',
));

Related

Filter admin grid with custom render values Magento

i have created a custom grid in which one of the columns i have render
my custom date.On the basis of data i am returning
yes or no
like below
public function render(Varien_Object $row)
{
$currentruleid = $this->getRequest()->getParam('id');
$value = (int)$row->getData($this->getColumn()->getIndex());
$read = Mage::getSingleton('core/resource')->getConnection('core_read');
$write = Mage::getSingleton("core/resource")->getConnection("core_write");
$query = "SELECT exclusive_coupon_id FROM mutually_exclusive
WHERE rule_id ='$currentruleid' AND exclusive_coupon_id ='$value' ";
$result = $read->query($query);
$affected_rows = $result->rowCount();
if($affected_rows > 0){
return 'Yes';
}
else{
return 'No';
}
}
and in grid my column code is
$this->addColumn('', array(
'header' => Mage::helper('salesrule')->__('Exclusive'),
'index' => 'coupon_id',
'width' => '100',
'type' => 'options',
'options' => array(
Mage::helper('adminhtml')->__('No'),
Mage::helper('adminhtml')->__('Yes')
),
'renderer' => 'adminhtml/promo_quote_edit_tab_exclusivecoupons_grid_column_renderer_used',
));
my renderer is returning accurate data
and its showing in columns
i need to filter also with yes or no. But filtering is not working.
Can you please suggest me how can i do this.
thanks
i have check that but my renderer is not any direct field , its a custom data like below is my renderer function
public function render(Varien_Object $row)
{
$currentruleid = $this->getRequest()->getParam('id');
$value = (int)$row->getData($this->getColumn()->getIndex());
$read = Mage::getSingleton('core/resource')->getConnection('core_read');
$write = Mage::getSingleton("core/resource")->getConnection("core_write");
$query = "SELECT exclusive_coupon_id FROM mutually_exclusive
WHERE rule_id ='$currentruleid' AND exclusive_coupon_id ='$value' ";
$result = $read->query($query);
$affected_rows = $result->rowCount();
if($affected_rows > 0){
return 'Yes';
}
else{
return 'No';
}
}

Magento admin filter by order_id can only filter 12 results max

I am trying to create a filter in the Magento Admin order grid. I have created a new class that extends the Mage_Adminhtml_Block_Widget_Grid. I use the real_order_id column and filter_condition_callback to call a function that separates the input by ",".
The problem is that if I try to filter more than 12 items, it fails (going back to the homepage). Less than 12 is fine. I would like to filter a much larger amount of id's. Does anyone have any suggestions?
Here the part of the column:
protected function _prepareColumns()
{
$this->addColumn('real_order_id', array(
'header'=> Mage::helper('sales')->__('Order #'),
'width' => '80px',
'type' => 'text',
'index' => 'increment_id',
'filter_condition_callback' => array($this, 'separatedFilter')
));
Here the separate function
protected function separatedFilter($collection, $column)
{
if (!$value = $column->getFilter()->getValue()) {
return $this;
} else if(strpos($column->getFilter()->getValue(), ',') !== false) {
//explode by comma, getting array of IDs
$val = explode(",", $value);
//filter the collection, where collection index (order_id) is present in $val array
$this->getCollection()->addAttributeToFilter($column->getData('index'), array('in' => $val));
} else {
//else use default grid filter functionality (like $value input)
$this->getCollection()->addAttributeToFilter($column->getData('index'), array('like' => '%'.$value.'%'));
}
return $this;
}

Searching sales order grid redirects to dashboard in magento 1.7

I have used a custom module to add new columns to Sales_Order_Grid in Magento but If I search for Order_Id the page redirects to the Dashboard.
If I try to select sales/orders again I get an error:
a:5:{i:0;s:104:"SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'increment_id' in where clause is ambiguous";i:1;s:6229:"#0
I have no idea how to solve this and could really do with some guidance please?
Here's the Grid.php code:
<?php
class Excellence_Salesgrid_Block_Adminhtml_Sales_Order_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid
{
protected function _addColumnFilterToCollection($column)
{
if ($this->getCollection()) {
if ($column->getId() == 'shipping_telephone') {
$cond = $column->getFilter()->getCondition();
$field = 't4.telephone';
$this->getCollection()->addFieldToFilter($field , $cond);
return $this;
}else if ($column->getId() == 'shipping_city') {
$cond = $column->getFilter()->getCondition();
$field = 't4.city';
$this->getCollection()->addFieldToFilter($field , $cond);
return $this;
}else if ($column->getId() == 'shipping_region') {
$cond = $column->getFilter()->getCondition();
$field = 't4.region';
$this->getCollection()->addFieldToFilter($field , $cond);
return $this;
}else if ($column->getId() == 'shipping_postcode') {
$cond = $column->getFilter()->getCondition();
$field = 't4.postcode';
$this->getCollection()->addFieldToFilter($field , $cond);
return $this;
}else if($column->getId() == 'product_count'){
$cond = $column->getFilter()->getCondition();
$field = ( $column->getFilterIndex() ) ? $column->getFilterIndex() : $column->getIndex();
$this->getCollection()->getSelect()->having($this->getCollection()->getResource()->getReadConnection()->prepareSqlCondition($field, $cond));
return $this;
}else if($column->getId() == 'skus'){
$cond = $column->getFilter()->getCondition();
$field = 't6.sku';
$this->getCollection()->joinSkus();
$this->getCollection()->addFieldToFilter($field , $cond);
return $this;
}else{
return parent::_addColumnFilterToCollection($column);
}
}
}
protected function _prepareColumns()
{
$this->addColumnAfter('shipping_description', array(
'header' => Mage::helper('sales')->__('Shipping Method'),
'index' => 'shipping_description',
),'shipping_name');
$this->addColumnAfter('method', array(
'header' => Mage::helper('sales')->__('Payment Method'),
'index' => 'method',
'type' => 'options',
'options' => Mage::helper('payment')->getPaymentMethodList()
),'shipping_description');
$this->addColumnAfter('shipping_city', array(
'header' => Mage::helper('sales')->__('Shipping City'),
'index' => 'shipping_city',
),'method');
$this->addColumnAfter('shipping_telephone', array(
'header' => Mage::helper('sales')->__('Shipping Telephone'),
'index' => 'shipping_telephone',
),'method');
$this->addColumnAfter('shipping_region', array(
'header' => Mage::helper('sales')->__('Shipping Region'),
'index' => 'shipping_region',
),'method');
$this->addColumnAfter('shipping_postcode', array(
'header' => Mage::helper('sales')->__('Shipping Postcode'),
'index' => 'shipping_postcode',
),'method');
/*$this->addColumnAfter('product_count', array(
'header' => Mage::helper('sales')->__('Product Count'),
'index' => 'product_count',
'type' => 'number'
),'increment_id');
/*$this->addColumnAfter('skus', array(
'header' => Mage::helper('sales')->__('Product Purchased'),
'index' => 'skus',
),'increment_id');*/
return parent::_prepareColumns();
}
}
This is what worked in my case:
$collection->addFilterToMap('increment_id', 'main_table.increment_id');
Looks like you are adding some more columns to the admin sales grid? It also sounds like you are working with a collection containing a join against a second table, you need to add the table alias name into the where statement with the increment_id column definition, so table_alias.increment_id.
To check this, and assuming calling $this->getCollection() from Excellence_Salesgrid_Block_Adminhtml_Sales_Order_Grid returns the collection, get the select object from the collection:
$select = $this->getCollection()->getSelect();
Hopefully you are using xdebug with an IDE and can set breakpoints in your code. If so set a breakpoint and inspect the the $select object pulled by the line above. Inside this object you will see a _parts array which describes the way your select statement is constructed. Inside this you will see a from array which contains information about the tables which are part of the statement. If you have a JOIN, this will contain more than one entry.
Under here you can also see where which will describe the where clauses which are part of the statement - this is where the problem likely lies. You need to identify the alias for the correct table inside the from array and then where the where clause is added, instead of doing something like:
$this->getCollection()->getSelect()->where('increment_id = ?', $id);
instead do:
$this->getCollection()->getSelect()->where('table_alias.increment_id = ?', $id);

Magento 1.7 - show order quantity in grid without decimals

I have this column definition in a grid for the ordered pieces of an order item:
$this->addColumn('qty_ordered',
array(
'header'=> $this->__('Quantity'),
'align' =>'right',
'type=' => 'number',
'index' => 'qty_ordered'
)
);
xxx http://img688.imageshack.us/img688/6690/scr031.jpg
How can I show the quantity without any decimals?
One way to do this is to create a custom module
$this->addColumn('qty_ordered',
array(
'header'=> $this->__('Quantity'),
'align' =>'right',
'type=' => 'number',
'index' => 'qty_ordered'
'renderer' = new MageIgniter_RemoveQtyDecimals_Block_Adminhtml_Renderer_Date()
)
);
class MageIgniter_RemoveQtyDecimals_Block_Adminhtml_Renderer_Date extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Action
{
public function render(Varien_Object $row)
{
return $this->_getValue($row);
}
public function _getValue(Varien_Object $row)
{
$val = $row->getData($this->getColumn()->getIndex());
return intval($val);
}
}
See https://stackoverflow.com/a/12695286/1191288
I modified the sku array definition instead of creating qty_ordered.
$collection = Mage::getResourceModel($this->_getCollectionClass())
->join(
'sales/order_item',
'`sales/order_item`.order_id=`main_table`.entity_id',
array(
'skus' => new Zend_Db_Expr('group_concat( `sales/order_item`.sku, "(", floor(`sales/order_item`.qty_ordered), ")" SEPARATOR ", ")'),
));
I believe you can use floor(sales/order_item.qty_ordered) on your qty_ordered definition as the same.
SKU column on Order Grid results something like:
MBA001(2), MOF004(1)

Magento Sales Order Grid shows incorrect number of records when added Names and Skus columns

I'm working with Magento version 1.4 and I added extra grid columns (names and skus) to Sales Order Grid, the returned data is correct but I'm having problems with pagination and total number of records, my code as follow:
First I Edited Mage_Adminhtml_Block_Sales_Order_Grid:
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass())
->join(
'sales/order_item',
'`sales/order_item`.order_id=`main_table`.entity_id',
array(
'skus' => new Zend_Db_Expr('group_concat(`sales/order_item`.sku SEPARATOR ", ")'),
'names' => new Zend_Db_Expr('group_concat(`sales/order_item`.name SEPARATOR ", ")'),
)
);
$collection->getSelect()->group('entity_id');
$this->setCollection($collection);
return parent::_prepareCollection();
}
Then I override this method to return correct results when filter by names or skus
protected function _addColumnFilterToCollection($column)
{
if($this->getCollection() && $column->getFilter()->getValue())
{
if($column->getId() == 'skus'){
$this->getCollection()->join(
'sales/order_item',
'`sales/order_item`.order_id=`main_table`.entity_id',
array(
'skus' => new Zend_Db_Expr('group_concat(`sales/order_item`.sku SEPARATOR ", ")'),
)
)->getSelect()
->having('find_in_set(?, skus)', $column->getFilter()->getValue());
return $this;
}
if($column->getId() == 'names'){
$this->getCollection()->join(
'sales/order_item',
'`sales/order_item`.order_id=`main_table`.entity_id',
array(
'names' => new Zend_Db_Expr('group_concat(`sales/order_item`.name SEPARATOR ", ")'),
)
)->getSelect()
->having('find_in_set(?, names)', $column->getFilter()->getValue());
return $this;
}
}
return parent::_addColumnFilterToCollection($column);
}
Then I edited this method getSelectCountSql() in Mage_Sales_Model_Mysql4_Order_Collection class:
public function getSelectCountSql()
{
$countSelect = parent::getSelectCountSql();
//added
$countSelect->reset(Zend_Db_Select::HAVING);
//end
$countSelect->resetJoinLeft();
return $countSelect;
}
How can I calculate number of rows?
Maybe its a bit to late but in your code try using GROUP insted of HAVING:
$countSelect->reset(Zend_Db_Select::GROUP);
Because you are using this statemen:
$collection->getSelect()->group('entity_id');
$collection->getSelect()->join(array(
'item'=>$collection->getTable('sales/order_item')),
'item.order_id=`main_table`.entity_id AND item.product_type="simple"',
array(
'skus' => new Zend_Db_Expr('group_concat(item.sku SEPARATOR ", ")'),
'name' => new Zend_Db_Expr('group_concat(item.name SEPARATOR ", ")')
));
$this->addColumn('skus', array(
'header' => Mage::helper('sales')->__('SKU'),
'index' => 'skus',
'type' => 'text',
));
$this->addColumn('name', array(
'header' => Mage::helper('sales')->__('NAME'),
'index' => 'name',
'type' => 'text'
));
I had this issue and i have got it working by implementing custom getSize() function in the collection i am using
public function getSize()
{
$select = clone $this->getSelect();
$select->reset();
$select = $this->getConnection()->fetchOne('SELECT COUNT(*) FROM Table GROUP BY FIELD'); // or you can use select count(distinct field) from table
return $select;
}
and to achieve Grid storing i have override
protected function _setCollectionOrder($column)
{
$collection = $this->getCollection();
if ($collection) {
$columnIndex = $column->getFilterIndex() ?
$column->getFilterIndex() : $column->getIndex();
$collection->getSelect()->order(array($columnIndex.' '.$column->getDir()));
}
return $this;
}
and Set filter_index of the columns TO
in _prepareColumns() function
'filter_index' => 'SUM(tablename.field)'
and you can use Callback function on filters for the columns

Resources