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 include_once($path_to_root . "/includes/remote_url.inc");
13 //---------------------------------------------------------------------------------------------
14 function get_exchange_rate($rate_id)
16 $sql = "SELECT * FROM ".TB_PREF."exchange_rates WHERE id=".db_escape($rate_id);
17 $result = db_query($sql, "could not get exchange rate for $rate_id");
19 return db_fetch($result);
22 // Retrieves buy exchange rate for given currency/date, zero if no result
23 function get_date_exchange_rate($curr_code, $date_)
25 $date = date2sql($date_);
26 $sql = "SELECT rate_buy FROM ".TB_PREF."exchange_rates WHERE curr_code=".db_escape($curr_code)
27 ." AND date_='$date'";
28 $result = db_query($sql, "could not get exchange rate for $curr_code - $date_");
30 if(db_num_rows($result) == 0)
32 $row = db_fetch($result);
36 //---------------------------------------------------------------------------------------------
38 function update_exchange_rate($curr_code, $date_, $buy_rate, $sell_rate)
40 if (is_company_currency($curr_code))
41 display_db_error("Exchange rates cannot be set for company currency", "", true);
43 $date = date2sql($date_);
45 $sql = "UPDATE ".TB_PREF."exchange_rates SET rate_buy=$buy_rate, rate_sell=".db_escape($sell_rate)
46 ." WHERE curr_code=".db_escape($curr_code)." AND date_='$date'";
48 db_query($sql, "could not add exchange rate for $curr_code");
51 //---------------------------------------------------------------------------------------------
53 function add_exchange_rate($curr_code, $date_, $buy_rate, $sell_rate)
55 if (is_company_currency($curr_code))
56 display_db_error("Exchange rates cannot be set for company currency", "", true);
58 $date = date2sql($date_);
60 $sql = "INSERT INTO ".TB_PREF."exchange_rates (curr_code, date_, rate_buy, rate_sell)
61 VALUES (".db_escape($curr_code).", '$date', ".db_escape($buy_rate)
62 .", ".db_escape($sell_rate).")";
63 db_query($sql, "could not add exchange rate for $curr_code");
66 //---------------------------------------------------------------------------------------------
68 function add_new_exchange_rate($curr_code, $date_, $ex_rate)
70 if (is_company_currency($curr_code) || !$ex_rate)
73 if (!get_date_exchange_rate($curr_code, $date_))
74 add_exchange_rate($curr_code, $date_, $ex_rate, $ex_rate);
77 //---------------------------------------------------------------------------------------------
79 function delete_exchange_rate($rate_id)
81 $sql = "DELETE FROM ".TB_PREF."exchange_rates WHERE id=".db_escape($rate_id);
82 db_query($sql, "could not delete exchange rate $rate_id");
85 //-----------------------------------------------------------------------------
86 // Retrieve exchange rate as of date $date from external source (usually inet)
88 // Exchange rate for currency revaluation purposes is defined in FA as home_currency/curr_b ratio i.e.
90 // amount [home] = amount [curr] * ex_rate
92 function retrieve_exrate($curr_b, $date)
94 global $xr_providers, $dflt_xr_provider;
95 $xchg_rate_provider = ((isset($xr_providers) && isset($dflt_xr_provider)) ? $xr_providers[$dflt_xr_provider] : 'ECB');
97 $rate = hook_retrieve_exrate($curr_b, $date);
98 if (is_numeric($rate))
100 return get_extern_rate($curr_b, $xchg_rate_provider, $date);
102 //-----------------------------------------------------------------------------
104 function get_extern_rate($curr_b, $provider = 'ECB', $date)
106 global $path_to_root;
108 if ($date != Today()) // no historical rates available
111 $curr_a = get_company_pref('curr_default');
112 if ($provider == 'ECB')
114 $filename = "/stats/eurofxref/eurofxref-daily.xml";
115 $site = "www.ecb.europa.eu";
117 elseif ($provider == 'YAHOO')
119 $filename = "/d/quotes.csv?s={$curr_a}{$curr_b}=X&f=sl1d1t1ba&e=.csv"; // new URL's for YAHOO
120 $site = "download.finance.yahoo.com";
121 //$filename = "/q?s={$curr_a}{$curr_b}=X"; // Let old code be here for a while, Joe.
122 //$site = "finance.yahoo.com";
124 elseif ($provider == 'GOOGLE')
126 $filename = "/finance/converter?a=1&from={$curr_a}&to={$curr_b}";
127 $site = "www.google.com";
129 elseif ($provider == 'BLOOMBERG')
131 $filename = "/quote/{$curr_b}{$curr_a}:CUR";
132 $site = "www.bloomberg.com";
135 if (function_exists('curl_init'))
136 { // first check with curl as we can set short timeout;
140 curl_setopt ($ch, CURLOPT_URL, 'http://'.$site.$filename);
141 curl_setopt ($ch, CURLOPT_COOKIEJAR, "$path_to_root/tmp/cookie.txt");
142 curl_setopt ($ch, CURLOPT_HEADER, 0);
143 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
144 // prevent warning while safe_mode/open_basedir on (redireciton doesn't occur at least on ECB page)
145 if (!ini_get('safe_mode') && !ini_get('open_basedir'))
146 curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
147 curl_setopt ($ch, CURLOPT_TIMEOUT, 3);
148 $contents = curl_exec ($ch);
150 // due to resolver bug in some curl versions (e.g. 7.15.5)
151 // try again for constant IP.
152 $site="172.230.157.137";
153 } while( ($contents == '') && $retry--);
156 $contents = url_get_contents("http://".$site.$filename);
158 if ($provider == 'ECB')
160 $contents = str_replace ("<Cube currency='USD'", " <Cube currency='EUR' rate='1'/> <Cube currency='USD'", $contents);
161 $from_mask = "|<Cube\s*currency=\'" . $curr_a . "\'\s*rate=\'([\d.,]*)\'\s*/>|i";
162 preg_match ( $from_mask, $contents, $out );
163 $val_a = isset($out[1]) ? $out[1] : 0;
164 $val_a = str_replace ( ',', '', $val_a );
165 $to_mask = "|<Cube\s*currency=\'" . $curr_b . "\'\s*rate=\'([\d.,]*)\'\s*/>|i";
166 preg_match ( $to_mask, $contents, $out );
167 $val_b = isset($out[1]) ? $out[1] : 0;
168 $val_b = str_replace ( ',', '', $val_b );
171 $val = $val_a / $val_b;
178 elseif ($provider == 'YAHOO')
181 $array = explode(',',$contents); // New operations for YAHOO. Safer.
185 /* Let old code be here for a while, Joe.
186 //if (preg_match('/Last\sTrade:(.*?)Trade\sTime/s', $contents, $matches)) {
187 $val = strip_tags($matches[1]);
188 $val = str_replace(',', '', $val);
194 elseif ($provider == 'GOOGLE')
198 $regexp = "%([\d|.]+)\s+{$curr_a}\s+=\s+<span\sclass=(.*)>([\d|.]+)\s+{$curr_b}\s*</span>%s";
199 if (preg_match($regexp, $contents, $matches))
202 $val = str_replace(',', '', $val);
207 elseif ($provider == 'BLOOMBERG')
210 $stmask = '<span class=" price">';
211 $val = trim(strstr($contents, $stmask));
213 $val = trim(strstr($val, $stmask));
214 $val = trim(strtok($val, $stmask));
217 } /* end function get_extern_rate */
219 //-----------------------------------------------------------------------------
221 function get_sql_for_exchange_rates()
223 $sql = "SELECT date_, rate_buy, id FROM "
224 .TB_PREF."exchange_rates "
225 ."WHERE curr_code=".db_escape($_POST['curr_abrev'])."
226 ORDER BY date_ DESC";