ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilPaymentShoppingCart.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
12 include_once './Services/Payment/classes/class.ilPaymentPrices.php';
13 include_once './Services/Payment/classes/class.ilPaymentObject.php';
14 include_once './Services/Payment/classes/class.ilPaymentCoupons.php';
15 
17 {
18  /*
19  * id of vendor, admin or trustee
20  */
21  public $user_obj = null;
22  public $db = null;
23 
24  public $coupon_obj = null;
25 
26  public $sc_entries = array();
27  public $session_id =null;
28 
29 
31  {
32  global $ilDB;
33 
34  $this->user_obj = $user_obj;
35  $this->db = $ilDB;
36  $this->session_id = session_id();
37  $this->coupon_obj = new ilPaymentCoupons($this->user_obj);
38 
39  $this->__read();
40  }
41 
42  public function setSessionId($a_session_id)
43  {
44  $this->session_id = $a_session_id;
45  }
46 
47  public function getSessionId()
48  {
49  return $this->session_id;
50  }
51 
52  public function setPobjectId($a_pobject_id)
53  {
54  $this->pobject_id = $a_pobject_id;
55  }
56  public function getPobjectId()
57  {
58  return $this->pobject_id;
59  }
60  public function setPriceId($a_price_id)
61  {
62  $this->price_id = $a_price_id;
63  }
64  public function getPriceId()
65  {
66  return $this->price_id;
67  }
68 
69  public function getEntries($a_pay_method = 0)
70  {
71 
72  if ($a_pay_method == 0)
73  {
74  return $this->sc_entries ? $this->sc_entries : array();
75  }
76  else
77  {
78  $tmp_entries = array();
79  foreach($this->sc_entries as $entry)
80  {
81  if ($entry['pay_method'] == $a_pay_method)
82  {
83  $tmp_entries[$entry['psc_id']] = $entry;
84  }
85  }
86  return $tmp_entries;
87  }
88  }
89  public function setTotalAmount($a_total_amount)
90  {
91  $this->total_amount = $a_total_amount;
92  }
93  public function getTotalAmount()
94  {
95  return $this->total_amount;
96  }
97 
98  public static function _assignObjectsToUserId($a_user_id)
99  {
100  global $ilDB;
101 
102  $session_id = session_id();
103 
104  $ilDB->update('payment_shopping_cart',
105  array('customer_id' => array('integer', (int) $a_user_id)),
106  array('session_id'=> array('text', $session_id)));
107 
108  return true;
109  }
110 
111  public static function _migrateShoppingcart($a_old_sessid, $a_new_sessid)
112  {
113  global $ilDB;
114 
115  $ilDB->update('payment_shopping_cart',
116  array('session_id' => array('text', $a_new_sessid)),
117  array('session_id'=> array('text', $a_old_sessid)));
118 
119  }
120 
121  public function isInShoppingCart($a_pobject_id)
122  {
123  global $ilUser;
124 
125  $session_id = $this->getSessionId();
126 
127  if(ANONYMOUS_USER_ID == $ilUser->getId())
128  {
129  $res = $this->db->queryf('
130  SELECT * FROM payment_shopping_cart
131  WHERE session_id = %s
132  AND pobject_id = %s',
133  array('text', 'integer'),
134  array($session_id, $a_pobject_id));
135  }
136  else
137  {
138  $res = $this->db->queryf('
139  SELECT * FROM payment_shopping_cart
140  WHERE customer_id = %s
141  AND pobject_id = %s',
142  array('integer', 'integer'),
143  array($this->user_obj->getId(), $a_pobject_id));
144  }
145  return $res->numRows() ? true : false;
146  }
147 
148  public function getEntry($a_pobject_id)
149  {
150  global $ilUser;
151 
152  if(ANONYMOUS_USER_ID == $ilUser->getId())
153  {
154  $res = $this->db->queryf('
155  SELECT * FROM payment_shopping_cart
156  WHERE session_id = %s
157  AND pobject_id = %s',
158  array('text', 'integer'),
159  array($this->getSessionId(), $a_pobject_id));
160  }
161  else
162  {
163  $res = $this->db->queryf('
164  SELECT * FROM payment_shopping_cart
165  WHERE customer_id = %s
166  AND pobject_id = %s',
167  array('integer', 'integer'),
168  array($this->user_obj->getId(), $a_pobject_id));
169  }
170  if (is_object($res))
171  {
172  return $this->db->fetchAssoc($res);
173  }
174  }
175 
176  public function add()
177  {
178  global $ilUser;
179 
180  if(ANONYMOUS_USER_ID == $ilUser->getId())
181  {
182  // Delete old entries for same pobject_id
183  $statement = $this->db->manipulateF('
184  DELETE FROM payment_shopping_cart
185  WHERE session_id = %s
186  AND pobject_id = %s',
187  array('integer', 'integer'),
188  array($this->getSessionId(), $this->getPobjectId()));
189 
190  }
191  else
192  {
193  // Delete old entries for same pobject_id
194  $statement = $this->db->manipulateF('
195  DELETE FROM payment_shopping_cart
196  WHERE customer_id = %s
197  AND pobject_id = %s',
198  array('integer', 'integer'),
199  array($this->user_obj->getId(), $this->getPobjectId()));
200  }
201 
202  $next_id = $this->db->nextId('payment_shopping_cart');
203 
204  $this->db->insert('payment_shopping_cart',
205  array('psc_id' => array('integer', $next_id),
206  'customer_id'=> array('integer', $this->user_obj->getId()),
207  'pobject_id' => array('integer', $this->getPobjectId()),
208  'price_id' => array('integer', $this->getPriceId()),
209  'session_id' => array('text', $this->getSessionId()))
210  );
211 
212  $this->__read();
213 
214  return true;
215  }
216 
217  public function update($a_psc_id)
218  {
219  global $ilUser;
220  if(ANONYMOUS_USER_ID == $ilUser->getId())
221  {
222  $this->db->update('payment_shopping_cart',
223  array('pobject_id'=> array('integer', $this->getPobjectId()),
224  'price_id' => array('integer', $this->getPriceId()),
225  'session_id'=> array('text', $this->getSessionId())),
226  array('psc_id' => array('integer', (int)$a_psc_id)));
227 
228  }
229  else
230  {
231  $this->db->update('payment_shopping_cart',
232  array('customer_id' => array('integer', $this->user_obj->getId()),
233  'pobject_id' => array('integer', $this->getPobjectId()),
234  'price_id' => array('integer', $this->getPriceId()),
235  'session_id' => array('text', $this->getSessionId())),
236  array( 'psc_id' => array('integer', (int)$a_psc_id)));
237  }
238  $this->__read();
239 
240  return true;
241  }
242 
243  public function delete($a_psc_id)
244  {
245  $statement = $this->db->manipulateF('
246  DELETE FROM payment_shopping_cart
247  WHERE psc_id = %s',
248  array('integer'), array($a_psc_id));
249 
250  $this->__read();
251  }
252 
253  public function emptyShoppingCart()
254  {
255  global $ilUser;
256  if(ANONYMOUS_USER_ID == $ilUser->getId())
257  {
258  $statement = $this->db->manipulateF('
259  DELETE FROM payment_shopping_cart
260  WHERE session_id = %s',
261  array('text'), array($this->getSessionId())
262  );
263  }
264  else
265  {
266  $statement = $this->db->manipulateF('
267  DELETE FROM payment_shopping_cart
268  WHERE customer_id = %s',
269  array('integer'), array($this->user_obj->getId())
270  );
271  }
272  $this->__read();
273 
274  return true;
275  }
276 
277  // STATIC
278  public static function _hasEntries($a_user_id)
279  {
280  global $ilDB, $ilUser;
281 
282  if(ANONYMOUS_USER_ID == $ilUser->getId())
283  {
284  $res = $ilDB->queryf('
285  SELECT * FROM payment_shopping_cart
286  WHERE session_id = %s',
287  array('text'), array($this->getSessionId()));
288  }
289  else
290  {
291  $res = $ilDB->queryf('
292  SELECT * FROM payment_shopping_cart
293  WHERE customer_id = %s',
294  array('integer'), array($a_user_id));
295  }
296  return $res->numRows() ? true : false;
297  }
298 
299 
300  // PRIVATE
301  private function __read()
302  {
303  global $ilUser;
304  include_once './Services/Payment/classes/class.ilPaymentPrices.php';
305 
306  $this->sc_entries = array();
307 
308 
309  if(ANONYMOUS_USER_ID == $ilUser->getId())
310  {
311  $res = $this->db->queryf('
312  SELECT * FROM payment_shopping_cart
313  WHERE session_id = %s',
314  array('text'), array($this->getSessionId()));
315  }
316  else
317  {
318  $res = $this->db->queryf('
319  SELECT * FROM payment_shopping_cart
320  WHERE customer_id = %s',
321  array('integer'), array($this->user_obj->getId()));
322  }
323 
324  while($row = $this->db->fetchObject($res))
325  {
326  $this->sc_entries[$row->psc_id]["psc_id"] = $row->psc_id;
327  $this->sc_entries[$row->psc_id]["customer_id"] = $row->customer_id;
328  $this->sc_entries[$row->psc_id]["pobject_id"] = $row->pobject_id;
329  $this->sc_entries[$row->psc_id]["price_id"] = $row->price_id;
330  $this->sc_entries[$row->psc_id]['session_id'] = $row->session_id;
331  }
332 
333  // Delete all entries with not valid prices or pay_method
334  unset($prices);
335  foreach($this->sc_entries as $entry)
336  {
337  // check if price_id exists for pobject
338  if(!ilPaymentPrices::_priceExists($entry['price_id'],$entry['pobject_id']))
339  {
340  $this->delete($entry['psc_id']);
341  return false;
342  }
343 
344  // check pay method
345  $tmp_pobj = new ilPaymentObject($this->user_obj, $entry['pobject_id']);
346 
347  $pay_method = $tmp_pobj->getPayMethod();
348  if($pay_method == $tmp_pobj->PAY_METHOD_NOT_SPECIFIED)
349  {
350  $this->delete($entry['psc_id']);
351  return false;
352  }
353 
354  // if payment is expired
355  if($tmp_pobj->getStatus() == $tmp_pobj->STATUS_EXPIRES)
356  {
357  $this->delete($entry['psc_id']);
358 
359  return false;
360  }
361 
362 
363  $this->sc_entries[$entry['psc_id']]['pay_method'] = $pay_method;
364 
365  $prices[] = array(
366  'id' => $entry['price_id'],
367  'pay_method' => $pay_method
368  );
369  unset($tmp_pobj);
370  }
371 
372  // set total amount
373  $this->setTotalAmount(ilPaymentPrices::_getTotalAmount($prices ? $prices : array()));
374 
375  $this->setPobjectId($entry['pobject_id']);
376 
377  return true;
378  }
379 
380  function getShoppingCart($a_pay_method = 0)
381  {
382 
383  if(!count($items = $this->getEntries($a_pay_method)))
384  {
385  return 0;
386  }
387 
388  $counter = 0;
389  foreach($items as $item)
390  {
391  $tmp_pobject = new ilPaymentObject($this->user_obj,$item['pobject_id']);
392 
393  $tmp_obj = ilObjectFactory::getInstanceByRefId($tmp_pobject->getRefId(), false);
394 
395 
396  $f_result[$counter]["psc_id"] = $item['psc_id'];
397  $f_result[$counter]["pobject_id"] = $item['pobject_id'];
398  if($tmp_obj)
399  {
400  $f_result[$counter]["obj_id"] = $tmp_obj->getId();
401  $f_result[$counter]["type"] = $tmp_obj->getType();
402  $f_result[$counter]["object_title"] = $tmp_obj->getTitle();
403  }
404  else
405  {
406  $f_result[$counter]["obj_id"] = '';
407  $f_result[$counter]["type"] = '';
408  $f_result[$counter]["object_title"] = $this->lng->txt('object_not_found');
409  }
410  $price_data = ilPaymentPrices::_getPrice($item['price_id']);
411  $price_string = ilPaymentPrices::_getPriceString($item['price_id']);
412 
413  $price = (float)$price_data['price'];
414 
415  $f_result[$counter]["price"] = $price;
416  $f_result[$counter]["price_string"] = $price_string;
417 
418  require_once './Services/Payment/classes/class.ilShopVats.php';
419  $oVAT = new ilShopVats((int)$tmp_pobject->getVatId());
420  $f_result[$counter]['vat_rate'] = $oVAT->getRate();
421  $f_result[$counter]['vat_unit'] = $tmp_pobject->getVat($price);
422 
423  $f_result[$counter]["duration"] = $price_data["duration"];
424  $f_result[$counter]['unlimited_duration'] = $price_data['unlimited_duration'];
425 
426  unset($tmp_obj);
427  unset($tmp_pobject);
428 
429  ++$counter;
430  }
431  return $f_result;
432  }
433 
434  function getTotalAmountValue($a_pay_method = 0)
435  {
436  $amount = 0.0;
437 
438  if (is_array($result = $this->getShoppingCart($a_pay_method)))
439  {
440  for ($i = 0; $i < count($result); $i++)
441  {
442  $amount += $result[$i]["price"];
443  }
444  }
445  return (float) $amount;
446  }
447 
448  function getVat($a_amount = 0, $a_pobject_id = 0)
449  {
450  global $ilDB;
451 
452  include_once './Services/Payment/classes/class.ilShopVats.php';
453 
454  $res = $ilDB->queryF('
455  SELECT * FROM payment_objects WHERE pobject_id = %s',
456  array('integer'), array($a_pobject_id));
457 
458  while($row = $ilDB->fetchObject($res))
459  {
460  $this->vat_id = $row->vat_id;
461  }
462 
463  $res = $ilDB->queryF('
464  SELECT * FROM payment_vats WHERE vat_id = %s',
465  array('integer'),array($this->vat_id));
466 
467  while($row = $ilDB->fetchObject($res))
468  {
469  $this->vat_rate = $row->vat_rate;
470  }
471  return (float) ($a_amount - (round(($a_amount / (1 + ($this->vat_rate / 100.0))) * 100) / 100));
472  }
473 
475  {
476  if (!empty($_SESSION['coupons']))
477  {
478  foreach ($_SESSION['coupons'] as $payment_type => $coupons_array)
479  {
480  if (is_array($coupons_array))
481  {
482  foreach ($coupons_array as $coupon_key => $coupon)
483  {
484  $_SESSION['coupons'][$payment_type][$coupon_key]['total_objects_coupon_price'] = 0.0;
485  $_SESSION['coupons'][$payment_type][$coupon_key]['items'] = array();
486  }
487  }
488  }
489  }
490  }
491 
492  function calcDiscountPrices($coupons)
493  {
494  if (is_array($coupons))
495  {
496  $r_items = array();
497 
498  foreach ($coupons as $coupon)
499  {
500  $this->coupon_obj->setId($coupon['pc_pk']);
501  $this->coupon_obj->setCurrentCoupon($coupon);
502 
503  if (is_array($coupon['items']) && $coupon['total_objects_coupon_price'] > 0)
504  {
505  $bonus = ($this->coupon_obj->getCouponBonus($coupon['total_objects_coupon_price']));
506 
507  foreach ($coupon['items'] as $item)
508  {
509  if (!array_key_exists($item['pobject_id'], $r_items))
510  {
511  $r_items[$item['pobject_id']] = $item;
512  $r_items[$item['pobject_id']]['discount_price'] = (float) $item['math_price'];
513  }
514 
515  $ratio = (float) $item['math_price'] / $coupon['total_objects_coupon_price'];
516  $r_items[$item['pobject_id']]['discount_price'] += ($ratio * $bonus * (-1));
517  }
518  }
519  }
520 
521  return $r_items;
522  }
523 
524  return array();
525  }
526 
527  public static function _deleteExpiredSessionsPSC()
528  {
529  global $ilDB;
530 
531  $query = "DELETE FROM payment_shopping_cart "
532  . "WHERE psc_id IN "
533  . " (SELECT psc_id FROM payment_shopping_cart "
534  . " LEFT JOIN usr_session ON usr_session.session_id = payment_shopping_cart.session_id "
535  . " WHERE customer_id = %s AND usr_session.session_id IS NULL)";
536  $ilDB->manipulateF($query, array('integer'), array(ANONYMOUS_USER_ID));
537 
538  }
539 
540 }
541 ?>