我正在尝试创建一个我已成功创建的自定义报告,但我无法使用过滤器值,例如句点(日/月/年)。当它的一天显示出完美的结果,但当它的月份和年份它会降低结果。
我的文件详情 块\ 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');
}
}
块\ 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 \控制器\ 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 \模型\ 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;
}
}
?>
当我选择月份时,它会显示所有记录的完美结果
但是当我们选择月份或年份时会减少结果
答案 0 :(得分:0)
从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()将过滤数据分配给以params传递的块。
您还可以查看Mage_Adminhtml_Report_ProductController :: seenAction()以获取详细用法示例。
答案 1 :(得分:0)
您可以添加功能
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&gt; = $ grid-&gt; getSubReportSize()默认值为5.