I believe you're not getting the same number of results returned because you're adjusting your time ($from_date
, $to_date
) to GMT.
As far as I can tell, Magento does not adjust the "from" and "to" dates to GMT when it queries the database for the reports. It really should, but sometimes Magento just lacks consistency. See Mage_Adminhtml_Block_Report_Grid_Abstract
. I've inspected the collection's query, and it literally uses the inputs given.
i.e. WHERE (period >= '2014-02-01') AND (period <= '2016-03-26')
(period
is constructed somewhere in the process, but dates are not converted to GMT).
Correct me I'm wrong, of course!
I wrote a InstallSchema.php in my custom module to add a column(sales_repesentative) to sales_order table.
<?php
/**
* Store Admin Name Order table save
*
* @category Store
* @package Store_Sales
*/
namespace Store\Sales\Setup;
use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
class InstallSchema implements InstallSchemaInterface
{
/**
* Install DB schema for a module
*
* @param SchemaSetupInterface $setup
* @param ModuleContextInterface $context
* @return void
*/
public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
//$quote = 'quote';
$orderTable = 'sales_order';
//Order Grid table
$setup->getConnection()
->addColumn(
$setup->getTable($orderTable),
'sales_representative',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'length' => 255,
'comment' =>'Sales Representative Name'
]
);
$setup->endSetup();
}
}
Created events.xml (app/code/Store/Sales/etc/adminhtml/events.xml)
<?xml version="1.0"?>
<!--
/**
* Store Admin Name Order table save
*
* @category Store
* @package Store_Sales
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="checkout_submit_all_after">
<observer name="store_admin_checkout_submit_all_after" instance="Store\Sales\Observer\AdminCheckoutSubmitAllAfter" />
</event>
</config>
Created an observer AdminCheckoutSubmitAllAfter.php to save the admin user name to the newly created column.
<?php
/**
* Store Admin Name Order table save
*
* @category Store
* @package Store_Sales
*/
namespace Store\Sales\Observer;
use Magento\Framework\Event\ObserverInterface;
class AdminCheckoutSubmitAllAfter implements ObserverInterface
{
/**
* @var \Magento\Backend\Model\Auth\Session
*/
protected $backendAuthSession;
protected $logger;
/**
* @param \Magento\Backend\Model\Auth\Session $backendAuthSession
*/
public function __construct(
\Magento\Backend\Model\Auth\Session $backendAuthSession
) {
$this->backendAuthSession = $backendAuthSession;
}
/**
* Add sale repsentative name to the order table
*
* @param \Magento\Framework\Event\Observer $observer
* @return void
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$adminUserName = $this->backendAuthSession->getUser()->getUserName();
if ($adminUserName) {
$order = $observer->getEvent()->getOrder();
$order->setSalesRepresentative($adminUserName);
$order->save();
}
}
}
and then got the sales representative like this
$order->getSalesRepresentative();
Best Answer
check if the statistics data is up to date. Data for the report needs to be rebuilt by cron or manually
check your timezone settings. I had issues with this in magento 1 when my timezone was not UTC, and orders where placed around 0am +/- a few hours This might also be an issue for the results of the db query in comparison to any other data, because the date in the database is actually stored in UTC, but shown converted to your timezone in the backend