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