DataObjects/Core_curr_rate.php
[Pman.Core] / DataObjects / Core_curr_rate.php
1 <?php
2 /**
3  * Table Definition for core_curr_rate
4  */
5 require_once 'DB/DataObject.php';
6
7 class Pman_Core_DataObjects_Core_curr_rate extends DB_DataObject 
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11   
12     public $__table = 'core_curr_rate';    // table name
13     public $id;
14     public $curr;
15     public $rate;  // always to USD...
16     public $from_dt;
17     public $to_dt;
18
19     /* the code above is auto generated do not remove the tag below */
20     ###END_AUTOCODE
21     
22     function applyFilters($q, $au, $roo)
23     {
24         
25     }
26     
27     
28     
29     
30     /**
31      * current rate fetching
32      *
33      * - we used to do historical rates... (but sources keep disappearing for that..)
34      *
35      * this just get's the current rates from the ecb..
36      * 
37      * 
38      */ 
39     function loadRates()
40     {
41         
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()');
46         if ($x->count()) {
47             // got some data for beyond today..
48             return;
49         }
50         
51         // load our default rates to start with..
52         $dom = simplexml_load_file(dirname(__FILE__).'/../eurofxref-daily.xml');
53         $rates['EUR'] = 1.0;
54         $rates['TWD'] = 46.7008412; // taiwan dorlar
55         $rates['VND'] = 25282.24; // veitnam dong?
56        
57        
58         foreach($dom->Cube->Cube->Cube as $c) {
59            //echo '<PRE>';print_r($c );
60             $rates[(string)$c['currency']] = (string)$c['rate'];
61         }
62         $rates['RMB'] = $rates['CNY'] ;
63         
64         
65         
66         // now try loading from latest..
67        
68              // this may fail...
69         $f = @file_get_contents('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml');
70         if (!strlen($f)) {
71             return false;
72         }
73         
74         $dom = simplexml_load_file($target);
75         $rates['EUR'] = 1.0;
76         
77         foreach($dom->Cube->Cube->Cube as $c) {
78            //echo '<PRE>';print_r($c );
79             $rates[(string)$c['currency']] = (string)$c['rate'];
80         }
81         $rates['RMB'] = $rates['CNY'] ;
82         
83         foreach($rates as $cur=>$euro) {
84             
85
86             $rate = $this->rates['USD'] * $euro;
87             
88             
89             
90             
91             
92             
93             
94             
95             $ov = DB_DataObject::Factory('core_curr_rate');
96             $ov->curr = $cur;
97             $nl = clone($x);
98             $ov->orderBy('to_date DESC');
99             $ov->limit(1);
100             
101             
102             $nl->from_dt = DB_DataObject::sqlValue("NOW()");
103             $nl->to_dt = DB_DataObject::sqlValue("NOW() + INTERVAL 7 DAY");
104             if ($ov->find(true)) {
105                 if (strtotime($ov->to_date) > time()) {
106                     continue;
107                 }
108                 $nl->from_dt = $ov->to_date;
109                 
110             
111                 if ($ov->rate == $rate) {
112                     // modify the old one to expire
113                     $oo = clone($ov);
114                     $ov->to_date = $nv->to_from_dt;
115                     $ov->update($oo);
116                     continue;
117                 }
118             } else {
119                 // no previous record...
120                 $nl->from_dt = '1970-01-01 00:00:00';
121             }
122             $nl->rate = $rate;
123             // create a new row.
124             $nl->insert();
125             
126             
127             
128         }
129         
130         
131     }
132     function lastRate($when)
133     {
134         
135         
136     }
137     
138     function convert($val, $from, $to, $when = false)
139     {
140         
141         
142         
143         if ($r === false) {
144             return false;
145         }
146         if (!isset($this->rates[$from]) || !isset($this->rates[$to]) ) {
147             return false;
148         }
149         //echo '<PRE>';print_R($this->rates);
150         $base = (1.0 / $this->rates[$from]) * $val;
151   
152         return $this->rates[$to] * $base;
153     
154     }
155     
156     
157     
158 }