2 //---------------------------------------------------------------------------------------------
3 function get_exchange_rate($rate_id)
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");
8 return db_fetch($result);
11 // Retrieves buy exchange rate for given currency/date, zero if no result
12 function get_date_exchange_rate($curr_code, $date_)
14 $date = date2sql($date_);
15 $sql = "SELECT rate_buy FROM ".TB_PREF."exchange_rates WHERE curr_code='$curr_code'
17 $result = db_query($sql, "could not get exchange rate for $curr_code - $date_");
19 if(db_num_rows($result) == 0)
21 $row = db_fetch($result);
25 //---------------------------------------------------------------------------------------------
27 function update_exchange_rate($curr_code, $date_, $buy_rate, $sell_rate)
29 if (is_company_currency($curr_code))
30 display_db_error("Exchange rates cannot be set for company currency", "", true);
32 $date = date2sql($date_);
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'";
37 db_query($sql, "could not add exchange rate for $curr_code");
40 //---------------------------------------------------------------------------------------------
42 function add_exchange_rate($curr_code, $date_, $buy_rate, $sell_rate)
44 if (is_company_currency($curr_code))
45 display_db_error("Exchange rates cannot be set for company currency", "", true);
47 $date = date2sql($date_);
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");
54 //---------------------------------------------------------------------------------------------
56 function delete_exchange_rate($rate_id)
58 $sql = "DELETE FROM ".TB_PREF."exchange_rates WHERE id=$rate_id";
59 db_query($sql, "could not delete exchange rate $rate_id");
62 //---------------------------------------------------------------------------------------------
64 function get_ecb_rate($curr_b)
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');
74 $data = @fread( $handle, 4096 );
75 if ( strlen ( $data ) == 0 )
77 $contents .= $data; // with this syntax only text will be translated, whole text with htmlspecialchars($data)
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);
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 );
105 $val = $val_a / $val_b;
112 } // end function get_ecb_rate