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_Mysql4_Currency extends Mage_Core_Model_Mysql4_Abstract
00035 {
00036
00037
00038
00039
00040
00041 protected $_currencyRateTable;
00042
00043
00044
00045
00046
00047
00048 protected static $_rateCache;
00049
00050 protected function _construct()
00051 {
00052 $this->_init('directory/currency', 'currency_code');
00053 }
00054
00055 public function __construct()
00056 {
00057 $resource = Mage::getSingleton('core/resource');
00058 $this->_currencyRateTable = $resource->getTableName('directory/currency_rate');
00059
00060 parent::__construct();
00061 }
00062
00063
00064
00065
00066
00067
00068
00069
00070 public function getRate($currencyFrom, $currencyTo)
00071 {
00072 if ($currencyFrom instanceof Mage_Directory_Model_Currency) {
00073 $currencyFrom = $currencyFrom->getCode();
00074 }
00075
00076 if ($currencyTo instanceof Mage_Directory_Model_Currency) {
00077 $currencyTo = $currencyTo->getCode();
00078 }
00079
00080 if ($currencyFrom == $currencyTo) {
00081 return 1;
00082 }
00083
00084 if (!isset(self::$_rateCache[$currencyFrom][$currencyTo])) {
00085 $read = $this->_getReadAdapter();
00086 $select = $read->select()
00087 ->from($this->_currencyRateTable, 'rate')
00088 ->where('currency_from=?', strtoupper($currencyFrom))
00089 ->where('currency_to=?', strtoupper($currencyTo));
00090
00091 self::$_rateCache[$currencyFrom][$currencyTo] = $read->fetchOne($select);
00092 }
00093
00094 return self::$_rateCache[$currencyFrom][$currencyTo];
00095 }
00096
00097
00098
00099
00100
00101
00102 public function saveRates($rates)
00103 {
00104 if( is_array($rates) && sizeof($rates) > 0 ) {
00105 $write = $this->_getWriteAdapter();
00106 $table = $write->quoteIdentifier($this->_currencyRateTable);
00107 $colFrom= $write->quoteIdentifier('currency_from');
00108 $colTo = $write->quoteIdentifier('currency_to');
00109 $colRate= $write->quoteIdentifier('rate');
00110
00111 $sql = 'REPLACE INTO ' . $table . ' (' . $colFrom . ', ' . $colTo . ', ' . $colRate . ') VALUES ';
00112 $values = array();
00113 foreach ($rates as $currencyCode => $rate) {
00114 foreach( $rate as $currencyTo => $value ) {
00115 $value = abs($value);
00116 if( $value == 0 ) {
00117 continue;
00118 }
00119 $values[] = $write->quoteInto('(?)', array($currencyCode, $currencyTo, $value));
00120 }
00121 }
00122 $sql.= implode(',', $values);
00123 $write->query($sql);
00124 } else {
00125 Mage::throwException(Mage::helper('directory')->__('Invalid rates received'));
00126 }
00127 }
00128
00129
00130
00131
00132
00133
00134
00135
00136 public function getConfigCurrencies($model, $path)
00137 {
00138 $read = $this->_getReadAdapter();
00139 $select = $read->select()
00140 ->from($this->getTable('core/config_data'))
00141 ->where($read->quoteInto(' path = ? ', $path))
00142
00143 ->order(' value ASC ');
00144
00145 $data = $read->fetchAll($select);
00146 $tmp_array = array();
00147 foreach( $data as $configRecord ) {
00148 $tmp_array = array_merge($tmp_array, explode(',', $configRecord['value']));
00149 }
00150
00151 $data = array_unique($tmp_array);
00152 return $data;
00153 }
00154
00155
00156
00157
00158
00159
00160
00161
00162 public function getCurrencyRates($currency, $toCurrencies=null)
00163 {
00164 $rates = array();
00165 if( is_array($currency) ) {
00166 foreach( $currency as $code ) {
00167 $rates[$code] = $this->_getRatesByCode($code, $toCurrencies);
00168 }
00169 } else {
00170 $rates = $this->_getRatesByCode($currency, $toCurrencies);
00171 }
00172
00173 return $rates;
00174 }
00175
00176
00177
00178
00179
00180
00181
00182
00183 protected function _getRatesByCode($code, $toCurrencies=null)
00184 {
00185 $read = $this->_getReadAdapter();
00186 $select = $read->select()
00187 ->from($this->getTable('directory/currency_rate'), array('currency_to', 'rate'))
00188 ->where($read->quoteInto('currency_from = ?', $code))
00189 ->where($read->quoteInto('currency_to IN(?)', $toCurrencies));
00190
00191 $data = $read->fetchAll($select);
00192
00193 $tmp_array = array();
00194 foreach( $data as $currencyFrom => $rate ) {
00195 $tmp_array[$rate['currency_to']] = $rate['rate'];
00196 }
00197 $data = $tmp_array;
00198
00199 return $data;
00200 }
00201
00202 }