typo
[Pman.Core] / DataObjects / Core_curr_rate.php
index a3b6b6f..5b6858c 100644 (file)
@@ -2,7 +2,7 @@
 /**
  * Table Definition for core_curr_rate
  */
-require_once 'DB/DataObject.php';
+class_exists('DB_DataObject') ? '' : require_once 'DB/DataObject.php';
 
 class Pman_Core_DataObjects_Core_curr_rate extends DB_DataObject 
 {
@@ -12,7 +12,7 @@ class Pman_Core_DataObjects_Core_curr_rate extends DB_DataObject
     public $__table = 'core_curr_rate';    // table name
     public $id;
     public $curr;
-    public $rate;
+    public $rate;  // always to USD...
     public $from_dt;
     public $to_dt;
 
@@ -24,6 +24,9 @@ class Pman_Core_DataObjects_Core_curr_rate extends DB_DataObject
         
     }
     
+    
+    
+    
     /**
      * current rate fetching
      *
@@ -32,59 +35,156 @@ class Pman_Core_DataObjects_Core_curr_rate extends DB_DataObject
      * this just get's the current rates from the ecb..
      * 
      * 
-     */
-    
-    var $rates = array();
+     */ 
     function loadRates()
     {
         
+        static $checked = false;
+        if ($checked ) {
+            return true;
+        }
+        $checked  = true;
+        
         // how often do we need to know this..?
         // let's assume we do it once a week..
         $x = DB_DataObject::Factory('core_curr_rate');
-        $x->whereAdd('from_date > NOW() - INTERVAL 7 DAY');
+        $x->whereAdd('to_dt > NOW()');
         if ($x->count()) {
+            // got some data for beyond today..
+            
             return;
         }
         
-        
-        
-        
-        if (!empty($this->rates)) {
-            return true;
-        }
         // load our default rates to start with..
-        $dom = simplexml_load_file(dirname(__FILE__).'/eurofxref-daily.xml');
-        $this->rates['EUR'] = 1.0;
-        $this->rates['TWD'] = 46.7008412;
-        $this->rates['VND'] = 26405.3;
+        $dom = simplexml_load_file(dirname(__FILE__).'/../eurofxref-daily.xml');
+        $rates['EUR'] = 1.0;
+        $rates['TWD'] = 46.7008412; // taiwan dorlar
+        $rates['VND'] = 25282.24; // veitnam dong?
        
        
         foreach($dom->Cube->Cube->Cube as $c) {
            //echo '<PRE>';print_r($c );
-            $this->rates[(string)$c['currency']] = (string)$c['rate'];
+            $rates[(string)$c['currency']] = (string)$c['rate'];
         }
-        $this->rates['RMB'] = $this->rates['CNY'] ;
+        $rates['RMB'] = $rates['CNY'] ;
+        
+        
+        
         // now try loading from latest..
-        $target = ini_get('session.save_path').'/eurofxref-daily.xml';
-        
-        if (!file_exists($target) || filemtime($target) < (time() - 60*60*24)) {
-            // this may fail...
-            $f = @file_get_contents('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml');
-            if (!strlen($f)) {
-                return;
-            } 
-            file_put_contents($target,$f);
-        
-        } 
-        $dom = simplexml_load_file($target);
-        $this->rates['EUR'] = 1.0;
-        $this->rates['TWD'] = 46.7008412;
-        $this->rates['VND'] = 26405.3;
        
+             // this may fail...
+        $f = @file_get_contents('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml');
+        if (!strlen($f)) {
+            return false;
+        }
+        
+        $dom = simplexml_load_string($f);
+        $rates['EUR'] = 1.0;
+        
         foreach($dom->Cube->Cube->Cube as $c) {
            //echo '<PRE>';print_r($c );
-            $this->rates[(string)$c['currency']] = (string)$c['rate'];
+            $rates[(string)$c['currency']] = (string)$c['rate'];
+        }
+        $rates['RMB'] = $rates['CNY'] ;
+        
+        foreach($rates as $cur=>$in_euro) {
+            
+
+            $rate = (1.0 / $rates['USD']) * $in_euro;
+             
+            
+            
+            $ov = DB_DataObject::Factory('core_curr_rate');
+            $ov->curr = $cur;
+            $nl = clone($ov);
+            $ov->orderBy('to_dt DESC');
+            $ov->limit(1);
+            
+            
+            $nl->from_dt = DB_DataObject::sqlValue("NOW()");
+            $nl->to_dt = DB_DataObject::sqlValue("NOW() + INTERVAL 7 DAY");
+            if ($ov->find(true)) {
+                if (strtotime($ov->to_dt) > time()) {
+                    continue;
+                }
+                $nl->from_dt = $ov->to_dt;
+                
+            
+                if ($ov->rate == $rate) {
+                    // modify the old one to expire
+                    $oo = clone($ov);
+                    $ov->to_dt = $nl->from_dt;
+                    $ov->update($oo);
+                    continue;
+                }
+            } else {
+                // no previous record...
+                $nl->from_dt = '1970-01-01 00:00:00';
+            }
+            $nl->rate = $rate;
+            // create a new row.
+            $nl->insert();
+            
+            
+            
         }
-        $this->rates['RMB'] = $this->rates['CNY'] ;
+        
+        
     }
+    function rate($cur, $when)
+    {
+        $when === false ? date('Y-m-d H:i:s') : $when;
+        $this->loadRates(); // check if we have an rates.
+        
+        $r = DB_DataObject::factory('core_curr_rate');
+        $r->curr = $cur;
+        $r->whereAdd("from_dt < '" . date('Y-m-d H:i:s', strtotime($when)) . "'");
+        
+        $r->orderBy('to_dt ASC');
+        $r->limit(1);
+        if ($r->find(true)) {
+            return $r->rate;
+        }
+        return false;
+    }
+    
+    function convert($val, $from, $to, $when = false)
+    {
+        
+        
+        $fr = $this->rate($from, $when);
+        $tr = $this->rate($to, $when);
+        
+        // crappy error handling..
+        if ($fr === false) {
+            return false;
+        }
+        if ($tr === false) {
+            return false;
+        }
+        
+        return ((1.0 / $fr) * $val) * $tr;
+  
+        
+    
+    }
+    
+    function currentRates()
+    {
+        $this->loadRates();
+       // DB_DataObject::debugLevel(1);
+        $c = DB_DAtaObject::factory('core_curr_rate');
+        $c->whereAdd('from_dt < NOW() AND to_dt > NOW()');
+        $c->find();
+        $ret = array();
+        while($c->fetch()) {
+            $ret[$c->curr] = $c->rate;
+        }
+        return $ret;
+        
+        
+        
+    }
+    
+    
 }