[英]Magento Custom Report
我正在尝试创建已成功创建的自定义报告,但无法使用过滤器值,例如period(day / month / year)。 当它的一天显示完美的结果,而当它的月份和年份显示降低的结果。
我的文件详细信息Block \\ Adminhtml \\ Commissionreport.php
<?php
class Magestore_Commissionreport_Block_Adminhtml_Commissionreport extends Mage_Adminhtml_Block_Widget_Grid_Container {
public function __construct()
{
$this->_controller = 'adminhtml_commissionreport';
$this->_blockGroup = 'commissionreport';
$this->_headerText = Mage::helper('commissionreport')->__('Commission Report');
parent::__construct();
$this->_removeButton('add');
}
}
Block \\ Adminhtml \\ Commissionreport \\ Grid.php
<?php
class Magestore_Commissionreport_Block_Adminhtml_Commissionreport_Grid extends Mage_Adminhtml_Block_Report_Grid
{
public function __construct()
{
parent::__construct();
$this->setId('commissionreportGrid');
$this->setDefaultSort('created_at');
$this->setDefaultDir('ASC');
$this->setSaveParametersInSession(true);
}
protected function _prepareCollection()
{
parent::_prepareCollection();
// Get the data collection from the model
$this->getCollection()->initReport('commissionreport/commissionreport');
}
protected function _prepareColumns()
{
$this->addColumn('created_at', array(
'header' => Mage::helper('commissionreport')->__('Created At'),
'align' =>'left',
'sortable' => true,
'index' => 'created_at',
));
$this->addColumn('order_id', array(
'header' => Mage::helper('commissionreport')->__('Order Id'),
'align' =>'left',
'sortable' => true,
'index' => 'order_id',
));
$this->addColumn('fullname', array(
'header' => Mage::helper('commissionreport')->__('Customer Name'),
'align' =>'left',
'width' =>'550px',
'index' => 'fullname',
));
$this->addColumn('total_commission', array(
'header' => Mage::helper('commissionreport')->__('Commission Earned'),
'align' =>'left',
'total' =>'sum',
'width' =>'350px',
'index' => 'total_commission',
));
$this->addExportType('*/*/exportCsv', Mage::helper('commissionreport')->__('CSV'));
$this->addExportType('*/*/exportXml', Mage::helper('commissionreport')->__('XML'));
return parent::_prepareColumns();
}
public function getRowUrl($row)
{
return false;
}
}
Commissionreport \\ controllers \\ Adminhtml \\ CommissionreportController.php
<?php
class Magestore_Commissionreport_Adminhtml_CommissionreportController extends Mage_Adminhtml_Controller_Action
{
protected function _initAction() {
$this->loadLayout()
->_setActiveMenu('rewards/rewards')
->_addBreadcrumb(Mage::helper('adminhtml')->__('Rewards Transaction'), Mage::helper('adminhtml')->__('Rewards Transaction'));
return $this;
}
public function indexAction() {
$this->_initAction()
->renderLayout();
}
}
Commissionreport \\ Model \\ Commissionreport.php
<?php
class Magestore_Commissionreport_Model_Commissionreport extends Mage_Core_Model_Mysql4_Collection_Abstract
{
protected function _construct()
{
$this->_init('rewards/rewards');
}
protected function _joinFields($from = '', $to = '')
{
$firstnameAttr = Mage::getModel('eav/entity_attribute')->loadByCode('1', 'firstname');
$lastnameAttr = Mage::getModel('eav/entity_attribute')->loadByCode('1', 'lastname');
$this->getSelect()
->join( array('order_item'=> sales_flat_order_item), 'order_item.quote_item_id = main_table.item_id', array('order_item.created_at'))
->join( array('quote_item'=> sales_flat_quote_item), 'quote_item.item_id = main_table.item_id', array('quote_item.commission', 'quote_item.product_id', 'quote_item.qty'));
$this->addFieldToFilter('order_item.created_at' , array("from" => $from, "to" => $to, "datetime" => false));
return $this;
}
public function setDateRange($from, $to)
{
$this->_reset()
->_joinFields($from, $to);
return $this;
}
public function setStoreIds($storeIds)
{
return $this;
}
}
?>
当我选择月份时,它将显示出理想的结果,所有记录
但是当我们选择月份或年份时,它会降低结果
从Mage_Adminhtml_Controller_Report_Abstract继承控制器,并在indexAction()中使用以下代码:
$gridBlock = $this->getLayout()->getBlock('your_report_name.grid');
$filterFormBlock = $this->getLayout()->getBlock('your_report_filter_name');
$this->_initReportAction(array(
$gridBlock,
$filterFormBlock
));
_initReportAction()将过滤器数据分配给以参数形式传递的块。
您还可以查看Mage_Adminhtml_Report_ProductController :: viewedAction()以获取详细的用法示例。
您可以添加功能
public function setPageSize()
{
return $this;
}
到您的模型。 因为在核心具有功能
public function countTotals($grid, $from, $to)
{
$columns = array();
foreach ($grid->getColumns() as $col) {
$columns[$col->getIndex()] = array("total" => $col->getTotal(), "value" => 0);
}
$count = 0;
$report = $grid->getCollection()->getReportFull($from, $to);
foreach ($report as $item) {
if ($grid->getSubReportSize() && $count >= $grid->getSubReportSize()) {
continue;
}
$data = $item->getData();
foreach ($columns as $field=>$a) {
if ($field !== '') {
$columns[$field]['value'] = $columns[$field]['value'] + (isset($data[$field]) ? $data[$field] : 0);
}
}
$count++;
}
$data = array();
foreach ($columns as $field => $a) {
if ($a['total'] == 'avg') {
if ($field !== '') {
if ($count != 0) {
$data[$field] = $a['value']/$count;
} else {
$data[$field] = 0;
}
}
} else if ($a['total'] == 'sum') {
if ($field !== '') {
$data[$field] = $a['value'];
}
} else if (strpos($a['total'], '/') !== FALSE) {
if ($field !== '') {
$data[$field] = 0;
}
}
}
$totals = new Varien_Object();
$totals->setData($data);
return $totals;
}
$ count> = $ grid-> getSubReportSize()的默认值为5。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.