3 * Table Definition for core_curr_rate
5 require_once 'DB/DataObject.php';
7 class Pman_Core_DataObjects_Core_curr_rate extends DB_DataObject
10 /* the code below is auto generated do not remove the above tag */
12 public $__table = 'core_curr_rate'; // table name
15 public $rate; // always to USD...
19 /* the code above is auto generated do not remove the tag below */
22 function applyFilters($q, $au, $roo)
31 * current rate fetching
33 * - we used to do historical rates... (but sources keep disappearing for that..)
35 * this just get's the current rates from the ecb..
42 static $checked = false;
48 // how often do we need to know this..?
49 // let's assume we do it once a week..
50 $x = DB_DataObject::Factory('core_curr_rate');
51 $x->whereAdd('to_date > NOW()');
53 // got some data for beyond today..
58 // load our default rates to start with..
59 $dom = simplexml_load_file(dirname(__FILE__).'/../eurofxref-daily.xml');
61 $rates['TWD'] = 46.7008412; // taiwan dorlar
62 $rates['VND'] = 25282.24; // veitnam dong?
65 foreach($dom->Cube->Cube->Cube as $c) {
66 //echo '<PRE>';print_r($c );
67 $rates[(string)$c['currency']] = (string)$c['rate'];
69 $rates['RMB'] = $rates['CNY'] ;
73 // now try loading from latest..
76 $f = @file_get_contents('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml');
81 $dom = simplexml_load_file($target);
84 foreach($dom->Cube->Cube->Cube as $c) {
85 //echo '<PRE>';print_r($c );
86 $rates[(string)$c['currency']] = (string)$c['rate'];
88 $rates['RMB'] = $rates['CNY'] ;
90 foreach($rates as $cur=>$euro) {
93 $rate = $this->rates['USD'] * $euro;
102 $ov = DB_DataObject::Factory('core_curr_rate');
105 $ov->orderBy('to_date DESC');
109 $nl->from_dt = DB_DataObject::sqlValue("NOW()");
110 $nl->to_dt = DB_DataObject::sqlValue("NOW() + INTERVAL 7 DAY");
111 if ($ov->find(true)) {
112 if (strtotime($ov->to_date) > time()) {
115 $nl->from_dt = $ov->to_date;
118 if ($ov->rate == $rate) {
119 // modify the old one to expire
121 $ov->to_date = $nv->to_from_dt;
126 // no previous record...
127 $nl->from_dt = '1970-01-01 00:00:00';
139 function rate($cur, $when)
141 $when === false ? date('Y-m-d H:i:s') : $when;
142 $this->loadRates(); // check if we have an rates.
144 $r = DB_DataObject::factory('core_curr_rate');
146 $r->whereAdd("from_dt < '" . date('Y-m-d H:i:s', strtotime($when)) . "'");
148 $r->orderBy('to_dt ASC');
150 if ($r->find(true)) {
156 function convert($val, $from, $to, $when = false)
160 $fr = $this->rate($from, $when);
161 $tr = $this->rate($to, $when);
169 //echo '<PRE>';print_R($this->rates);
170 $base = (1.0 / $fr) * $val;