ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilPaymentPrices.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
12 {
13  private $ilDB;
14 
15  private $pobject_id;
16 
17  private $price;
18  private $currency;
19  private $duration;
20  private $unlimited_duration = 0;
21  private $extension = 0;
22 
23  // TODO later -> this is for using different currencies
25 
26  private $prices = array();
27 
28  public function ilPaymentPrices($a_pobject_id = 0)
29  {
30  global $ilDB;
31 
32  $this->db = $ilDB;
33 
34  $this->pobject_id = $a_pobject_id;
35 
36  $this->__read();
37  }
38 
39  // SET GET
40  public function getPobjectId()
41  {
42  return $this->pobject_id;
43  }
44 
45  public function getPrices()
46  {
47  return $this->prices ? $this->prices : array();
48  }
49  function getPrice($a_price_id)
50  {
51  return $this->prices[$a_price_id] ? $this->prices[$a_price_id] : array();
52  }
53 
54 
55  // STATIC
56  public static function _getPrice($a_price_id)
57  {
58  global $ilDB, $ilSettings;
59 
60  $res = $ilDB->queryf('
61  SELECT * FROM payment_prices
62  WHERE price_id = %s',
63  array('integer'), array($a_price_id));
64 
65  while($row = $ilDB->fetchObject($res))
66  {
67  $price['duration'] = $row->duration;
68  $price['unlimited_duration'] = $row->unlimited_duration;
69  $price['currency'] = $row->currency;
70  $price['price'] = number_format($row->price, 2, '.', '');
71  $price['extension'] = $row->extension;
72 
73  }
74 
75  return count($price) ? $price : array();
76  }
77 
78  public static function _countPrices($a_pobject_id)
79  {
80  global $ilDB;
81 
82  $res = $ilDB->queryf('
83  SELECT count(price_id) FROM payment_prices
84  WHERE pobject_id = %s',
85  array('integer'),
86  array($a_pobject_id));
87 
88 # $row = $res->fetchRow(DB_FETCHMODE_ARRAY);
89  $row = $ilDB->fetchAssoc($res);
90 
91  return ($row[0]);
92  }
93 
94  public static function _getPriceString($a_price_id)
95  {
96  $price = ilPaymentPrices::_getPrice($a_price_id);
97  $gui_price = self::_getGUIPrice($price['price']);
98 
99  return $gui_price;
100  }
101 
102 
103  public static function _getGUIPrice($a_price)
104  {
105  global $lng;
106 
107  $system_lng = $lng->getDefaultLanguage();
108 
109  // CODES: ISO 639
110  $use_comma_seperator = array('ar','bg','cs','de','da','es','et','it',
111  'fr','nl','el','sr','uk','ru','ro','tr','pl','lt','pt','sq','hu');
112 
113  $use_point_separator = array('en','ja','zh','vi');
114 
115  if(in_array($system_lng, $use_comma_seperator))
116  {
117  $gui_price = number_format((float)$a_price, 2, ',', '');
118  }
119  else
120  $gui_price = number_format((float)$a_price, 2, '.', '');
121 
122  return $gui_price;
123  }
124 
125  public static function _formatPriceToString($a_price)
126  {
127  include_once './Services/Payment/classes/class.ilPaymentSettings.php';
128 
130  $currency_unit = $genSet->get('currency_unit');
131 
132  $gui_price = self::_getGUIPrice($a_price);
133 
134  return $gui_price . ' ' . $currency_unit;
135 
136 /* TODO: after currency implementation is finished -> replace whole function
137  * include_once './Services/Payment/classes/class.ilPaymentCurrency.php';
138 
139  $separator= ilPaymentCurrency::_getDecimalSeparator();
140  $currency_symbol = ilPaymentCurrency::_getSymbol($a_currency_id);
141  $price_string = number_format($a_price,'2',$separator,'');
142 
143  return $price_string . ' ' . $currency_symbol;
144  */
145  }
146 
147 
148  public static function _getPriceStringFromAmount($a_price)
149  {
150  include_once './Services/Payment/classes/class.ilPaymentCurrency.php';
151  include_once './Services/Payment/classes/class.ilPaymentSettings.php';
152 
153  global $lng;
154 
156  $currency_unit = $genSet->get("currency_unit");
157 
158  $pr_str = '';
159 
160  $pr_str = number_format($a_price , 2, ",", ".");
161 /* TODO: CURRENCY $pr_str = number_format($a_price * $this->getCurrencyConversionRate() , 2, ",", ".");
162  * remove genset
163  * */
164 
165  return $pr_str . " " . $currency_unit;
166  }
167 
168 
169  public static function _getTotalAmount($a_price_ids)
170  {
171 
172  include_once './Services/Payment/classes/class.ilPaymentPrices.php';
173 # include_once './Services/Payment/classes/class.ilPaymentCurrency.php';
174  include_once './Services/Payment/classes/class.ilPaymentSettings.php';
175 
176  global $ilDB, $lng;
177 
179  $currency_unit = $genSet->get("currency_unit");
180 
181  $amount = array();
182 
183  if (is_array($a_price_ids))
184  {
185  for ($i = 0; $i < count($a_price_ids); $i++)
186  {
187  $price_data = ilPaymentPrices::_getPrice($a_price_ids[$i]["id"]);
188 
189  $price = (float) $price_data["price"];
190  $amount[$a_price_ids[$i]["pay_method"]] += (float) $price;
191  }
192  }
193 /* TODO: CURRENCY replace 'if' & remove genset
194  if (is_array($a_price_ids))
195  {
196  $default_currency = ilPaymentCurrency::_getDefaultcurrency();
197 
198  for ($i = 0; $i < count($a_price_ids); $i++)
199  {
200  $price_data = ilPaymentPrices::_getPrice($a_price_ids[$i]["id"]);
201 
202  if($price_data['currency'] != $default_currency['currency_id'])
203  {
204  $conversion_rate = ilPaymentCurrency::_getConversionRate($price_data['currency']);
205  $price = round(((float) $price_data['price'] * $conversion_rate),2);
206  }
207  else
208  $price = (float) $price_data["price"];
209 
210  $amount[$a_price_ids[$i]["pay_method"]] += (float) $price;
211  }
212  }
213  */
214  return $amount;
215  }
216 
217 
218  public function setPrice($a_price = 0)
219  {
220  $this->price = preg_replace('/,/','.',$a_price);
221  $this->price = (float)$a_price;
222  }
223 
224  public function setCurrency($a_currency_id)
225  {
226  $this->currency = $a_currency_id;
227  }
228  public function setDuration($a_duration)
229  {
230  if($this->unlimited_duration == '1' && ($a_duration == '' || null))
231  $a_duration = 0;
232 
233  $this->duration = (int)$a_duration;
234  }
235 
236  public function setUnlimitedDuration($a_unlimited_duration)
237  {
238  if($a_unlimited_duration)
239  $this->unlimited_duration = (int)$a_unlimited_duration;
240  else
241  $this->unlimited_duration = 0;
242  }
243 
244 
245  public function setExtension($a_extension)
246  {
247  if($this->extension == '1' && ($a_extension == '' || null))
248  $a_extension = 0;
249  $this->extension = $a_extension;
250  }
251 
252  public function getExtension()
253  {
254  return $this->extension;
255  }
256 
257 
258  public function add()
259  {
260  $next_id = $this->db->nextId('payment_prices');
261 
262  $res = $this->db->insert('payment_prices', array(
263  'price_id' => array('integer', $next_id),
264  'pobject_id' => array('integer', $this->getPobjectId()),
265  'currency' => array('integer', $this->__getCurrency()),
266  'duration' => array('integer', $this->__getDuration()),
267  'unlimited_duration'=> array('integer', $this->__getUnlimitedDuration()),
268  'price' => array('float', $this->__getPrice()),
269  'extension' => array('integer', $this->getExtension())
270  ));
271 
272  $this->__read(true);
273  return true;
274  }
275  public function update($a_price_id)
276  {
277  $this->db->update('payment_prices',
278  array( 'pobject_id' => array('integer', $this->getPobjectId()),
279  'currency' => array('integer', $this->__getCurrency()),
280  'duration' => array('integer', $this->__getDuration()),
281  'unlimited_duration'=> array('integer', $this->__getUnlimitedDuration()),
282  'price' => array('float', $this->__getPrice()),
283  'extension' => array('integer', $this->getExtension())),
284  array('price_id'=> array('integer', $a_price_id)));
285 
286  $this->__read(true);
287 
288  return true;
289  }
290  public function delete($a_price_id)
291  {
292  $statement = $this->db->manipulateF('
293  DELETE FROM payment_prices
294  WHERE price_id = %s',
295  array('integer'), array($a_price_id));
296 
297  $this->__read(true);
298 
299  return true;
300  }
301  public function deleteAllPrices()
302  {
303  $statement = $this->db->manipulateF('
304  DELETE FROM payment_prices
305  WHERE pobject_id = %s',
306  array('integer'),
307  array($this->getPobjectId()));
308 
309  $this->__read(true);
310 
311  return true;
312  }
313 
314  public function validate()
315  {
316 
317  $duration_valid = false;
318  $price_valid = false;
319 
320  if(preg_match('/^(([1-9][0-9]{0,1})|[0])?$/',$this->__getDuration())
321  || ((int)$this->__getDuration() == 0 && $this->__getUnlimitedDuration() == 1))
322  {
323  $duration_valid = true;
324  }
325 
326  if(preg_match('/[0-9]/',$this->__getPrice()))
327  {
328 
329  $price_valid = true;
330  }
331 
332  if($duration_valid == true && $price_valid == true)
333  {
334  return true;
335  }
336 
337  else return false;
338 
339  }
340  // STATIC
341  public static function _priceExists($a_price_id,$a_pobject_id)
342  {
343  global $ilDB;
344 
345  $res = $ilDB->queryf('
346  SELECT * FROM payment_prices
347  WHERE price_id = %s
348  AND pobject_id = %s',
349  array('integer', 'integer'),
350  array($a_price_id, $a_pobject_id));
351 
352  return $res->numRows() ? true : false;
353  }
354 
355  // PRIVATE
356 
357  private function __getPrice()
358  {
359  return $this->price;
360  }
361  private function __getCurrency()
362  {
363  /*TODO: CURRENCY not finished yet -> return 1 as default */
364  if($this->currency == null)
365  $this->currency = 1;
366  return $this->currency;
367  }
368  private function __getDuration()
369  {
370  return $this->duration;
371  }
372  private function __getUnlimitedDuration()
373  {
375  }
376 
377  private function __read($with_extension_prices = false)
378  {
379  $this->prices = array();
380 
381  if(!$with_extension_prices)
382  {
383  $res = $this->db->queryf('
384  SELECT * FROM payment_prices
385  WHERE pobject_id = %s
386  AND extension = %s
387  ORDER BY duration',
388  array('integer','integer'),
389  array($this->getPobjectId(), 0));
390  }
391  else
392  {
393  // needed for administration view
394  $res = $this->db->queryf('
395  SELECT * FROM payment_prices
396  WHERE pobject_id = %s
397  ORDER BY duration',
398  array('integer'),
399  array($this->getPobjectId()));
400  }
401 
402  while($row = $this->db->fetchObject($res))
403  {
404  $this->prices[$row->price_id]['pobject_id'] = $row->pobject_id;
405  $this->prices[$row->price_id]['price_id'] = $row->price_id;
406  $this->prices[$row->price_id]['currency'] = $row->currency;
407  $this->prices[$row->price_id]['duration'] = $row->duration;
408  $this->prices[$row->price_id]['unlimited_duration'] = $row->unlimited_duration;
409  $this->prices[$row->price_id]['price'] = $row->price;
410  $this->prices[$row->price_id]['extension'] = $row->extension;
411 //TODO: CURRENCY $this->prices[$row->price_id]['price'] = $row->price * $this->getCurrencyConversionRate();
412  }
413  }
414 
415  public function getExtensionPrices()
416  {
417  $prices = array();
418 
419  $res = $this->db->queryf('
420  SELECT * FROM payment_prices
421  WHERE pobject_id = %s
422  AND extension = %s
423  ORDER BY duration',
424  array('integer','integer'),
425  array($this->getPobjectId(), 1));
426 
427  while($row = $this->db->fetchObject($res))
428  {
429  $prices[$row->price_id]['pobject_id'] = $row->pobject_id;
430  $prices[$row->price_id]['price_id'] = $row->price_id;
431  $prices[$row->price_id]['currency'] = $row->currency;
432  $prices[$row->price_id]['duration'] = $row->duration;
433  $prices[$row->price_id]['unlimited_duration'] = $row->unlimited_duration;
434  $prices[$row->price_id]['price'] = $row->price;
435  $prices[$row->price_id]['extension'] = $row->extension;
436  }
437  return $prices;
438  }
439 
440  public function getNumberOfPrices()
441  {
442  return count($this->prices);
443  }
444 
445  public function getLowestPrice()
446  {
447  $lowest_price_id = 0;
448  $lowest_price = 0;
449 
450  foreach ($this->prices as $price_id => $data)
451  {
452  $current_price = $data['price'];
453 
454  if($lowest_price == 0||
455  $lowest_price > (float)$current_price)
456  {
457  $lowest_price = (float)$current_price;
458  $lowest_price_id = $price_id;
459  }
460  }
461 
462  return is_array($this->prices[$lowest_price_id]) ? $this->prices[$lowest_price_id] : array();
463  }
464 }
465 ?>