ILIAS  release_4-3 Revision
 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  public $pobject_id = null;
30  public $price_id = null;
31  public $total_amount = null;
32  public $vat_id = null;
33  public $vat_rate = 0;
34 
35 
36 
37  public function __construct($user_obj)
38  {
39  global $ilDB;
40 
41  $this->user_obj = $user_obj;
42  $this->db = $ilDB;
43  $this->session_id = session_id();
44  $this->coupon_obj = new ilPaymentCoupons($this->user_obj);
45 
46  $this->__deleteDoubleEntries();
47  $this->__read();
48  }
49 
50  public function setSessionId($a_session_id)
51  {
52  $this->session_id = $a_session_id;
53  }
54 
55  public function getSessionId()
56  {
57  return $this->session_id;
58  }
59 
60  public function setPobjectId($a_pobject_id)
61  {
62  $this->pobject_id = $a_pobject_id;
63  }
64  public function getPobjectId()
65  {
66  return $this->pobject_id;
67  }
68  public function setPriceId($a_price_id)
69  {
70  $this->price_id = $a_price_id;
71  }
72  public function getPriceId()
73  {
74  return $this->price_id;
75  }
76 
77  public function getEntries($a_pay_method = 0)
78  {
79  if ($a_pay_method == 0)
80  {
81  return $this->sc_entries ? $this->sc_entries : array();
82  }
83  else
84  {
85  $tmp_entries = array();
86  foreach($this->sc_entries as $entry)
87  {
88  if ($entry['pay_method'] == $a_pay_method)
89  {
90  $tmp_entries[$entry['psc_id']] = $entry;
91  }
92  }
93  return $tmp_entries;
94  }
95  }
96  public function setTotalAmount($a_total_amount)
97  {
98  $this->total_amount = $a_total_amount;
99  }
100  public function getTotalAmount()
101  {
102  return $this->total_amount;
103  }
104 
105  public static function _assignObjectsToUserId($a_user_id)
106  {
107  global $ilDB;
108 
109  $session_id = session_id();
110 
111  $ilDB->update('payment_shopping_cart',
112  array('customer_id' => array('integer', (int) $a_user_id)),
113  array('session_id'=> array('text', $session_id)));
114 
115  return true;
116  }
117 
118  public static function _migrateShoppingcart($a_old_sessid, $a_new_sessid)
119  {
120  global $ilDB;
121 
122  $ilDB->update('payment_shopping_cart',
123  array('session_id' => array('text', $a_new_sessid)),
124  array('session_id'=> array('text', $a_old_sessid)));
125 
126  }
127 
128  public function isInShoppingCart($a_pobject_id)
129  {
130  global $ilUser;
131 
132  $session_id = $this->getSessionId();
133 
134  if(ANONYMOUS_USER_ID == $ilUser->getId())
135  {
136  $res = $this->db->queryf('
137  SELECT * FROM payment_shopping_cart
138  WHERE session_id = %s
139  AND pobject_id = %s',
140  array('text', 'integer'),
141  array($session_id, $a_pobject_id));
142  }
143  else
144  {
145  $res = $this->db->queryf('
146  SELECT * FROM payment_shopping_cart
147  WHERE customer_id = %s
148  AND pobject_id = %s',
149  array('integer', 'integer'),
150  array($this->user_obj->getId(), $a_pobject_id));
151  }
152  return $res->numRows() ? true : false;
153  }
154 
155  public function getEntry($a_pobject_id)
156  {
157  global $ilUser;
158 
159  if(ANONYMOUS_USER_ID == $ilUser->getId())
160  {
161  $res = $this->db->queryf('
162  SELECT * FROM payment_shopping_cart
163  WHERE session_id = %s
164  AND pobject_id = %s',
165  array('text', 'integer'),
166  array($this->getSessionId(), $a_pobject_id));
167  }
168  else
169  {
170  $res = $this->db->queryf('
171  SELECT * FROM payment_shopping_cart
172  WHERE customer_id = %s
173  AND pobject_id = %s',
174  array('integer', 'integer'),
175  array($this->user_obj->getId(), $a_pobject_id));
176  }
177  if (is_object($res))
178  {
179  return $this->db->fetchAssoc($res);
180  }
181  return array();
182  }
183 
184  public function add()
185  {
186  global $ilUser;
187 
188  if(ANONYMOUS_USER_ID == $ilUser->getId())
189  {
190  // Delete old entries for same pobject_id
191  $statement = $this->db->manipulateF('
192  DELETE FROM payment_shopping_cart
193  WHERE session_id = %s
194  AND pobject_id = %s',
195  array('integer', 'integer'),
196  array($this->getSessionId(), $this->getPobjectId()));
197 
198  }
199  else
200  {
201  // Delete old entries for same pobject_id
202  $statement = $this->db->manipulateF('
203  DELETE FROM payment_shopping_cart
204  WHERE customer_id = %s
205  AND pobject_id = %s',
206  array('integer', 'integer'),
207  array($this->user_obj->getId(), $this->getPobjectId()));
208  }
209 
210  $next_id = $this->db->nextId('payment_shopping_cart');
211 
212  $this->db->insert('payment_shopping_cart',
213  array('psc_id' => array('integer', $next_id),
214  'customer_id'=> array('integer', $this->user_obj->getId()),
215  'pobject_id' => array('integer', $this->getPobjectId()),
216  'price_id' => array('integer', $this->getPriceId()),
217  'session_id' => array('text', $this->getSessionId()))
218  );
219 
220  $this->__read();
221 
222  return true;
223  }
224 
225  public function update($a_psc_id)
226  {
227  global $ilUser;
228  if(ANONYMOUS_USER_ID == $ilUser->getId())
229  {
230  $this->db->update('payment_shopping_cart',
231  array('pobject_id'=> array('integer', $this->getPobjectId()),
232  'price_id' => array('integer', $this->getPriceId()),
233  'session_id'=> array('text', $this->getSessionId())),
234  array('psc_id' => array('integer', (int)$a_psc_id)));
235 
236  }
237  else
238  {
239  $this->db->update('payment_shopping_cart',
240  array('customer_id' => array('integer', $this->user_obj->getId()),
241  'pobject_id' => array('integer', $this->getPobjectId()),
242  'price_id' => array('integer', $this->getPriceId()),
243  'session_id' => array('text', $this->getSessionId())),
244  array( 'psc_id' => array('integer', (int)$a_psc_id)));
245  }
246  $this->__read();
247 
248  return true;
249  }
250 
251  public function delete($a_psc_id)
252  {
253  $statement = $this->db->manipulateF('
254  DELETE FROM payment_shopping_cart
255  WHERE psc_id = %s',
256  array('integer'), array($a_psc_id));
257 
258  $this->__read();
259  }
260 
261  public function emptyShoppingCart()
262  {
263  global $ilUser;
264  if(ANONYMOUS_USER_ID == $ilUser->getId())
265  {
266  $statement = $this->db->manipulateF('
267  DELETE FROM payment_shopping_cart
268  WHERE session_id = %s',
269  array('text'), array($this->getSessionId())
270  );
271  }
272  else
273  {
274  $statement = $this->db->manipulateF('
275  DELETE FROM payment_shopping_cart
276  WHERE customer_id = %s',
277  array('integer'), array($this->user_obj->getId())
278  );
279  }
280  $this->__read();
281 
282  return true;
283  }
284 
285  // STATIC
286  public static function _hasEntries($a_user_id)
287  {
288  global $ilDB, $ilUser;
289 
290  if(ANONYMOUS_USER_ID == $ilUser->getId())
291  {
292  $res = $ilDB->queryf('
293  SELECT * FROM payment_shopping_cart
294  WHERE session_id = %s',
295  array('text'), array(self::getSessionId()));
296  }
297  else
298  {
299  $res = $ilDB->queryf('
300  SELECT * FROM payment_shopping_cart
301  WHERE customer_id = %s',
302  array('integer'), array($a_user_id));
303  }
304 
305  return $ilDB->numRows($res) ? true : false;
306  }
307 
308 
309  // PRIVATE
310  private function __deleteDoubleEntries()
311  {
312  global $ilUser;
313  if(ANONYMOUS_USER_ID != $ilUser->getId())
314  {
315  $res = $this->db->queryf('
316  SELECT pobject_id, count(pobject_id) count FROM payment_shopping_cart
317  WHERE customer_id = %s
318  GROUP BY pobject_id',
319  array('integer'), array($this->user_obj->getId()));
320 
321  while($row = $this->db->fetchAssoc($res))
322  {
323  if($row['count'] > 1)
324  {
325  $this->db->setLimit(1);
326  $this->db->manipulateF('
327  DELETE FROM payment_shopping_cart
328  WHERE customer_id = %s
329  AND pobject_id = %s',
330  array('integer','integer'),
331  array($this->user_obj->getId(),$row['pobject_id']));
332  }
333  }
334  }
335  }
336 
337 
338  private function __read()
339  {
340  global $ilUser;
341  include_once './Services/Payment/classes/class.ilPaymentPrices.php';
342 
343  $this->sc_entries = array();
344 
345 
346  if(ANONYMOUS_USER_ID == $ilUser->getId())
347  {
348  $res = $this->db->queryf('
349  SELECT * FROM payment_shopping_cart
350  WHERE session_id = %s',
351  array('text'), array($this->getSessionId()));
352  }
353  else
354  {
355  $res = $this->db->queryf('
356  SELECT * FROM payment_shopping_cart
357  WHERE customer_id = %s',
358  array('integer'), array($this->user_obj->getId()));
359  }
360 
361  while($row = $this->db->fetchObject($res))
362  {
363  $this->sc_entries[$row->psc_id]["psc_id"] = $row->psc_id;
364  $this->sc_entries[$row->psc_id]["customer_id"] = $row->customer_id;
365  $this->sc_entries[$row->psc_id]["pobject_id"] = $row->pobject_id;
366  $this->sc_entries[$row->psc_id]["price_id"] = $row->price_id;
367  $this->sc_entries[$row->psc_id]['session_id'] = $row->session_id;
368  }
369 
370  // Delete all entries with not valid prices or pay_method
371  unset($prices);
372  $prices = array();
373  foreach($this->sc_entries as $entry)
374  {
375  // check if price_id exists for pobject
376  if(!ilPaymentPrices::_priceExists($entry['price_id'],$entry['pobject_id']))
377  {
378  $this->delete($entry['psc_id']);
379  return false;
380  }
381 
382  // check pay method
383  $tmp_pobj = new ilPaymentObject($this->user_obj, $entry['pobject_id']);
384 
385  $pay_method = $tmp_pobj->getPayMethod();
386  if($pay_method == $tmp_pobj->PAY_METHOD_NOT_SPECIFIED)
387  {
388  $this->delete($entry['psc_id']);
389  return false;
390  }
391 
392  // if payment is expired
393  if($tmp_pobj->getStatus() == $tmp_pobj->STATUS_EXPIRES)
394  {
395  $this->delete($entry['psc_id']);
396 
397  return false;
398  }
399 
400 
401  $this->sc_entries[$entry['psc_id']]['pay_method'] = $pay_method;
402 
403  $prices[] = array(
404  'id' => $entry['price_id'],
405  'pay_method' => $pay_method
406  );
407  unset($tmp_pobj);
408  }
409 
410  // set total amount
411  $this->setTotalAmount(ilPaymentPrices::_getTotalAmount($prices ? $prices : array()));
412 
413  $this->setPobjectId($entry['pobject_id']);
414 
415  return true;
416  }
417 
418  function getShoppingCart($a_pay_method = 0)
419  {
420 
421  if(!count($items = $this->getEntries($a_pay_method)))
422  {
423  return 0;
424  }
425 
426  $counter = 0;
427  $f_result = array();
428  foreach($items as $item)
429  {
430  $tmp_pobject = new ilPaymentObject($this->user_obj,$item['pobject_id']);
431 
432  $tmp_obj = ilObjectFactory::getInstanceByRefId($tmp_pobject->getRefId(), false);
433 
434  $f_result[$counter]["psc_id"] = $item['psc_id'];
435  $f_result[$counter]["pobject_id"] = $item['pobject_id'];
436  if($tmp_obj)
437  {
438  $f_result[$counter]["obj_id"] = $tmp_obj->getId();
439  $f_result[$counter]["type"] = $tmp_obj->getType();
440  $f_result[$counter]["object_title"] = $tmp_obj->getTitle();
441  }
442  else
443  {
444  global $lng;
445  $f_result[$counter]["obj_id"] = '';
446  $f_result[$counter]["type"] = '';
447  $f_result[$counter]["object_title"] = $lng->txt('object_deleted');
448  }
449 
450  $price_data = ilPaymentPrices::_getPrice($item['price_id']);
451  $price_string = ilPaymentPrices::_getPriceString($item['price_id']);
452 
453  $price = number_format($price_data['price'], 2, '.', '');
454 
455  $f_result[$counter]["price"] = $price;
456  $f_result[$counter]["price_string"] = $price_string;
457  $f_result[$counter]['extension'] = $price_data['extension'];
458 
459  require_once './Services/Payment/classes/class.ilShopVats.php';
460  $oVAT = new ilShopVats((int)$tmp_pobject->getVatId());
461  $f_result[$counter]['vat_rate'] = $oVAT->getRate();
462  $f_result[$counter]['vat_unit'] = $tmp_pobject->getVat($price);
463 
464  $f_result[$counter]["duration"] = $price_data["duration"];
465  $f_result[$counter]['unlimited_duration'] = $price_data['unlimited_duration'];
466 
467  $f_result[$counter]["price_type"] = $price_data["price_type"];
468  $f_result[$counter]["duration_from"] = $price_data["duration_from"];
469  $f_result[$counter]["duration_until"] = $price_data["duration_until"];
470  $f_result[$counter]["description"] = $price_data["description"];
471  unset($tmp_obj);
472  unset($tmp_pobject);
473 
474  ++$counter;
475  }
476  return $f_result;
477  }
478 
479  function getTotalAmountValue($a_pay_method = 0)
480  {
481  $amount = 0.0;
482 
483  if (is_array($result = $this->getShoppingCart($a_pay_method)))
484  {
485  for ($i = 0; $i < count($result); $i++)
486  {
487  $amount += $result[$i]["price"];
488  }
489  }
490  return (float) $amount;
491  }
492 
493  function getVat($a_amount = 0, $a_pobject_id = 0)
494  {
495  global $ilDB;
496 
497  include_once './Services/Payment/classes/class.ilShopVats.php';
498 
499  $res = $ilDB->queryF('
500  SELECT * FROM payment_objects WHERE pobject_id = %s',
501  array('integer'), array($a_pobject_id));
502 
503  while($row = $ilDB->fetchObject($res))
504  {
505  $this->vat_id = $row->vat_id;
506  }
507 
508  $res = $ilDB->queryF('
509  SELECT * FROM payment_vats WHERE vat_id = %s',
510  array('integer'),array($this->vat_id));
511 
512  while($row = $ilDB->fetchObject($res))
513  {
514  $this->vat_rate = $row->vat_rate;
515  }
516  return (float) ($a_amount - (round(($a_amount / (1 + ($this->vat_rate / 100.0))) * 100) / 100));
517  }
518 
520  {
521  if (!empty($_SESSION['coupons']))
522  {
523  foreach ($_SESSION['coupons'] as $payment_type => $coupons_array)
524  {
525  if (is_array($coupons_array))
526  {
527  foreach ($coupons_array as $coupon_key => $coupon)
528  {
529  $_SESSION['coupons'][$payment_type][$coupon_key]['total_objects_coupon_price'] = 0.0;
530  $_SESSION['coupons'][$payment_type][$coupon_key]['items'] = array();
531  }
532  }
533  }
534  }
535  }
536 
537  function calcDiscountPrices($coupons)
538  {
539  if (is_array($coupons))
540  {
541  $r_items = array();
542 
543  foreach ($coupons as $coupon)
544  {
545  $this->coupon_obj->setId($coupon['pc_pk']);
546  $this->coupon_obj->setCurrentCoupon($coupon);
547 
548  if (is_array($coupon['items']) && $coupon['total_objects_coupon_price'] > 0)
549  {
550  $bonus = ($this->coupon_obj->getCouponBonus($coupon['total_objects_coupon_price']));
551 
552  foreach ($coupon['items'] as $item)
553  {
554  if (!array_key_exists($item['pobject_id'], $r_items))
555  {
556  $r_items[$item['pobject_id']] = $item;
557  $r_items[$item['pobject_id']]['discount_price'] = (float) $item['math_price'];
558  }
559 
560  $ratio = (float) $item['math_price'] / $coupon['total_objects_coupon_price'];
561  $r_items[$item['pobject_id']]['discount_price'] += ($ratio * $bonus * (-1));
562  }
563  }
564  }
565 
566  return $r_items;
567  }
568 
569  return array();
570  }
571 
572  public static function _deleteExpiredSessionsPSC()
573  {
574  global $ilDB;
575 
576  $query = "DELETE FROM payment_shopping_cart "
577  . "WHERE psc_id IN "
578  . " (SELECT psc_id FROM payment_shopping_cart "
579  . " LEFT JOIN usr_session ON usr_session.session_id = payment_shopping_cart.session_id "
580  . " WHERE customer_id = %s AND usr_session.session_id IS NULL)";
581  $ilDB->manipulateF($query, array('integer'), array(ANONYMOUS_USER_ID));
582 
583  }
584 
585  public static function _deleteShoppingCartEntries($a_pobject_id)
586  {
587  global $ilDB;
588 
589  $ilDB->manipulateF('
590  DELETE FROM payment_shopping_cart
591  WHERE pobject_id = %s',
592  array('integer'), array($a_pobject_id));
593  }
594 
595 
600  public static function getShoppingcartEntries($a_paymethod = null)
601  {
602  global $ilUser, $ilDB;
603 
604  $user_id = $ilUser->getId();
605 
606  if($user_id == ANONYMOUS_USER_ID)
607  {
608  return false;
609  }
610  else
611  {
612  if($a_paymethod != null)
613  {
614  $res = $ilDB->queryF('
615  SELECT psc_id, ref_id, vat_rate, duration, currency, price, unlimited_duration, extension, duration_from, duration_until, description,price_type, pay_method
616  FROM payment_shopping_cart psc
617  LEFT JOIN payment_prices pp ON psc.price_id
618  LEFT JOIN payment_objects po ON psc.pobject_id
619  LEFT JOIN payment_vats pv ON po.vat_id
620  WHERE customer_id = %s
621  AND status = %s
622  AND pay_method = %s',
623  array('integer', 'integer', 'integer'), array($user_id, 1, $a_paymethod));
624  }
625  else
626  {
627  // select all entries for current user
628  $res = $ilDB->queryF('
629  SELECT psc_id, ref_id, vat_rate, duration, currency, price, unlimited_duration, extension, duration_from, duration_until, description, price_type, pay_method
630  FROM payment_shopping_cart psc
631  LEFT JOIN payment_prices pp ON psc.price_id
632  LEFT JOIN payment_objects po ON psc.pobject_id
633  LEFT JOIN payment_vats pv ON po.vat_id
634  WHERE customer_id = %s
635  AND status = %s',
636  array('integer', 'integer'), array($user_id, 1));
637  }
638 
639  $entries = array();
640  while($row = $ilDB->fetchAssoc($res))
641  {
642  $entries[] = $row;
643  }
644  return $entries;
645  }
646  }
647 }
648