ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilPaymentCoupons.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once './Services/Payment/classes/class.ilPaymentVendors.php';
5 include_once './Services/Payment/classes/class.ilPaymentTrustees.php';
6 
15 {
16  private $db = null;
17 
18  private $user_obj = null;
19  private $vendor_view = false;
20 
21  private $coupons = array();
22  private $current_coupon = array();
23  private $codes = array();
24  private $used_codes = array();
25  private $objects = array();
26 
27  private $id = null;
28  private $coupon_user = null;
29  private $title = null;
30  private $description = null;
31  private $type = null;
32  private $value = null;
33  private $from = null;
34  private $till = null;
35  private $from_date_enabled = null;
36  private $till_date_enabled = null;
37  private $change_date = null;
38  private $uses = null;
39  private $search_title_type = null;
40  private $search_title_value = null;
41  private $search_type = null;
42  private $search_from_day = null;
43  private $search_from_month = null;
44  private $search_from_year = null;
45  private $search_till_day = null;
46  private $search_till_month = null;
47  private $search_till_year = null;
48  private $search_from_enabled = null;
49  private $search_till_enabled = null;
50 
51 
52 
53  public function __construct($user_obj, $a_vendor_view = false)
54  {
55  global $ilDB;
56 
57  $this->db = $ilDB;
58  $this->vendor_view = $a_vendor_view;
59  $this->user_obj = $user_obj;
60 
61  $this->COUPON_VALID = 0;
62  $this->COUPON_OUT_OF_DATE = 1;
63  $this->COUPON_TOO_MUCH_USED = 2;
64  $this->COUPON_NOT_FOUND = 3;
65  }
66 
67  public function getCoupons()
68  {
69 
70  $this->coupons = array();
71 
72  $data = array();
73  $data_types = array();
74 
75  $query = 'SELECT * FROM payment_coupons WHERE 1 = 1 ';
76 
77  if ($_SESSION['pay_coupons']['from']['date']['d'] != '' &&
78  $_SESSION['pay_coupons']['from']['date']['m'] != '' &&
79  $_SESSION['pay_coupons']['from']['date']['y'] != '')
80  {
81 
82  $from = date('Y-m-d',mktime(0, 0, 0, $_SESSION['pay_coupons']['from']['date']['m'],
83  $_SESSION['pay_coupons']['from']['date']['d'],
84  $_SESSION['pay_coupons']['from']['date']['y']));
85 
86 
87  $query .= 'AND pc_from >= %s ';
88  $data_types[] = 'date';
89  $data[] = $from;
90  }
91  if ($_SESSION['pay_coupons']['til']['date']['d'] != '' &&
92  $_SESSION['pay_coupons']['til']['date']['m'] != '' &&
93  $_SESSION['pay_coupons']['til']['date']['y'] != '')
94  {
95  $til = date('Y-m-d',mktime(23, 59, 59, $_SESSION['pay_coupons']['til']['date']['m'],
96  $_SESSION['pay_coupons']['til']['date']['d'],
97  $_SESSION['pay_coupons']['til']['date']['y']));
98  $query .= 'AND pc_till <= %s ';
99  $data_types[] = 'date';
100  $data[] = $til;
101  }
102 
103  if ($this->getSearchTitleValue() != "")
104  {
105  if ($this->getSearchTitleType() == 0)
106  {
107  $query .= " AND pc_title LIKE %s ";
108  array_push($data, $this->getSearchTitleValue().'%');
109  array_push($data_types, 'text');
110  }
111  else if ($this->getSearchTitleType() == 1)
112  {
113  $query .= " AND pc_title LIKE %s ";
114  array_push($data, '%'.$this->getSearchTitleValue());
115  array_push($data_types,'text');
116  }
117  }
118 
119  if ($this->getSearchType() != "")
120  {
121  $query .= ' AND pc_type = %s';
122  array_push($data, $this->getSearchType());
123  array_push($data_types, 'text');
124  }
125 
126  $vendors = $this->getVendorIds();
127  if (is_array($vendors) &&
128  count($vendors) > 0)
129  {
130  $in = 'usr_id IN (';
131  $counter = 0;
132  foreach($vendors as $vendor)
133  {
134  array_push($data, $vendor);
135  array_push($data_types, 'integer');
136 
137  if($counter > 0) $in .= ',';
138  $in .= '%s';
139  ++$counter;
140  }
141  $in .= ')';
142 
143  $query .= ' AND '.$in;
144  }
145 
146 
147  $cnt_data = count($data);
148  $cnt_data_types = count($data_types);
149 
150  if($cnt_data == 0 && $cnt_data_types == 0)
151  {
152  $res = $this->db->query($query);
153  }
154  else
155  {
156  $res= $this->db->queryf($query, $data_types, $data);
157  }
158 
159  while($row = $this->db->fetchObject($res))
160  {
161  $this->coupons[$row->pc_pk]['pc_pk'] = $row->pc_pk;
162  $this->coupons[$row->pc_pk]['usr_id'] = $row->usr_id;
163  $this->coupons[$row->pc_pk]['pc_title'] = $row->pc_title;
164  $this->coupons[$row->pc_pk]['pc_description'] = $row->pc_description;
165  $this->coupons[$row->pc_pk]['pc_type'] = $row->pc_type;
166  $this->coupons[$row->pc_pk]['pc_value'] = $row->pc_value;
167  $this->coupons[$row->pc_pk]['pc_from'] = $row->pc_from;
168  $this->coupons[$row->pc_pk]['pc_till'] = $row->pc_till;
169  $this->coupons[$row->pc_pk]['pc_from_enabled'] = $row->pc_from_enabled;
170  $this->coupons[$row->pc_pk]['pc_till_enabled'] = $row->pc_till_enabled;
171  $this->coupons[$row->pc_pk]['pc_uses'] = $row->pc_uses;
172  $this->coupons[$row->pc_pk]['pc_last_change_usr_id'] = $row->pc_last_change_usr_id;
173  $this->coupons[$row->pc_pk]['pc_last_changed'] = $row->pc_last_changed;
174  $this->coupons[$row->pc_pk]['number_of_codes'] = count($this->getCodesByCouponId($row->pc_pk));
175  $this->coupons[$row->pc_pk]['usage_of_codes'] = count($this->getUsedCouponsByCouponId($row->pc_pk));
176  $this->coupons[$row->pc_pk]['objects'] = $this->getObjectsByCouponId($row->pc_pk);
177  }
178 
179  return $this->coupons;
180  }
181 
182  private function getVendorIds()
183  {
184  $vendors[] = $this->user_obj->getId();
185  if (ilPaymentVendors::_isVendor($this->user_obj->getId()))
186  {
187  $ptObj = new ilPaymentTrustees($this->user_obj);
188 
189  if ($trustees = $ptObj->getTrustees())
190  {
191  foreach ($trustees as $trustee)
192  {
193  if ((bool) $trustee["perm_coupons"])
194  {
195  $vendors[] = $trustee["trustee_id"];
196  }
197  }
198  }
199  }
200  if ($vend = ilPaymentTrustees::_getVendorsForCouponsByTrusteeId($this->user_obj->getId()))
201  {
202  foreach ($vend as $v)
203  {
204  $vendors[] = $v;
206  {
207  foreach ($trustees as $t)
208  {
209  $vendors[] = $t;
210  }
211  }
212  }
213  }
214 
215  return $vendors ? $vendors : array();
216  }
217 
218  // Object Data
219  public function setId($a_id)
220  {
221  return $this->id = $a_id;
222  }
223  public function getId()
224  {
225  return $this->id;
226  }
227  public function setCouponUser($a_user_id)
228  {
229  $this->coupon_user = $a_user_id;
230  }
231  public function getCouponUser()
232  {
233  return $this->coupon_user;
234  }
235  public function setTitle($a_title)
236  {
237  $this->title = $a_title;
238  }
239  public function getTitle()
240  {
241  return $this->title;
242  }
243  public function setDescription($a_description)
244  {
245  $this->description = $a_description;
246  }
247  public function getDescription()
248  {
249  return $this->description;
250  }
251  public function setType($a_type)
252  {
253  $this->type = $a_type;
254  }
255  public function getType()
256  {
257  return $this->type;
258  }
259  public function setValue($a_value)
260  {
261  $this->value = $a_value;
262  }
263  public function getValue()
264  {
265  return $this->value;
266  }
267  public function setFromDate($a_from)
268  {
269  $this->from = $a_from;
270  }
271  public function getFromDate()
272  {
273  return $this->from;
274  }
275  public function setTillDate($a_till)
276  {
277  $this->till = $a_till;
278  }
279  public function getTillDate()
280  {
281  return $this->till;
282  }
283  public function setFromDateEnabled($a_from_date_enabled = 0)
284  {
285  if($a_from_date_enabled == NULL) $a_from_date_enabled = 0;
286  $this->from_date_enabled = $a_from_date_enabled;
287  }
288  public function getFromDateEnabled()
289  {
291  }
292  public function setTillDateEnabled($a_till_date_enabled = 0)
293  {
294  if($a_till_date_enabled == NULL) $a_till_date_enabled = 0;
295  $this->till_date_enabled = $a_till_date_enabled;
296  }
297  public function getTillDateEnabled()
298  {
300  }
301  public function setChangeDate($a_date)
302  {
303  if($a_date == '0000-00-00 00:00:00')
304  $this->change_date = NULL;
305  else
306  $this->change_date = $a_date;
307  }
308  public function getChangeDate()
309  {
310  return $this->change_date;
311  }
312  public function setUses($a_uses)
313  {
314  $this->uses = $a_uses;
315  }
316  public function getUses()
317  {
318  return $this->uses;
319  }
320 
321  // Search Data
322  public function setSearchTitleType($a_title_type)
323  {
324  $this->search_title_type = $a_title_type;
325  }
326  public function getSearchTitleType()
327  {
329  }
330  public function setSearchTitleValue($a_title_value)
331  {
332  $this->search_title_value = $a_title_value;
333  }
334  public function getSearchTitleValue()
335  {
337  }
338  public function setSearchType($a_type)
339  {
340  $this->search_type = $a_type;
341  }
342  public function getSearchType()
343  {
344  return $this->search_type;
345  }
346  public function setSearchFromDay($a_day)
347  {
348  $this->search_from_day = $a_day;
349  }
350  public function getSearchFromDay()
351  {
352  return $this->search_from_day;
353  }
354  public function setSearchFromMonth($a_month)
355  {
356 
357  $this->search_from_month = $a_month;
358  }
359  public function getSearchFromMonth()
360  {
362  }
363  public function setSearchFromYear($a_year)
364  {
365  $this->search_from_year = $a_year;
366  }
367  public function getSearchFromYear()
368  {
370  }
371  public function setSearchTillDay($a_day)
372  {
373  $this->search_till_day = $a_day;
374  }
375  public function getSearchTillDay()
376  {
377  return $this->search_till_day;
378  }
379  public function setSearchTillMonth($a_month)
380  {
381  $this->search_till_month = $a_month;
382  }
383  public function getSearchTillMonth()
384  {
386  }
387  public function setSearchTillYear($a_year)
388  {
389  $this->search_till_year = $a_year;
390  }
391  public function getSearchTillYear()
392  {
394  }
395  public function setSearchFromDateEnabled($a_from_enabled)
396  {
397  $this->search_from_enabled = $a_from_enabled;
398  }
399  public function getSearchFromDateEnabled()
400  {
402  }
403  public function setSearchTillDateEnabled($a_till_enabled)
404  {
405  $this->search_till_enabled = $a_till_enabled;
406  }
407  public function getSearchTillDateEnabled()
408  {
410  }
411 
412  public function setCurrentCoupon($coupon = array())
413  {
414  $this->current_coupon = $coupon;
415  }
416  public function getCurrentCoupon()
417  {
418  return $this->current_coupon;
419  }
420 
421  public function add()
422  {
423  $next_id = $this->db->nextId('payment_coupons');
424 
425  $statement = $this->db->manipulateF('
426  INSERT INTO payment_coupons
427  ( pc_pk,
428  usr_id,
429  pc_title,
430  pc_description,
431  pc_type,
432  pc_value,
433  pc_from,
434  pc_till,
435  pc_from_enabled,
436  pc_till_enabled,
437  pc_uses,
438  pc_last_change_usr_id,
439  pc_last_changed
440  )
441  VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)',
442  array( 'integer',
443  'integer',
444  'text',
445  'text',
446  'text',
447  'float',
448  'date',
449  'date',
450  'integer',
451  'integer',
452  'integer',
453  'integer',
454  'timestamp'),
455  array( $next_id,
456  $this->getCouponUser(),
457  $this->getTitle(),
458  $this->getDescription(),
459  $this->getType(),
460  $this->getValue(),
461  $this->getFromDate(),
462  $this->getTillDate(),
463  $this->getFromDateEnabled(),
464  $this->getTillDateEnabled(),
465  $this->getUses(),
466  $this->getCouponUser(),
467  $this->getChangeDate() )
468  );
469 
470  return $next_id;
471  }
472 
473  public function update()
474  {
475  if ($this->getId())
476  {
477  $statement = $this->db->manipulateF('
478  UPDATE payment_coupons
479  SET pc_title = %s,
480  pc_description = %s,
481  pc_type = %s,
482  pc_value = %s,
483  pc_from = %s,
484  pc_till = %s,
485  pc_from_enabled = %s,
486  pc_till_enabled = %s,
487  pc_uses = %s,
488  pc_last_change_usr_id = %s,
489  pc_last_changed = %s
490  WHERE pc_pk = %s',
491  array( 'text',
492  'text',
493  'text',
494  'float',
495  'date',
496  'date',
497  'integer',
498  'integer',
499  'integer',
500  'integer',
501  'timestamp',
502  'integer'),
503  array( $this->getTitle(),
504  $this->getDescription(),
505  $this->getType(),
506  $this->getValue(),
507  $this->getFromDate(),
508  $this->getTillDate(),
509  $this->getFromDateEnabled(),
510  $this->getTillDateEnabled(),
511  $this->getUses(),
512  $this->getCouponUser(),
513  $this->getChangeDate(),
514  $this->getId()
515  ));
516 
517  return true;
518  }
519  return false;
520  }
521 
522  public function delete()
523  {
524  if ($this->getId())
525  {
526  $statement = $this->db->manipulateF('
527  DELETE FROM payment_coupons WHERE pc_pk = %s',
528  array('integer'),
529  array($this->getId()));
530 
531  return true;
532  }
533  return false;
534  }
535 
536  public function getCouponById($a_coupon_id)
537  {
538  $res = $this->db->queryf('
539  SELECT * FROM payment_coupons
540  WHERE pc_pk = %s', array('integer'), array($a_coupon_id));
541 
542  while($row = $this->db->fetchObject($res))
543  {
544  $this->setId($row->pc_pk);
545  $this->setCouponUser($row->usr_id);
546  $this->setTitle($row->pc_title);
547  $this->setDescription($row->pc_description);
548  $this->setType($row->pc_type);
549  $this->setValue($row->pc_value);
550  $this->setFromDate($row->pc_from);
551  $this->setTillDate($row->pc_till);
552  $this->setFromDateEnabled($row->pc_from_enabled);
553  $this->setTillDateEnabled($row->pc_till_enabled);
554  $this->setUses($row->pc_uses);
555  $this->setChangeDate(date("Y-m-h H:i:s"));
556  }
557  }
558 
559  public function getCouponBonus($a_item_price)
560  {
561  if (is_array($coupon = $this->getCurrentCoupon()))
562  {
563  switch ($coupon["pc_type"])
564  {
565  case "fix":
566  return (float) $coupon["pc_value"];
567  case "percent":
568  return (float) $a_item_price * ($coupon["pc_value"] / 100);
569  }
570  }
571 
572  return 0;
573  }
574 
575  public function getObjectsByCouponId($a_coupon_id)
576  {
577  $this->objects = array();
578 
579  $res = $this->db->queryf('
580  SELECT * FROM payment_coupons_obj
581  WHERE pco_pc_fk = %s',
582  array('integer'),
583  array($a_coupon_id));
584 
585  while($row = $this->db->fetchObject($res))
586  {
587  $this->objects[] = $row->ref_id;
588  }
589 
590  return $this->objects;
591  }
592 
593  public function getCodesByCouponId($a_coupon_id)
594  {
595  $this->codes = array();
596 
597  $res = $this->db->queryf('
598  SELECT payment_coupons_codes.*, COUNT(pct_pcc_fk) pcc_used, pcc_pk
599  FROM payment_coupons_codes
600  LEFT JOIN payment_coupons_track ON pct_pcc_fk = pcc_pk
601  WHERE pcc_pc_fk = %s
602  GROUP BY pcc_pk, payment_coupons_codes.pcc_pc_fk ,pcc_code',
603  array('integer'),
604  array($a_coupon_id));
605 
606  while($row = $this->db->fetchObject($res))
607  {
608  $this->codes[$row->pcc_pk]['pcc_pk'] = $row->pcc_pk;
609  $this->codes[$row->pcc_pk]['pcc_code'] = $row->pcc_code;
610  $this->codes[$row->pcc_pk]['pcc_used'] = $row->pcc_used;
611  }
612 
613  return $this->codes;
614  }
615 
616  public function getUsedCouponsByCouponId($a_coupon_id)
617  {
618  $this->used_codes = array();
619 
620  $res = $this->db->queryf('
621  SELECT * FROM payment_coupons_track
622  INNER JOIN payment_coupons_codes ON pcc_pk = pct_pcc_fk
623  WHERE pcc_pc_fk = %s',
624  array('integer'),
625  array($a_coupon_id));
626 
627  while($row = $this->db->fetchObject($res))
628  {
629  $this->used_codes[$row->pct_pk]['pct_pk'] = $row->pct_pk;
630  $this->used_codes[$row->pct_pk]['pcc_code'] = $row->pcc_code;
631  $this->used_codes[$row->pct_pk]['usr_id'] = $row->usr_id;
632  $this->used_codes[$row->pct_pk]['pct_date'] = $row->pct_date;
633  $this->used_codes[$row->pct_pk]['pct_ps_fk'] = $row->pct_ps_fk;
634  }
635 
636  return $this->used_codes;
637  }
638 
639  public function getCouponByCode($a_coupon_code)
640  {
641  $res = $this->db->queryf('
642  SELECT * FROM payment_coupons_codes
643  INNER JOIN payment_coupons ON pc_pk = pcc_pc_fk
644  WHERE pcc_code = %s',
645  array('text'),
646  array($a_coupon_code));
647 
648  $coupon = array();
649 
650  if (is_object($row = $this->db->fetchObject($res)))
651  {
652  $coupon['pc_pk'] = $row->pc_pk;
653  $coupon['pc_title'] = $row->pc_title;
654  $coupon['pc_description'] = $row->pc_description;
655  $coupon['pc_type'] = $row->pc_type;
656  $coupon['pc_value'] = $row->pc_value;
657  $coupon['pc_type'] = $row->pc_type;
658  $coupon['pc_from'] = $row->pc_from;
659  $coupon['pc_till'] = $row->pc_till;
660  $coupon['pc_uses'] = $row->pc_uses;
661  $coupon['pcc_pk'] = $row->pcc_pk;
662  $coupon['pcc_code'] = $row->pcc_code;
663  $coupon['objects'] = $this->getObjectsByCouponId($row->pc_pk);
664  }
665 
666  $this->setId($coupon['pc_pk']);
667  $this->setCurrentCoupon($coupon);
668 
669  return $coupon ? $coupon : array();
670  }
671 
672  public function checkCouponValidity()
673  {
674  $coupon = $this->getCurrentCoupon();
675 
676  if (empty($coupon)) return $this->COUPON_NOT_FOUND;
677 
678  $current_date = date("Y-m-d");
679  if ($coupon["pc_from"] != "0000-00-00" && $coupon["pc_from_enabled"] == '1' &&
680  $coupon["pc_till"] != "0000-00-00" && $coupon["pc_till_enabled"] == '1'
681  )
682  {
683  if (! ($coupon["pc_from"] <= $current_date && $current_date <= $coupon["pc_till"]))
684  {
685  return $this->COUPON_OUT_OF_DATE;
686  }
687  }
688  else if ($coupon["pc_from"] != "0000-00-00" && $coupon["pc_from_enabled"] == '1')
689  {
690  if ($coupon["pc_from"] > $current_date)
691  {
692  return $this->COUPON_OUT_OF_DATE;
693  }
694  }
695  else if ($coupon["pc_till"] != "0000-00-00" && $coupon["pc_till_enabled"] == '1')
696  {
697  if ($coupon["pc_till"] < $current_date)
698  {
699  return $this->COUPON_OUT_OF_DATE;
700  }
701  }
702 
703  if (is_numeric($coupon["pc_uses"]) && $coupon["pc_uses"] > 0)
704  {
705  $res = $this->db->queryf('
706  SELECT COUNT(*) used_coupons
707  FROM payment_coupons_track
708  WHERE pct_pcc_fk = %s',
709  array('integer'),
710  array($coupon['pcc_pk']));
711 
712  $row = $this->db->fetchObject($res);
713 
714  if ($row->used_coupons >= $coupon["pc_uses"]) return $this->COUPON_TOO_MUCH_USED;
715  }
716 
717  return $this->COUPON_VALID;
718  }
719 
720  public function deleteCode($a_code_id)
721  {
722  if ($a_code_id)
723  {
724  $statement = $this->db->manipulateF('
725  DELETE FROM payment_coupons_codes WHERE pcc_pk = %s',
726  array('integer'), array($a_code_id));
727 
728  return true;
729  }
730  return false;
731  }
732 
733  public function deleteAllCodesByCouponId($a_coupon_id)
734  {
735  if ($a_coupon_id)
736  {
737  $statement = $this->db->manipulateF('
738  DELETE FROM payment_coupons_codes WHERE pcc_pc_fk = %s',
739  array('integer'),array($a_coupon_id));
740 
741  return true;
742  }
743  return false;
744  }
745 
746  public function getCode($a_code_id)
747  {
748  $res = $this->db->queryf('
749  SELECT * FROM payment_coupons_codes
750  WHERE pcc_pk = %s',
751  array('integer'),
752  array($a_code_id));
753 
754  $code = array();
755 
756  while($row = $this->db->fetchObject($res))
757  {
758  $code['pcc_pk'] = $row->pcc_pk;
759  $code['pcc_pc_fk'] = $row->pcc_pc_fk;
760  $code['pcc_code'] = $row->pcc_code;
761  }
762  return $code ? $code : array();
763  }
764 
765  public function addCode($a_code, $a_coupon_id)
766  {
767  if ($a_code && $a_coupon_id)
768  {
769  $next_id = $this->db->nextId('payment_coupons_codes');
770  $statement = $this->db->manipulateF('
771  INSERT INTO payment_coupons_codes
772  ( pcc_pk,
773  pcc_pc_fk,
774  pcc_code
775  )
776  VALUES (%s,%s,%s)',
777  array('integer','integer', 'text'),
778  array($next_id, $a_coupon_id, $a_code));
779 
780  return $next_id;
781  }
782  return false;
783  }
784 
785  public function addCouponForBookingId($a_booking_id)
786  {
788 
789  if ($a_booking_id && is_array($current_coupon))
790  {
791  $statement = $this->db->manipulateF('
792  INSERT INTO payment_statistic_coup
793  ( psc_ps_fk,
794  psc_pc_fk,
795  psc_pcc_fk
796  ) VALUES(%s,%s,%s)',
797  array('integer', 'integer', 'integer'),
798  array($a_booking_id, $current_coupon['pc_pk'], $current_coupon['pcc_pk']));
799  }
800  return false;
801  }
802 
803  public function addTracking()
804  {
806 
807  if (is_array($current_coupon))
808  {
809  $next_id = $this->db->nextId('payment_coupons_track');
810  $statement = $this->db->manipulateF('
811  INSERT INTO payment_coupons_track
812  ( pct_pk,
813  pct_pcc_fk ,
814  usr_id,
815  pct_date
816  )
817  VALUES (%s, %s, %s, %s)',
818  array('integer','integer', 'integer', 'timestamp'),
819  array($next_id, $current_coupon['pcc_pk'], $this->user_obj->getId(), date("Y-m-d H:i:s")));
820 
821  return $next_id;
822  }
823  return false;
824  }
825 
832  public function isObjectAssignedToCoupon($a_ref_id)
833  {
834  if ($a_ref_id && is_numeric($this->getId()))
835  {
836  $res = $this->db->queryf('
837  SELECT * FROM payment_coupons_obj
838  WHERE ref_id = %s
839  AND pco_pc_fk = %s',
840  array('integer', 'integer'),
841  array($a_ref_id, $this->getId()));
842 
843  if ($res->numRows()) return true;
844 
845  return false;
846  }
847  return false;
848  }
849 
856  public function assignObjectToCoupon($a_ref_id)
857  {
858  if ($a_ref_id && is_numeric($this->getId()))
859  {
860  $statement = $this->db->manipulateF('
861  INSERT INTO payment_coupons_obj
862  ( pco_pc_fk,
863  ref_id
864  ) VALUES(%s, %s)',
865  array('integer', 'integer'),
866  array($this->getId(), $a_ref_id));
867 
868  return true;
869  }
870  return false;
871  }
872 
879  public function unassignObjectFromCoupon($a_ref_id)
880  {
881  if ($a_ref_id && is_numeric($this->getId()))
882  {
883  $statement = $this->db->manipulateF('
884  DELETE FROM payment_coupons_obj
885  WHERE ref_id = %s
886  AND pco_pc_fk = %s',
887  array('integer', 'integer'),
888  array($a_ref_id, $this->getId()));
889 
890  return true;
891  }
892  return false;
893  }
899  public function deleteCouponByCouponId($a_pc_pk)
900  {
901  global $ilDB;
902 
903  $res = $ilDB->queryF('
904  SELECT pcc_pk
905  FROM payment_coupons_codes
906  WHERE pcc_pc_fk = %s',
907  array('integer'),array($a_pc_pk));
908 
909  $code_ids = array();
910  while($row = $ilDB->fetchAssoc($res))
911  {
912  $code_ids[] = $row['pcc_pk'];
913  }
914 
915  $ilDB->manipulate('
916  DELETE FROM payment_coupons_track
917  WHERE '. $ilDB->in('pct_pcc_fk', $code_ids, false, 'integer'));
918 
919  $ilDB->manipulate('
920  DELETE FROM payment_statistic_coup
921  WHERE '.$ilDB->in('psc_pcc_fk', $code_ids, false, 'integer'));
922 
923  $ilDB->manipulateF('
924  DELETE FROM payment_coupons
925  WHERE pc_pk = %s',
926  array('integer'),array($a_pc_pk));
927 
928  $ilDB->manipulateF('
929  DELETE FROM payment_coupons_obj
930  WHERE pco_pc_fk = %s',
931  array('integer'),array($a_pc_pk));
932 
933  $ilDB->manipulateF('
934  DELETE FROM payment_coupons_codes
935  WHERE pcc_pk = %s',
936  array('integer'),array($a_pc_pk));
937 
938  }
939 
940  public static function _lookupTitle($a_coupon_id)
941  {
942  global $ilDB;
943 
944  $res = $ilDB->queryF('SELECT pc_title FROM payment_coupons WHERE pc_pk = %s',
945  array('integer'), array($a_coupon_id));
946 
947  $row = $ilDB->fetchAssoc($res);
948  return $row['pc_title'];
949  }
950 }
951 ?>