Sales report on the dashboard in Magento admin

The sales report is one of the main progress indicators in e-commerce. We often create a sales report using the default Magento features – just go to Reports -> Sales -> Orders, and then, fill in the filter form on that page. This way allows us to get full information about sales on the web store.

Nevertheless, getting back to all these actions every time is not handy, especially if we do not need the full report. So, let’s find out how to display the reports in different parts in the admin panel.

For example, we want to display some simplified report on the admin dashboard, and this report will display only the difference between the total amount of the current month and the refunded amount for the same month.

dashbord

First of all, we add a part of the code that will display our report on the dashboard in the admin panel – so, to do this place the following code snippet to [Magento root]/app/design/adminhtml/default/default/template/dashboard/index.phtml:

<div class="dashboard-container">
    <!-- 
        ...
        Default code before our code snippet
-->
                <div class="entry-edit">
                    <div class="entry-edit-head"><h4><?php echo $this->__('Month`s totals') ?></h4></div>
                    <fieldset class="a-center bold"><span class="nowrap" style="font-size:18px;">
                            <span class="price"><?php echo Mage::helper('atwix_report')->getTotals();?></span>
                    </fieldset>
                </div>
<!-- 
        Default code after our code snippet
        ...
-->
  </div><!-- End .dashboard-container div -->

In this code snippet we have a standard markup and use custom helper Mage::helper(‘atwix_report’)->getTotals(). In this case, the helper returns an information about the report.

Pay attention to the code helper which generates information about the report. Here are several methods that build the report.

/**
     * Return month's totals
     *
     * @return mixed
     */
    public function getTotals()
    {
        $item = $this->_prepareCollection();
        $total = $item->getTotalIncomeAmount() - $item->getTotalRefundedAmount();
        return (string)Mage::helper('core')->currency($total, true, false);
    }

    /**
     * Get report month's amount totals
     * @return mixed
     */
    protected function _prepareCollection()
    {
        $aggregatedColumns = array('total_income_amount'=>'sum(total_income_amount)',
            'total_refunded_amount'=>'sum(total_refunded_amount)');

        $totalsCollection = Mage::getResourceModel('sales/report_order_collection')
            ->setPeriod('month')
            ->setDateRange($this->getStartMonth(), $this->getCurrentDate())
            ->addStoreFilter(1)
            ->setAggregatedColumns($aggregatedColumns)
            ->addOrderStatusFilter(null)
            ->isTotals(true);

        foreach ($totalsCollection as $item) {
            return $item;
            break;
        }
    }

    /**
     * Return current date
     *
     * @return string
     */
    
/**
     * Return current day
     *
     * @return string
     */
public function getCurrentDate()
    {
        $date = date('Y-m-d');
        return (string)$date;
    }

    /**
     * Return first day for current date
     *
     * @return string
     */
    public function getStartMonth()
    {
        $startCurrentMonth = date('Y').'-'.date('m').'-01';
        return (string)$startCurrentMonth;
    }

Moreover, you can change getStartMonth() or getCurrentDate() for a custom period of the report. Now Magento will display month totals on the dashboard.

Note that this report depends on the timezone configuration. The report is being updated using the Magento cron job once a day by default. Otherwise, it can be updated manually using the link on the report’s page.

Reports

We hope our article will help you to simplify your work with reports. Thanks for reading our blog.