Sales.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Block\Adminhtml\Edit\Tab\View;
  7. use Magento\Customer\Controller\RegistryConstants;
  8. use Magento\Directory\Model\Currency;
  9. use Magento\Sales\Model\Order;
  10. /**
  11. * Adminhtml customer view sales block
  12. */
  13. class Sales extends \Magento\Backend\Block\Template
  14. {
  15. /**
  16. * Sales entity collection
  17. *
  18. * @var \Magento\Sales\Model\ResourceModel\Sale\Collection
  19. */
  20. protected $_collection;
  21. /**
  22. * @var array
  23. */
  24. protected $_groupedCollection;
  25. /**
  26. * @var int[]
  27. */
  28. protected $_websiteCounts;
  29. /**
  30. * Currency model
  31. *
  32. * @var Currency
  33. */
  34. protected $_currency;
  35. /**
  36. * Core registry
  37. *
  38. * @var \Magento\Framework\Registry
  39. */
  40. protected $_coreRegistry = null;
  41. /**
  42. * @var \Magento\Directory\Model\CurrencyFactory
  43. */
  44. protected $_currencyFactory;
  45. /**
  46. * @var \Magento\Sales\Model\ResourceModel\Sale\CollectionFactory
  47. */
  48. protected $_collectionFactory;
  49. /**
  50. * Constructor
  51. *
  52. * @param \Magento\Backend\Block\Template\Context $context
  53. * @param \Magento\Directory\Model\CurrencyFactory $currencyFactory
  54. * @param \Magento\Sales\Model\ResourceModel\Sale\CollectionFactory $collectionFactory
  55. * @param \Magento\Framework\Registry $coreRegistry
  56. * @param array $data
  57. */
  58. public function __construct(
  59. \Magento\Backend\Block\Template\Context $context,
  60. \Magento\Directory\Model\CurrencyFactory $currencyFactory,
  61. \Magento\Sales\Model\ResourceModel\Sale\CollectionFactory $collectionFactory,
  62. \Magento\Framework\Registry $coreRegistry,
  63. array $data = []
  64. ) {
  65. $this->_coreRegistry = $coreRegistry;
  66. $this->_currencyFactory = $currencyFactory;
  67. $this->_collectionFactory = $collectionFactory;
  68. parent::__construct($context, $data);
  69. }
  70. /**
  71. * Initialize the sales grid.
  72. *
  73. * @return void
  74. */
  75. protected function _construct()
  76. {
  77. parent::_construct();
  78. $this->setId('customer_view_sales_grid');
  79. }
  80. /**
  81. * Execute before toHtml() code.
  82. *
  83. * @return $this
  84. */
  85. public function _beforeToHtml()
  86. {
  87. $this->_currency = $this->_currencyFactory->create()->load(
  88. $this->_scopeConfig->getValue(
  89. Currency::XML_PATH_CURRENCY_BASE,
  90. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  91. )
  92. );
  93. $this->_collection = $this->_collectionFactory->create()->setCustomerIdFilter(
  94. (int)$this->_coreRegistry->registry(RegistryConstants::CURRENT_CUSTOMER_ID)
  95. )->setOrderStateFilter(
  96. Order::STATE_CANCELED,
  97. true
  98. )->load();
  99. $this->_groupedCollection = [];
  100. foreach ($this->_collection as $sale) {
  101. if ($sale->getStoreId() !== null) {
  102. $store = $this->_storeManager->getStore($sale->getStoreId());
  103. $websiteId = $store->getWebsiteId();
  104. $groupId = $store->getGroupId();
  105. $storeId = $store->getId();
  106. $sale->setWebsiteId($store->getWebsiteId());
  107. $sale->setWebsiteName($store->getWebsite()->getName());
  108. $sale->setGroupId($store->getGroupId());
  109. $sale->setGroupName($store->getGroup()->getName());
  110. } else {
  111. $websiteId = 0;
  112. $groupId = 0;
  113. $storeId = 0;
  114. $sale->setStoreName(__('Deleted Stores'));
  115. }
  116. $this->_groupedCollection[$websiteId][$groupId][$storeId] = $sale;
  117. $this->_websiteCounts[$websiteId] = isset(
  118. $this->_websiteCounts[$websiteId]
  119. ) ? $this->_websiteCounts[$websiteId] + 1 : 1;
  120. }
  121. return parent::_beforeToHtml();
  122. }
  123. /**
  124. * Retrieve the website count for the specified website Id
  125. *
  126. * @param int $websiteId
  127. * @return int
  128. */
  129. public function getWebsiteCount($websiteId)
  130. {
  131. return $this->_websiteCounts[$websiteId] ?? 0;
  132. }
  133. /**
  134. * Returns Grouped Collection Rows
  135. *
  136. * @return array
  137. */
  138. public function getRows()
  139. {
  140. return $this->_groupedCollection;
  141. }
  142. /**
  143. * Return totals data
  144. *
  145. * @return \Magento\Framework\DataObject
  146. */
  147. public function getTotals()
  148. {
  149. return $this->_collection->getTotals();
  150. }
  151. /**
  152. * Format price by specified website
  153. *
  154. * @param float $price
  155. * @param null|int $websiteId
  156. *
  157. * @return string
  158. * @throws \Magento\Framework\Exception\LocalizedException
  159. */
  160. public function formatCurrency($price, $websiteId = null)
  161. {
  162. return $this->_storeManager->getWebsite($websiteId)->getBaseCurrency()->format($price);
  163. }
  164. /**
  165. * Is single store mode
  166. *
  167. * @return bool
  168. */
  169. public function isSingleStoreMode()
  170. {
  171. return $this->_storeManager->isSingleStoreMode();
  172. }
  173. }