|
3 | 3 | * Copyright © Magento, Inc. All rights reserved.
|
4 | 4 | * See COPYING.txt for license details.
|
5 | 5 | */
|
| 6 | +declare(strict_types=1); |
6 | 7 |
|
7 | 8 | namespace Magento\Reports\Block\Adminhtml\Grid\Column\Renderer;
|
8 | 9 |
|
| 10 | +use Magento\Backend\Block\Widget\Grid\Column\Renderer\Currency as BackendCurrency; |
| 11 | +use Magento\Backend\Block\Context; |
| 12 | +use Magento\Directory\Model\Currency\DefaultLocator; |
| 13 | +use Magento\Directory\Model\CurrencyFactory; |
| 14 | +use Magento\Framework\DataObject; |
| 15 | +use Magento\Framework\Exception\LocalizedException; |
| 16 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 17 | +use Magento\Framework\Locale\CurrencyInterface; |
| 18 | +use Magento\Store\Model\ScopeInterface; |
| 19 | +use Magento\Store\Model\Store; |
| 20 | +use Magento\Store\Model\StoreManagerInterface; |
| 21 | +use Zend_Currency_Exception; |
| 22 | + |
9 | 23 | /**
|
10 | 24 | * Adminhtml grid item renderer currency
|
11 | 25 | *
|
12 | 26 | * @author Magento Core Team <core@magentocommerce.com>
|
13 | 27 | * @api
|
14 | 28 | * @since 100.0.2
|
| 29 | + * |
| 30 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
15 | 31 | */
|
16 |
| -class Currency extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\Currency |
| 32 | +class Currency extends BackendCurrency |
17 | 33 | {
|
| 34 | + /** |
| 35 | + * @var CurrencyFactory |
| 36 | + */ |
| 37 | + private $currencyFactory; |
| 38 | + |
| 39 | + /** |
| 40 | + * @param Context $context |
| 41 | + * @param StoreManagerInterface $storeManager |
| 42 | + * @param DefaultLocator $currencyLocator |
| 43 | + * @param CurrencyFactory $currencyFactory |
| 44 | + * @param CurrencyInterface $localeCurrency |
| 45 | + * @param array $data |
| 46 | + */ |
| 47 | + public function __construct( |
| 48 | + Context $context, |
| 49 | + StoreManagerInterface $storeManager, |
| 50 | + DefaultLocator $currencyLocator, |
| 51 | + CurrencyFactory $currencyFactory, |
| 52 | + CurrencyInterface $localeCurrency, |
| 53 | + array $data = [] |
| 54 | + ) { |
| 55 | + parent::__construct( |
| 56 | + $context, |
| 57 | + $storeManager, |
| 58 | + $currencyLocator, |
| 59 | + $currencyFactory, |
| 60 | + $localeCurrency, |
| 61 | + $data |
| 62 | + ); |
| 63 | + $this->currencyFactory = $currencyFactory; |
| 64 | + } |
| 65 | + |
18 | 66 | /**
|
19 | 67 | * Renders grid column
|
20 | 68 | *
|
21 |
| - * @param \Magento\Framework\DataObject $row |
| 69 | + * @param DataObject $row |
22 | 70 | * @return string
|
| 71 | + * @throws LocalizedException |
| 72 | + * @throws NoSuchEntityException |
| 73 | + * @throws Zend_Currency_Exception |
23 | 74 | */
|
24 |
| - public function render(\Magento\Framework\DataObject $row) |
| 75 | + public function render(DataObject $row) |
25 | 76 | {
|
26 | 77 | $data = $row->getData($this->getColumn()->getIndex());
|
27 |
| - $currencyCode = $this->_getCurrencyCode($row); |
| 78 | + $currencyCode = $this->getStoreCurrencyCode($row); |
28 | 79 |
|
29 | 80 | if (!$currencyCode) {
|
30 | 81 | return $data;
|
31 | 82 | }
|
32 | 83 |
|
33 |
| - $data = (float)$data * $this->_getRate($row); |
| 84 | + $rate = $this->getStoreCurrencyRate($currencyCode, $row); |
| 85 | + |
| 86 | + $data = (float)$data * $rate; |
34 | 87 | $data = sprintf("%f", $data);
|
35 | 88 | $data = $this->_localeCurrency->getCurrency($currencyCode)->toCurrency($data);
|
36 | 89 | return $data;
|
37 | 90 | }
|
| 91 | + |
| 92 | + /** |
| 93 | + * Get admin currency code |
| 94 | + * |
| 95 | + * @return string |
| 96 | + * @throws LocalizedException |
| 97 | + * @throws NoSuchEntityException |
| 98 | + */ |
| 99 | + private function getAdminCurrencyCode(): string |
| 100 | + { |
| 101 | + $adminWebsiteId = (int) $this->_storeManager |
| 102 | + ->getStore(Store::ADMIN_CODE) |
| 103 | + ->getWebsiteId(); |
| 104 | + return (string) $this->_storeManager |
| 105 | + ->getWebsite($adminWebsiteId) |
| 106 | + ->getBaseCurrencyCode(); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Get store currency code |
| 111 | + * |
| 112 | + * @param DataObject $row |
| 113 | + * @return string |
| 114 | + * @throws NoSuchEntityException |
| 115 | + */ |
| 116 | + private function getStoreCurrencyCode(DataObject $row): string |
| 117 | + { |
| 118 | + $catalogPriceScope = $this->getCatalogPriceScope(); |
| 119 | + $storeId = $this->_request->getParam('store_ids'); |
| 120 | + if ($catalogPriceScope != 0 && !empty($storeId)) { |
| 121 | + $currencyCode = $this->_storeManager->getStore($storeId)->getBaseCurrencyCode(); |
| 122 | + } elseif ($catalogPriceScope != 0) { |
| 123 | + $currencyCode = $this->_currencyLocator->getDefaultCurrency($this->_request); |
| 124 | + } else { |
| 125 | + $currencyCode = $this->_getCurrencyCode($row); |
| 126 | + } |
| 127 | + return $currencyCode; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Get store currency rate |
| 132 | + * |
| 133 | + * @param string $currencyCode |
| 134 | + * @param DataObject $row |
| 135 | + * @return float |
| 136 | + * @throws LocalizedException |
| 137 | + * @throws NoSuchEntityException |
| 138 | + */ |
| 139 | + private function getStoreCurrencyRate(string $currencyCode, DataObject $row): float |
| 140 | + { |
| 141 | + $catalogPriceScope = $this->getCatalogPriceScope(); |
| 142 | + $adminCurrencyCode = $this->getAdminCurrencyCode(); |
| 143 | + |
| 144 | + if (($catalogPriceScope != 0 |
| 145 | + && $adminCurrencyCode !== $currencyCode)) { |
| 146 | + $storeCurrency = $this->currencyFactory->create()->load($adminCurrencyCode); |
| 147 | + $currencyRate = $storeCurrency->getRate($currencyCode); |
| 148 | + } else { |
| 149 | + $currencyRate = $this->_getRate($row); |
| 150 | + } |
| 151 | + return (float) $currencyRate; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Get catalog price scope from the admin config |
| 156 | + * |
| 157 | + * @return int |
| 158 | + */ |
| 159 | + private function getCatalogPriceScope(): int |
| 160 | + { |
| 161 | + return (int) $this->_scopeConfig->getValue( |
| 162 | + Store::XML_PATH_PRICE_SCOPE, |
| 163 | + ScopeInterface::SCOPE_WEBSITE |
| 164 | + ); |
| 165 | + } |
38 | 166 | }
|
0 commit comments