ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilPaymentObject.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
13 {
14  private $db = null;
15  private $user_obj = null;
16  private $pobject_id = null;
17  private $ref_id = null;
18  private $status = null;
19  private $pay_method = null;
20  private $vendor_id = null;
21  private $topic_id = 0;
22  private $vat_id = 0;
23  private $subtype = null;
24  private $is_special = 0;
25 
26 
27  public function __construct($user_obj, $a_pobject_id = null)
28  {
29  global $ilDB;
30 
31  $this->db = $ilDB;
32  $this->user_obj = $user_obj;
33 
34  $this->STATUS_NOT_BUYABLE = 0;
35  $this->STATUS_BUYABLE = 1;
36  $this->STATUS_EXPIRES = 2;
37 
38  $this->PAY_METHOD_NOT_SPECIFIED = 0;
39 
40  $this->pobject_id = $a_pobject_id;
41 
42  include_once './Services/Payment/classes/class.ilPayMethods.php';
43  $pmObj = new ilPayMethods();
44  $tmp = $pmObj->readAll();
45 
46  foreach($tmp as $pm)
47  {
48  $tmp = strtoupper($pm['pm_title']);
49  $this->PAY_METHOD_.$tmp = $pm['pm_id'];
50  }
51  $this->__read();
52  }
53 
54  // SETTER GETTER
55  public function getTopicId()
56  {
57  return $this->topic_id;
58  }
59  public function setTopicId($a_topic_id)
60  {
61  $this->topic_id = $a_topic_id;
62  }
63  public function getPobjectId()
64  {
65  return $this->pobject_id;
66  }
67  public function setRefId($a_ref_id)
68  {
69  $this->ref_id = $a_ref_id;
70  }
71  public function getRefId()
72  {
73  return $this->ref_id;
74  }
75  public function setStatus($a_status)
76  {
77  $this->status = $a_status;
78  }
79  public function getStatus()
80  {
81  return $this->status;
82  }
83  public function setPayMethod($a_method)
84  {
85  $this->pay_method = $a_method;
86  }
87  public function getPayMethod()
88  {
89  return $this->pay_method;
90  }
91  public function setVendorId($a_vendor_id)
92  {
93  $this->vendor_id = $a_vendor_id;
94  }
95  public function getVendorId()
96  {
97  return $this->vendor_id;
98  }
99  public function getVatId()
100  {
101  return $this->vat_id;
102  }
103  public function setVatId($a_vat_id)
104  {
105  $this->vat_id = $a_vat_id;
106  }
107 
108  public function setSubtype($a_subtype)
109  {
110  $this->subtype = $a_subtype;
111  }
112 
113  public function getSubtype()
114  {
115  return $this->subtype;
116  }
117  public function setSpecial($a_special)
118  {
119  $this->is_special = $a_special;
120  }
121  public function getSpecial()
122  {
123  return $this->is_special;
124  }
125 
126  function getVat($a_amount = 0, $type = 'CALCULATION')
127  {
128  $oVAT = new ilShopVats($this->getVatId());
129  switch($type)
130  {
131  case 'CALCULATION':
132  $val = (float)($a_amount - (round(($a_amount / (1 + ($oVAT->getRate() / 100))) * 100) / 100));
133  return number_format((float)$val,'2','.','');
134  default:
135  $val = (float)($a_amount - (round(($a_amount / (1 + ($oVAT->getRate() / 100))) * 100) / 100));
136  $val = ilShopUtils::_formatFloat($val);
137  return $val;
138  }
139  }
140 
141  public function add()
142  {
143  $next_id = $this->db->nextId('payment_objects');
144 
145  $this->db->insert('payment_objects',array(
146  'pobject_id' => array('integer', $next_id),
147  'ref_id' => array('integer', $this->getRefId()),
148  'status' => array('integer', $this->getStatus()),
149  'pay_method' => array('integer', $this->getPayMethod()),
150  'vendor_id' => array('integer', $this->getVendorId()),
151  'pt_topic_fk' => array('integer', $this->getTopicId()),
152  'vat_id' => array('integer', $this->getVatId()),
153  'subtype' => array('text', $this->getSubtype()),
154  'is_special' => array('integer', $this->getSpecial())
155  ));
156  return $next_id;
157  }
158 
159  public function delete()
160  {
161  if($this->getPobjectId())
162  {
163  include_once 'Services/Payment/classes/class.ilFileDataShop.php';
164  $oFileData = new ilFileDataShop($this->getPobjectId());
165  $oFileData->deassignFileFromPaymentObject();
166 
167  $statement = $this->db->manipulateF('DELETE FROM payment_objects WHERE pobject_id = %s',
168  array('integer'), array($this->getPobjectId()));
169 
170 
171  return true;
172  }
173 
174  return false;
175  }
176 
177  public function update()
178  {
179  if((int)$this->getPobjectId())
180  {
181  $this->db->update('payment_objects',array(
182  'ref_id' => array('integer', $this->getRefId()),
183  'status' => array('integer', $this->getStatus()),
184  'pay_method' => array('integer', $this->getPayMethod()),
185  'vendor_id' => array('integer', $this->getVendorId()),
186  'pt_topic_fk' => array('integer', $this->getTopicId()),
187  'vat_id' => array('integer', $this->getVatId()),
188  'subtype' => array('text', $this->getSubtype()),
189  'is_special' => array('integer', $this->getSpecial())
190  ),
191  array('pobject_id' => array('integer', $this->getPobjectId())));
192 
193  return true;
194  }
195  else
196  return false;
197  }
198  // STATIC
199  public static function _lookupPobjectId($a_ref_id)
200  {
201  global $ilDB;
202 
203  $res = $ilDB->queryf('
204  SELECT * FROM payment_objects
205  WHERE ref_id = %s',
206  array('integer'),
207  array($a_ref_id));
208 
209  while($row = $ilDB->fetchAssoc($res))
210  {
211  return $row['pobject_id'];
212  }
213  return 0;
214  }
215 
216  public static function _lookupTopicId($a_ref_id)
217  {
218  global $ilDB;
219 
220  static $cache = array();
221  if(isset($cache[$a_ref_id]))
222  {
223  return $cache[$a_ref_id];
224  }
225 
226  $result = $ilDB->queryf('SELECT pt_topic_fk FROM payment_objects WHERE ref_id = %s',
227  array('integer'),array($a_ref_id));
228 
229  while($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
230  {
231  $cache[$a_ref_id] = $row->pt_topic_fk;
232  return (int)$cache[$a_ref_id];
233  }
234 
235  return 0;
236  }
237 
238  public static function _getCountObjectsByPayMethod($a_id)
239  {
240  global $ilDB;
241 
242 
243  $result = $ilDB->queryf('SELECT COUNT(pay_method) pm FROM payment_objects WHERE pay_method = %s',
244  array('integer'), array($a_id));
245 
246  while($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
247  {
248  return (int)$row->pm;
249  }
250 
251  return 0;
252  }
253 
254  public static function _getObjectsData($a_user_id)
255  {
256  global $ilDB;
257 
258  // get all vendors user is assigned to
259  include_once './Services/Payment/classes/class.ilPaymentTrustees.php';
260  include_once './Services/Payment/classes/class.ilPaymentVendors.php';
261 
262  $vendors = ilPaymentTrustees::_getVendorsForObjects($a_user_id);
263 
264  if(ilPaymentVendors::_isVendor($a_user_id))
265  {
266  $vendors[] = $a_user_id;
267  }
268 
269  if(!count($vendors))
270  {
271  return array();
272  }
273 
274  $data_types = array();
275  $data_values = array();
276  $cnt_vendors = count($vendors);
277 
278  $query = 'SELECT * FROM payment_objects WHERE vendor_id IN';
279 
280  if (is_array($vendors) &&
281  $cnt_vendors > 0)
282  {
283  $in = '(';
284  $counter = 0;
285  foreach($vendors as $vendor)
286  {
287  array_push($data_values, $vendor);
288  array_push($data_types, 'integer');
289 
290  if($counter > 0) $in .= ',';
291  $in .= '%s';
292  ++$counter;
293  }
294  $in .= ')';
295  $query .= $in;
296  }
297 
298  $res= $ilDB->queryf($query, $data_types, $data_values);
299 
300 
301  while($row = $ilDB->fetchObject($res))
302  {
303  $objects[$row->pobject_id]['pobject_id'] = $row->pobject_id;
304  $objects[$row->pobject_id]['ref_id'] = $row->ref_id;
305  $objects[$row->pobject_id]['status'] = $row->status;
306  $objects[$row->pobject_id]['pay_method'] = $row->pay_method;
307  $objects[$row->pobject_id]['vendor_id'] = $row->vendor_id;
308  $objects[$row->pobject_id]['topic_id'] = $row->pt_topic_fk;
309  $objects[$row->pobject_id]['vat_id'] = $row->vat_id;
310  $objects[$row->pobject_id]['subtype'] = $row->subtype;
311  $objects[$row->pobject_id]['is_special'] = $row->is_special;
312  }
313  return $objects ? $objects : array();
314  }
315 
316  public static function _getAllObjectsData()
317  {
318  global $ilDB;
319 
320  $data_types = array();
321  $data_values = array();
322 
323  $query = 'SELECT * FROM payment_objects ';
324 
325  if ($_SESSION['pay_objects']['title_value'] != '')
326  {
327  $query .= ', object_reference obr, object_data od ';
328  }
329 
330  if ($_SESSION['pay_objects']['vendor'] != '')
331  {
332  $query .= ', usr_data ud ';
333  }
334 
335  $query .= ' WHERE 1 = 1 ';
336 
337  if ($_SESSION['pay_objects']['title_value'])
338  {
339  $query .= ' AND obr.ref_id = payment_objects.ref_id AND od.obj_id = obr.obj_id ';
340 
341  $search_string = '';
342 
343  $title_search = explode(' ', trim($_SESSION['pay_objects']['title_value']));
344  for ($i = 0; $i < count($title_search); $i++)
345  {
346  $title_search[$i] = trim($title_search[$i]);
347 
348  if ($title_search[$i] != '')
349  {
350  //$search_string .= " od.title LIKE ".$ilDB->quote("%".$title_search[$i]."%")." ";
351  $search_string .= ' od.title LIKE %s '; // ".$ilDB->quote("%".$title_search[$i]."%")." ";
352  array_push($data_types, 'text');
353  array_push($data_values,'%'.$title_search[$i].'%');
354 
355  switch ($_SESSION['pay_objects']['title_type'])
356  {
357  case 'or' :
358  if ($i < count($title_search) - 1)
359  {
360  $search_string .= ' OR ';
361  }
362  break;
363  case 'and' :
364  if ($i < count($title_search) - 1)
365  {
366  $search_string .= ' AND ';
367  }
368  break;
369  }
370  }
371  }
372 
373  if ($search_string != '')
374  {
375  $query .= ' AND (' . $search_string . ') ';
376  }
377  }
378 
379  if ($_SESSION['pay_objects']['vendor'] != '')
380  {
381  $query .= ' AND ud.usr_id = payment_objects.vendor_id AND login = %s';
382  array_push($data_types, 'text');
383  array_push($data_values, $_SESSION['pay_objects']['vendor']);
384  }
385 
386  if($_SESSION['pay_objects']['pay_method'] > 0)
387  {
388  $query .= ' AND pay_method = %s';
389  array_push($data_types, 'integer');
390  array_push($data_values, $_SESSION['pay_objects']['pay_method']);
391  }
392 
393  $res = $ilDB->queryf($query, $data_types, $data_values);
394  $objects = array();
395  while($row = $ilDB->fetchObject($res))
396  {
397  $objects[$row->pobject_id]['pobject_id'] = $row->pobject_id;
398  $objects[$row->pobject_id]['ref_id'] = $row->ref_id;
399  $objects[$row->pobject_id]['status'] = $row->status;
400  $objects[$row->pobject_id]['pay_method'] = $row->pay_method;
401  $objects[$row->pobject_id]['vendor_id'] = $row->vendor_id;
402  $objects[$row->pobject_id]['topic_id'] = $row->pt_topic_fk;
403  $objects[$row->pobject_id]['vat_id'] = $row->vat_id;
404  $objects[$row->pobject_id]['subtype'] = $row->subtype;
405  $objects[$row->pobject_id]['is_special'] = $row->is_special;
406  }
407  return $objects ? $objects : array();
408  }
409 
410  public static function _getObjectData($a_id)
411  {
412  global $ilDB;
413 
414  $res = $ilDB->queryf('
415  SELECT * FROM payment_objects
416  WHERE pobject_id = %s',
417  array('integer'), array($a_id));
418 
419  if (is_object($res))
420  {
421  return $ilDB->fetchAssoc($res);
422 
423  }
424 
425  return false;
426  }
427 
428  public static function _isPurchasable($a_ref_id, $a_vendor_id = 0, $a_check_trustee = false, $a_check_status = false)
429  {
430  global $ilDB;
431 
432  // In the moment it's not possible to sell one object twice
433 
434  $data = array();
435  $data_types = array();
436 
437 
438  $query = 'SELECT * FROM payment_objects WHERE ref_id = %s ';
439  $data_types[] = 'integer';
440  $data[]= $a_ref_id;
441 
442  // check if object is buyable !!
443  if($a_check_status)
444  {
445  $query .= 'AND status > %s ';
446  $data_types[] = 'integer';
447  $data[] = 0;
448  }
449 
450  if ($a_vendor_id > 0)
451  {
452  $query .= 'AND vendor_id = %s';
453  $data_types[] = 'integer';
454  $data[] = $a_vendor_id;
455 
456  if($a_check_trustee)
457  {
458  include_once './Services/Payment/classes/class.ilPaymentTrustees.php';
459  include_once './Services/Payment/classes/class.ilPaymentVendors.php';
460 
461  $vendors = ilPaymentTrustees::_getVendorsForObjects($a_vendor_id);
462  if(ilPaymentVendors::_isVendor($a_user_id))
463  {
464  $vendors[] = $a_user_id;
465  }
466 
467  if(is_array($vendors) && count($vendors))
468  {
469  $query .= ' OR '.$ilDB->in('vendor_id', $vendors, false, 'integer');
470  }
471  }
472  }
473 
474  $res = $ilDB->queryf($query, $data_types, $data);
475  $rows = $ilDB->numRows($res);
476 
477  return $rows ? true : false;
478 
479  }
480 
481  // base method to check access for a specific object
482  public static function _hasAccess($a_ref_id, $a_transaction = 0, $a_subtype ='')
483  {
484  include_once './Services/Payment/classes/class.ilPaymentBookings.php';
485  include_once './Services/Payment/classes/class.ilPaymentTrustees.php';
486  include_once './Services/Payment/classes/class.ilPaymentVendors.php';
487 
488  global $rbacsystem,$ilDB, $ilUser;
489 
490  // check write access
491  if($rbacsystem->checkAccess('write', $a_ref_id))
492  {
493  return true;
494  }
495  // check if user is vendor/trustee
496  $vendors_of_trustee = ilPaymentTrustees::_getVendorIdsByTrustee($ilUser->getId());
497 
498  if(!IS_PAYMENT_ENABLED)
499  {
500  return true;
501  }
502 
503  if($a_subtype)
504  {
505  $result = $ilDB->queryf('
506  SELECT * FROM payment_objects
507  WHERE ref_id = %s AND (status = %s or status = %s)
508  AND subtype = %s',
509  array('integer', 'integer', 'integer','text'),
510  array($a_ref_id, '1', '2',$a_subtype));
511  }
512  else
513  {
514  $result = $ilDB->queryf('
515  SELECT * FROM payment_objects
516  WHERE ref_id = %s
517  AND (status = %s OR status = %s)
518  OR (vendor_id = %s)',
519  array('integer', 'integer', 'integer','integer'),
520  array($a_ref_id, '1', '2',$ilUser->getId()));
521  }
522  while($row = $ilDB->fetchObject($result))
523  {
524  if($row->vendor_id == $ilUser->getId() || in_array($row->vendor_id, $vendors_of_trustee))
525  {
526  return true;
527  }
528  else
529  if(!ilPaymentBookings::_hasAccess($row->pobject_id, '', $a_transaction))
530  {
531  return false;
532  }
533  else
534  {
535  return true;
536  }
537  }
538  return false;
539  }
540 
541  public static function _isBuyable($a_ref_id, $a_subtype = '')
542  {
543  global $ilDB;
544 
545 
546  if(!IS_PAYMENT_ENABLED)
547  {
548  return false;
549  }
550 
551  if($a_subtype)
552  {
553  $result = $ilDB->queryf('
554  SELECT * FROM payment_objects
555  WHERE ref_id = %s
556  AND (status = %s or status = %s)
557  AND subtype = %s',
558  array('integer', 'integer', 'integer','text'),
559  array($a_ref_id, '1', '2',$a_subtype));
560  }
561  else
562  {
563  $result = $ilDB->queryf('
564  SELECT * FROM payment_objects
565  WHERE ref_id = %s AND (status = %s or status = %s)',
566  array('integer', 'integer', 'integer'),
567  array($a_ref_id, '1', '2'));
568  }
569 
570  while($row = $ilDB->fetchObject($result))
571  {
572  return true;
573  }
574  return false;
575  }
576 
577  // checks if this new object already exists in payment_objects
578  public static function _isNewObject($a_ref_id)
579  {
580  global $ilDB;
581 
582  $res = $ilDB->queryF('SELECT * FROM payment_objects WHERE ref_id = %s',
583  array('integer'), array($a_ref_id));
584 
585  $rows = $ilDB->numRows($res);
586 
587  return $rows ? false : true;
588  }
589 
590 
598  public static function _requiresPurchaseToAccess($a_ref_id, $a_purchasetype = '')
599  {
600  /* Check:
601  * - User has no Access -> return false
602  * - User has Access but there are also Extension-Prices available -> User is able to buy the Object AGAIN to extend duration
603  */
604 
605  if( (self::_isBuyable($a_ref_id) && !self::_hasAccess($a_ref_id))
606  || (self::_isBuyable($a_ref_id) && self::_hasAccess($a_ref_id)
607  && self::_hasExtensions($a_ref_id) && isset($a_purchasetype)))
608  {
609  return true;
610  }
611  else return false;
612  }
613 
614  public static function _isInCart($a_ref_id)
615  {
616  global $ilDB, $ilUser;
617 
618  if(ANONYMOUS_USER_ID == $ilUser->getId())
619  {
620  $result = $ilDB->queryf('
621  SELECT psc_id FROM payment_objects po, payment_shopping_cart psc
622  WHERE ref_id = %s
623  AND session_id = %s
624  AND po.pobject_id = psc.pobject_id',
625  array('integer', 'text'),
626  array($a_ref_id, session_id()));
627  }
628  else
629  {
630  $result = $ilDB->queryf('
631  SELECT psc_id FROM payment_objects po, payment_shopping_cart psc
632  WHERE ref_id = %s
633  AND customer_id = %s
634  AND po.pobject_id = psc.pobject_id',
635  array('integer', 'integer'),
636  array($a_ref_id, $ilUser->getId()));
637  }
638  while($row = $ilDB->fetchObject($result))
639  {
640  return true;
641  }
642 
643  return false;
644  }
645 
646  public static function _hasExtensions($a_ref_id)
647  {
648  global $ilDB;
649 
650  // user has already access to current object
651  // object contain buyable extension-prices
652 
653  $res = $ilDB->queryf('
654  SELECT * FROM payment_prices pp, payment_objects po
655  WHERE po.ref_id = %s
656  AND pp.pobject_id = po.pobject_id
657  AND pp.extension = %s',
658  array('integer', 'integer'), array($a_ref_id, 1));
659 
660  if($row = $ilDB->numRows($res))
661  {
662  return true;
663  }
664  return false;
665  }
666 
667  private function __read()
668  {
669  if($this->getPobjectId())
670  {
671  $result = $this->db->queryf('SELECT * FROM payment_objects WHERE pobject_id = %s',
672  array('integer'), array($this->getPobjectId()));
673 
674  while($row = $this->db->fetchObject($result))
675  {
676  $this->setRefId($row->ref_id);
677  $this->setStatus($row->status);
678  $this->setPayMethod($row->pay_method);
679  $this->setVendorId($row->vendor_id);
680  $this->setTopicId($row->pt_topic_fk);
681  $this->setVatId($row->vat_id);
682  $this->setSubtype($row->subtype);
683  $this->setSpecial($row->is_special);
684 
685  return true;
686  }
687  }
688 
689  return false;
690  }
691 
692  public static function _checkExcSubtype($a_ref_id)
693  {
694  global $ilDB;
695 
696  $res = $ilDB->queryF('SELECT * FROM payment_objects WHERE
697  ref_id = %s', array('integer'), array($a_ref_id));
698  $subtypes = array();
699  while($row = $ilDB->fetchAssoc($res))
700  {
701  $subtypes[] = $row['subtype'];
702  }
703  return $subtypes;
704  }
705 
706  public static function getAllBuyableObjects()
707  {
708  global $ilDB;
709 
710 
711  $res = $ilDB->queryF('SELECT ref_id FROM payment_objects WHERE status = %s',
712  array('integer'), array(1));
713 
714  $obj = array();
715  while($row = $ilDB->fetchAssoc($res))
716  {
717  $obj[] = $row['ref_id'];
718  }
719  return $obj;
720  }
721 
722  public static function _getSpecialObjects()
723  {
724  global $ilDB;
725 
726  $res = $ilDB->queryF('SELECT * FROM payment_objects
727  WHERE is_special = %s AND status = %s
728  ORDER BY pt_topic_fk',
729  array('integer', 'integer'), array(1,1));
730 
731  $obj = array();
732  while($row = $ilDB->fetchAssoc($res))
733  {
734  $obj[] = $row;
735  }
736  return $obj;
737  }
738 
739  public static function _getContainerObjects($a_ref_id)
740  {
741  global $ilDB, $tree;
742 
743  $filter = array( "lm", "sahs", "htlm",'file', 'exc', 'tst', 'icrs','crs','crsr','rcrs', 'glo');
744  $objects = $tree->getChildsByTypeFilter($a_ref_id, $filter);
745 
746  $counter = 0;
747  $obj_res = array();
748  foreach($objects as $object)
749  {
750  if(self::_isBuyable($object['ref_id']))
751  {
752  $res = $ilDB->queryF('SELECT * FROM payment_objects
753  WHERE ref_id = %s AND status = %s
754  ORDER BY pt_topic_fk',
755  array('integer', 'integer'), array($object['ref_id'],1));
756 
757  while($row = $ilDB->fetchAssoc($res))
758  {
759  $obj_res[$counter] = $row;
760  #$obj_res[$counter]['ref_id'] = $object['ref_id'];
761  $obj_res[$counter]['obj_id'] = $object['obj_id'];
762  $obj_res[$counter]['type'] = $object['type'];
763  $obj_res[$counter]['title'] = $object['title'];
764  $obj_res[$counter]['child'] = $object['child'];
765  $obj_res[$counter]['description'] = $object['description'];
766  }
767  }
768 
769  $counter++;
770  }
771  return $obj_res;
772  }
773 
774 } // END class.ilPaymentObject
775 ?>