00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_Directory_Model_Currency extends Mage_Core_Model_Abstract
00035 {
00036
00037
00038
00039 const XML_PATH_CURRENCY_ALLOW = 'currency/options/allow';
00040 const XML_PATH_CURRENCY_DEFAULT = 'currency/options/default';
00041 const XML_PATH_CURRENCY_BASE = 'currency/options/base';
00042
00043 protected $_filter;
00044
00045
00046
00047
00048
00049
00050 protected $_rates;
00051
00052
00053 protected function _construct()
00054 {
00055 $this->_init('directory/currency');
00056 }
00057
00058
00059
00060
00061
00062
00063 public function getCode()
00064 {
00065 return $this->_getData('currency_code');
00066 }
00067
00068 public function getCurrencyCode()
00069 {
00070 return $this->_getData('currency_code');
00071 }
00072
00073
00074
00075
00076
00077
00078 public function getRates()
00079 {
00080 return $this->_rates;
00081 }
00082
00083
00084
00085
00086
00087
00088
00089 public function setRates(array $rates)
00090 {
00091 $this->_rates = $rates;
00092 return $this;
00093 }
00094
00095
00096
00097
00098
00099
00100
00101
00102 public function load($id, $field=null)
00103 {
00104 $this->unsRate();
00105 $this->setData('currency_code', $id);
00106 return $this;
00107 }
00108
00109
00110
00111
00112
00113
00114
00115 public function getRate($toCurrency)
00116 {
00117 if (is_string($toCurrency)) {
00118 $code = $toCurrency;
00119 } elseif ($toCurrency instanceof Mage_Directory_Model_Currency) {
00120 $code = $toCurrency->getCurrencyCode();
00121 } else {
00122 throw Mage::exception('Mage_Directory', Mage::helper('directory')->__('Invalid target currency'));
00123 }
00124 $rates = $this->getRates();
00125 if (!isset($rates[$code])) {
00126 $rates[$code] = $this->_getResource()->getRate($this->getCode(), $toCurrency);
00127 $this->setRates($rates);
00128 }
00129 return $rates[$code];
00130 }
00131
00132
00133
00134
00135
00136
00137
00138
00139 public function convert($price, $toCurrency=null)
00140 {
00141 if (is_null($toCurrency)) {
00142 return $price;
00143 }
00144 elseif ($rate = $this->getRate($toCurrency)) {
00145 return $price*$rate;
00146 }
00147
00148 throw new Exception(Mage::helper('directory')->__('Undefined rate from "%s-%s"', $this->getCode(), $toCurrency->getCode()));
00149 }
00150
00151
00152
00153
00154
00155
00156 public function getFilter()
00157 {
00158 if (!$this->_filter) {
00159 $this->_filter = new Mage_Directory_Model_Currency_Filter($this->getCode());
00160 }
00161
00162 return $this->_filter;
00163 }
00164
00165
00166
00167
00168
00169
00170
00171
00172 public function format($price, $options=array(), $includeContainer = true, $addBrackets = false)
00173 {
00174 if ($includeContainer) {
00175 return '<span class="price">' . ($addBrackets ? '[' : '') . $this->formatTxt($price, $options) . ($addBrackets ? ']' : '') . '</span>';
00176 }
00177 return $this->formatTxt($price, $options);
00178 }
00179
00180 public function formatTxt($price, $options=array())
00181 {
00182 if (!is_numeric($price)) {
00183 $price = Mage::app()->getLocale()->getNumber($price);
00184 }
00185
00186
00187
00188 $price = sprintf("%f", $price);
00189 return Mage::app()->getLocale()->currency($this->getCode())->toCurrency($price, $options);
00190 }
00191
00192 public function getOutputFormat()
00193 {
00194 $formated = $this->formatTxt(0);
00195 $number = $this->formatTxt(0, array('display'=>Zend_Currency::NO_SYMBOL));
00196 return str_replace($number, '%s', $formated);
00197 }
00198
00199
00200
00201
00202
00203 public function getConfigAllowCurrencies()
00204 {
00205 $allowedCurrencies = $this->_getResource()->getConfigCurrencies($this, self::XML_PATH_CURRENCY_ALLOW);
00206 $appBaseCurrencyCode = Mage::app()->getBaseCurrencyCode();
00207 if (!in_array($appBaseCurrencyCode, $allowedCurrencies)) {
00208 $allowedCurrencies[] = $appBaseCurrencyCode;
00209 }
00210 foreach (Mage::app()->getStores() as $store) {
00211 $code = $store->getBaseCurrencyCode();
00212 if (!in_array($code, $allowedCurrencies)) {
00213 $allowedCurrencies[] = $code;
00214 }
00215 }
00216
00217 return $allowedCurrencies;
00218 }
00219
00220
00221
00222
00223
00224 public function getConfigDefaultCurrencies()
00225 {
00226 $defaultCurrencies = $this->_getResource()->getConfigCurrencies($this, self::XML_PATH_CURRENCY_DEFAULT);
00227 return $defaultCurrencies;
00228 }
00229
00230
00231 public function getConfigBaseCurrencies()
00232 {
00233 $defaultCurrencies = $this->_getResource()->getConfigCurrencies($this, self::XML_PATH_CURRENCY_BASE);
00234 return $defaultCurrencies;
00235 }
00236
00237
00238
00239
00240
00241
00242
00243
00244 public function getCurrencyRates($currency, $toCurrencies=null)
00245 {
00246 if ($currency instanceof Mage_Directory_Model_Currency) {
00247 $currency = $currency->getCode();
00248 }
00249 $data = $this->_getResource()->getCurrencyRates($currency, $toCurrencies);
00250 return $data;
00251 }
00252
00253
00254
00255
00256
00257
00258
00259 public function saveRates($rates)
00260 {
00261 $this->_getResource()->saveRates($rates);
00262 return $this;
00263 }
00264 }