PageRenderTime 53ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Directory/Model/Currency.php

https://gitlab.com/LisovyiEvhenii/ismextensions
PHP | 357 lines | 159 code | 27 blank | 171 comment | 18 complexity | f24df11a73ec1ac000d4ee91fe4cccf4 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magento.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Directory
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Currency model
  28. *
  29. * @category Mage
  30. * @package Mage_Directory
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Directory_Model_Currency extends Mage_Core_Model_Abstract
  34. {
  35. /**
  36. * CONFIG path constant: ALLOW
  37. */
  38. const XML_PATH_CURRENCY_ALLOW = 'currency/options/allow';
  39. /**
  40. * CONFIG path constant: DEFAULT
  41. */
  42. const XML_PATH_CURRENCY_DEFAULT = 'currency/options/default';
  43. /**
  44. * CONFIG path constant: BASE
  45. */
  46. const XML_PATH_CURRENCY_BASE = 'currency/options/base';
  47. /**
  48. * @var Mage_Directory_Model_Currency_Filter - currency filter
  49. */
  50. protected $_filter;
  51. /**
  52. * Currency Rates
  53. *
  54. * @var array
  55. */
  56. protected $_rates;
  57. /**
  58. * Class constructor
  59. */
  60. protected function _construct()
  61. {
  62. $this->_init('directory/currency');
  63. }
  64. /**
  65. * Get currency code
  66. *
  67. * @return string
  68. */
  69. public function getCode()
  70. {
  71. return $this->_getData('currency_code');
  72. }
  73. /**
  74. * Get currency code
  75. *
  76. * @return string
  77. */
  78. public function getCurrencyCode()
  79. {
  80. return $this->_getData('currency_code');
  81. }
  82. /**
  83. * Currency Rates getter
  84. *
  85. * @return array
  86. */
  87. public function getRates()
  88. {
  89. return $this->_rates;
  90. }
  91. /**
  92. * Currency Rates setter
  93. *
  94. * @param array Currency Rates
  95. * @return Mage_Directory_Model_Currency
  96. */
  97. public function setRates(array $rates)
  98. {
  99. $this->_rates = $rates;
  100. return $this;
  101. }
  102. /**
  103. * Loading currency data
  104. *
  105. * @param string $id
  106. * @param string $field
  107. * @return Mage_Directory_Model_Currency
  108. */
  109. public function load($id, $field = null)
  110. {
  111. $this->unsRate();
  112. $this->setData('currency_code', $id);
  113. return $this;
  114. }
  115. /**
  116. * Get currency rate (only base=>allowed)
  117. *
  118. * @param string|Mage_Directory_Model_Currency $toCurrency
  119. * @return string
  120. * @throws Mage_Core_Exception
  121. */
  122. public function getRate($toCurrency)
  123. {
  124. if (is_string($toCurrency)) {
  125. $code = $toCurrency;
  126. } elseif ($toCurrency instanceof Mage_Directory_Model_Currency) {
  127. $code = $toCurrency->getCurrencyCode();
  128. } else {
  129. throw Mage::exception('Mage_Directory', Mage::helper('directory')->__('Invalid target currency.'));
  130. }
  131. $rates = $this->getRates();
  132. if (!isset($rates[$code])) {
  133. $rates[$code] = $this->_getResource()->getRate($this->getCode(), $toCurrency);
  134. $this->setRates($rates);
  135. }
  136. return $rates[$code];
  137. }
  138. /**
  139. * Get currency rate (base=>allowed or allowed=>base)
  140. *
  141. * @param string|Mage_Directory_Model_Currency $toCurrency
  142. * @return string
  143. * @throws Mage_Core_Exception
  144. */
  145. public function getAnyRate($toCurrency)
  146. {
  147. if (is_string($toCurrency)) {
  148. $code = $toCurrency;
  149. } elseif ($toCurrency instanceof Mage_Directory_Model_Currency) {
  150. $code = $toCurrency->getCurrencyCode();
  151. } else {
  152. throw Mage::exception('Mage_Directory', Mage::helper('directory')->__('Invalid target currency.'));
  153. }
  154. $rates = $this->getRates();
  155. if (!isset($rates[$code])) {
  156. $rates[$code] = $this->_getResource()->getAnyRate($this->getCode(), $toCurrency);
  157. $this->setRates($rates);
  158. }
  159. return $rates[$code];
  160. }
  161. /**
  162. * Convert price to currency format
  163. *
  164. * @param float $price
  165. * @param null|string|Mage_Directory_Model_Currency $toCurrency
  166. * @return float
  167. * @throws Exception
  168. */
  169. public function convert($price, $toCurrency = null)
  170. {
  171. if (is_null($toCurrency)) {
  172. return $price;
  173. } else {
  174. $rate = $this->getRate($toCurrency);
  175. if ($rate) {
  176. return $price * $rate;
  177. }
  178. }
  179. throw new Exception(Mage::helper('directory')->__('Undefined rate from "%s-%s".', $this->getCode(),
  180. $toCurrency->getCode()));
  181. }
  182. /**
  183. * Get currency filter
  184. *
  185. * @return Mage_Directory_Model_Currency_Filter
  186. */
  187. public function getFilter()
  188. {
  189. if (!$this->_filter) {
  190. $this->_filter = new Mage_Directory_Model_Currency_Filter($this->getCode());
  191. }
  192. return $this->_filter;
  193. }
  194. /**
  195. * Format price to currency format
  196. *
  197. * @param float $price
  198. * @param array $options
  199. * @param bool $includeContainer
  200. * @param bool $addBrackets
  201. * @return string
  202. */
  203. public function format($price, $options = array(), $includeContainer = true, $addBrackets = false)
  204. {
  205. return $this->formatPrecision($price, 2, $options, $includeContainer, $addBrackets);
  206. }
  207. /**
  208. * Apply currency format to number with specific rounding precision
  209. *
  210. * @param float $price
  211. * @param int $precision
  212. * @param array $options
  213. * @param bool $includeContainer
  214. * @param bool $addBrackets
  215. * @return string
  216. */
  217. public function formatPrecision($price, $precision, $options = array(), $includeContainer = true,
  218. $addBrackets = false)
  219. {
  220. if (!isset($options['precision'])) {
  221. $options['precision'] = $precision;
  222. }
  223. if ($includeContainer) {
  224. return '<span class="price">' . ($addBrackets ? '[' : '') . $this->formatTxt($price, $options) .
  225. ($addBrackets ? ']' : '') . '</span>';
  226. }
  227. return $this->formatTxt($price, $options);
  228. }
  229. /**
  230. * Returns the formatted price
  231. *
  232. * @param float $price
  233. * @param null|array $options
  234. * @return string
  235. */
  236. public function formatTxt($price, $options = array())
  237. {
  238. if (!is_numeric($price)) {
  239. $price = Mage::app()->getLocale()->getNumber($price);
  240. }
  241. /**
  242. * Fix problem with 12 000 000, 1 200 000
  243. *
  244. * %f - the argument is treated as a float, and presented as a floating-point number (locale aware).
  245. * %F - the argument is treated as a float, and presented as a floating-point number (non-locale aware).
  246. */
  247. $price = sprintf("%F", $price);
  248. if ($price == -0) {
  249. $price = 0;
  250. }
  251. return Mage::app()->getLocale()->currency($this->getCode())->toCurrency($price, $options);
  252. }
  253. /**
  254. * Returns the formatting template for numbers
  255. *
  256. * @return string
  257. */
  258. public function getOutputFormat()
  259. {
  260. $formated = $this->formatTxt(0);
  261. $number = $this->formatTxt(0, array('display' => Zend_Currency::NO_SYMBOL));
  262. return str_replace($number, '%s', $formated);
  263. }
  264. /**
  265. * Retrieve allowed currencies according to config
  266. *
  267. * @return array
  268. */
  269. public function getConfigAllowCurrencies()
  270. {
  271. $allowedCurrencies = $this->_getResource()->getConfigCurrencies($this, self::XML_PATH_CURRENCY_ALLOW);
  272. $appBaseCurrencyCode = Mage::app()->getBaseCurrencyCode();
  273. if (!in_array($appBaseCurrencyCode, $allowedCurrencies)) {
  274. $allowedCurrencies[] = $appBaseCurrencyCode;
  275. }
  276. foreach (Mage::app()->getStores() as $store) {
  277. $code = $store->getBaseCurrencyCode();
  278. if (!in_array($code, $allowedCurrencies)) {
  279. $allowedCurrencies[] = $code;
  280. }
  281. }
  282. return $allowedCurrencies;
  283. }
  284. /**
  285. * Retrieve default currencies according to config
  286. *
  287. * @return array
  288. */
  289. public function getConfigDefaultCurrencies()
  290. {
  291. $defaultCurrencies = $this->_getResource()->getConfigCurrencies($this, self::XML_PATH_CURRENCY_DEFAULT);
  292. return $defaultCurrencies;
  293. }
  294. /**
  295. * Retrieve base currencies according to config
  296. *
  297. * @return array
  298. */
  299. public function getConfigBaseCurrencies()
  300. {
  301. $defaultCurrencies = $this->_getResource()->getConfigCurrencies($this, self::XML_PATH_CURRENCY_BASE);
  302. return $defaultCurrencies;
  303. }
  304. /**
  305. * Retrieve currency rates to other currencies
  306. *
  307. * @param string $currency
  308. * @param array $toCurrencies
  309. * @return array
  310. */
  311. public function getCurrencyRates($currency, $toCurrencies = null)
  312. {
  313. if ($currency instanceof Mage_Directory_Model_Currency) {
  314. $currency = $currency->getCode();
  315. }
  316. $data = $this->_getResource()->getCurrencyRates($currency, $toCurrencies);
  317. return $data;
  318. }
  319. /**
  320. * Save currency rates
  321. *
  322. * @param array $rates
  323. * @return object
  324. */
  325. public function saveRates($rates)
  326. {
  327. $this->_getResource()->saveRates($rates);
  328. return $this;
  329. }
  330. }