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