Magento:向自定义报告添加过滤器

时间:2014-06-06 11:03:21

标签: magento zend-framework report

创建自己的报告并添加名为vendor的自定义列后,我想按供应商过滤结果。

我用addAttributeToSelect('vendor')尝试了但是我收到了以下错误:

  

致命错误:调用未定义的方法Mage_Reports_Model_Resource_Report_Collection :: addAttributeToSelect()

我的网格类:

    public function __construct() {
    parent::__construct();
    $this->setId('salesreportsGrid');
    $this->setDefaultSort('created_at');
    $this->setDefaultDir('ASC');
    $this->setSaveParametersInSession(true);
    $this->setSubReportSize(false);
}

protected function _prepareCollection() {
    parent::_prepareCollection();
    $this->getCollection()->addAttributeToSelect('vendor')->initReport('salesreports/salesreports');
    return $this;
}

protected function _prepareColumns() {

    $this->addColumn('sku', array(
        'header'    =>Mage::helper('reports')->__('SKU'),
        'index'     =>'sku'
    ));

    $this->addColumn('name', array(
        'header'    =>Mage::helper('reports')->__('Product Name'),
        'index'     =>'order_items_name'
    ));

    $this->addColumn('vendor', array(
        'header'    =>Mage::helper('reports')->__('Vendor'),
        'index'     =>'vendor',
        'final_index' =>'vendor',
        'type'      =>'options',
        'options'   =>$this->_getAttributeOptions('vendor')
    ));

    $this->addColumn('ordered_qty', array(
        'header'    =>Mage::helper('reports')->__('Quantity Ordered'),
        'width'     =>'120px',
        'align'     =>'right',
        'index'     =>'ordered_qty',
        'total'     =>'sum',
        'type'      =>'number'
    ));

    $this->addColumn('base_cost', array(
        'header'    =>Mage::helper('reports')->__('Cost'),
        'width'     =>'120px',
        'align'     =>'right',
        'index'     =>'base_cost',
        'total'     =>'sum',
        'currency_code' => Mage::app()->getStore()->getCurrentCurrencyCode(),
        'type'      =>'price'
    ));

    $this->addExportType('*/*/exportCsv', Mage::helper('salesreports')->__('CSV'));
    $this->addExportType('*/*/exportXml', Mage::helper('salesreports')->__('XML'));
    return parent::_prepareColumns();
}

public function getRowUrl($row) {
    return false;
}

public function getReport($from, $to) {
    if ($from == '') {
        $from = $this->getFilter('report_from');
    }
    if ($to == '') {
        $to = $this->getFilter('report_to');
    }
    $totalObj = Mage::getModel('reports/totals');
    $totals = $totalObj->countTotals($this, $from, $to);
    $this->setTotals($totals);
    $this->addGrandTotals($totals);
    return $this->getCollection()->getReport($from, $to);
}


protected function _getAttributeOptions($attribute_code)
{
    $attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', $attribute_code);
    $options = array();
    foreach( $attribute->getSource()->getAllOptions(true, true) as $option ) {
        $options[$option['value']] = $option['label'];
    }
    return $options;
}

我的模特课:

    function __construct()
{
    parent::__construct();
    $this->setResourceModel('sales/order_item');
    $this->_init('sales/order_item','item_id');

}

public function setDateRange($from, $to)
{
        $this->getSelect()->reset()
        ->from(
            array('order_items' => $this->getTable('sales/order_item')),
            array(
                'ordered_qty' => 'order_items.qty_ordered',
                'order_items_name' => 'order_items.name',
                'vendor' => 'attrval.value',
                'base_cost' => '(SUM(order_items.qty_ordered) * order_items.base_cost)',
                'sku' => 'order_items.sku'
            ))
        ->joinLeft(array('p' => 'catalog_product_entity'), 'order_items.product_id = p.entity_id')
        ->joinLeft(array('eav' => 'eav_attribute'), 'p.entity_type_id = eav.entity_type_id')
        ->joinLeft(array('attr' =>'eav_attribute_option'), 'attr.attribute_id = eav.attribute_id')
        ->joinLeft(array('attrval' =>'eav_attribute_option_value'), 'attrval.option_id = attr.option_id')
        ->where("eav.attribute_code='vendor'")
        ->where("order_items.created_at BETWEEN '".$from."' AND '".$to."'")
        ->where('parent_item_id IS NULL')
        ->group('order_items.product_id')
        ->having('SUM(order_items.qty_ordered) > ?', 0)
        ->order(
            array(
                'SUM(order_items.qty_ordered) DESC'
            ));

        #echo $this->getSelect()->__toString();exit;

    return $this;
}

public function setStoreIds($storeIds)
{
    return $this;
}

2 个答案:

答案 0 :(得分:0)

您需要将报告集合转换为产品集合,以使产品在网格中可见。

protected function _prepareCollection() {
    // $this->getCollection()->addAttributeToSelect('vendor')->initReport('salesreports/salesreports');
    $collection = $this->getCollection();
    // store product ids
    $common_ids = array();
    foreach($collection as $item){
        $common_ids[] = $item->getId();
    }
    if(!count($common_ids)){
        $collection = null;
    }else{
        $collection = Mage::getResourceModel('catalog/product_collection')
            ->addFieldToFilter('entity_id', $common_ids)
            ->addAttributeToSelect('*');
    }
    $this->setCollection($collection);
    parent::_prepareCollection();
    return $this;
}

答案 1 :(得分:0)

试试这个

protected function _prepareCollection()
{
    $collection = $this->getCollection()->addAttributeToSelect('vendor')->initReport('salesreports/salesreports');
    $this->setCollection($collection);
    return parent::_prepareCollection();
}