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
27 FROM ".TB_PREF."exchange_rates
28 WHERE curr_code=".db_escape($curr_code)
29 ." AND date_='$date'";
30 $result = db_query($sql, "could not get exchange rate for $curr_code - $date_");
32 if(db_num_rows($result) == 0)
34 $row = db_fetch($result);
39 Return last exchange rate of $currency not later than $date.
41 function get_last_exchange_rate($curr_code, $date_)
43 $date = date2sql($date_);
45 $sql = "SELECT rate_buy, max(date_) as date_
46 FROM ".TB_PREF."exchange_rates
47 WHERE curr_code = ".db_escape($curr_code)."
48 AND date_ <= '$date' GROUP BY rate_buy ORDER BY date_ Desc LIMIT 1";
50 $result = db_query($sql, "could not query exchange rates");
53 if (db_num_rows($result) == 0)
56 return db_fetch_assoc($result);
59 //---------------------------------------------------------------------------------------------
61 function update_exchange_rate($curr_code, $date_, $buy_rate, $sell_rate)
63 if (is_company_currency($curr_code))
64 display_db_error("Exchange rates cannot be set for company currency", "", true);
66 $date = date2sql($date_);
68 $sql = "UPDATE ".TB_PREF."exchange_rates SET rate_buy=$buy_rate, rate_sell=".db_escape($sell_rate)
69 ." WHERE curr_code=".db_escape($curr_code)." AND date_='$date'";
71 db_query($sql, "could not add exchange rate for $curr_code");
74 //---------------------------------------------------------------------------------------------
76 function add_exchange_rate($curr_code, $date_, $buy_rate, $sell_rate)
78 if (is_company_currency($curr_code))
79 display_db_error("Exchange rates cannot be set for company currency", "", true);
81 $date = date2sql($date_);
83 $sql = "INSERT INTO ".TB_PREF."exchange_rates (curr_code, date_, rate_buy, rate_sell)
84 VALUES (".db_escape($curr_code).", '$date', ".db_escape($buy_rate)
85 .", ".db_escape($sell_rate).")";
86 db_query($sql, "could not add exchange rate for $curr_code");
89 //---------------------------------------------------------------------------------------------
91 function add_new_exchange_rate($curr_code, $date_, $ex_rate)
93 if (is_company_currency($curr_code) || !$ex_rate)
96 if (!get_date_exchange_rate($curr_code, $date_))
97 add_exchange_rate($curr_code, $date_, $ex_rate, $ex_rate);
100 //---------------------------------------------------------------------------------------------
102 function delete_exchange_rate($rate_id)
104 $sql = "DELETE FROM ".TB_PREF."exchange_rates WHERE id=".db_escape($rate_id);
105 db_query($sql, "could not delete exchange rate $rate_id");
108 //-----------------------------------------------------------------------------
109 // Retrieve exchange rate as of date $date from external source (usually inet)
111 // Exchange rate for currency revaluation purposes is defined in FA as home_currency/curr_b ratio i.e.
113 // amount [home] = amount [curr] * ex_rate
115 function retrieve_exrate($curr_b, $date)
119 $xchg_rate_provider = ((isset($SysPrefs->xr_providers) && isset($SysPrefs->dflt_xr_provider))
120 ? $SysPrefs->xr_providers[$SysPrefs->dflt_xr_provider] : 'ECB');
122 $rate = hook_retrieve_exrate($curr_b, $date);
123 if (is_numeric($rate))
125 return get_extern_rate($curr_b, $xchg_rate_provider, $date);
127 //-----------------------------------------------------------------------------
129 function get_extern_rate($curr_b, $provider = 'ECB', $date)
131 global $path_to_root;
133 if ($date != Today()) // no historical rates available
136 $curr_a = get_company_pref('curr_default');
137 if ($provider == 'ECB')
139 $filename = "/stats/eurofxref/eurofxref-daily.xml";
140 $site = "www.ecb.europa.eu";
142 elseif ($provider == 'YAHOO')
144 $filename = "/d/quotes.csv?s={$curr_a}{$curr_b}=X&f=sl1d1t1ba&e=.csv"; // new URL's for YAHOO
145 $site = "download.finance.yahoo.com";
146 //$filename = "/q?s={$curr_a}{$curr_b}=X"; // Let old code be here for a while, Joe.
147 //$site = "finance.yahoo.com";
149 elseif ($provider == 'GOOGLE')
151 $filename = "/finance/converter?a=1&from={$curr_a}&to={$curr_b}";
152 $site = "www.google.com";
154 elseif ($provider == 'BLOOMBERG')
156 $filename = "/quote/{$curr_b}{$curr_a}:CUR";
157 $site = "www.bloomberg.com";
160 if (function_exists('curl_init'))
161 { // first check with curl as we can set short timeout;
165 curl_setopt ($ch, CURLOPT_URL, 'http://'.$site.$filename);
166 curl_setopt ($ch, CURLOPT_COOKIEJAR, "$path_to_root/tmp/cookie.txt");
167 curl_setopt ($ch, CURLOPT_HEADER, 0);
168 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
169 // prevent warning while safe_mode/open_basedir on (redireciton doesn't occur at least on ECB page)
170 if (!ini_get('safe_mode') && !ini_get('open_basedir'))
171 curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
172 curl_setopt ($ch, CURLOPT_TIMEOUT, 3);
173 $contents = curl_exec ($ch);
175 // due to resolver bug in some curl versions (e.g. 7.15.5)
176 // try again for constant IP.
177 $site="172.230.157.137";
178 } while( ($contents == '') && $retry--);
181 $contents = url_get_contents("http://".$site.$filename);
183 if ($provider == 'ECB')
185 $contents = str_replace ("<Cube currency='USD'", " <Cube currency='EUR' rate='1'/> <Cube currency='USD'", $contents);
186 $from_mask = "|<Cube\s*currency=\'" . $curr_a . "\'\s*rate=\'([\d.,]*)\'\s*/>|i";
187 preg_match ( $from_mask, $contents, $out );
188 $val_a = isset($out[1]) ? $out[1] : 0;
189 $val_a = str_replace ( ',', '', $val_a );
190 $to_mask = "|<Cube\s*currency=\'" . $curr_b . "\'\s*rate=\'([\d.,]*)\'\s*/>|i";
191 preg_match ( $to_mask, $contents, $out );
192 $val_b = isset($out[1]) ? $out[1] : 0;
193 $val_b = str_replace ( ',', '', $val_b );
196 $val = $val_a / $val_b;
203 elseif ($provider == 'YAHOO')
206 $array = explode(',',$contents); // New operations for YAHOO. Safer.
210 /* Let old code be here for a while, Joe.
211 //if (preg_match('/Last\sTrade:(.*?)Trade\sTime/s', $contents, $matches)) {
212 $val = strip_tags($matches[1]);
213 $val = str_replace(',', '', $val);
219 elseif ($provider == 'GOOGLE')
223 $regexp = "%([\d|.]+)\s+{$curr_a}\s+=\s+<span\sclass=(.*)>([\d|.]+)\s+{$curr_b}\s*</span>%s";
224 if (preg_match($regexp, $contents, $matches))
227 $val = str_replace(',', '', $val);
232 elseif ($provider == 'BLOOMBERG')
235 $stmask = '<span class=" price">';
236 $val = trim(strstr($contents, $stmask));
238 $val = trim(strstr($val, $stmask));
239 $val = trim(strtok($val, $stmask));
242 } /* end function get_extern_rate */
244 //-----------------------------------------------------------------------------
246 function get_sql_for_exchange_rates($curr)
248 $sql = "SELECT date_, rate_buy, id
249 FROM ".TB_PREF."exchange_rates "
250 ."WHERE curr_code=".db_escape($curr)."
251 ORDER BY date_ DESC";