ILIAS  Release_4_4_x_branch Revision 61816
 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 
16  const TYPE_DURATION_DATE = 2;
18 
19  private $pobject_id;
20 
21  private $price;
22  private $currency;
23  private $duration = 0;
24  private $unlimited_duration = 0;
25  private $extension = 0;
26 
27  private $duration_from;
28  private $duration_until;
29  private $description;
31 
32 
33  // TODO later -> this is for using different currencies
34 // private $currency_conversion_rate = 1;
35 
36  private $prices = array();
37 
38  public function __construct($a_pobject_id = 0)
39  {
40  global $ilDB;
41 
42  $this->db = $ilDB;
43 
44  $this->pobject_id = $a_pobject_id;
45 
46  $this->__read();
47  }
48 
49  public function setPriceType($a_price_type)
50  {
51  $this->price_type = $a_price_type;
52 
53  return $this;
54  }
55 
56  public function getPriceType()
57  {
58  return $this->price_type;
59  }
60 
61 //
62  // SET GET
63  public function getPobjectId()
64  {
65  return $this->pobject_id;
66  }
67 
68  public function getPrices()
69  {
70  return $this->prices ? $this->prices : array();
71  }
72  function getPrice($a_price_id)
73  {
74  return $this->prices[$a_price_id] ? $this->prices[$a_price_id] : array();
75  }
76 
77 
78  // STATIC
79  public static function _getPrice($a_price_id)
80  {
81  global $ilDB;
82 
83  $res = $ilDB->queryf('
84  SELECT * FROM payment_prices
85  WHERE price_id = %s',
86  array('integer'), array($a_price_id));
87 
88  while($row = $ilDB->fetchObject($res))
89  {
90  $price['duration'] = $row->duration;
91  $price['duration_from'] = $row->duration_from;
92  $price['duration_until'] = $row->duration_until;
93  $price['description'] = $row->description;
94  $price['unlimited_duration'] = $row->unlimited_duration;
95  $price['currency'] = $row->currency;
96  $price['price'] = number_format($row->price, 2, '.', '');
97  $price['extension'] = $row->extension;
98  $price['price_type'] = $row->price_type;
99  }
100 
101  return count($price) ? $price : array();
102  }
103 
104  public static function _countPrices($a_pobject_id)
105  {
106  global $ilDB;
107 
108  $res = $ilDB->queryf('
109  SELECT count(price_id) FROM payment_prices
110  WHERE pobject_id = %s',
111  array('integer'),
112  array($a_pobject_id));
113 
114  $row = $ilDB->fetchAssoc($res);
115 
116  return ($row[0]);
117  }
118 
119  public static function _getPriceString($a_price_id)
120  {
121  $price = ilPaymentPrices::_getPrice($a_price_id);
122  $gui_price = self::_getGUIPrice($price['price']);
123 
124  return $gui_price;
125  }
126 
127  public static function _getGUIPrice($a_price)
128  {
129  global $lng;
130 
131  $system_lng = $lng->getDefaultLanguage();
132 
133  // CODES: ISO 639
134  $use_comma_seperator = array('ar','bg','cs','de','da','es','et','it',
135  'fr','nl','el','sr','uk','ru','ro','tr','pl','lt','pt','sq','hu');
136 
137 // $use_point_separator = array('en','ja','zh','vi');
138 
139  if(in_array($system_lng, $use_comma_seperator))
140  {
141  $gui_price = number_format((float)$a_price, 2, ',', '');
142  }
143  else
144  $gui_price = number_format((float)$a_price, 2, '.', '');
145 
146  return $gui_price;
147  }
148 
149  public static function _formatPriceToString($a_price)
150  {
151  include_once './Services/Payment/classes/class.ilPaymentSettings.php';
152 
154  $currency_unit = $genSet->get('currency_unit');
155 
156  $gui_price = self::_getGUIPrice($a_price);
157 
158  return $gui_price . ' ' . $currency_unit;
159 
160 /* TODO: after currency implementation is finished -> replace whole function
161  * include_once './Services/Payment/classes/class.ilPaymentCurrency.php';
162 
163  $separator= ilPaymentCurrency::_getDecimalSeparator();
164  $currency_symbol = ilPaymentCurrency::_getSymbol($a_currency_id);
165  $price_string = number_format($a_price,'2',$separator,'');
166 
167  return $price_string . ' ' . $currency_symbol;
168  */
169  }
170 
171 
172  public static function _getPriceStringFromAmount($a_price)
173  {
174  include_once './Services/Payment/classes/class.ilPaymentCurrency.php';
175  include_once './Services/Payment/classes/class.ilPaymentSettings.php';
176 
178  $currency_unit = $genSet->get("currency_unit");
179 
180  $pr_str = '';
181  $pr_str .= number_format($a_price , 2, ",", ".");
182 /* TODO: CURRENCY $pr_str = number_format($a_price * $this->getCurrencyConversionRate() , 2, ",", ".");
183  * remove genset
184  * */
185 
186  return $pr_str . " " . $currency_unit;
187  }
188 
189 
190  public static function _getTotalAmount($a_price_ids)
191  {
192  include_once './Services/Payment/classes/class.ilPaymentPrices.php';
193 
194  $amount = array();
195 
196  if (is_array($a_price_ids))
197  {
198  for ($i = 0; $i < count($a_price_ids); $i++)
199  {
200  $price_data = ilPaymentPrices::_getPrice($a_price_ids[$i]["id"]);
201 
202  $price = (float) $price_data["price"];
203  $amount[$a_price_ids[$i]["pay_method"]] += (float) $price;
204  }
205  }
206  return $amount;
207  }
208 
209 
210  public function setPrice($a_price = 0)
211  {
212  $this->price = preg_replace('/,/','.',$a_price);
213  $this->price = (float)$a_price;
214  }
215 
216  public function setCurrency($a_currency_id)
217  {
218  $this->currency = $a_currency_id;
219  }
220  public function setDuration($a_duration)
221  {
222  if($this->unlimited_duration == '1' && ($a_duration == '' || null))
223  $a_duration = 0;
224 
225  $this->duration = (int)$a_duration;
226  }
227 
228  public function setUnlimitedDuration($a_unlimited_duration)
229  {
230  if($a_unlimited_duration)
231  $this->unlimited_duration = (int)$a_unlimited_duration;
232  else
233  $this->unlimited_duration = 0;
234  }
235 
236 
237  public function setExtension($a_extension)
238  {
239  $this->extension = (int)$a_extension;
240  }
241 
242  public function getExtension()
243  {
244  return $this->extension;
245  }
246 
247 
248  public function add()
249  {
250  $next_id = $this->db->nextId('payment_prices');
251 
252  $res = $this->db->insert('payment_prices', array(
253  'price_id' => array('integer', $next_id),
254  'pobject_id' => array('integer', $this->getPobjectId()),
255  'currency' => array('integer', $this->__getCurrency()),
256  'duration' => array('integer', $this->__getDuration()),
257  'unlimited_duration'=> array('integer', $this->__getUnlimitedDuration()),
258  'price' => array('float', $this->__getPrice()),
259  'extension' => array('integer', $this->getExtension()),
260  'duration_from' => array('date', $this->__getDurationFrom()),
261  'duration_until' => array('date', $this->__getDurationUntil()),
262  'description' => array('text', $this->__getDescription()),
263  'price_type' => array('integer', $this->getPriceType())
264  ));
265 
266  $this->__read(true);
267  return true;
268  }
269  public function update($a_price_id)
270  {
271  $this->db->update('payment_prices',
272  array( 'pobject_id' => array('integer', $this->getPobjectId()),
273  'currency' => array('integer', $this->__getCurrency()),
274  'duration' => array('integer', $this->__getDuration()),
275  'unlimited_duration'=> array('integer', $this->__getUnlimitedDuration()),
276  'price' => array('float', $this->__getPrice()),
277  'extension' => array('integer', $this->getExtension()),
278  'duration_from' => array('date', $this->__getDurationFrom()),
279  'duration_until' => array('date', $this->__getDurationUntil()),
280  'description' => array('text', $this->__getDescription()),
281  'price_type' => array('integer', $this->getPriceType())
282  ),
283  array('price_id'=> array('integer', $a_price_id)));
284 
285  $this->__read(true);
286 
287  return true;
288  }
289  public function delete($a_price_id)
290  {
291  $statement = $this->db->manipulateF('
292  DELETE FROM payment_prices
293  WHERE price_id = %s',
294  array('integer'), array($a_price_id));
295 
296  $this->__read(true);
297 
298  return true;
299  }
300  public function deleteAllPrices()
301  {
302  $statement = $this->db->manipulateF('
303  DELETE FROM payment_prices
304  WHERE pobject_id = %s',
305  array('integer'),
306  array($this->getPobjectId()));
307 
308  $this->__read(true);
309 
310  return true;
311  }
312 
319  function validate()
320  {
321  global $lng;
322 
323  include_once 'Services/Payment/exceptions/class.ilShopException.php';
324 
325  switch($this->getPriceType())
326  {
327  case self::TYPE_DURATION_MONTH:
328  if(!preg_match('/^[1-9][0-9]{0,1}$/', $this->__getDuration()))
329  throw new ilShopException($lng->txt('paya_price_not_valid'));
330  break;
331 
332  case self::TYPE_DURATION_DATE:
333  if(!preg_match('/^[0-9]{4,4}-[0-9]{1,2}-[0-9]{1,2}$/', $this->__getDurationFrom()))
334  throw new ilShopException($lng->txt('payment_price_invalid_date_from'));
335 
336  $from_date = explode('-', $this->__getDurationFrom());
337  if(!checkdate($from_date[1], $from_date[2], $from_date[0]))
338  throw new ilShopException($lng->txt('payment_price_invalid_date_from'));
339 
340  if(!preg_match('/^[0-9]{4,4}-[0-9]{1,2}-[0-9]{1,2}$/', $this->__getDurationUntil()))
341  throw new ilShopException($lng->txt('payment_price_invalid_date_until'));
342 
343  $till_date = explode('-', $this->__getDurationUntil());
344  if(!checkdate($till_date[1], $till_date[2], $till_date[0]))
345  throw new ilShopException($lng->txt('payment_price_invalid_date_until'));
346 
347  $from = mktime(12, 12, 12, $from_date[1], $from_date[2], $from_date[0]);
348  $till = mktime(12, 12, 12, $till_date[1], $till_date[2], $till_date[0]);
349 
350  if($from >= $till)
351  throw new ilShopException($lng->txt('payment_price_invalid_date_from_gt_until'));
352  break;
353 
354  case self::TYPE_UNLIMITED_DURATION:
355  return true;
356  break;
357 
358  default:
359  throw new ilShopException($lng->txt('payment_no_price_type_selected_sdf'));
360  break;
361  }
362 
363  if(preg_match('/[0-9]/',$this->__getPrice()))
364  {
365  return true;
366  }
367  throw new ilShopException($lng->txt('payment_price_invalid_price'));
368  }
369 
370  // STATIC
371  public static function _priceExists($a_price_id,$a_pobject_id)
372  {
373  global $ilDB;
374 
375  $res = $ilDB->queryf('
376  SELECT * FROM payment_prices
377  WHERE price_id = %s
378  AND pobject_id = %s',
379  array('integer', 'integer'),
380  array($a_price_id, $a_pobject_id));
381 
382  return $res->numRows() ? true : false;
383  }
384 
385  // PRIVATE
386 
387  private function __getPrice()
388  {
389  return $this->price;
390  }
391  private function __getCurrency()
392  {
393  /*TODO: CURRENCY not finished yet -> return 1 as default */
394  if($this->currency == null)
395  $this->currency = 1;
396  return $this->currency;
397  }
398  private function __getDuration()
399  {
400  return $this->duration;
401  }
402  private function __getUnlimitedDuration()
403  {
405  }
406 
407  private function __read($with_extension_prices = false)
408  {
409  $this->prices = array();
410 
411  if(!$with_extension_prices)
412  {
413  $res = $this->db->queryf('
414  SELECT * FROM payment_prices
415  WHERE pobject_id = %s
416  AND extension = %s
417  ORDER BY duration',
418  array('integer','integer'),
419  array($this->getPobjectId(), 0));
420  }
421  else
422  {
423  // needed for administration view
424  $res = $this->db->queryf('
425  SELECT * FROM payment_prices
426  WHERE pobject_id = %s
427  ORDER BY duration',
428  array('integer'),
429  array($this->getPobjectId()));
430  }
431 
432  while($row = $this->db->fetchObject($res))
433  {
434  $this->prices[$row->price_id]['pobject_id'] = $row->pobject_id;
435  $this->prices[$row->price_id]['price_id'] = $row->price_id;
436  $this->prices[$row->price_id]['currency'] = $row->currency;
437  $this->prices[$row->price_id]['duration'] = $row->duration;
438  $this->prices[$row->price_id]['unlimited_duration'] = $row->unlimited_duration;
439  $this->prices[$row->price_id]['price'] = $row->price;
440  $this->prices[$row->price_id]['extension'] = $row->extension;
441  $this->prices[$row->price_id]['duration_from'] = $row->duration_from;
442  $this->prices[$row->price_id]['duration_until'] = $row->duration_until;
443  $this->prices[$row->price_id]['description'] = $row->description;
444  $this->prices[$row->price_id]['price_type'] = $row->price_type;
445  }
446  }
447 
448  public function getExtensionPrices()
449  {
450  $prices = array();
451 
452  $res = $this->db->queryf('
453  SELECT * FROM payment_prices
454  WHERE pobject_id = %s
455  AND extension = %s
456  ORDER BY duration',
457  array('integer','integer'),
458  array($this->getPobjectId(), 1));
459 
460  while($row = $this->db->fetchObject($res))
461  {
462  $prices[$row->price_id]['pobject_id'] = $row->pobject_id;
463  $prices[$row->price_id]['price_id'] = $row->price_id;
464  $prices[$row->price_id]['currency'] = $row->currency;
465  $prices[$row->price_id]['duration'] = $row->duration;
466  $prices[$row->price_id]['unlimited_duration'] = $row->unlimited_duration;
467  $prices[$row->price_id]['price'] = $row->price;
468  $prices[$row->price_id]['extension'] = $row->extension;
469  $prices[$row->price_id]['duration_from'] = $row->duration_from;
470  $prices[$row->price_id]['duration_until'] = $row->duration_until;
471  $prices[$row->price_id]['description'] = $row->description;
472  $prices[$row->price_id]['price_type'] = $row->price_type;
473  }
474  return $prices;
475  }
476 
477  public function getNumberOfPrices()
478  {
479  return count($this->prices);
480  }
481 
482  public function getLowestPrice()
483  {
484  $lowest_price_id = 0;
485  $lowest_price = 0;
486 
487  foreach ($this->prices as $price_id => $data)
488  {
489  $current_price = $data['price'];
490 
491  if($lowest_price == 0||
492  $lowest_price > (float)$current_price)
493  {
494  $lowest_price = (float)$current_price;
495  $lowest_price_id = $price_id;
496  }
497  }
498 
499  return is_array($this->prices[$lowest_price_id]) ? $this->prices[$lowest_price_id] : array();
500  }
501 
502  public function setDurationFrom($a_duration_from)
503  {
504  // $a_duration_from = "dd.mm.YYYY HH:ii:ss"
505  $this->duration_from = $a_duration_from;
506  }
507 
508  public function setDurationUntil($a_duration_until)
509  {
510  $this->duration_until = $a_duration_until;
511  }
512 
513  public function setDescription($a_description)
514  {
515  $this->description = $a_description;
516  }
517 
518  private function __getDurationFrom()
519  {
520  return $this->duration_from;
521  }
522 
523  private function __getDurationUntil()
524  {
525  return $this->duration_until;
526  }
527  private function __getDescription()
528  {
529  return $this->description;
530  }
531 }
532 ?>