ILIAS  Release_5_0_x_branch Revision 61816
 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 
463  // @todo $a_user_id not defined?!?!
464  if(ilPaymentVendors::_isVendor($a_user_id))
465  {
466  $vendors[] = $a_user_id;
467  }
468 
469  if(is_array($vendors) && count($vendors))
470  {
471  $query .= ' OR '.$ilDB->in('vendor_id', $vendors, false, 'integer');
472  }
473  }
474  }
475 
476  $res = $ilDB->queryf($query, $data_types, $data);
477  $rows = $ilDB->numRows($res);
478 
479  return $rows ? true : false;
480 
481  }
482 
483  // base method to check access for a specific object
484  public static function _hasAccess($a_ref_id, $a_transaction = 0, $a_subtype ='')
485  {
486  include_once './Services/Payment/classes/class.ilPaymentBookings.php';
487  include_once './Services/Payment/classes/class.ilPaymentTrustees.php';
488  include_once './Services/Payment/classes/class.ilPaymentVendors.php';
489 
490  global $rbacsystem,$ilDB, $ilUser;
491 
492  // check write access
493  if($rbacsystem->checkAccess('write', $a_ref_id))
494  {
495  return true;
496  }
497  // check if user is vendor/trustee
498  $vendors_of_trustee = ilPaymentTrustees::_getVendorIdsByTrustee($ilUser->getId());
499 
500  if(!IS_PAYMENT_ENABLED)
501  {
502  return true;
503  }
504 
505  if($a_subtype)
506  {
507  $result = $ilDB->queryf('
508  SELECT * FROM payment_objects
509  WHERE ref_id = %s AND (status = %s or status = %s)
510  AND subtype = %s',
511  array('integer', 'integer', 'integer','text'),
512  array($a_ref_id, '1', '2',$a_subtype));
513  }
514  else
515  {
516  $result = $ilDB->queryf('
517  SELECT * FROM payment_objects
518  WHERE ref_id = %s
519  AND (status = %s OR status = %s)
520  OR (vendor_id = %s)',
521  array('integer', 'integer', 'integer','integer'),
522  array($a_ref_id, '1', '2',$ilUser->getId()));
523  }
524  while($row = $ilDB->fetchObject($result))
525  {
526  if($row->vendor_id == $ilUser->getId() || in_array($row->vendor_id, $vendors_of_trustee))
527  {
528  return true;
529  }
530  else
531  if(!ilPaymentBookings::_hasAccess($row->pobject_id, '', $a_transaction))
532  {
533  return false;
534  }
535  else
536  {
537  return true;
538  }
539  }
540  return false;
541  }
542 
543  public static function _isBuyable($a_ref_id, $a_subtype = '')
544  {
545  global $ilDB;
546 
547 
548  if(!IS_PAYMENT_ENABLED)
549  {
550  return false;
551  }
552 
553  if($a_subtype)
554  {
555  $result = $ilDB->queryf('
556  SELECT * FROM payment_objects
557  WHERE ref_id = %s
558  AND (status = %s or status = %s)
559  AND subtype = %s',
560  array('integer', 'integer', 'integer','text'),
561  array($a_ref_id, '1', '2',$a_subtype));
562  }
563  else
564  {
565  $result = $ilDB->queryf('
566  SELECT * FROM payment_objects
567  WHERE ref_id = %s AND (status = %s or status = %s)',
568  array('integer', 'integer', 'integer'),
569  array($a_ref_id, '1', '2'));
570  }
571 
572  while($row = $ilDB->fetchObject($result))
573  {
574  return true;
575  }
576  return false;
577  }
578 
579  // checks if this new object already exists in payment_objects
580  public static function _isNewObject($a_ref_id)
581  {
582  global $ilDB;
583 
584  $res = $ilDB->queryF('SELECT * FROM payment_objects WHERE ref_id = %s',
585  array('integer'), array($a_ref_id));
586 
587  $rows = $ilDB->numRows($res);
588 
589  return $rows ? false : true;
590  }
591 
592 
600  public static function _requiresPurchaseToAccess($a_ref_id, $a_purchasetype = '')
601  {
602  /* Check:
603  * - User has no Access -> return false
604  * - User has Access but there are also Extension-Prices available -> User is able to buy the Object AGAIN to extend duration
605  */
606 
607  if( (self::_isBuyable($a_ref_id) && !self::_hasAccess($a_ref_id))
608  || (self::_isBuyable($a_ref_id) && self::_hasAccess($a_ref_id)
609  && self::_hasExtensions($a_ref_id) && isset($a_purchasetype)))
610  {
611  return true;
612  }
613  else return false;
614  }
615 
616  public static function _isInCart($a_ref_id)
617  {
618  global $ilDB, $ilUser;
619 
620  if(ANONYMOUS_USER_ID == $ilUser->getId())
621  {
622  $result = $ilDB->queryf('
623  SELECT psc_id FROM payment_objects po, payment_shopping_cart psc
624  WHERE ref_id = %s
625  AND session_id = %s
626  AND po.pobject_id = psc.pobject_id',
627  array('integer', 'text'),
628  array($a_ref_id, session_id()));
629  }
630  else
631  {
632  $result = $ilDB->queryf('
633  SELECT psc_id FROM payment_objects po, payment_shopping_cart psc
634  WHERE ref_id = %s
635  AND customer_id = %s
636  AND po.pobject_id = psc.pobject_id',
637  array('integer', 'integer'),
638  array($a_ref_id, $ilUser->getId()));
639  }
640  while($row = $ilDB->fetchObject($result))
641  {
642  return true;
643  }
644 
645  return false;
646  }
647 
648  public static function _hasExtensions($a_ref_id)
649  {
650  global $ilDB;
651 
652  // user has already access to current object
653  // object contain buyable extension-prices
654 
655  $res = $ilDB->queryf('
656  SELECT * FROM payment_prices pp, payment_objects po
657  WHERE po.ref_id = %s
658  AND pp.pobject_id = po.pobject_id
659  AND pp.extension = %s',
660  array('integer', 'integer'), array($a_ref_id, 1));
661 
662  if($row = $ilDB->numRows($res))
663  {
664  return true;
665  }
666  return false;
667  }
668 
669  private function __read()
670  {
671  if($this->getPobjectId())
672  {
673  $result = $this->db->queryf('SELECT * FROM payment_objects WHERE pobject_id = %s',
674  array('integer'), array($this->getPobjectId()));
675 
676  while($row = $this->db->fetchObject($result))
677  {
678  $this->setRefId($row->ref_id);
679  $this->setStatus($row->status);
680  $this->setPayMethod($row->pay_method);
681  $this->setVendorId($row->vendor_id);
682  $this->setTopicId($row->pt_topic_fk);
683  $this->setVatId($row->vat_id);
684  $this->setSubtype($row->subtype);
685  $this->setSpecial($row->is_special);
686 
687  return true;
688  }
689  }
690 
691  return false;
692  }
693 
694  public static function _checkExcSubtype($a_ref_id)
695  {
696  global $ilDB;
697 
698  $res = $ilDB->queryF('SELECT * FROM payment_objects WHERE
699  ref_id = %s', array('integer'), array($a_ref_id));
700  $subtypes = array();
701  while($row = $ilDB->fetchAssoc($res))
702  {
703  $subtypes[] = $row['subtype'];
704  }
705  return $subtypes;
706  }
707 
708  public static function getAllBuyableObjects()
709  {
710  global $ilDB;
711 
712 
713  $res = $ilDB->queryF('SELECT ref_id FROM payment_objects WHERE status = %s',
714  array('integer'), array(1));
715 
716  $obj = array();
717  while($row = $ilDB->fetchAssoc($res))
718  {
719  $obj[] = $row['ref_id'];
720  }
721  return $obj;
722  }
723 
724  public static function _getSpecialObjects()
725  {
726  global $ilDB;
727 
728  $res = $ilDB->queryF('SELECT * FROM payment_objects
729  WHERE is_special = %s AND status = %s
730  ORDER BY pt_topic_fk',
731  array('integer', 'integer'), array(1,1));
732 
733  $obj = array();
734  while($row = $ilDB->fetchAssoc($res))
735  {
736  $obj[] = $row;
737  }
738  return $obj;
739  }
740 
745  public static function _getTopicsObjects($topic_id = NULL)
746  {
747  global $ilDB;
748 
749  if($topic_id === NULL)
750  {
751  $res = $ilDB->queryF('
752  SELECT * FROM payment_objects
753  WHERE status = %s
754  ORDER BY pt_topic_fk', array('integer'), array(1));
755  }
756  else
757  {
758  $res = $ilDB->queryF('
759  SELECT * FROM payment_objects
760  WHERE pt_topic_fk = %s AND status = %s',
761  array('integer', 'integer'), array((int)$topic_id, 1));
762  }
763  $obj = array();
764  while($row = $ilDB->fetchAssoc($res))
765  {
766  $obj[] = $row;
767  }
768  return $obj;
769  }
770 
771  public static function _getContainerObjects($a_ref_id)
772  {
773  global $ilDB, $tree;
774 
775  $filter = array( "lm", "sahs", "htlm",'file', 'exc', 'tst', 'icrs','crs','crsr','rcrs', 'glo');
776  $objects = $tree->getChildsByTypeFilter($a_ref_id, $filter);
777 
778  $counter = 0;
779  $obj_res = array();
780  foreach($objects as $object)
781  {
782  if(self::_isBuyable($object['ref_id']))
783  {
784  $res = $ilDB->queryF('SELECT * FROM payment_objects
785  WHERE ref_id = %s AND status = %s
786  ORDER BY pt_topic_fk',
787  array('integer', 'integer'), array($object['ref_id'],1));
788 
789  while($row = $ilDB->fetchAssoc($res))
790  {
791  $obj_res[$counter] = $row;
792  #$obj_res[$counter]['ref_id'] = $object['ref_id'];
793  $obj_res[$counter]['obj_id'] = $object['obj_id'];
794  $obj_res[$counter]['type'] = $object['type'];
795  $obj_res[$counter]['title'] = $object['title'];
796  $obj_res[$counter]['child'] = $object['child'];
797  $obj_res[$counter]['description'] = $object['description'];
798  }
799  }
800 
801  $counter++;
802  }
803  return $obj_res;
804  }
805 
806 } // END class.ilPaymentObject
807 ?>