Fixed invalid gettextized strings
[fa-stable.git] / gl / includes / db / gl_db_rates.inc
1 <?php
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)
15 {
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");   
18
19         return db_fetch($result);
20 }
21
22 // Retrieves buy exchange rate for given currency/date, zero if no result
23 function get_date_exchange_rate($curr_code, $date_)
24 {
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_");        
29
30         if(db_num_rows($result) == 0) 
31                 return 0;
32         $row = db_fetch($result);
33                 return $row[0];
34 }
35
36 //---------------------------------------------------------------------------------------------
37
38 function update_exchange_rate($curr_code, $date_, $buy_rate, $sell_rate)
39 {
40         if (is_company_currency($curr_code))
41                 display_db_error("Exchange rates cannot be set for company currency", "", true);
42                         
43         $date = date2sql($date_);
44                 
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'";
47                                 
48         db_query($sql, "could not add exchange rate for $curr_code");                           
49 }
50
51 //---------------------------------------------------------------------------------------------
52
53 function add_exchange_rate($curr_code, $date_, $buy_rate, $sell_rate)
54 {
55         if (is_company_currency($curr_code))
56                 display_db_error("Exchange rates cannot be set for company currency", "", true);
57
58         $date = date2sql($date_);
59                 
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");                           
64 }
65
66 //---------------------------------------------------------------------------------------------
67
68 function delete_exchange_rate($rate_id)
69 {
70         $sql = "DELETE FROM ".TB_PREF."exchange_rates WHERE id=".db_escape($rate_id);
71         db_query($sql, "could not delete exchange rate $rate_id");              
72 }
73
74 //-----------------------------------------------------------------------------
75 //      Retrieve exchange rate as of date $date from external source (usually inet)
76 //
77 function retrieve_exrate($curr_b, $date)
78 {
79         $rate = hook_retrieve_exrate($curr_b, $date);
80         if (is_numeric($rate))
81                 return $rate;
82         return get_extern_rate($curr_b, 'ECB', $date);
83 }
84 //-----------------------------------------------------------------------------
85
86 function get_extern_rate($curr_b, $provider = 'ECB', $date) 
87 {
88         $curr_a = get_company_pref('curr_default');
89         if ($provider == 'ECB')
90         {
91                 $filename = "/stats/eurofxref/eurofxref-daily.xml";
92                 $site = "www.ecb.int";
93         }
94         elseif ($provider == 'YAHOO')
95         {
96                 $filename = "/q?s={$curr_a}{$curr_b}=X";
97                 $site = "finance.yahoo.com";
98         }
99         elseif ($provider == 'GOOGLE')
100         {
101                 $filename = "/finance/converter?a=1&from={$curr_a}&to={$curr_b}";
102                 $site = "finance.google.com";
103         }
104         $contents = '';
105
106         if (function_exists('curl_init'))
107         {       // first check with curl as we can set short timeout;
108                 $retry = 1;
109                 do {
110                $ch = curl_init();
111            curl_setopt ($ch, CURLOPT_URL, 'http://'.$site.$filename);
112                curl_setopt ($ch, CURLOPT_COOKIEJAR, "cookie.txt");
113            curl_setopt ($ch, CURLOPT_HEADER, 0);
114                curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
115            curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
116                curl_setopt ($ch, CURLOPT_TIMEOUT, 3);
117            $contents = curl_exec ($ch);
118                curl_close($ch);
119                         // due to resolver bug in some curl versions (e.g. 7.15.5) 
120                         // try again for constant IP.
121                    $site="195.128.2.97";
122            } while( ($contents == '') && $retry--);
123            
124         } else {
125                 $contents = url_get_contents("http://".$site.$filename);
126         }
127         if (!$contents) {
128                 display_warning(sprintf(_("Cannot retrieve currency rate from %s page. Please set the rate manually."), $provider));
129         }
130         if ($provider == 'ECB')
131         {
132                 $contents = str_replace ("<Cube currency='USD'", " <Cube currency='EUR' rate='1'/> <Cube currency='USD'", $contents);
133                 $from_mask = "|<Cube\s*currency=\'" . $curr_a . "\'\s*rate=\'([\d.,]*)\'\s*/>|i";
134                 preg_match ( $from_mask, $contents, $out );
135                 $val_a = isset($out[1]) ? $out[1] : 0;
136                 $val_a = str_replace ( ',', '', $val_a );
137                 $to_mask = "|<Cube\s*currency=\'" . $curr_b . "\'\s*rate=\'([\d.,]*)\'\s*/>|i";
138                 preg_match ( $to_mask, $contents, $out );
139                 $val_b = isset($out[1]) ? $out[1] : 0;
140                 $val_b = str_replace ( ',', '', $val_b );
141                 if ($val_b) 
142                 {
143                         $val = $val_a / $val_b;
144                 } 
145                 else 
146                 {
147                         $val = 0;
148                 }
149         }
150         elseif ($provider == 'YAHOO')
151         {
152                 $val = '';
153                 if (preg_match('/Last\sTrade:(.*?)Trade\sTime/s', $contents, $matches)) {
154                         $val = strip_tags($matches[1]);
155                         $val = str_replace(',', '', $val);
156                         if ($val != 0)
157                                 $val = 1 / $val;
158                 }
159         }
160         elseif ($provider == 'GOOGLE')
161         {
162                 $val = '';
163                 $regexp = "%([\d|.]+)\s+{$curr_a}\s+=\s+<span\sclass=(.*)>([\d|.]+)\s+{$curr_b}\s*</span>%s";
164                 if (preg_match($regexp, $contents, $matches)) 
165                 {
166                         $val = $matches[3];
167                         $val = str_replace(',', '', $val);
168                         if ($val != 0)
169                                 $val = 1 / $val;
170                 }
171     }    
172         return $val;
173 }  /* end function get_extern_rate */
174
175 //-----------------------------------------------------------------------------
176
177 function get_sql_for_exchange_rates()
178 {
179         $sql = "SELECT date_, rate_buy, id FROM "
180                 .TB_PREF."exchange_rates "
181                 ."WHERE curr_code=".db_escape($_POST['curr_abrev'])."
182                  ORDER BY date_ DESC";
183         return $sql;     
184 }
185 ?>