2014-06-06 77 views
0

創建自己的報告並添加一個自定義列稱爲供應商,我想過濾供應商的結果。Magento:添加過濾器自定義報告

addAttributeToSelect('vendor')嘗試過,但我得到了以下錯誤:

Fatal error: Call to undefined method 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; 
} 
+0

是廠商自定義屬性? – Slimshadddyyy

+0

是的,vendor是attribute_code。 – user3684098

回答

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; 
} 
+0

產品顯示在網格中,但沒有供應商屬性的過濾器。如果我使用你的代碼,我得到這個錯誤:調用一個成員函數getReportFull()在應用程序/代碼/核心/法師/報告/模型/資源/報告/ Collection.php中的非對象線286 – user3684098

+0

@ user3684098我沒有使用上面的'getReportFull()'函數..我不確定這個錯誤。我更新了我的帖子,因爲有一些變量錯誤拼寫。 –

+0

順便說一句,在'_prepareCollection'函數中如何使用'$ this-> getCollection()'怎麼可能?你有沒有使用'$ this-> setCollection($ yourCollection)'設置集合? –

0

試試這個

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

嗯,那麼我得到這個錯誤:致命錯誤:調用未定義的方法Mage_Reports_Model_Resource_Report_Collection :: addAttributeToSelect()in ... – user3684098