Currency.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Directory\Model;
  7. use Magento\Framework\App\ObjectManager;
  8. use Magento\Framework\Exception\InputException;
  9. use Magento\Directory\Model\Currency\Filter;
  10. /**
  11. * Currency model
  12. *
  13. * @api
  14. *
  15. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  16. * @since 100.0.2
  17. */
  18. class Currency extends \Magento\Framework\Model\AbstractModel
  19. {
  20. /**
  21. * CONFIG path constants
  22. */
  23. const XML_PATH_CURRENCY_ALLOW = 'currency/options/allow';
  24. const XML_PATH_CURRENCY_DEFAULT = 'currency/options/default';
  25. const XML_PATH_CURRENCY_BASE = 'currency/options/base';
  26. /**
  27. * @var Filter
  28. */
  29. protected $_filter;
  30. /**
  31. * Currency Rates
  32. *
  33. * @var array
  34. */
  35. protected $_rates;
  36. /**
  37. * @var \Magento\Framework\Locale\FormatInterface
  38. */
  39. protected $_localeFormat;
  40. /**
  41. * @var \Magento\Store\Model\StoreManagerInterface
  42. */
  43. protected $_storeManager;
  44. /**
  45. * @var \Magento\Directory\Helper\Data
  46. */
  47. protected $_directoryHelper;
  48. /**
  49. * @var \Magento\Directory\Model\Currency\FilterFactory
  50. */
  51. protected $_currencyFilterFactory;
  52. /**
  53. * @var \Magento\Framework\Locale\CurrencyInterface
  54. */
  55. protected $_localeCurrency;
  56. /**
  57. * @var CurrencyConfig
  58. */
  59. private $currencyConfig;
  60. /**
  61. * @param \Magento\Framework\Model\Context $context
  62. * @param \Magento\Framework\Registry $registry
  63. * @param \Magento\Framework\Locale\FormatInterface $localeFormat
  64. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  65. * @param \Magento\Directory\Helper\Data $directoryHelper
  66. * @param Currency\FilterFactory $currencyFilterFactory
  67. * @param \Magento\Framework\Locale\CurrencyInterface $localeCurrency
  68. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  69. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  70. * @param array $data
  71. * @param CurrencyConfig $currencyConfig
  72. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  73. */
  74. public function __construct(
  75. \Magento\Framework\Model\Context $context,
  76. \Magento\Framework\Registry $registry,
  77. \Magento\Framework\Locale\FormatInterface $localeFormat,
  78. \Magento\Store\Model\StoreManagerInterface $storeManager,
  79. \Magento\Directory\Helper\Data $directoryHelper,
  80. \Magento\Directory\Model\Currency\FilterFactory $currencyFilterFactory,
  81. \Magento\Framework\Locale\CurrencyInterface $localeCurrency,
  82. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  83. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  84. array $data = [],
  85. CurrencyConfig $currencyConfig = null
  86. ) {
  87. parent::__construct(
  88. $context,
  89. $registry,
  90. $resource,
  91. $resourceCollection,
  92. $data
  93. );
  94. $this->_localeFormat = $localeFormat;
  95. $this->_storeManager = $storeManager;
  96. $this->_directoryHelper = $directoryHelper;
  97. $this->_currencyFilterFactory = $currencyFilterFactory;
  98. $this->_localeCurrency = $localeCurrency;
  99. $this->currencyConfig = $currencyConfig ?: ObjectManager::getInstance()->get(CurrencyConfig::class);
  100. }
  101. /**
  102. * @return void
  103. */
  104. protected function _construct()
  105. {
  106. $this->_init(\Magento\Directory\Model\ResourceModel\Currency::class);
  107. }
  108. /**
  109. * Get currency code
  110. *
  111. * @return string
  112. */
  113. public function getCode()
  114. {
  115. return $this->_getData('currency_code');
  116. }
  117. /**
  118. * Get currency code
  119. *
  120. * @return string
  121. */
  122. public function getCurrencyCode()
  123. {
  124. return $this->_getData('currency_code');
  125. }
  126. /**
  127. * Currency Rates getter
  128. *
  129. * @return array
  130. */
  131. public function getRates()
  132. {
  133. return $this->_rates;
  134. }
  135. /**
  136. * Currency Rates setter
  137. *
  138. * @param array $rates Currency Rates
  139. * @return $this
  140. */
  141. public function setRates(array $rates)
  142. {
  143. $this->_rates = $rates;
  144. return $this;
  145. }
  146. /**
  147. * Loading currency data
  148. *
  149. * @param string $id
  150. * @param string $field
  151. * @return $this
  152. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  153. */
  154. public function load($id, $field = null)
  155. {
  156. $this->unsRate();
  157. $this->setData('currency_code', $id);
  158. return $this;
  159. }
  160. /**
  161. * Get currency rate (only base => allowed)
  162. *
  163. * @param mixed $toCurrency
  164. * @return float
  165. */
  166. public function getRate($toCurrency)
  167. {
  168. $code = $this->getCurrencyCodeFromToCurrency($toCurrency);
  169. $rates = $this->getRates();
  170. if (!isset($rates[$code])) {
  171. $rates[$code] = $this->_getResource()->getRate($this->getCode(), $toCurrency);
  172. $this->setRates($rates);
  173. }
  174. return $rates[$code];
  175. }
  176. /**
  177. * Get currency rate (base=>allowed or allowed=>base)
  178. *
  179. * @param mixed $toCurrency
  180. * @return float
  181. */
  182. public function getAnyRate($toCurrency)
  183. {
  184. $code = $this->getCurrencyCodeFromToCurrency($toCurrency);
  185. $rates = $this->getRates();
  186. if (!isset($rates[$code])) {
  187. $rates[$code] = $this->_getResource()->getAnyRate($this->getCode(), $toCurrency);
  188. $this->setRates($rates);
  189. }
  190. return $rates[$code];
  191. }
  192. /**
  193. * Convert price to currency format
  194. *
  195. * @param float $price
  196. * @param mixed $toCurrency
  197. * @return float
  198. * @throws \Exception
  199. */
  200. public function convert($price, $toCurrency = null)
  201. {
  202. if ($toCurrency === null) {
  203. return $price;
  204. } elseif ($rate = $this->getRate($toCurrency)) {
  205. return (float)$price * (float)$rate;
  206. }
  207. throw new \Exception(__(
  208. 'Undefined rate from "%1-%2".',
  209. $this->getCode(),
  210. $this->getCurrencyCodeFromToCurrency($toCurrency)
  211. ));
  212. }
  213. /**
  214. * @param mixed $toCurrency
  215. * @return string
  216. * @throws \Magento\Framework\Exception\InputException
  217. */
  218. private function getCurrencyCodeFromToCurrency($toCurrency)
  219. {
  220. if (is_string($toCurrency)) {
  221. $code = $toCurrency;
  222. } elseif ($toCurrency instanceof \Magento\Directory\Model\Currency) {
  223. $code = $toCurrency->getCurrencyCode();
  224. } else {
  225. throw new InputException(__('Please correct the target currency.'));
  226. }
  227. return $code;
  228. }
  229. /**
  230. * Get currency filter
  231. *
  232. * @return Filter
  233. */
  234. public function getFilter()
  235. {
  236. if (!$this->_filter) {
  237. $this->_filter = $this->_currencyFilterFactory->create(['code' => $this->getCode()]);
  238. }
  239. return $this->_filter;
  240. }
  241. /**
  242. * Format price to currency format
  243. *
  244. * @param float $price
  245. * @param array $options
  246. * @param bool $includeContainer
  247. * @param bool $addBrackets
  248. * @return string
  249. */
  250. public function format($price, $options = [], $includeContainer = true, $addBrackets = false)
  251. {
  252. return $this->formatPrecision($price, 2, $options, $includeContainer, $addBrackets);
  253. }
  254. /**
  255. * Apply currency format to number with specific rounding precision
  256. *
  257. * @param float $price
  258. * @param int $precision
  259. * @param array $options
  260. * @param bool $includeContainer
  261. * @param bool $addBrackets
  262. * @return string
  263. */
  264. public function formatPrecision(
  265. $price,
  266. $precision,
  267. $options = [],
  268. $includeContainer = true,
  269. $addBrackets = false
  270. ) {
  271. if (!isset($options['precision'])) {
  272. $options['precision'] = $precision;
  273. }
  274. if ($includeContainer) {
  275. return '<span class="price">' . ($addBrackets ? '[' : '') . $this->formatTxt(
  276. $price,
  277. $options
  278. ) . ($addBrackets ? ']' : '') . '</span>';
  279. }
  280. return $this->formatTxt($price, $options);
  281. }
  282. /**
  283. * @param float $price
  284. * @param array $options
  285. * @return string
  286. */
  287. public function formatTxt($price, $options = [])
  288. {
  289. if (!is_numeric($price)) {
  290. $price = $this->_localeFormat->getNumber($price);
  291. }
  292. /**
  293. * Fix problem with 12 000 000, 1 200 000
  294. *
  295. * %f - the argument is treated as a float, and presented as a floating-point number (locale aware).
  296. * %F - the argument is treated as a float, and presented as a floating-point number (non-locale aware).
  297. */
  298. $price = sprintf("%F", $price);
  299. return $this->_localeCurrency->getCurrency($this->getCode())->toCurrency($price, $options);
  300. }
  301. /**
  302. * Return currency symbol for current locale and currency code
  303. *
  304. * @return string
  305. */
  306. public function getCurrencySymbol()
  307. {
  308. return $this->_localeCurrency->getCurrency($this->getCode())->getSymbol();
  309. }
  310. /**
  311. * @return string
  312. */
  313. public function getOutputFormat()
  314. {
  315. $formatted = $this->formatTxt(0);
  316. $number = $this->formatTxt(0, ['display' => \Magento\Framework\Currency::NO_SYMBOL]);
  317. return str_replace($this->trimUnicodeDirectionMark($number), '%s', $formatted);
  318. }
  319. /**
  320. * Retrieve allowed currencies according to config
  321. *
  322. * @return array
  323. */
  324. public function getConfigAllowCurrencies()
  325. {
  326. $allowedCurrencies = $this->currencyConfig->getConfigCurrencies(self::XML_PATH_CURRENCY_ALLOW);
  327. $appBaseCurrencyCode = $this->_directoryHelper->getBaseCurrencyCode();
  328. if (!in_array($appBaseCurrencyCode, $allowedCurrencies)) {
  329. $allowedCurrencies[] = $appBaseCurrencyCode;
  330. }
  331. foreach ($this->_storeManager->getStores() as $store) {
  332. $code = $store->getBaseCurrencyCode();
  333. if (!in_array($code, $allowedCurrencies)) {
  334. $allowedCurrencies[] = $code;
  335. }
  336. }
  337. return $allowedCurrencies;
  338. }
  339. /**
  340. * Retrieve default currencies according to config
  341. *
  342. * @return array
  343. */
  344. public function getConfigDefaultCurrencies()
  345. {
  346. return $this->currencyConfig->getConfigCurrencies(self::XML_PATH_CURRENCY_DEFAULT);
  347. }
  348. /**
  349. * @return array
  350. */
  351. public function getConfigBaseCurrencies()
  352. {
  353. return $this->currencyConfig->getConfigCurrencies(self::XML_PATH_CURRENCY_BASE);
  354. }
  355. /**
  356. * Retrieve currency rates to other currencies
  357. *
  358. * @param string $currency
  359. * @param array|null $toCurrencies
  360. * @return array
  361. */
  362. public function getCurrencyRates($currency, $toCurrencies = null)
  363. {
  364. if ($currency instanceof \Magento\Directory\Model\Currency) {
  365. $currency = $currency->getCode();
  366. }
  367. $data = $this->_getResource()->getCurrencyRates($currency, $toCurrencies);
  368. return $data;
  369. }
  370. /**
  371. * Save currency rates
  372. *
  373. * @param array $rates
  374. * @return $this
  375. */
  376. public function saveRates($rates)
  377. {
  378. $this->_getResource()->saveRates($rates);
  379. return $this;
  380. }
  381. /**
  382. * This method removes LRM and RLM marks from string
  383. *
  384. * @param string $string
  385. * @return $this
  386. */
  387. private function trimUnicodeDirectionMark($string)
  388. {
  389. if (preg_match('/^(\x{200E}|\x{200F})/u', $string, $match)) {
  390. $string = preg_replace('/^'.$match[1].'/u', '', $string);
  391. }
  392. return $string;
  393. }
  394. }