2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU General Public License, GPL,
5 as published by the Free Software Foundation, either version 3
6 of the License, or (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 //---------------------------------------------------------------------------------------------
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 // Retrieves buy exchange rate for given currency/date, zero if no result
22 function get_date_exchange_rate($curr_code, $date_)
24 $date = date2sql($date_);
25 $sql = "SELECT rate_buy FROM ".TB_PREF."exchange_rates WHERE curr_code='$curr_code'
27 $result = db_query($sql, "could not get exchange rate for $curr_code - $date_");
29 if(db_num_rows($result) == 0)
31 $row = db_fetch($result);
35 //---------------------------------------------------------------------------------------------
37 function update_exchange_rate($curr_code, $date_, $buy_rate, $sell_rate)
39 if (is_company_currency($curr_code))
40 display_db_error("Exchange rates cannot be set for company currency", "", true);
42 $date = date2sql($date_);
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'";
47 db_query($sql, "could not add exchange rate for $curr_code");
50 //---------------------------------------------------------------------------------------------
52 function add_exchange_rate($curr_code, $date_, $buy_rate, $sell_rate)
54 if (is_company_currency($curr_code))
55 display_db_error("Exchange rates cannot be set for company currency", "", true);
57 $date = date2sql($date_);
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");
64 //---------------------------------------------------------------------------------------------
66 function delete_exchange_rate($rate_id)
68 $sql = "DELETE FROM ".TB_PREF."exchange_rates WHERE id=$rate_id";
69 db_query($sql, "could not delete exchange rate $rate_id");
72 //-----------------------------------------------------------------------------
73 // Retrieve exchange rate as of date $date from external source (usually inet)
75 function retrieve_exrate($curr_b, $date)
79 if (method_exists($Hooks, 'retrieve_exrate'))
80 return $Hooks->retrieve_exrate($curr_b, $date);
82 return get_ecb_rate($curr_b);
84 //-----------------------------------------------------------------------------
86 function get_ecb_rate($curr_b)
88 $curr_a = get_company_pref('curr_default');
89 $ecb_filename = '/stats/eurofxref/eurofxref-daily.xml';
90 $ecb_site = 'www.ecb.int';
93 if (function_exists('curl_init'))
94 { // first check with curl as we can set short timeout;
98 curl_setopt ($ch, CURLOPT_URL, 'http://'.$ecb_site.$ecb_filename);
99 curl_setopt ($ch, CURLOPT_COOKIEJAR, "cookie.txt");
100 curl_setopt ($ch, CURLOPT_HEADER, 0);
101 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
102 curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
103 curl_setopt ($ch, CURLOPT_TIMEOUT, 3);
104 $contents = curl_exec ($ch);
106 // due to resolver bug in some curl versions (e.g. 7.15.5)
107 // try again for constant IP.
108 $ecb_site="195.128.2.97";
109 } while( ($contents == '') && $retry--);
112 $handle = @fopen("http://".$ecb_site.$ecb_filename, 'rb');
116 $data = @fread( $handle, 4096 );
117 if ( strlen ( $data ) == 0 )
119 $contents .= $data; // with this syntax only text will be translated, whole text with htmlspecialchars($data)
126 display_warning(_('Cannot retrieve currency rate from ECB page. Please set the rate manually.'));
128 $contents = str_replace ("<Cube currency='USD'", " <Cube currency='EUR' rate='1'/> <Cube currency='USD'", $contents);
129 $from_mask = "|<Cube\s*currency=\'" . $curr_a . "\'\s*rate=\'([\d.,]*)\'\s*/>|i";
130 preg_match ( $from_mask, $contents, $out );
131 $val_a = isset($out[1]) ? $out[1] : 0;
132 $val_a = str_replace ( ',', '', $val_a );
133 $to_mask = "|<Cube\s*currency=\'" . $curr_b . "\'\s*rate=\'([\d.,]*)\'\s*/>|i";
134 preg_match ( $to_mask, $contents, $out );
135 $val_b = isset($out[1]) ? $out[1] : 0;
136 $val_b = str_replace ( ',', '', $val_b );
139 $val = $val_a / $val_b;
146 } // end function get_ecb_rate