Currency.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Block\Widget\Grid\Column\Renderer;
  7. /**
  8. * Backend grid item renderer currency
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Currency extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer
  14. {
  15. /**
  16. * @var int
  17. */
  18. protected $_defaultWidth = 100;
  19. /**
  20. * Currency objects cache
  21. *
  22. * @var \Magento\Framework\DataObject[]
  23. */
  24. protected static $_currencies = [];
  25. /**
  26. * Application object
  27. *
  28. * @var \Magento\Store\Model\StoreManagerInterface
  29. */
  30. protected $_storeManager;
  31. /**
  32. * @var \Magento\Directory\Model\Currency\DefaultLocator
  33. */
  34. protected $_currencyLocator;
  35. /**
  36. * @var \Magento\Directory\Model\Currency
  37. */
  38. protected $_defaultBaseCurrency;
  39. /**
  40. * @var \Magento\Framework\Locale\CurrencyInterface
  41. */
  42. protected $_localeCurrency;
  43. /**
  44. * @param \Magento\Backend\Block\Context $context
  45. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  46. * @param \Magento\Directory\Model\Currency\DefaultLocator $currencyLocator
  47. * @param \Magento\Directory\Model\CurrencyFactory $currencyFactory
  48. * @param \Magento\Framework\Locale\CurrencyInterface $localeCurrency
  49. * @param array $data
  50. */
  51. public function __construct(
  52. \Magento\Backend\Block\Context $context,
  53. \Magento\Store\Model\StoreManagerInterface $storeManager,
  54. \Magento\Directory\Model\Currency\DefaultLocator $currencyLocator,
  55. \Magento\Directory\Model\CurrencyFactory $currencyFactory,
  56. \Magento\Framework\Locale\CurrencyInterface $localeCurrency,
  57. array $data = []
  58. ) {
  59. parent::__construct($context, $data);
  60. $this->_storeManager = $storeManager;
  61. $this->_currencyLocator = $currencyLocator;
  62. $this->_localeCurrency = $localeCurrency;
  63. $defaultBaseCurrencyCode = $currencyLocator->getDefaultCurrency($this->_request);
  64. $this->_defaultBaseCurrency = $currencyFactory->create()->load($defaultBaseCurrencyCode);
  65. }
  66. /**
  67. * Renders grid column
  68. *
  69. * @param \Magento\Framework\DataObject $row
  70. * @return string
  71. */
  72. public function render(\Magento\Framework\DataObject $row)
  73. {
  74. if ($data = (string)$this->_getValue($row)) {
  75. $currency_code = $this->_getCurrencyCode($row);
  76. $data = (float)$data * $this->_getRate($row);
  77. $sign = (bool)(int)$this->getColumn()->getShowNumberSign() && $data > 0 ? '+' : '';
  78. $data = sprintf("%f", $data);
  79. $data = $this->_localeCurrency->getCurrency($currency_code)->toCurrency($data);
  80. return $sign . $data;
  81. }
  82. return $this->getColumn()->getDefault();
  83. }
  84. /**
  85. * Returns currency code, false on error
  86. *
  87. * @param \Magento\Framework\DataObject $row
  88. * @return string
  89. */
  90. protected function _getCurrencyCode($row)
  91. {
  92. if ($code = $this->getColumn()->getCurrencyCode()) {
  93. return $code;
  94. }
  95. if ($code = $row->getData($this->getColumn()->getCurrency())) {
  96. return $code;
  97. }
  98. return $this->_currencyLocator->getDefaultCurrency($this->_request);
  99. }
  100. /**
  101. * Get rate for current row, 1 by default
  102. *
  103. * @param \Magento\Framework\DataObject $row
  104. * @return float|int
  105. */
  106. protected function _getRate($row)
  107. {
  108. if ($rate = $this->getColumn()->getRate()) {
  109. return (float)$rate;
  110. }
  111. if ($rate = $row->getData($this->getColumn()->getRateField())) {
  112. return (float)$rate;
  113. }
  114. return $this->_defaultBaseCurrency->getRate($this->_getCurrencyCode($row));
  115. }
  116. /**
  117. * Returns HTML for CSS
  118. *
  119. * @return string
  120. */
  121. public function renderCss()
  122. {
  123. return parent::renderCss() . ' a-right';
  124. }
  125. }