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:
35: class Mage_Directory_Model_Resource_Currency extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39: 40: 41:
42: protected $_currencyRateTable;
43:
44: 45: 46: 47: 48:
49: protected static $_rateCache;
50:
51: 52: 53: 54:
55: protected function _construct()
56: {
57: $this->_init('directory/currency', 'currency_code');
58: $this->_currencyRateTable = $this->getTable('directory/currency_rate');
59: }
60:
61: 62: 63: 64: 65: 66: 67:
68: public function getRate($currencyFrom, $currencyTo)
69: {
70: if ($currencyFrom instanceof Mage_Directory_Model_Currency) {
71: $currencyFrom = $currencyFrom->getCode();
72: }
73:
74: if ($currencyTo instanceof Mage_Directory_Model_Currency) {
75: $currencyTo = $currencyTo->getCode();
76: }
77:
78: if ($currencyFrom == $currencyTo) {
79: return 1;
80: }
81:
82: if (!isset(self::$_rateCache[$currencyFrom][$currencyTo])) {
83: $read = $this->_getReadAdapter();
84: $bind = array(
85: ':currency_from' => strtoupper($currencyFrom),
86: ':currency_to' => strtoupper($currencyTo)
87: );
88: $select = $read->select()
89: ->from($this->_currencyRateTable, 'rate')
90: ->where('currency_from = :currency_from')
91: ->where('currency_to = :currency_to');
92:
93: self::$_rateCache[$currencyFrom][$currencyTo] = $read->fetchOne($select, $bind);
94: }
95:
96: return self::$_rateCache[$currencyFrom][$currencyTo];
97: }
98:
99: 100: 101: 102: 103: 104: 105:
106: public function getAnyRate($currencyFrom, $currencyTo)
107: {
108: if ($currencyFrom instanceof Mage_Directory_Model_Currency) {
109: $currencyFrom = $currencyFrom->getCode();
110: }
111:
112: if ($currencyTo instanceof Mage_Directory_Model_Currency) {
113: $currencyTo = $currencyTo->getCode();
114: }
115:
116: if ($currencyFrom == $currencyTo) {
117: return 1;
118: }
119:
120: if (!isset(self::$_rateCache[$currencyFrom][$currencyTo])) {
121: $adapter = $this->_getReadAdapter();
122: $bind = array(
123: ':currency_from' => strtoupper($currencyFrom),
124: ':currency_to' => strtoupper($currencyTo)
125: );
126: $select = $adapter->select()
127: ->from($this->_currencyRateTable, 'rate')
128: ->where('currency_from = :currency_from')
129: ->where('currency_to = :currency_to');
130:
131: $rate = $adapter->fetchOne($select, $bind);
132: if ($rate === false) {
133: $select = $adapter->select()
134: ->from($this->_currencyRateTable, new Zend_Db_Expr('1/rate'))
135: ->where('currency_to = :currency_from')
136: ->where('currency_from = :currency_to');
137: $rate = $adapter->fetchOne($select, $bind);
138: }
139: self::$_rateCache[$currencyFrom][$currencyTo] = $rate;
140: }
141:
142: return self::$_rateCache[$currencyFrom][$currencyTo];
143: }
144:
145: 146: 147: 148: 149:
150: public function saveRates($rates)
151: {
152: if (is_array($rates) && sizeof($rates) > 0) {
153: $adapter = $this->_getWriteAdapter();
154: $data = array();
155: foreach ($rates as $currencyCode => $rate) {
156: foreach ($rate as $currencyTo => $value) {
157: $value = abs($value);
158: if ($value == 0) {
159: continue;
160: }
161: $data[] = array(
162: 'currency_from' => $currencyCode,
163: 'currency_to' => $currencyTo,
164: 'rate' => $value,
165: );
166: }
167: }
168: if ($data) {
169: $adapter->insertOnDuplicate($this->_currencyRateTable, $data, array('rate'));
170: }
171: } else {
172: Mage::throwException(Mage::helper('directory')->__('Invalid rates received'));
173: }
174: }
175:
176: 177: 178: 179: 180: 181: 182: 183:
184: public function getConfigCurrencies($model, $path)
185: {
186: $adapter = $this->_getReadAdapter();
187: $bind = array(':config_path' => $path);
188: $select = $adapter->select()
189: ->from($this->getTable('core/config_data'))
190: ->where('path = :config_path');
191: $result = array();
192: $rowSet = $adapter->fetchAll($select, $bind);
193: foreach ($rowSet as $row) {
194: $result = array_merge($result, explode(',', $row['value']));
195: }
196: sort($result);
197:
198: return array_unique($result);
199: }
200:
201: 202: 203: 204: 205: 206: 207: 208:
209: public function getCurrencyRates($currency, $toCurrencies = null)
210: {
211: $rates = array();
212: if (is_array($currency)) {
213: foreach ($currency as $code) {
214: $rates[$code] = $this->_getRatesByCode($code, $toCurrencies);
215: }
216: } else {
217: $rates = $this->_getRatesByCode($currency, $toCurrencies);
218: }
219:
220: return $rates;
221: }
222:
223: 224: 225: 226: 227: 228: 229:
230: protected function _getRatesByCode($code, $toCurrencies = null)
231: {
232: $adapter = $this->_getReadAdapter();
233: $bind = array(
234: ':currency_from' => $code
235: );
236: $select = $adapter->select()
237: ->from($this->getTable('directory/currency_rate'), array('currency_to', 'rate'))
238: ->where('currency_from = :currency_from')
239: ->where('currency_to IN(?)', $toCurrencies);
240: $rowSet = $adapter->fetchAll($select, $bind);
241: $result = array();
242:
243: foreach ($rowSet as $row) {
244: $result[$row['currency_to']] = $row['rate'];
245: }
246:
247: return $result;
248: }
249: }
250: