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
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 // how often do we need to know this..?
43 // let's assume we do it once a week..
44 $x = DB_DataObject::Factory('core_curr_rate');
45 $x->whereAdd('to_date > NOW()');
47 // got some data for beyond today..
51 // load our default rates to start with..
52 $dom = simplexml_load_file(dirname(__FILE__).'/../eurofxref-daily.xml');
54 $rates['TWD'] = 46.7008412; // taiwan dorlar
55 $rates['VND'] = 25282.24; // veitnam dong?
58 foreach($dom->Cube->Cube->Cube as $c) {
59 //echo '<PRE>';print_r($c );
60 $rates[(string)$c['currency']] = (string)$c['rate'];
62 $rates['RMB'] = $rates['CNY'] ;
66 // now try loading from latest..
69 $f = @file_get_contents('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml');
74 $dom = simplexml_load_file($target);
77 foreach($dom->Cube->Cube->Cube as $c) {
78 //echo '<PRE>';print_r($c );
79 $rates[(string)$c['currency']] = (string)$c['rate'];
81 $rates['RMB'] = $rates['CNY'] ;
83 foreach($rates as $cur=>$rate) {
85 $ov = DB_DataObject::Factory('core_curr_rate');
88 $ov->orderBy('to_date DESC');
92 $nl->from_dt = DB_DataObject::sqlValue("NOW()");
93 $nl->to_dt = DB_DataObject::sqlValue("NOW() + INTERVAL 7 DAY");
94 if ($ov->find(true)) {
95 if (strtotime($ov->to_date) > time()) {
98 $nl->from_dt = $ov->to_date;
101 if ($ov->rate == $rate) {
102 // modify the old one to expire
104 $ov->to_date = $nv->to_from_dt;
109 // no previous record...
110 $nl->from_dt = '1970-01-01 00:00:00';