ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilPaymentCurrency.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
16 {
17 
18  private $currency_id;
19  private $unit;
20  private $iso_code;
21  private $symbol;
23 
24  public function ilPaymentCurrency($a_currency_id = '')
25  {
26  global $ilDB;
27 
28  $this->db = $ilDB;
29 
30  $this->currency_id = $a_currency_id;
31 
32  }
33 
34 
35  public function setCurrencyId($a_currency_id)
36  {
37  $this->currency_id = $a_currency_id;
38  }
39  public function getCurrencyId()
40  {
41  return $this->currency_id;
42  }
43 
44  public function setUnit($a_unit)
45  {
46  $this->unit = $a_unit;
47  }
48  public function getUnit()
49  {
50  return $this->unit;
51  }
52  public function setIsoCode($a_iso_code)
53  {
54  $this->iso_code = $a_iso_code;
55  }
56  public function getIsoCode()
57  {
58  return $this->iso_code;
59  }
60 
61  public function setSymbol($a_symbol)
62  {
63  $this->symbol = $a_symbol;
64  }
65  public function getSymbol()
66  {
67  return $this->symbol;
68  }
69  public function setConversionRate($a_conversion_rate)
70  {
71  $this->conversion_rate = (float)$a_conversion_rate;
72  }
73  public function getConversionRate()
74  {
76  }
77 
78  public function addCurrency()
79  {
80  $nextId = $this->db->nextID('payment_currencies');
81 
82  $this->db->manipulateF('INSERT INTO payment_currencies
83  (currency_id, unit, iso_code, symbol, conversion_rate)
84  VALUES (%s, %s, %s, %s, %s)',
85  array('integer', 'text','text','text','float'),
86  array($nextId, $this->getUnit(), $this->getIsoCode(), $this->getSymbol(), $this->getConversionRate()));
87  return true;
88  }
89 
90  public function deleteCurrency()
91  {
92  $this->db->manipulateF('DELETE FROM payment_currencies WHERE currency_id = %s',
93  array('integer'), array($this->getCurrencyId()));
94 
95  }
96  public function updateCurrency()
97  {
98  $this->db->manipulateF('UPDATE payment_currencies
99  SET unit = %s,
100  iso_code = %s,
101  symbol = %s,
102  conversion_rate = %s
103  WHERE currency_id = %s',
104  array('text','text','text','float','integer'),
105  array($this->getUnit(), $this->getIsoCode(), $this->getSymbol(),
106  $this->getConversionRate(), $this->getCurrencyId()));
107  }
108 
109  public static function _getAvailableCurrencies()
110  {
111  global $ilDB;
112 
113  $res = $ilDB->query('SELECT * FROM payment_currencies');
114 
115 
116  while($row = $ilDB->fetchAssoc($res))
117  {
118  $currencies[$row['currency_id']] = $row;
119 /* $currencies[$row->currency_id]['currency_id'] = $row->currency_id;
120  $currencies[$row->currency_id]['unit'] = $row->unit;
121  $currencies[$row->currency_id]['iso_code'] = $row->subunit;
122  */
123  }
124  return $currencies ? $currencies : array();
125  }
126 
127  public static function _getCurrency($a_currency_id)
128  {
129  global $ilDB;
130 
131  $res = $ilDB->queryf('
132  SELECT * FROM payment_currencies WHERE currency_id = %s',
133  array('integer'), array($a_currency_id));
134 
135 
136  while($row = $ilDB->fetchAssoc($res))
137  {
138  $currencies[$row['currency_id']] = $row;
139 
140  /* while($row = $ilDB->fetchObject($res))
141  {
142  $currencies['currency_id'] = $row->currency_id;
143  $currencies['unit'] = $row->unit;
144  $currencies['subunit'] = $row->subunit;*/
145  }
146  return $currencies;
147  }
148 
149  public static function _getUnit($a_currency_id)
150  {
151  global $ilDB;
152 
153  $res = $ilDB->queryf('
154  SELECT unit FROM payment_currencies WHERE currency_id = %s',
155  array('integer'), array($a_currency_id));
156 
157  while($row = $ilDB->fetchObject($res))
158  {
159  return $row->unit;
160  }
161  return false;
162  }
163 
164  public static function _getSymbol($a_currency_id)
165  {
166  global $ilDB;
167 
168  $res = $ilDB->queryf('
169  SELECT symbol FROM payment_currencies WHERE currency_id = %s',
170  array('integer'), array($a_currency_id));
171 
172  while($row = $ilDB->fetchObject($res))
173  {
174  return $row->symbol;
175  }
176  return false;
177  }
178 
179  public static function _getConversionRate($a_currency_id)
180  {
181  global $ilDB;
182 
183  $res = $ilDB->queryF('
184  SELECT conversion_rate FROM payment_currencies WHERE currency_id = %s',
185  array('integer'), array($a_currency_id));
186 
187  while($row = $ilDB->fetchObject($res))
188  {
189  return (float)$row->conversion_rate;
190  }
191  return false;
192  }
193  public static function _getCurrencyBySymbol($a_currency_symbol)
194  {
195  global $ilDB;
196 
197  $res = $ilDB->queryF('SELECT * FROM payment_currencies WHERE symbol = %s',
198  array('text'), array($a_currency_symbol));
199  $row = $ilDB->fetchAssoc($res);
200 
201  return $row;
202  }
203 
204  public static function _getDefaultCurrency()
205  {
206  global $ilDB;
207 
208  $res = $ilDB->query('SELECT * FROM payment_currencies WHERE is_default = 1');
209  $row = $ilDB->fetchAssoc($res);
210 
211  return $row;
212  }
213 
214  public static function _updateIsDefault($a_currency_id)
215  {
216  global $ilDB;
217 
218  // calculate other currencies to default_currency
219  $conversion_rate = self::_getConversionRate($a_currency_id);
220  $currencies = self::_getAvailableCurrencies();
221 
222  foreach ($currencies as $tmp_cur)
223  {
224  //calculate conversion rates
225  $con_result = round((float)$tmp_cur['conversion_rate'] / (float)$conversion_rate, 4);
226 
227  $upd = $ilDB->update('payment_currencies',
228  array( 'conversion_rate' => array('float', $con_result),
229  'is_default' => array('integer', 0)),
230  array('currency_id' => array('integer', $tmp_cur['currency_id'])));
231  }
232  $new_default = $ilDB->update('payment_currencies',
233  array( 'is_default' => array('integer', 1)),
234  array('currency_id' => array('integer', $a_currency_id)));
235  }
236 
237  static public function _getDecimalSeparator()
238  {
239  global $ilUser;
240 
241  $user_lang = $ilUser->getLanguage();
242 
243  // look for ISO 639-1
244  $comma_countries = array(
245  'sq','es','fr','pt', 'bg','de','da','et','fo','fi','el','id','is','it',
246  'hr','lv','lt','lb','mk','mo','nl','no','pl','pt','ro','ru','sv','sr',
247  'sk','sl','af','ce','cs','tr','uk','hu');
248 
249  in_array($user_lang, $comma_countries) ? $separator = ',' : $separator = '.';
250 
251  return $separator;
252  }
253 
254  public static function _formatPriceToString($a_price, $a_currency_symbol = false)
255  {
256  if(!$a_currency_symbol)
257  {
258  $currency_obj = $_SESSION['payment_currency'];
259  $currency_symbol = $currency_obj['symbol'];
260  }
261  else $currency_symbol = $a_currency_symbol;
263 
264  $price_string = number_format($a_price,'2',$separator,'');
265 
266  return $price_string . ' ' . $currency_symbol;
267  }
268 
269  public static function _isDefault($a_currency_id)
270  {
271  global $ilDB;
272 
273  $res = $ilDB->queryF('SELECT is_default FROM payment_currencies WHERE currency_id = %s',
274  array('integer'), array((int)$a_currency_id));
275 
276  $row = $ilDB->fetchAssoc($res);
277 
278  if($row['is_default'] == '1') {
279  return true;
280  }else
281  return false;
282 
283  }
284 }
285 
286 ?>