PageRenderTime 22ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/Ecart/Locale/Model/Currency.php

https://code.google.com/p/ecartcommerce/
PHP | 354 lines | 199 code | 37 blank | 118 comment | 28 complexity | cd961bb2af3ce78a5128494c88ef1cd5 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Ecart
  4. *
  5. * This file is part of Ecart.
  6. *
  7. * Ecart is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Ecart is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Ecart. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * @category Ecart
  21. * @package Ecart_Locale
  22. * @copyright Copyright 2008-2009 E-Cart LLC
  23. * @license GNU Public License V3.0
  24. */
  25. /**
  26. *
  27. * @category Ecart
  28. * @package Ecart_Locale
  29. * @subpackage Model
  30. * @author Ecart Core Team <core@ecartcommerce.com>
  31. */
  32. class Ecart_Locale_Model_Currency extends Ecart_Db_Table
  33. {
  34. protected $_name = 'locale_currency';
  35. /**
  36. * Currencies data (code, rate, etc.)
  37. *
  38. * @var array
  39. */
  40. private $_data = array();
  41. /**
  42. * @var string - 'USD'
  43. */
  44. private $_currentCurrencyCode = null;
  45. /**
  46. * Array of Ecart_Currency objects
  47. *
  48. * @var array
  49. */
  50. private $_currency = array();
  51. /**
  52. *
  53. * @return array
  54. */
  55. public function getFormat()
  56. {
  57. $row = $this->getData();
  58. $currency = $this->getCurrency();
  59. $position = $row['position'];
  60. if ($position == 8) { // Standard
  61. $position = $currency->toCurrency(1);
  62. $position = strpos($position, $currency->getSymbol());
  63. if ($position) {
  64. $position = 'Right';
  65. } else {
  66. $position = 'Left';
  67. }
  68. } elseif ($position == 16) {
  69. $position = 'Right';
  70. } else {
  71. $position = 'Left';
  72. }
  73. $symbols = Zend_Locale_Data::getList($row['format'], 'symbols');
  74. return array(
  75. 'precision' => $row['currency_precision'],
  76. 'requiredPrecision' => 2,
  77. 'integerRequired' => 1,
  78. 'decimalSymbol' => $symbols['decimal'],
  79. 'groupSymbol' => $symbols['group'],
  80. 'groupLength' => 3,
  81. 'position' => $position,
  82. 'symbol' => null === $currency->getSymbol() ?
  83. $currency->getShortName() : $currency->getSymbol(),
  84. 'shortName' => $currency->getShortName(),
  85. 'name' => $currency->getName(),
  86. 'display' => $row['display']
  87. );
  88. }
  89. /**
  90. *
  91. * @param string $code
  92. * @return bool
  93. */
  94. public function isExists($code)
  95. {
  96. if (!empty($code) && $this->getData($code)) {
  97. return true;
  98. }
  99. return false;
  100. }
  101. /**
  102. * Return Ecart_Currency object
  103. *
  104. * @param string $code
  105. * @return Ecart_Currency
  106. */
  107. public function getCurrency($code = '')
  108. {
  109. if (empty($code)) {
  110. $code = $this->getCode();
  111. }
  112. if (!isset($this->_instanses[$code])) {
  113. $options = $this->_getCurrencyOptions($code);
  114. $currency = new Ecart_Currency(
  115. $options['currency'],
  116. $options['format'] === null ?
  117. Ecart_Locale::getLocale() : $options['format']
  118. );
  119. $currency->setFormat($options);
  120. $this->_currency[$code] = $currency;
  121. }
  122. return $this->_currency[$code];
  123. }
  124. /**
  125. * @static
  126. * @return const array
  127. */
  128. public static function getPositionOptions()
  129. {
  130. return array(
  131. '8' => Ecart::translate('locale')->__('Standard'),
  132. '16' => Ecart::translate('locale')->__('Right'),
  133. '32' => Ecart::translate('locale')->__('Left')
  134. );
  135. }
  136. /**
  137. * @static
  138. * @return const array
  139. */
  140. public static function getDisplayOptions()
  141. {
  142. return array(
  143. '1' => Ecart::translate('locale')->__('No Symbol'),
  144. '2' => Ecart::translate('locale')->__('Use Symbol'),
  145. '3' => Ecart::translate('locale')->__('Use Shortname'),
  146. '4' => Ecart::translate('locale')->__('Use Name')
  147. );
  148. }
  149. /**
  150. *
  151. * @return string
  152. */
  153. public function getCode()
  154. {
  155. if (null !== $this->_currentCurrencyCode) {
  156. return $this->_currentCurrencyCode;
  157. }
  158. if (isset(Ecart::session()->currency)
  159. && $this->isExists(Ecart::session()->currency)) {
  160. $this->_currentCurrencyCode = Ecart::session()->currency;
  161. } elseif (isset(Ecart::config()->locale->main->currency)
  162. && $this->isExists(Ecart::config()->locale->main->currency)) {
  163. $this->_currentCurrencyCode = Ecart::config()->locale->main->currency;
  164. } elseif ($this->isExists(Ecart_Locale::DEFAULT_CURRENCY)) {
  165. $this->_currentCurrencyCode = Ecart_Locale::DEFAULT_CURRENCY;
  166. } else {
  167. $this->_currentCurrencyCode = $this->select('code')
  168. ->order('id')
  169. ->fetchOne();
  170. if (!$this->_currentCurrencyCode) {
  171. throw new Ecart_Exception(
  172. Ecart::translate('locale')->__('No currencies found')
  173. );
  174. }
  175. }
  176. return $this->_currentCurrencyCode;
  177. }
  178. /**
  179. *
  180. * @param string $code
  181. * @return array
  182. */
  183. private function _getCurrencyOptions($code)
  184. {
  185. $row = $this->getData($code);
  186. return array(
  187. 'currency' => $row['code'],
  188. 'position' => (int) $row['position'],
  189. 'display' => (int) $row['display'],
  190. 'format' => $row['format'],
  191. 'precision' => (int) $row['currency_precision']
  192. );
  193. }
  194. //@todo swap arguments
  195. public function getData($code = '', $key = '')
  196. {
  197. if (empty($code)) {
  198. $code = $this->getCode();
  199. }
  200. if (!isset($this->_data[$code])) {
  201. $this->_data[$code] = $this->select()
  202. ->where('code = ?', $code)
  203. ->fetchRow();
  204. }
  205. if (!empty($key)) {
  206. return $this->_data[$code][$key];
  207. }
  208. return $this->_data[$code];
  209. }
  210. /**
  211. *
  212. * @param float $price
  213. * @param bool $useRate [optional]
  214. * @param string $code [optional]
  215. * @param bool $format [optional]
  216. * @return float
  217. */
  218. public function toCurrency($price, $code = '', $format = true)
  219. {
  220. $price *= $this->getData($code, 'rate');
  221. if (!$format) {
  222. return $price;
  223. }
  224. return $this->getCurrency($code)->toCurrency($price);
  225. }
  226. /**
  227. * From some currency to abstract currency
  228. *
  229. * @param float $price
  230. * @param string $code
  231. * @return float
  232. */
  233. public function from($price, $code = '')
  234. {
  235. return $price / $this->getData($code, 'rate');
  236. }
  237. /**
  238. * From abstract currency to code currency
  239. *
  240. * @param float $price
  241. * @param string $code
  242. * @return float
  243. */
  244. public function to($price, $code = '')
  245. {
  246. return $price * $this->getData($code, 'rate');
  247. }
  248. /**
  249. * Convert currency
  250. *
  251. * @param float $price
  252. * @param string $from
  253. * @param string $to
  254. * @return float
  255. */
  256. public function convert($price, $from, $to)
  257. {
  258. return ($price * $this->getData($to, 'rate')) /
  259. $this->getData($from, 'rate');
  260. }
  261. /**
  262. *
  263. * @param array $data
  264. * @return bool
  265. */
  266. public function batchSave($data)
  267. {
  268. $result = true;
  269. foreach ($data as $id => $values) {
  270. $format = empty($values['format']) ?
  271. new Zend_Db_Expr('NULL') : $values['format'];
  272. $rowData = array(
  273. 'id' => $id,
  274. 'code' => $values['code'],
  275. 'title' => $values['title'],
  276. 'position' => $values['position'],
  277. 'display' => $values['display'],
  278. 'format' => $format,
  279. 'currency_precision' => $values['currency_precision'],
  280. 'rate' => $values['rate']
  281. );
  282. $result = $result && (bool) $this->save($rowData);
  283. }
  284. return $result;
  285. }
  286. /**
  287. *
  288. * @param array $rowData
  289. * @return mixed The primary key value(s), as an associative array if the
  290. * key is compound, or a scalar if the key is single-column.
  291. */
  292. public function save(array $rowData)
  293. {
  294. $id = false;
  295. if (isset($rowData['id']) && null !== $rowData['id']) {
  296. $id = $rowData['id'];
  297. unset($rowData['id']);
  298. }
  299. if (!$id || !$row = $this->fetchRow(
  300. $this->getAdapter()->quoteInto('id = ?', $id)
  301. )) {
  302. $row = $this->createRow($rowData);
  303. } else {
  304. $row->setFromArray($rowData);
  305. if ($row->code === Ecart::config()->locale->main->currency &&
  306. $row->rate != 1) {
  307. // throw new Ecart_Exception(
  308. Ecart::message()->addError(Ecart::translate('locale')->__(
  309. 'Base currency rate should be 1.00'
  310. ));
  311. return false;
  312. }
  313. }
  314. $row->rate = str_replace(',', '.', $row->rate);
  315. return $row->save();
  316. }
  317. }