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