Removed obsolete function.
[fa-stable.git] / gl / includes / db / gl_db_rates.inc
1 <?php
2 //---------------------------------------------------------------------------------------------
3 function get_exchange_rate($rate_id)
4 {
5         $sql = "SELECT * FROM ".TB_PREF."exchange_rates WHERE id=$rate_id";
6         $result = db_query($sql, "could not get exchange rate for $rate_id");   
7
8         return db_fetch($result);
9 }
10
11 // Retrieves buy exchange rate for given currency/date, zero if no result
12 function get_date_exchange_rate($curr_code, $date_)
13 {
14         $date = date2sql($date_);
15         $sql = "SELECT rate_buy FROM ".TB_PREF."exchange_rates WHERE curr_code='$curr_code' 
16                 AND date_='$date'";
17         $result = db_query($sql, "could not get exchange rate for $curr_code - $date_");        
18
19         if(db_num_rows($result) == 0) 
20                 return 0;
21         $row = db_fetch($result);
22                 return $row[0];
23 }
24
25 //---------------------------------------------------------------------------------------------
26
27 function update_exchange_rate($curr_code, $date_, $buy_rate, $sell_rate)
28 {
29         if (is_company_currency($curr_code))
30                 display_db_error("Exchange rates cannot be set for company currency", "", true);
31                         
32         $date = date2sql($date_);
33                 
34         $sql = "UPDATE ".TB_PREF."exchange_rates SET rate_buy=$buy_rate, rate_sell=$sell_rate
35                 WHERE curr_code='$curr_code' AND date_='$date'";
36                                 
37         db_query($sql, "could not add exchange rate for $curr_code");                           
38 }
39
40 //---------------------------------------------------------------------------------------------
41
42 function add_exchange_rate($curr_code, $date_, $buy_rate, $sell_rate)
43 {
44         if (is_company_currency($curr_code))
45                 display_db_error("Exchange rates cannot be set for company currency", "", true);
46
47         $date = date2sql($date_);
48                 
49         $sql = "INSERT INTO ".TB_PREF."exchange_rates (curr_code, date_, rate_buy, rate_sell)
50                 VALUES ('$curr_code', '$date', $buy_rate, $sell_rate)";
51         db_query($sql, "could not add exchange rate for $curr_code");                           
52 }
53
54 //---------------------------------------------------------------------------------------------
55
56 function delete_exchange_rate($rate_id)
57 {
58         $sql = "DELETE FROM ".TB_PREF."exchange_rates WHERE id=$rate_id";
59         db_query($sql, "could not delete exchange rate $rate_id");              
60 }
61
62 //---------------------------------------------------------------------------------------------
63
64 function get_ecb_rate($curr_b) 
65 {
66         $curr_a = get_company_pref('curr_default');
67         $ecb_filename = 'http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml';
68         $handle = @fopen($ecb_filename, 'rb');
69         $contents = '';
70         if ($handle)
71         {
72                 do 
73                 {
74                         $data = @fread( $handle, 4096 );
75                         if ( strlen ( $data ) == 0 ) 
76                                 break;
77                         $contents .= $data; // with this syntax only text will be translated, whole text with htmlspecialchars($data)
78                 } 
79                 while (true);
80                 @fclose( $handle );
81         } // end handle
82         else
83         {
84        $ch = curl_init();
85        curl_setopt ($ch, CURLOPT_URL, $ecb_filename);
86        curl_setopt ($ch, CURLOPT_COOKIEJAR, "cookie.txt");
87        curl_setopt ($ch, CURLOPT_HEADER, 0);
88        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
89        curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
90        curl_setopt ($ch, CURLOPT_TIMEOUT, 120);
91        $contents = curl_exec ($ch);
92        curl_close($ch);
93         }
94         $contents = str_replace ("<Cube currency='USD'", " <Cube currency='EUR' rate='1'/> <Cube currency='USD'", $contents);
95         $from_mask = "|<Cube\s*currency=\'" . $curr_a . "\'\s*rate=\'([\d.,]*)\'\s*/>|i";
96         preg_match ( $from_mask, $contents, $out );
97         $val_a = isset($out[1]) ? $out[1] : 0;
98         $val_a = str_replace ( ',', '', $val_a );
99         $to_mask = "|<Cube\s*currency=\'" . $curr_b . "\'\s*rate=\'([\d.,]*)\'\s*/>|i";
100         preg_match ( $to_mask, $contents, $out );
101         $val_b = isset($out[1]) ? $out[1] : 0;
102         $val_b = str_replace ( ',', '', $val_b );
103         if ($val_b) 
104         {
105                 $val = $val_a / $val_b;
106         } 
107         else 
108         {
109                 $val = 0;
110         }
111         return $val;
112 } // end function get_ecb_rate
113
114 ?>