3 //--------------------------------------------------------------------------------------------------
5 function get_exchange_rates($curr_code)
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");
11 //---------------------------------------------------------------------------------------------
13 function get_exchange_rate($rate_id)
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");
18 return db_fetch($result);
21 function get_date_exchange_rate($curr_code, $date_)
23 $date = date2sql($date_);
24 $sql = "SELECT date_ FROM ".TB_PREF."exchange_rates WHERE curr_code='$curr_code'
26 $result = db_query($sql, "could not get exchange rate for $curr_code - $date_");
28 return (db_num_rows($result) == 1);
31 //---------------------------------------------------------------------------------------------
33 function update_exchange_rate($curr_code, $date_, $buy_rate, $sell_rate)
35 if (is_company_currency($curr_code))
36 display_db_error("Exchange rates cannot be set for company currency", "", true);
38 $date = date2sql($date_);
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'";
43 db_query($sql, "could not add exchange rate for $curr_code");
46 //---------------------------------------------------------------------------------------------
48 function add_exchange_rate($curr_code, $date_, $buy_rate, $sell_rate)
50 if (is_company_currency($curr_code))
51 display_db_error("Exchange rates cannot be set for company currency", "", true);
53 $date = date2sql($date_);
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");
60 //---------------------------------------------------------------------------------------------
62 function delete_exchange_rate($rate_id)
64 $sql = "DELETE FROM ".TB_PREF."exchange_rates WHERE id=$rate_id";
65 db_query($sql, "could not delete exchange rate $rate_id");
68 //---------------------------------------------------------------------------------------------
70 function get_ecb_rate($curr_b)
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');
80 $data = @fread( $handle, 4096 );
81 if ( strlen ( $data ) == 0 )
83 $contents .= $data; // with this syntax only text will be translated, whole text with htmlspecialchars($data)
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);
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 );
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 );
108 $val_b = str_replace ( ',', '', $val_b );
111 $val = $val_a / $val_b;
118 } // end function get_ecb_rate