Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Totals.php
Go to the documentation of this file.
1 <?php
13 
14 class Totals extends \Magento\Backend\Block\Dashboard\Bar
15 {
19  protected $_template = 'Magento_Backend::dashboard/totalbar.phtml';
20 
24  protected $_moduleManager;
25 
32  public function __construct(
33  \Magento\Backend\Block\Template\Context $context,
34  \Magento\Reports\Model\ResourceModel\Order\CollectionFactory $collectionFactory,
35  \Magento\Framework\Module\Manager $moduleManager,
36  array $data = []
37  ) {
38  $this->_moduleManager = $moduleManager;
39  parent::__construct($context, $collectionFactory, $data);
40  }
41 
45  protected function _prepareLayout()
46  {
47  if (!$this->_moduleManager->isEnabled('Magento_Reports')) {
48  return $this;
49  }
50  $isFilter = $this->getRequest()->getParam(
51  'store'
52  ) || $this->getRequest()->getParam(
53  'website'
54  ) || $this->getRequest()->getParam(
55  'group'
56  );
57  $period = $this->getRequest()->getParam('period', '24h');
58 
59  /* @var $collection \Magento\Reports\Model\ResourceModel\Order\Collection */
60  $collection = $this->_collectionFactory->create()->addCreateAtPeriodFilter(
61  $period
62  )->calculateTotals(
63  $isFilter
64  );
65 
66  if ($this->getRequest()->getParam('store')) {
67  $collection->addFieldToFilter('store_id', $this->getRequest()->getParam('store'));
68  } else {
69  if ($this->getRequest()->getParam('website')) {
70  $storeIds = $this->_storeManager->getWebsite($this->getRequest()->getParam('website'))->getStoreIds();
71  $collection->addFieldToFilter('store_id', ['in' => $storeIds]);
72  } else {
73  if ($this->getRequest()->getParam('group')) {
74  $storeIds = $this->_storeManager->getGroup($this->getRequest()->getParam('group'))->getStoreIds();
75  $collection->addFieldToFilter('store_id', ['in' => $storeIds]);
76  } elseif (!$collection->isLive()) {
77  $collection->addFieldToFilter(
78  'store_id',
79  ['eq' => $this->_storeManager->getStore(\Magento\Store\Model\Store::ADMIN_CODE)->getId()]
80  );
81  }
82  }
83  }
84 
85  $collection->load();
86 
87  $totals = $collection->getFirstItem();
88 
89  $this->addTotal(__('Revenue'), $totals->getRevenue());
90  $this->addTotal(__('Tax'), $totals->getTax());
91  $this->addTotal(__('Shipping'), $totals->getShipping());
92  $this->addTotal(__('Quantity'), $totals->getQuantity() * 1, true);
93  }
94 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__()
Definition: __.php:13
$totals
Definition: totalbar.phtml:10
$moduleManager
Definition: products.php:75
addTotal($label, $value, $isQuantity=false)
Definition: Bar.php:39
__construct(\Magento\Backend\Block\Template\Context $context, \Magento\Reports\Model\ResourceModel\Order\CollectionFactory $collectionFactory, \Magento\Framework\Module\Manager $moduleManager, array $data=[])
Definition: Totals.php:32