ILIAS  eassessment Revision 61809
 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 
24 
25  public function __construct($user_obj, $a_pobject_id = null)
26  {
27 
28  global $ilDB;
29 
30  $this->db = $ilDB;
31  $this->user_obj = $user_obj;
32 
33  $this->STATUS_NOT_BUYABLE = 0;
34  $this->STATUS_BUYABLE = 1;
35  $this->STATUS_EXPIRES = 2;
36 
37  $this->PAY_METHOD_NOT_SPECIFIED = 0;
38  include_once './Services/Payment/classes/class.ilPayMethods.php';
39  $pmObj = new ilPayMethods();
40  $tmp = $pmObj->readAll();
41 
42  foreach($tmp as $pm)
43  {
44  $tmp = strtoupper($pm['pm_title']);
45  $this->PAY_METHOD_.$tmp = $pm['pm_id'];
46  }
47 
48  $this->pobject_id = $a_pobject_id;
49  $this->__read();
50 
51  }
52 
53  // SETTER GETTER
54  public function getTopicId()
55  {
56  return $this->topic_id;
57  }
58  public function setTopicId($a_topic_id)
59  {
60  $this->topic_id = $a_topic_id;
61  }
62  public function getPobjectId()
63  {
64  return $this->pobject_id;
65  }
66  public function setRefId($a_ref_id)
67  {
68  $this->ref_id = $a_ref_id;
69  }
70  public function getRefId()
71  {
72  return $this->ref_id;
73  }
74  public function setStatus($a_status)
75  {
76  $this->status = $a_status;
77  }
78  public function getStatus()
79  {
80  return $this->status;
81  }
82  public function setPayMethod($a_method)
83  {
84  $this->pay_method = $a_method;
85  }
86  public function getPayMethod()
87  {
88  return $this->pay_method;
89  }
90  public function setVendorId($a_vendor_id)
91  {
92  $this->vendor_id = $a_vendor_id;
93  }
94  public function getVendorId()
95  {
96  return $this->vendor_id;
97  }
98  public function getVatId()
99  {
100  return $this->vat_id;
101  }
102  public function setVatId($a_vat_id)
103  {
104  $this->vat_id = $a_vat_id;
105  }
106 
107  function getVat($a_amount = 0, $type = 'CALCULATION')
108  {
109  $oVAT = new ilShopVats($this->getVatId());
110  switch($type)
111  {
112  case 'CALCULATION':
113  $val = (float)($a_amount - (round(($a_amount / (1 + ($oVAT->getRate() / 100))) * 100) / 100));
114 
115  return number_format($val,'2');
116  default:
117  global $lng;
118 
119  $val = (float)($a_amount - (round(($a_amount / (1 + ($oVAT->getRate() / 100))) * 100) / 100));
120  $val = ilShopUtils::_formatFloat($val);
121  return $val;
122  }
123  }
124 
125  public function add()
126  {
127  $next_id = $this->db->nextId('payment_objects');
128 
129  $this->db->insert('payment_objects',array(
130  'pobject_id' => array('integer', $next_id),
131  'ref_id' => array('integer', $this->getRefId()),
132  'status' => array('integer', $this->getStatus()),
133  'pay_method' => array('integer', $this->getPayMethod()),
134  'vendor_id' => array('integer', $this->getVendorId()),
135  'pt_topic_fk' => array('integer', $this->getTopicId()),
136  'vat_id' => array('integer', $this->getVatId())
137  ));
138  return $next_id;
139  }
140 
141  public function delete()
142  {
143  if($this->getPobjectId())
144  {
145  include_once 'Services/Payment/classes/class.ilFileDataShop.php';
146  $oFileData = new ilFileDataShop($this->getPobjectId());
147  $oFileData->deassignFileFromPaymentObject();
148 
149  $statement = $this->db->manipulateF('DELETE FROM payment_objects WHERE pobject_id = %s',
150  array('integer'), array($this->getPobjectId()));
151 
152 
153  return true;
154  }
155 
156  return false;
157  }
158 
159  public function update()
160  {
161  if((int)$this->getPobjectId())
162  {
163  $this->db->update('payment_objects',array(
164  'ref_id' => array('integer', $this->getRefId()),
165  'status' => array('integer', $this->getStatus()),
166  'pay_method' => array('integer', $this->getPayMethod()),
167  'vendor_id' => array('integer', $this->getVendorId()),
168  'pt_topic_fk' => array('integer', $this->getTopicId()),
169  'vat_id' => array('integer', $this->getVatId())
170  ),
171  array('pobject_id' => array('integer', $this->getPobjectId())));
172 
173  return true;
174  }
175  else
176  return false;
177  }
178  // STATIC
179  public function _lookupPobjectId($a_ref_id)
180  {
181  global $ilDB;
182 
183  $res = $ilDB->queryf('
184  SELECT * FROM payment_objects
185  WHERE ref_id = %s',
186  array('integer'),
187  array($a_ref_id));
188 
189  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
190  {
191  return $row->pobject_id;
192  }
193  return 0;
194  }
195 
196  public static function _lookupTopicId($a_ref_id)
197  {
198  global $ilDB;
199 
200  static $cache = array();
201  if(isset($cache[$a_ref_id]))
202  {
203  return $cache[$a_ref_id];
204  }
205 
206  $result = $ilDB->queryf('SELECT pt_topic_fk FROM payment_objects WHERE ref_id = %s',
207  array('integer'),array($a_ref_id));
208 
209  while($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
210  {
211  $cache[$a_ref_id] = $row->pt_topic_fk;
212  return (int)$cache[$a_ref_id];
213  }
214 
215  return 0;
216  }
217 
219  {
220  global $ilDB;
221 
222 
223  $result = $ilDB->queryf('SELECT COUNT(pay_method) pm FROM payment_objects WHERE pay_method = %s',
224  array('integer'), array($a_id));
225 
226  while($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
227  {
228  return (int)$row->pm;
229  }
230 
231  return 0;
232  }
233 
234  function _getObjectsData($a_user_id)
235  {
236  global $ilDB;
237 
238  // get all vendors user is assigned to
239  include_once './Services/Payment/classes/class.ilPaymentTrustees.php';
240  include_once './Services/Payment/classes/class.ilPaymentVendors.php';
241 
242  $vendors = ilPaymentTrustees::_getVendorsForObjects($a_user_id);
243 
244  if(ilPaymentVendors::_isVendor($a_user_id))
245  {
246  $vendors[] = $a_user_id;
247  }
248 
249  if(!count($vendors))
250  {
251  return array();
252  }
253 
254  $data_types = array();
255  $data_values = array();
256  $cnt_vendors = count($vendors);
257 
258  $query = 'SELECT * FROM payment_objects WHERE vendor_id IN';
259 
260  if (is_array($vendors) &&
261  $cnt_vendors > 0)
262  {
263  $in = '(';
264  $counter = 0;
265  foreach($vendors as $vendor)
266  {
267  array_push($data_values, $vendor);
268  array_push($data_types, 'integer');
269 
270  if($counter > 0) $in .= ',';
271  $in .= '%s';
272  ++$counter;
273  }
274  $in .= ')';
275  $query .= $in;
276  }
277 
278  $res= $ilDB->queryf($query, $data_types, $data_values);
279 
280 
281  while($row = $ilDB->fetchObject($res))
282  {
283  $objects[$row->pobject_id]['pobject_id'] = $row->pobject_id;
284  $objects[$row->pobject_id]['ref_id'] = $row->ref_id;
285  $objects[$row->pobject_id]['status'] = $row->status;
286  $objects[$row->pobject_id]['pay_method'] = $row->pay_method;
287  $objects[$row->pobject_id]['vendor_id'] = $row->vendor_id;
288  $objects[$row->pobject_id]['topic_id'] = $row->pt_topic_fk;
289  $objects[$row->pobject_id]['vat_id'] = $row->vat_id;
290  }
291  return $objects ? $objects : array();
292  }
293 
295  {
296  global $ilDB;
297 
298  $data_types = array();
299  $data_values = array();
300 
301  $query = 'SELECT * FROM payment_objects ';
302 
303  if ($_SESSION['pay_objects']['title_value'] != '')
304  {
305  $query .= ', object_reference obr, object_data od ';
306  }
307 
308  if ($_SESSION['pay_objects']['vendor'] != '')
309  {
310  $query .= ', usr_data ud ';
311  }
312 
313  $query .= ' WHERE 1 = 1 ';
314 
315  if ($_SESSION['pay_objects']['title_value'])
316  {
317  $query .= ' AND obr.ref_id = payment_objects.ref_id AND od.obj_id = obr.obj_id ';
318 
319  $search_string = '';
320 
321  $title_search = explode(' ', trim($_SESSION['pay_objects']['title_value']));
322  for ($i = 0; $i < count($title_search); $i++)
323  {
324  $title_search[$i] = trim($title_search[$i]);
325 
326  if ($title_search[$i] != '')
327  {
328  //$search_string .= " od.title LIKE ".$ilDB->quote("%".$title_search[$i]."%")." ";
329  $search_string .= ' od.title LIKE %s '; // ".$ilDB->quote("%".$title_search[$i]."%")." ";
330  array_push($data_types, 'text');
331  array_push($data_values,'%'.$title_search[$i].'%');
332 
333  switch ($_SESSION['pay_objects']['title_type'])
334  {
335  case 'or' :
336  if ($i < count($title_search) - 1)
337  {
338  $search_string .= ' OR ';
339  }
340  break;
341  case 'and' :
342  if ($i < count($title_search) - 1)
343  {
344  $search_string .= ' AND ';
345  }
346  break;
347  }
348  }
349  }
350 
351  if ($search_string != '')
352  {
353  $query .= ' AND (' . $search_string . ') ';
354  }
355  }
356 
357  if ($_SESSION['pay_objects']['vendor'] != '')
358  {
359  $query .= ' AND ud.usr_id = payment_objects.vendor_id AND login = %s';
360  array_push($data_types, 'text');
361  array_push($data_values, $_SESSION['pay_objects']['vendor']);
362  }
363 
364  if($_SESSION['pay_objects']['pay_method'] > 0)
365  {
366  $query .= ' AND pay_method = %s';
367  array_push($data_types, 'integer');
368  array_push($data_values, $_SESSION['pay_objects']['pay_method']);
369  }
370 
371  $res = $ilDB->queryf($query, $data_types, $data_values);
372 
373  while($row = $ilDB->fetchObject($res))
374  {
375  $objects[$row->pobject_id]['pobject_id'] = $row->pobject_id;
376  $objects[$row->pobject_id]['ref_id'] = $row->ref_id;
377  $objects[$row->pobject_id]['status'] = $row->status;
378  $objects[$row->pobject_id]['pay_method'] = $row->pay_method;
379  $objects[$row->pobject_id]['vendor_id'] = $row->vendor_id;
380  $objects[$row->pobject_id]['topic_id'] = $row->pt_topic_fk;
381  $objects[$row->pobject_id]['vat_id'] = $row->vat_id;
382  }
383  return $objects ? $objects : array();
384  }
385 
386  function _getObjectData($a_id)
387  {
388  global $ilDB;
389 
390  $res = $ilDB->queryf('
391  SELECT * FROM payment_objects
392  WHERE pobject_id = %s',
393  array('integer'), array($a_id));
394 
395  if (is_object($res))
396  {
397  return $ilDB->fetchAssoc($res);
398 
399  }
400 
401  return false;
402  }
403 
404  function _isPurchasable($a_ref_id, $a_vendor_id = 0, $a_check_trustee = false, $a_check_status = false)
405  {
406  global $ilDB;
407 
408  // In the moment it's not possible to sell one object twice
409 
410  $data = array();
411  $data_types = array();
412 
413 
414  $query = 'SELECT * FROM payment_objects WHERE ref_id = %s ';
415  $data_types[] = 'integer';
416  $data[]= $a_ref_id;
417 
418  // check if object is buyable !!
419  if($a_check_status)
420  {
421  $query .= 'AND status > %s ';
422  $data_types[] = 'integer';
423  $data[] = 0;
424  }
425 
426  if ($a_vendor_id > 0)
427  {
428  $query .= 'AND vendor_id = %s';
429  $data_types[] = 'integer';
430  $data[] = $a_vendor_id;
431 
432  if($a_check_trustee)
433  {
434  include_once './Services/Payment/classes/class.ilPaymentTrustees.php';
435  include_once './Services/Payment/classes/class.ilPaymentVendors.php';
436 
437  $vendors = ilPaymentTrustees::_getVendorsForObjects($a_vendor_id);
438  if(ilPaymentVendors::_isVendor($a_user_id))
439  {
440  $vendors[] = $a_user_id;
441  }
442 
443  if(is_array($vendors) && count($vendors))
444  {
445  $query .= ' OR '.$ilDB->in('vendor_id', $vendors, false, 'integer');
446  }
447  }
448  }
449 
450  $res = $ilDB->queryf($query, $data_types, $data);
451  $rows = $ilDB->numRows($res);
452 
453  return $rows ? true : false;
454 
455  }
456 
457  // base method to check access for a specific object
458  function _hasAccess($a_ref_id, $a_transaction = 0)
459  {
460  include_once './Services/Payment/classes/class.ilPaymentBookings.php';
461  include_once './Services/Payment/classes/class.ilPaymentTrustees.php';
462  include_once './Services/Payment/classes/class.ilPaymentVendors.php';
463 
464  global $rbacsystem,$ilDB, $ilUser;
465 
466  // check write access
467  if($rbacsystem->checkAccess('write', $a_ref_id))
468  {
469  return true;
470  }
471  // check if user is vendor/trustee
472  $vendors_of_trustee = ilPaymentTrustees::_getVendorIdsByTrustee($ilUser->getId());
473 
474  include_once './Services/Payment/classes/class.ilGeneralSettings.php';
475  if(!(bool)ilGeneralSettings::_getInstance()->get('shop_enabled'))
476  {
477  return true;
478  }
479 
480  $result = $ilDB->queryf('
481  SELECT * FROM payment_objects
482  WHERE ref_id = %s
483  AND (status = %s OR status = %s)
484  OR (vendor_id = %s)',
485  array('integer', 'integer', 'integer','integer'),
486  array($a_ref_id, '1', '2',$ilUser->getId()));
487  while($row = $ilDB->fetchObject($result))
488  {
489  if($row->vendor_id == $ilUser->getId() || in_array($row->vendor_id, $vendors_of_trustee))
490  {
491  return true;
492  }
493  else
494  if(!ilPaymentBookings::_hasAccess($row->pobject_id, '', $a_transaction))
495  {
496  return false;
497  }
498  }
499  return true;
500  }
501 
502  // base method to check access for a specific object
503  function _getActivation($a_ref_id)
504  {
505  include_once './Services/Payment/classes/class.ilPaymentBookings.php';
506 
507  global $rbacsystem,$ilDB;
508 
509  $res = $ilDB->queryf('
510  SELECT * FROM payment_objects
511  WHERE ref_id = %s
512  AND (status = %s OR status = %s)',
513  array('integer', 'integer', 'integer'),
514  array($a_ref_id, '1', '2'));
515 
516  $row = $ilDB->fetchObject($res);
517  return ilPaymentBookings::_getActivation($row->pobject_id);
518  }
519 
520  public static function _isBuyable($a_ref_id)
521  {
522  global $ilDB;
523 
524  include_once './Services/Payment/classes/class.ilGeneralSettings.php';
525  if(!(bool)ilGeneralSettings::_getInstance()->get('shop_enabled'))
526  {
527  return false;
528  }
529 
530  $result = $ilDB->queryf('
531  SELECT * FROM payment_objects
532  WHERE ref_id = %s AND (status = %s or status = %s)',
533  array('integer', 'integer', 'integer'),
534  array($a_ref_id, '1', '2'));
535 
536  while($row = $ilDB->fetchObject($result))
537  {
538  return true;
539  }
540  return false;
541  }
542 
543  // checks if this new object already exists in payment_objects
544  public static function _isNewObject($a_ref_id)
545  {
546  global $ilDB;
547 
548  $res = $ilDB->queryF('SELECT * FROM payment_objects WHERE ref_id = %s',
549  array('integer'), array($a_ref_id));
550 
551  $rows = $ilDB->numRows($res);
552 
553  return $rows ? false : true;
554  }
555 
556  public static function _requiresPurchaseToAccess($a_ref_id)
557  {
558  return (bool)(self::_isBuyable($a_ref_id) && !self::_hasAccess($a_ref_id));
559  }
560 
561  public static function _isInCart($a_ref_id)
562  {
563  global $ilDB, $ilUser;
564 
565  if(ANONYMOUS_USER_ID == $ilUser->getId())
566  {
567  $result = $ilDB->queryf('
568  SELECT psc_id FROM payment_objects po, payment_shopping_cart psc
569  WHERE ref_id = %s
570  AND session_id = %s
571  AND po.pobject_id = psc.pobject_id',
572  array('integer', 'text'),
573  array($a_ref_id, session_id()));
574  }
575  else
576  {
577  $result = $ilDB->queryf('
578  SELECT psc_id FROM payment_objects po, payment_shopping_cart psc
579  WHERE ref_id = %s
580  AND customer_id = %s
581  AND po.pobject_id = psc.pobject_id',
582  array('integer', 'integer'),
583  array($a_ref_id, $ilUser->getId()));
584  }
585  while($row = $ilDB->fetchObject($result))
586  {
587  return true;
588  }
589 
590  return false;
591  }
592 
593  private function __read()
594  {
595  if($this->getPobjectId())
596  {
597  $result = $this->db->queryf('SELECT * FROM payment_objects WHERE pobject_id = %s',
598  array('integer'), array($this->getPobjectId()));
599 
600  while($row = $this->db->fetchObject($result))
601  {
602  $this->setRefId($row->ref_id);
603  $this->setStatus($row->status);
604  $this->setPayMethod($row->pay_method);
605  $this->setVendorId($row->vendor_id);
606  $this->setTopicId($row->pt_topic_fk);
607  $this->setVatId($row->vat_id);
608 
609  return true;
610  }
611  }
612 
613  return false;
614  }
615 } // END class.ilPaymentObject
616 ?>