PriceCurrency.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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\ScopeInterface;
  8. use Magento\Store\Model\StoreManagerInterface;
  9. use Psr\Log\LoggerInterface as Logger;
  10. use Magento\Store\Model\Store;
  11. /**
  12. * Class PriceCurrency model for convert and format price value
  13. */
  14. class PriceCurrency implements \Magento\Framework\Pricing\PriceCurrencyInterface
  15. {
  16. /**
  17. * @var \Magento\Store\Model\StoreManagerInterface
  18. */
  19. protected $storeManager;
  20. /**
  21. * @var CurrencyFactory
  22. */
  23. protected $currencyFactory;
  24. /**
  25. * @var Logger
  26. */
  27. protected $logger;
  28. /**
  29. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  30. * @param CurrencyFactory $currencyFactory
  31. * @param Logger $logger
  32. */
  33. public function __construct(
  34. StoreManagerInterface $storeManager,
  35. CurrencyFactory $currencyFactory,
  36. Logger $logger
  37. ) {
  38. $this->storeManager = $storeManager;
  39. $this->currencyFactory = $currencyFactory;
  40. $this->logger = $logger;
  41. }
  42. /**
  43. * @inheritdoc
  44. */
  45. public function convert($amount, $scope = null, $currency = null)
  46. {
  47. $currentCurrency = $this->getCurrency($scope, $currency);
  48. return $this->getStore($scope)
  49. ->getBaseCurrency()
  50. ->convert($amount, $currentCurrency);
  51. }
  52. /**
  53. * @inheritdoc
  54. */
  55. public function convertAndRound($amount, $scope = null, $currency = null, $precision = self::DEFAULT_PRECISION)
  56. {
  57. return $this->roundPrice($this->convert($amount, $scope, $currency), $precision);
  58. }
  59. /**
  60. * @inheritdoc
  61. */
  62. public function format(
  63. $amount,
  64. $includeContainer = true,
  65. $precision = self::DEFAULT_PRECISION,
  66. $scope = null,
  67. $currency = null
  68. ) {
  69. return $this->getCurrency($scope, $currency)
  70. ->formatPrecision($amount, $precision, [], $includeContainer);
  71. }
  72. /**
  73. * @inheritdoc
  74. */
  75. public function convertAndFormat(
  76. $amount,
  77. $includeContainer = true,
  78. $precision = self::DEFAULT_PRECISION,
  79. $scope = null,
  80. $currency = null
  81. ) {
  82. $amount = $this->convert($amount, $scope, $currency);
  83. return $this->format($amount, $includeContainer, $precision, $scope, $currency);
  84. }
  85. /**
  86. * @inheritdoc
  87. */
  88. public function getCurrency($scope = null, $currency = null)
  89. {
  90. if ($currency instanceof Currency) {
  91. $currentCurrency = $currency;
  92. } elseif (is_string($currency)) {
  93. $currency = $this->currencyFactory->create()
  94. ->load($currency);
  95. $baseCurrency = $this->getStore($scope)
  96. ->getBaseCurrency();
  97. $currentCurrency = $baseCurrency->getRate($currency) ? $currency : $baseCurrency;
  98. } else {
  99. $currentCurrency = $this->getStore($scope)
  100. ->getCurrentCurrency();
  101. }
  102. return $currentCurrency;
  103. }
  104. /**
  105. * Get currrency symbol
  106. *
  107. * @param null|string|bool|int|\Magento\Framework\App\ScopeInterface $scope
  108. * @param \Magento\Framework\Model\AbstractModel|string|null $currency
  109. * @return string
  110. */
  111. public function getCurrencySymbol($scope = null, $currency = null)
  112. {
  113. return $this->getCurrency($scope, $currency)->getCurrencySymbol();
  114. }
  115. /**
  116. * Get store model
  117. *
  118. * @param null|string|bool|int|ScopeInterface $scope
  119. * @return Store
  120. */
  121. protected function getStore($scope = null)
  122. {
  123. try {
  124. if (!$scope instanceof Store) {
  125. $scope = $this->storeManager->getStore($scope);
  126. }
  127. } catch (\Exception $e) {
  128. $this->logger->critical($e);
  129. $scope = $this->storeManager->getStore();
  130. }
  131. return $scope;
  132. }
  133. /**
  134. * @inheritdoc
  135. */
  136. public function round($price)
  137. {
  138. return round($price, 2);
  139. }
  140. /**
  141. * Round price with precision
  142. *
  143. * @param float $price
  144. * @param int $precision
  145. * @return float
  146. */
  147. public function roundPrice($price, $precision = self::DEFAULT_PRECISION)
  148. {
  149. return round($price, $precision);
  150. }
  151. }