typo
[Pman.Core] / DataObjects / Core_curr_rate.php
1 <?php
2 /**
3  * Table Definition for core_curr_rate
4  */
5 class_exists('DB_DataObject') ? '' : 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         static $checked = false;
43         if ($checked ) {
44             return true;
45         }
46         $checked  = true;
47         
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_dt > NOW()');
52         if ($x->count()) {
53             // got some data for beyond today..
54             
55             return;
56         }
57         
58         // load our default rates to start with..
59         $dom = simplexml_load_file(dirname(__FILE__).'/../eurofxref-daily.xml');
60         $rates['EUR'] = 1.0;
61         $rates['TWD'] = 46.7008412; // taiwan dorlar
62         $rates['VND'] = 25282.24; // veitnam dong?
63        
64        
65         foreach($dom->Cube->Cube->Cube as $c) {
66            //echo '<PRE>';print_r($c );
67             $rates[(string)$c['currency']] = (string)$c['rate'];
68         }
69         $rates['RMB'] = $rates['CNY'] ;
70         
71         
72         
73         // now try loading from latest..
74        
75              // this may fail...
76         $f = @file_get_contents('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml');
77         if (!strlen($f)) {
78             return false;
79         }
80         
81         $dom = simplexml_load_string($f);
82         $rates['EUR'] = 1.0;
83         
84         foreach($dom->Cube->Cube->Cube as $c) {
85            //echo '<PRE>';print_r($c );
86             $rates[(string)$c['currency']] = (string)$c['rate'];
87         }
88         $rates['RMB'] = $rates['CNY'] ;
89         
90         foreach($rates as $cur=>$in_euro) {
91             
92
93             $rate = (1.0 / $rates['USD']) * $in_euro;
94              
95             
96             
97             $ov = DB_DataObject::Factory('core_curr_rate');
98             $ov->curr = $cur;
99             $nl = clone($ov);
100             $ov->orderBy('to_dt DESC');
101             $ov->limit(1);
102             
103             
104             $nl->from_dt = DB_DataObject::sqlValue("NOW()");
105             $nl->to_dt = DB_DataObject::sqlValue("NOW() + INTERVAL 7 DAY");
106             if ($ov->find(true)) {
107                 if (strtotime($ov->to_dt) > time()) {
108                     continue;
109                 }
110                 $nl->from_dt = $ov->to_dt;
111                 
112             
113                 if ($ov->rate == $rate) {
114                     // modify the old one to expire
115                     $oo = clone($ov);
116                     $ov->to_dt = $nl->from_dt;
117                     $ov->update($oo);
118                     continue;
119                 }
120             } else {
121                 // no previous record...
122                 $nl->from_dt = '1970-01-01 00:00:00';
123             }
124             $nl->rate = $rate;
125             // create a new row.
126             $nl->insert();
127             
128             
129             
130         }
131         
132         
133     }
134     function rate($cur, $when)
135     {
136         $when === false ? date('Y-m-d H:i:s') : $when;
137         $this->loadRates(); // check if we have an rates.
138         
139         $r = DB_DataObject::factory('core_curr_rate');
140         $r->curr = $cur;
141         $r->whereAdd("from_dt < '" . date('Y-m-d H:i:s', strtotime($when)) . "'");
142         
143         $r->orderBy('to_dt ASC');
144         $r->limit(1);
145         if ($r->find(true)) {
146             return $r->rate;
147         }
148         return false;
149     }
150     
151     function convert($val, $from, $to, $when = false)
152     {
153         
154         
155         $fr = $this->rate($from, $when);
156         $tr = $this->rate($to, $when);
157         
158         // crappy error handling..
159         if ($fr === false) {
160             return false;
161         }
162         if ($tr === false) {
163             return false;
164         }
165         
166         return ((1.0 / $fr) * $val) * $tr;
167   
168         
169     
170     }
171     
172     function currentRates()
173     {
174         $this->loadRates();
175        // DB_DataObject::debugLevel(1);
176         $c = DB_DAtaObject::factory('core_curr_rate');
177         $c->whereAdd('from_dt < NOW() AND to_dt > NOW()');
178         $c->find();
179         $ret = array();
180         while($c->fetch()) {
181             $ret[$c->curr] = $c->rate;
182         }
183         return $ret;
184         
185         
186         
187     }
188     
189     
190 }