1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_Directory_Model_Currency extends Mage_Core_Model_Abstract
35: {
36: 37: 38:
39: const XML_PATH_CURRENCY_ALLOW = 'currency/options/allow';
40: const XML_PATH_CURRENCY_DEFAULT = 'currency/options/default';
41: const XML_PATH_CURRENCY_BASE = 'currency/options/base';
42:
43: protected $_filter;
44:
45: 46: 47: 48: 49:
50: protected $_rates;
51:
52:
53: protected function _construct()
54: {
55: $this->_init('directory/currency');
56: }
57:
58: 59: 60: 61: 62:
63: public function getCode()
64: {
65: return $this->_getData('currency_code');
66: }
67:
68: public function getCurrencyCode()
69: {
70: return $this->_getData('currency_code');
71: }
72:
73: 74: 75: 76: 77:
78: public function getRates()
79: {
80: return $this->_rates;
81: }
82:
83: 84: 85: 86: 87: 88:
89: public function setRates(array $rates)
90: {
91: $this->_rates = $rates;
92: return $this;
93: }
94:
95: 96: 97: 98: 99: 100: 101:
102: public function load($id, $field=null)
103: {
104: $this->unsRate();
105: $this->setData('currency_code', $id);
106: return $this;
107: }
108:
109: 110: 111: 112: 113: 114:
115: public function getRate($toCurrency)
116: {
117: if (is_string($toCurrency)) {
118: $code = $toCurrency;
119: } elseif ($toCurrency instanceof Mage_Directory_Model_Currency) {
120: $code = $toCurrency->getCurrencyCode();
121: } else {
122: throw Mage::exception('Mage_Directory', Mage::helper('directory')->__('Invalid target currency.'));
123: }
124: $rates = $this->getRates();
125: if (!isset($rates[$code])) {
126: $rates[$code] = $this->_getResource()->getRate($this->getCode(), $toCurrency);
127: $this->setRates($rates);
128: }
129: return $rates[$code];
130: }
131:
132: 133: 134: 135: 136: 137:
138: public function getAnyRate($toCurrency)
139: {
140: if (is_string($toCurrency)) {
141: $code = $toCurrency;
142: } elseif ($toCurrency instanceof Mage_Directory_Model_Currency) {
143: $code = $toCurrency->getCurrencyCode();
144: } else {
145: throw Mage::exception('Mage_Directory', Mage::helper('directory')->__('Invalid target currency.'));
146: }
147: $rates = $this->getRates();
148: if (!isset($rates[$code])) {
149: $rates[$code] = $this->_getResource()->getAnyRate($this->getCode(), $toCurrency);
150: $this->setRates($rates);
151: }
152: return $rates[$code];
153: }
154:
155: 156: 157: 158: 159: 160: 161:
162: public function convert($price, $toCurrency=null)
163: {
164: if (is_null($toCurrency)) {
165: return $price;
166: }
167: elseif ($rate = $this->getRate($toCurrency)) {
168: return $price*$rate;
169: }
170:
171: throw new Exception(Mage::helper('directory')->__('Undefined rate from "%s-%s".', $this->getCode(), $toCurrency->getCode()));
172: }
173:
174: 175: 176: 177: 178:
179: public function getFilter()
180: {
181: if (!$this->_filter) {
182: $this->_filter = new Mage_Directory_Model_Currency_Filter($this->getCode());
183: }
184:
185: return $this->_filter;
186: }
187:
188: 189: 190: 191: 192: 193: 194:
195: public function format($price, $options=array(), $includeContainer = true, $addBrackets = false)
196: {
197: return $this->formatPrecision($price, 2, $options, $includeContainer, $addBrackets);
198: }
199:
200: 201: 202: 203: 204: 205: 206: 207: 208: 209:
210: public function formatPrecision($price, $precision, $options=array(), $includeContainer = true, $addBrackets = false)
211: {
212: if (!isset($options['precision'])) {
213: $options['precision'] = $precision;
214: }
215: if ($includeContainer) {
216: return '<span class="price">' . ($addBrackets ? '[' : '') . $this->formatTxt($price, $options) . ($addBrackets ? ']' : '') . '</span>';
217: }
218: return $this->formatTxt($price, $options);
219: }
220:
221: public function formatTxt($price, $options=array())
222: {
223: if (!is_numeric($price)) {
224: $price = Mage::app()->getLocale()->getNumber($price);
225: }
226: 227: 228: 229: 230: 231:
232: $price = sprintf("%F", $price);
233: return Mage::app()->getLocale()->currency($this->getCode())->toCurrency($price, $options);
234: }
235:
236: public function getOutputFormat()
237: {
238: $formated = $this->formatTxt(0);
239: $number = $this->formatTxt(0, array('display'=>Zend_Currency::NO_SYMBOL));
240: return str_replace($number, '%s', $formated);
241: }
242:
243: 244: 245: 246:
247: public function getConfigAllowCurrencies()
248: {
249: $allowedCurrencies = $this->_getResource()->getConfigCurrencies($this, self::XML_PATH_CURRENCY_ALLOW);
250: $appBaseCurrencyCode = Mage::app()->getBaseCurrencyCode();
251: if (!in_array($appBaseCurrencyCode, $allowedCurrencies)) {
252: $allowedCurrencies[] = $appBaseCurrencyCode;
253: }
254: foreach (Mage::app()->getStores() as $store) {
255: $code = $store->getBaseCurrencyCode();
256: if (!in_array($code, $allowedCurrencies)) {
257: $allowedCurrencies[] = $code;
258: }
259: }
260:
261: return $allowedCurrencies;
262: }
263:
264: 265: 266: 267:
268: public function getConfigDefaultCurrencies()
269: {
270: $defaultCurrencies = $this->_getResource()->getConfigCurrencies($this, self::XML_PATH_CURRENCY_DEFAULT);
271: return $defaultCurrencies;
272: }
273:
274:
275: public function getConfigBaseCurrencies()
276: {
277: $defaultCurrencies = $this->_getResource()->getConfigCurrencies($this, self::XML_PATH_CURRENCY_BASE);
278: return $defaultCurrencies;
279: }
280:
281: 282: 283: 284: 285: 286: 287:
288: public function getCurrencyRates($currency, $toCurrencies=null)
289: {
290: if ($currency instanceof Mage_Directory_Model_Currency) {
291: $currency = $currency->getCode();
292: }
293: $data = $this->_getResource()->getCurrencyRates($currency, $toCurrencies);
294: return $data;
295: }
296:
297: 298: 299: 300: 301: 302:
303: public function saveRates($rates)
304: {
305: $this->_getResource()->saveRates($rates);
306: return $this;
307: }
308: }
309: