I'm having trouble sorting by a column available_qty
in a custom admin report.
My select looks like:
$this->getSelect()->reset()
->from(
array("e" => $this->getTable('catalog_product_entity')),
"*"
)
->joinLeft(
$this->getTable('purchaseorder/purchaseorder_product'),
'product_id = entity_id',
array(
'qty_ordered' => 'qty_ordered',
'qty_recieved' => 'qty_received',
'qty_on_purchase_order' => new Zend_Db_Expr('SUM(qty_ordered) - SUM(qty_received)')
)
)
->joinLeft(
array('stock_item' => $this->getTable('cataloginventory/stock_item')),
'stock_item.product_id = entity_id',
array(
'qty_on_hand' => 'qty',
'qty_on_sales_order' => 'qty_on_sales_order'
)
)
->columns(array(
'available_qty' => new Zend_Db_Expr('qty - qty_on_sales_order') // this is the column I'm trying to order by
))
->group('e.entity_id');
And when I prepare the columns
$this->addColumn('qty_on_hand', array(
'header' => $this->__('Qty On Hand'),
'index' => 'qty_on_hand',
'type' => 'number',
'filter' => false
));
$this->addColumn('qty_on_sales_order', array(
'header' => $this->__('Qty On Sales Order'),
'index' => 'qty_on_sales_order',
'type' => 'number',
'filter' => false
));
$this->addColumn('available_qty', array(
'header' => $this->__('Qty Available'),
'index' => 'available_qty',
'type' => 'number',
'filter' => false
));
But it obviously isn't sorting. I'm not even seeing the ORDER BY
being added to the query. What am I doing wrong?
I've also tried
$this->addAttributeToSelect('status')
->addAttributeToSelect('vendor_id', 'left')
->addAttributeToSelect('name')
->addAttributeToSelect('cost', 'left')
->joinTable(
'purchaseorder/purchaseorder_product',
'product_id=entity_id',
array(
'qty_ordered' => 'qty_ordered',
'qty_recieved' => 'qty_received',
'qty_on_purchase_order' => new Zend_Db_Expr('SUM(qty_ordered) - SUM(qty_received)')
), null, 'left'
)->joinTable(
'cataloginventory/stock_item',
'product_id=entity_id',
array(
'qty_on_hand' => 'qty',
'qty_on_sales_order' => 'qty_on_sales_order',
'available_qty' => new Zend_Db_Expr('qty - qty_on_sales_order')
), null, 'left'
);
$this->getSelect()
->group('e.entity_id');
But then I get
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'cataloginventory_stock_item.qty - qty_on_sales_order' in 'order clause'
You can see that the order statement isn't being formed correctly.
ORDER BY `cataloginventory_stock_item`.`qty - qty_on_sales_order`