Grid.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Block\Dashboard\Orders;
  7. /**
  8. * Adminhtml dashboard recent orders grid
  9. *
  10. * @author Magento Core Team <core@magentocommerce.com>
  11. * @SuppressWarnings(PHPMD.DepthOfInheritance)
  12. */
  13. class Grid extends \Magento\Backend\Block\Dashboard\Grid
  14. {
  15. /**
  16. * @var \Magento\Reports\Model\ResourceModel\Order\CollectionFactory
  17. */
  18. protected $_collectionFactory;
  19. /**
  20. * @var \Magento\Framework\Module\Manager
  21. */
  22. protected $_moduleManager;
  23. /**
  24. * @param \Magento\Backend\Block\Template\Context $context
  25. * @param \Magento\Backend\Helper\Data $backendHelper
  26. * @param \Magento\Framework\Module\Manager $moduleManager
  27. * @param \Magento\Reports\Model\ResourceModel\Order\CollectionFactory $collectionFactory
  28. * @param array $data
  29. */
  30. public function __construct(
  31. \Magento\Backend\Block\Template\Context $context,
  32. \Magento\Backend\Helper\Data $backendHelper,
  33. \Magento\Framework\Module\Manager $moduleManager,
  34. \Magento\Reports\Model\ResourceModel\Order\CollectionFactory $collectionFactory,
  35. array $data = []
  36. ) {
  37. $this->_moduleManager = $moduleManager;
  38. $this->_collectionFactory = $collectionFactory;
  39. parent::__construct($context, $backendHelper, $data);
  40. }
  41. /**
  42. * @return void
  43. */
  44. protected function _construct()
  45. {
  46. parent::_construct();
  47. $this->setId('lastOrdersGrid');
  48. }
  49. /**
  50. * @return $this
  51. */
  52. protected function _prepareCollection()
  53. {
  54. if (!$this->_moduleManager->isEnabled('Magento_Reports')) {
  55. return $this;
  56. }
  57. $collection = $this->_collectionFactory->create()->addItemCountExpr()->joinCustomerName(
  58. 'customer'
  59. )->orderByCreatedAt();
  60. if ($this->getParam('store') || $this->getParam('website') || $this->getParam('group')) {
  61. if ($this->getParam('store')) {
  62. $collection->addAttributeToFilter('store_id', $this->getParam('store'));
  63. } elseif ($this->getParam('website')) {
  64. $storeIds = $this->_storeManager->getWebsite($this->getParam('website'))->getStoreIds();
  65. $collection->addAttributeToFilter('store_id', ['in' => $storeIds]);
  66. } elseif ($this->getParam('group')) {
  67. $storeIds = $this->_storeManager->getGroup($this->getParam('group'))->getStoreIds();
  68. $collection->addAttributeToFilter('store_id', ['in' => $storeIds]);
  69. }
  70. $collection->addRevenueToSelect();
  71. } else {
  72. $collection->addRevenueToSelect(true);
  73. }
  74. $this->setCollection($collection);
  75. return parent::_prepareCollection();
  76. }
  77. /**
  78. * Process collection after loading
  79. *
  80. * @return $this
  81. */
  82. protected function _afterLoadCollection()
  83. {
  84. foreach ($this->getCollection() as $item) {
  85. $item->getCustomer() ?: $item->setCustomer($item->getBillingAddress()->getName());
  86. }
  87. return $this;
  88. }
  89. /**
  90. * Prepares page sizes for dashboard grid with las 5 orders
  91. *
  92. * @return void
  93. */
  94. protected function _preparePage()
  95. {
  96. $this->getCollection()->setPageSize($this->getParam($this->getVarNameLimit(), $this->_defaultLimit));
  97. // Remove count of total orders
  98. // $this->getCollection()->setCurPage($this->getParam($this->getVarNamePage(), $this->_defaultPage));
  99. }
  100. /**
  101. * @return $this
  102. */
  103. protected function _prepareColumns()
  104. {
  105. $this->addColumn(
  106. 'customer',
  107. ['header' => __('Customer'), 'sortable' => false, 'index' => 'customer', 'default' => __('Guest')]
  108. );
  109. $this->addColumn(
  110. 'items',
  111. [
  112. 'header' => __('Items'),
  113. 'type' => 'number',
  114. 'sortable' => false,
  115. 'index' => 'items_count'
  116. ]
  117. );
  118. $baseCurrencyCode = $this->_storeManager->getStore((int)$this->getParam('store'))->getBaseCurrencyCode();
  119. $this->addColumn(
  120. 'total',
  121. [
  122. 'header' => __('Total'),
  123. 'sortable' => false,
  124. 'type' => 'currency',
  125. 'currency_code' => $baseCurrencyCode,
  126. 'index' => 'revenue'
  127. ]
  128. );
  129. $this->setFilterVisibility(false);
  130. $this->setPagerVisibility(false);
  131. return parent::_prepareColumns();
  132. }
  133. /**
  134. * {@inheritdoc}
  135. */
  136. public function getRowUrl($row)
  137. {
  138. return $this->getUrl('sales/order/view', ['order_id' => $row->getId()]);
  139. }
  140. }