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