3

I need for an CSV Export the value/number (column "Anz." in the screenshot) of 'ordered_qty' as a negative value/number. Everything I tried doesn't work.

enter image description here

What can I insert in these lines to get an - before the number? Hope somebody can help me fast and easy. Thanks for your time.

$this->addColumn('ordered_qty', array(
            'header'    =>Mage::helper('reports')->__('Anz.'),
            'width'     =>'120px',
            'align'     =>'right',
            'index'     =>'ordered_qty',
             ));


<?php


class Mage_Adminhtml_Block_Report_Product_Sold_Grid extends Mage_Adminhtml_Block_Report_Grid


{
    /**
     * Sub report size
     *
     * @var int
     */
    protected $_subReportSize = 0;

    /**
     * Initialize Grid settings
     *
     */
public function __construct()
{
    parent::__construct();
    $this->setId('gridProductsSold');
}

/**
 * Prepare collection object for grid
 *
 * @return Mage_Adminhtml_Block_Report_Product_Sold_Grid
 */
protected function _prepareCollection()
{
    parent::_prepareCollection();
    $this->getCollection()
        ->initReport('reports/product_sold_collection');
    return $this;
}

/**
 * Prepare Grid columns
 *
 * @return Mage_Adminhtml_Block_Report_Product_Sold_Grid
 */
protected function _prepareColumns()
{

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

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


$this->addColumn('ordered_qty', array(
        'header'    =>Mage::helper('reports')->__('Anz.'),
        'width'     =>'120px',
        'align'     =>'right',
        'index'     =>'ordered_qty',

    ));

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

$this->addColumn('qty', array(
        'header'    =>Mage::helper('reports')->__('Best.'),
        'index'     =>'""',

    ));

$this->addColumn('date', array(
        'header'    =>Mage::helper('reports')->__('Eingang'),
        'index'     =>'""'
    ));


    $this->addExportType('*/*/exportSoldCsv', Mage::helper('reports')->__('CSV'));
    $this->addExportType('*/*/exportSoldExcel', Mage::helper('reports')->__('Excel XML'));

    return parent::_prepareColumns();
}
}
2
  • You want it negative in exported csv only or in grid also?
    – Piyush
    Jan 5 '17 at 14:48
  • If it's only in csv negative it's also ok.
    – Mara
    Jan 5 '17 at 14:53
2

you can create a custom renderer for that column.

$this->addColumn('ordered_qty', array(
    'header'    =>Mage::helper('reports')->__('Anz.'),
    'width'     =>'120px',
    'align'     =>'right',
    'index'     =>'ordered_qty',
    'renderer' => '[Namespace]_[Module]_Block_Adminhtml_Column_Renderer_Negative'

));

then create the file [Namespace]/[Module]/Block/Adminhtml/Column/Renderer/Negative.php with this content

class [Namespace]_[Module]_Block_Adminhtml_Column_Renderer_Negative extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Number
{
    protected function _getValue(Varien_Object $row) 
    {
        $realValue = parent::getValue($row);
        //don't add minus if the value is 0.
        if ($value) {
            $value = 0 - $value;
        }
        return $value;
    }
}
0

Thanks Marius for your inspiration, my solution:

insert this line in my Grid.php:

'renderer' => 'Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Negative'

and use this code as in a local Negative.php:

class Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Negative
extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract

{
protected $_defaultWidth = 100;

/**
 * Returns value of the row
 *
 * @param Varien_Object $row
 * @return mixed|string
 */
protected function _getValue(Varien_Object $row)
{
    $data = parent::_getValue($row);
    if (!is_null($data)) {
        $value = $data * -1;
        $sign = (bool)(int)$this->getColumn()->getShowNumberSign() && ($value > 0) ? '+' : '';
        if ($sign) {
            $value = $sign . $value;
        }
        return $value ? $value : '0'; // fixed for showing zero in grid
    }
    return $this->getColumn()->getDefault();
}

/**
 * Renders CSS
 *
 * @return string
 */
public function renderCss()
{
    return parent::renderCss() . ' a-right';
}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.