Most.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Block\Dashboard\Tab\Customers;
  7. /**
  8. * Adminhtml dashboard most active buyers
  9. *
  10. * @api
  11. * @SuppressWarnings(PHPMD.DepthOfInheritance)
  12. * @since 100.0.2
  13. */
  14. class Most extends \Magento\Backend\Block\Dashboard\Grid
  15. {
  16. /**
  17. * @var \Magento\Reports\Model\ResourceModel\Order\CollectionFactory
  18. */
  19. protected $_collectionFactory;
  20. /**
  21. * @param \Magento\Backend\Block\Template\Context $context
  22. * @param \Magento\Backend\Helper\Data $backendHelper
  23. * @param \Magento\Reports\Model\ResourceModel\Order\CollectionFactory $collectionFactory
  24. * @param array $data
  25. */
  26. public function __construct(
  27. \Magento\Backend\Block\Template\Context $context,
  28. \Magento\Backend\Helper\Data $backendHelper,
  29. \Magento\Reports\Model\ResourceModel\Order\CollectionFactory $collectionFactory,
  30. array $data = []
  31. ) {
  32. $this->_collectionFactory = $collectionFactory;
  33. parent::__construct($context, $backendHelper, $data);
  34. }
  35. /**
  36. * @return void
  37. */
  38. protected function _construct()
  39. {
  40. parent::_construct();
  41. $this->setId('customersMostGrid');
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. protected function _prepareCollection()
  47. {
  48. $collection = $this->_collectionFactory->create();
  49. /* @var $collection \Magento\Reports\Model\ResourceModel\Order\Collection */
  50. $collection->groupByCustomer()->addOrdersCount()->joinCustomerName();
  51. $storeFilter = 0;
  52. if ($this->getParam('store')) {
  53. $collection->addAttributeToFilter('store_id', $this->getParam('store'));
  54. $storeFilter = 1;
  55. } elseif ($this->getParam('website')) {
  56. $storeIds = $this->_storeManager->getWebsite($this->getParam('website'))->getStoreIds();
  57. $collection->addAttributeToFilter('store_id', ['in' => $storeIds]);
  58. } elseif ($this->getParam('group')) {
  59. $storeIds = $this->_storeManager->getGroup($this->getParam('group'))->getStoreIds();
  60. $collection->addAttributeToFilter('store_id', ['in' => $storeIds]);
  61. }
  62. $collection->addSumAvgTotals($storeFilter)->orderByTotalAmount();
  63. $this->setCollection($collection);
  64. return parent::_prepareCollection();
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. protected function _prepareColumns()
  70. {
  71. $this->addColumn('name', ['header' => __('Customer'), 'sortable' => false, 'index' => 'name']);
  72. $this->addColumn(
  73. 'orders_count',
  74. [
  75. 'header' => __('Orders'),
  76. 'sortable' => false,
  77. 'index' => 'orders_count',
  78. 'type' => 'number',
  79. 'header_css_class' => 'col-orders',
  80. 'column_css_class' => 'col-orders'
  81. ]
  82. );
  83. $baseCurrencyCode = (string)$this->_storeManager->getStore(
  84. (int)$this->getParam('store')
  85. )->getBaseCurrencyCode();
  86. $this->addColumn(
  87. 'orders_avg_amount',
  88. [
  89. 'header' => __('Average'),
  90. 'sortable' => false,
  91. 'type' => 'currency',
  92. 'currency_code' => $baseCurrencyCode,
  93. 'index' => 'orders_avg_amount',
  94. 'header_css_class' => 'col-avg',
  95. 'column_css_class' => 'col-avg'
  96. ]
  97. );
  98. $this->addColumn(
  99. 'orders_sum_amount',
  100. [
  101. 'header' => __('Total'),
  102. 'sortable' => false,
  103. 'type' => 'currency',
  104. 'currency_code' => $baseCurrencyCode,
  105. 'index' => 'orders_sum_amount',
  106. 'header_css_class' => 'col-total',
  107. 'column_css_class' => 'col-total'
  108. ]
  109. );
  110. $this->setFilterVisibility(false);
  111. $this->setPagerVisibility(false);
  112. return parent::_prepareColumns();
  113. }
  114. /**
  115. * {@inheritdoc}
  116. */
  117. public function getRowUrl($row)
  118. {
  119. return $this->getUrl('customer/index/edit', ['id' => $row->getCustomerId()]);
  120. }
  121. }