Skip to content

Commit 026239e

Browse files
committed
Merge remote-tracking branch 'mpi/MC-37217' into PR-10-14
2 parents 8e9cba6 + f892998 commit 026239e

File tree

3 files changed

+435
-6
lines changed

3 files changed

+435
-6
lines changed

app/code/Magento/Reports/Block/Adminhtml/Grid/Column/Renderer/Currency.php

Lines changed: 133 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,164 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace Magento\Reports\Block\Adminhtml\Grid\Column\Renderer;
89

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+
923
/**
1024
* Adminhtml grid item renderer currency
1125
*
1226
* @author Magento Core Team <core@magentocommerce.com>
1327
* @api
1428
* @since 100.0.2
29+
*
30+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1531
*/
16-
class Currency extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\Currency
32+
class Currency extends BackendCurrency
1733
{
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+
1866
/**
1967
* Renders grid column
2068
*
21-
* @param \Magento\Framework\DataObject $row
69+
* @param DataObject $row
2270
* @return string
71+
* @throws LocalizedException
72+
* @throws NoSuchEntityException
73+
* @throws Zend_Currency_Exception
2374
*/
24-
public function render(\Magento\Framework\DataObject $row)
75+
public function render(DataObject $row)
2576
{
2677
$data = $row->getData($this->getColumn()->getIndex());
27-
$currencyCode = $this->_getCurrencyCode($row);
78+
$currencyCode = $this->getStoreCurrencyCode($row);
2879

2980
if (!$currencyCode) {
3081
return $data;
3182
}
3283

33-
$data = (float)$data * $this->_getRate($row);
84+
$rate = $this->getStoreCurrencyRate($currencyCode, $row);
85+
86+
$data = (float)$data * $rate;
3487
$data = sprintf("%f", $data);
3588
$data = $this->_localeCurrency->getCurrency($currencyCode)->toCurrency($data);
3689
return $data;
3790
}
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+
}
38166
}

0 commit comments

Comments
 (0)