How to override the graph in dashboard
سؤال
I have a table included some data about warehouse operation (order_id, phase, duration, datetime, order_canceled), so I need to draw a graph to present these data for performance management.
I tried to create a module named "Warehousereport" into mangento admin, so how can I override Magento graph in dashboard in this module ? Another way, anyone can give me a tutorial to do that ?
Please help me, I'm stucking. Thank you very much.
المحلول
According to https://stackoverflow.com/questions/14401516/change-the-dashboard-graph-in-version-1-7-1-12-of-magento and https://stackoverflow.com/questions/15370910/magento-module-to-change-dashboard-graph
In the module you create, you need to override
Mage_Reports_Model_Resource_Order_Collection
: so in yourconfig.xml
put:... <global> <models> <yourmodule> <class>Namespace_Yourmodule_Model</class> </yourmodule> <reports_resource> <rewrite> <order_collection>Namespace_Yourmodule_Model_Reports_Resource_Order_Collection</order_collection> </rewrite> </reports_resource> </models> </global> ...`
2.Then create the class (respecting the path) and in this class you need to override 2 methods:
<?php
/**
* Show all orders, not only the invoiced one
*/
class Namespace_Yourmodule_Model_Reports_Resource_Order_Collection extends Mage_Reports_Model_Resource_Order_Collection
{
protected function _prepareSummaryLive($range, $customStart, $customEnd, $isFilter = 0)
{
$this->setMainTable('sales/order');
$adapter = $this->getConnection();
/**
* Reset all columns, because result will group only by 'created_at' field
*/
$this->getSelect()->reset(Zend_Db_Select::COLUMNS);
$expression = sprintf('%s - %s - %s - (%s - %s - %s)',
$adapter->getIfNullSql('main_table.base_total_invoiced', 0),
$adapter->getIfNullSql('main_table.base_tax_invoiced', 0),
$adapter->getIfNullSql('main_table.base_shipping_invoiced', 0),
$adapter->getIfNullSql('main_table.base_total_refunded', 0),
$adapter->getIfNullSql('main_table.base_tax_refunded', 0),
$adapter->getIfNullSql('main_table.base_shipping_refunded', 0)
);
if ($isFilter == 0) {
$this->getSelect()->columns(array(
'revenue' => new Zend_Db_Expr(
sprintf('SUM((%s) * %s)', $expression,
$adapter->getIfNullSql('main_table.base_to_global_rate', 0)
)
)
));
} else {
$this->getSelect()->columns(array(
'revenue' => new Zend_Db_Expr(sprintf('SUM(%s)', $expression))
));
}
$dateRange = $this->getDateRange($range, $customStart, $customEnd);
$tzRangeOffsetExpression = $this->_getTZRangeOffsetExpression(
$range, 'created_at', $dateRange['from'], $dateRange['to']
);
$this->getSelect()
->columns(array(
'quantity' => 'COUNT(main_table.entity_id)',
'range' => $tzRangeOffsetExpression,
))
//BOF modification
// ->where('main_table.state NOT IN (?)', array(
// Mage_Sales_Model_Order::STATE_PENDING_PAYMENT,
// Mage_Sales_Model_Order::STATE_NEW)
// )
//EOF modification
->order('range', Zend_Db_Select::SQL_ASC)
->group($tzRangeOffsetExpression);
$this->addFieldToFilter('created_at', $dateRange);
return $this;
}
protected function _calculateTotalsLive($isFilter = 0)
{
$this->setMainTable('sales/order');
$this->removeAllFieldsFromSelect();
$adapter = $this->getConnection();
$baseTotalInvoiced = $adapter->getIfNullSql('main_table.base_grand_total', 0);
$baseTotalRefunded = $adapter->getIfNullSql('main_table.base_discount_refunded', 0);
$baseTaxInvoiced = $adapter->getIfNullSql('main_table.base_tax_amount', 0);
$baseTaxRefunded = $adapter->getIfNullSql('main_table.base_tax_refunded', 0);
$baseShippingInvoiced = $adapter->getIfNullSql('main_table.base_shipping_amount', 0);
$baseShippingRefunded = $adapter->getIfNullSql('main_table.base_shipping_refunded', 0);
$revenueExp = sprintf('%s - %s - %s - (%s - %s - %s)',
$baseTotalInvoiced,
$baseTaxInvoiced,
$baseShippingInvoiced,
$baseTotalRefunded,
$baseTaxRefunded,
$baseShippingRefunded
);
$taxExp = sprintf('%s - %s', $baseTaxInvoiced, $baseTaxRefunded);
$shippingExp = sprintf('%s - %s', $baseShippingInvoiced, $baseShippingRefunded);
if ($isFilter == 0) {
$rateExp = $adapter->getIfNullSql('main_table.base_to_global_rate', 0);
$this->getSelect()->columns(
array(
'revenue' => new Zend_Db_Expr(sprintf('SUM((%s) * %s)', $revenueExp, $rateExp)),
'tax' => new Zend_Db_Expr(sprintf('SUM((%s) * %s)', $taxExp, $rateExp)),
'shipping' => new Zend_Db_Expr(sprintf('SUM((%s) * %s)', $shippingExp, $rateExp))
)
);
} else {
$this->getSelect()->columns(
array(
'revenue' => new Zend_Db_Expr(sprintf('SUM(%s)', $revenueExp)),
'tax' => new Zend_Db_Expr(sprintf('SUM(%s)', $taxExp)),
'shipping' => new Zend_Db_Expr(sprintf('SUM(%s)', $shippingExp))
)
);
}
$this->getSelect()->columns(array(
'quantity' => 'COUNT(main_table.entity_id)'
));
//BOF modification
// ->where('main_table.state NOT IN (?)', array(
// Mage_Sales_Model_Order::STATE_PENDING_PAYMENT,
// Mage_Sales_Model_Order::STATE_NEW)
// );
//EOF modification
return $this;
}
}
In this example the filtering by order's status is commented, but you can easily uncomment it and put the order statuses you don't want to be counted in.