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;
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      * current rate fetching
29      *
30      * - we used to do historical rates... (but sources keep disappearing for that..)
31      *
32      * this just get's the current rates from the ecb..
33      * 
34      * 
35      */
36     
37     var $rates = array();
38     function loadRates()
39     {
40         
41         // how often do we need to know this..?
42         // let's assume we do it once a week..
43         $x = DB_DataObject::Factory('core_curr_rate');
44         $x->whereAdd('to_date > NOW()');
45         if ($x->count()) {
46             // got some data for beyond today..
47             return;
48         }
49         
50         // load our default rates to start with..
51         $dom = simplexml_load_file(dirname(__FILE__).'/../eurofxref-daily.xml');
52         $rates['EUR'] = 1.0;
53         $rates['TWD'] = 46.7008412; // taiwan dorlar
54         $rates['VND'] = 25282.24; // veitnam dong?
55        
56        
57         foreach($dom->Cube->Cube->Cube as $c) {
58            //echo '<PRE>';print_r($c );
59             $rates[(string)$c['currency']] = (string)$c['rate'];
60         }
61         $rates['RMB'] = $rates['CNY'] ;
62         
63         
64         
65         // now try loading from latest..
66        
67              // this may fail...
68         $f = @file_get_contents('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml');
69         if (!strlen($f)) {
70             return false;
71         }
72         
73         $dom = simplexml_load_file($target);
74         $rates['EUR'] = 1.0;
75         
76         foreach($dom->Cube->Cube->Cube as $c) {
77            //echo '<PRE>';print_r($c );
78             $rates[(string)$c['currency']] = (string)$c['rate'];
79         }
80         $rates['RMB'] = $rates['CNY'] ;
81         
82         foreach($rates as $cur=>$rate) {
83             
84             $ov = DB_DataObject::Factory('core_curr_rate');
85             $ov->curr = $cur;
86             $nl = clone($x);
87             $ov->orderBy('to_date DESC');
88             $ov->limit(1);
89             
90             
91             $nl->from_dt = DB_DataObject::sqlValue("NOW()");
92             $nl->to_dt = DB_DataObject::sqlValue("NOW() + INTERVAL 7 DAY");
93             if ($ov->find(true)) {
94                 if (strtotime($ov->to_date) > time()) {
95                     continue;
96                 }
97                 $nl->from_dt = $ov->to_date;
98                 
99             
100                 if ($ov->rate == $rate) {
101                     // modify the old one to expire
102                     $oo = clone($ov);
103                     $ov->to_date = $nv->to_from_dt;
104                     $ov->update($oo);
105                     continue;
106                 }
107             }
108             
109             // create a new row.
110             
111             
112             
113             
114             
115             
116             
117         }
118         
119         
120         
121         
122         
123         
124         
125     }
126 }