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