Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Currency.php
Go to the documentation of this file.
1 <?php
8 
16 {
23 
29  protected static $_rateCache;
30 
36  protected function _construct()
37  {
38  $this->_init('directory_currency', 'currency_code');
39  $this->_currencyRateTable = $this->getTable('directory_currency_rate');
40  }
41 
49  public function getRate($currencyFrom, $currencyTo)
50  {
51  if ($currencyFrom instanceof \Magento\Directory\Model\Currency) {
52  $currencyFrom = $currencyFrom->getCode();
53  }
54 
55  if ($currencyTo instanceof \Magento\Directory\Model\Currency) {
56  $currencyTo = $currencyTo->getCode();
57  }
58 
59  if ($currencyFrom == $currencyTo) {
60  return 1;
61  }
62 
63  if (!isset(self::$_rateCache[$currencyFrom][$currencyTo])) {
64  $connection = $this->getConnection();
65  $bind = [':currency_from' => strtoupper($currencyFrom), ':currency_to' => strtoupper($currencyTo)];
66  $select = $connection->select()->from(
67  $this->_currencyRateTable,
68  'rate'
69  )->where(
70  'currency_from = :currency_from'
71  )->where(
72  'currency_to = :currency_to'
73  );
74 
75  self::$_rateCache[$currencyFrom][$currencyTo] = $connection->fetchOne($select, $bind);
76  }
77 
78  return self::$_rateCache[$currencyFrom][$currencyTo];
79  }
80 
88  public function getAnyRate($currencyFrom, $currencyTo)
89  {
90  if ($currencyFrom instanceof \Magento\Directory\Model\Currency) {
91  $currencyFrom = $currencyFrom->getCode();
92  }
93 
94  if ($currencyTo instanceof \Magento\Directory\Model\Currency) {
95  $currencyTo = $currencyTo->getCode();
96  }
97 
98  if ($currencyFrom == $currencyTo) {
99  return 1;
100  }
101 
102  if (!isset(self::$_rateCache[$currencyFrom][$currencyTo])) {
103  $connection = $this->getConnection();
104  $bind = [':currency_from' => strtoupper($currencyFrom), ':currency_to' => strtoupper($currencyTo)];
105  $select = $connection->select()->from(
106  $this->_currencyRateTable,
107  'rate'
108  )->where(
109  'currency_from = :currency_from'
110  )->where(
111  'currency_to = :currency_to'
112  );
113 
114  $rate = $connection->fetchOne($select, $bind);
115  if ($rate === false) {
116  $select = $connection->select()->from(
117  $this->_currencyRateTable,
118  new \Zend_Db_Expr('1/rate')
119  )->where(
120  'currency_to = :currency_from'
121  )->where(
122  'currency_from = :currency_to'
123  );
124  $rate = $connection->fetchOne($select, $bind);
125  }
126  self::$_rateCache[$currencyFrom][$currencyTo] = $rate;
127  }
128 
129  return self::$_rateCache[$currencyFrom][$currencyTo];
130  }
131 
139  public function saveRates($rates)
140  {
141  if (is_array($rates) && sizeof($rates) > 0) {
142  $connection = $this->getConnection();
143  $data = [];
144  foreach ($rates as $currencyCode => $rate) {
145  foreach ($rate as $currencyTo => $value) {
146  $value = abs($value);
147  if ($value == 0) {
148  continue;
149  }
150  $data[] = ['currency_from' => $currencyCode, 'currency_to' => $currencyTo, 'rate' => $value];
151  }
152  }
153  if ($data) {
154  $connection->insertOnDuplicate($this->_currencyRateTable, $data, ['rate']);
155  }
156  } else {
157  throw new \Magento\Framework\Exception\LocalizedException(__('Please correct the rates received'));
158  }
159  }
160 
171  public function getConfigCurrencies($model, $path)
172  {
173  $connection = $this->getConnection();
174  $bind = [':config_path' => $path];
175  $select = $connection->select()->from($this->getTable('core_config_data'))->where('path = :config_path');
176  $result = [];
177  $rowSet = $connection->fetchAll($select, $bind);
178  foreach ($rowSet as $row) {
179  $result = array_merge($result, explode(',', $row['value']));
180  }
181  sort($result);
182 
183  return array_unique($result);
184  }
185 
193  public function getCurrencyRates($currency, $toCurrencies = null)
194  {
195  $rates = [];
196  if (is_array($currency)) {
197  foreach ($currency as $code) {
198  $rates[$code] = $this->_getRatesByCode($code, $toCurrencies);
199  }
200  } else {
201  $rates = $this->_getRatesByCode($currency, $toCurrencies);
202  }
203 
204  return $rates;
205  }
206 
214  protected function _getRatesByCode($code, $toCurrencies = null)
215  {
216  $connection = $this->getConnection();
217  $bind = [':currency_from' => $code];
218  $select = $connection->select()->from(
219  $this->getTable('directory_currency_rate'),
220  ['currency_to', 'rate']
221  )->where(
222  'currency_from = :currency_from'
223  )->where(
224  'currency_to IN(?)',
225  $toCurrencies
226  );
227  $rowSet = $connection->fetchAll($select, $bind);
228  $result = [];
229 
230  foreach ($rowSet as $row) {
231  $result[$row['currency_to']] = $row['rate'];
232  }
233 
234  return $result;
235  }
236 }
getRate($currencyFrom, $currencyTo)
Definition: Currency.php:49
__()
Definition: __.php:13
$rates
Definition: tax.phtml:35
$value
Definition: gender.phtml:16
getAnyRate($currencyFrom, $currencyTo)
Definition: Currency.php:88
getCurrencyRates($currency, $toCurrencies=null)
Definition: Currency.php:193
$connection
Definition: bulk.php:13
_getRatesByCode($code, $toCurrencies=null)
Definition: Currency.php:214
$code
Definition: info.phtml:12