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