I have Magento CE 1.9.2.1 installed in my site.
Now in the Invoice grid, I have calculated "Grand Total Excl. Tax" in the Grid's collection query and also set that as a column with the below query customization:
$collection->getSelect()->join(array('invoice' => 'sales_flat_invoice'),
'invoice.entity_id = main_table.entity_id', null)
->columns('(invoice.grand_total - invoice.tax_amount) AS grand_excl_tax');
It shows fine, but when I try to filter the grid by that column, then it generates error as following:
Column not found: 1054 Unknown column 'invoice.grand_excl_tax' in 'having clause', query was: SELECT COUNT(*) FROM
sales_flat_invoice_grid
ASmain_table
My column code:
$this->addColumn('grand_excl_tax', array(
'header' => Mage::helper('customer')->__('Amount Excl. Tax'),
'index' => 'grand_excl_tax',
##'filter_index' => 'invoice.grand_excl_tax',
'filter_condition_callback' => array($this, '_applyMyFilter'),
'type' => 'currency',
'align' => 'right',
'currency' => 'order_currency_code',
));
_applyMyFilter
function code:
protected function _applyMyFilter(Varien_Data_Collection_Db $collection, Mage_Adminhtml_Block_Widget_Grid_Column $column)
{
$select = $collection->getSelect();
$field = $column->getIndex();
$value = $column->getFilter()->getValue();
$select->having("invoice.grand_excl_tax=?", $value['from']);
}