2 include_once
'./payment/classes/class.ilPaymentVendors.php';
3 include_once
'./payment/classes/class.ilPaymentTrustees.php';
30 $this->vendor_view = $a_vendor_view;
33 $this->COUPON_VALID = 0;
34 $this->COUPON_OUT_OF_DATE = 1;
35 $this->COUPON_TOO_MUCH_USED = 2;
36 $this->COUPON_NOT_FOUND = 3;
41 $this->coupons = array();
43 $query =
"SELECT * FROM payment_coupons WHERE 1 ";
65 $query .=
" AND ((pc_from != '0000-00-00' AND pc_till != '0000-00-00' AND pc_from_enabled = '1' AND pc_till_enabled = '1' AND
66 (UNIX_TIMESTAMP(pc_from) >= " . $this->db->quote($from).
" AND UNIX_TIMESTAMP(pc_till) <= " . $this->db->quote($till).
"
67 OR UNIX_TIMESTAMP(pc_till) >= " . $this->db->quote($from).
" AND UNIX_TIMESTAMP(pc_till) <= " . $this->db->quote($till).
"
68 OR UNIX_TIMESTAMP(pc_from) >= " . $this->db->quote($from).
" AND UNIX_TIMESTAMP(pc_from) <= " . $this->db->quote($till).
"
69 OR UNIX_TIMESTAMP(pc_from) <= " . $this->db->quote($from).
" AND UNIX_TIMESTAMP(pc_till) >= " . $this->db->quote($till).
"
71 OR (pc_from != '0000-00-00' AND pc_from_enabled = '1' AND UNIX_TIMESTAMP(pc_from) <= " . $this->db->quote($till).
")
72 OR (pc_till != '0000-00-00' AND pc_till_enabled = '1' AND UNIX_TIMESTAMP(pc_till) >= " . $this->db->quote($from).
")
77 $query .=
" AND ((pc_till != '0000-00-00' AND pc_till_enabled = '1' AND UNIX_TIMESTAMP(pc_till) >= " . $this->db->quote($from).
") OR (pc_from != '0000-00-00' AND pc_till = '0000-00-00')) ";
81 $query .=
" AND ((pc_from != '0000-00-00' AND pc_from_enabled = '1' AND UNIX_TIMESTAMP(pc_from) <= " . $this->db->quote($till).
") OR (pc_from = '0000-00-00' AND pc_till != '0000-00-00')) ";
98 $query .=
" AND pc_type = " . $this->db->quote($this->
getSearchType()) .
" ";
102 if (is_array($vendors) &&
106 $in .= implode(
',',$vendors);
109 $query .=
" AND ".$in.
" ";
112 $res = $this->db->query($query);
115 $this->coupons[$row->pc_pk][
'pc_pk'] = $row->pc_pk;
116 $this->coupons[$row->pc_pk][
'usr_id'] = $row->usr_id;
117 $this->coupons[$row->pc_pk][
'pc_title'] = $row->pc_title;
118 $this->coupons[$row->pc_pk][
'pc_description'] = $row->pc_description;
119 $this->coupons[$row->pc_pk][
'pc_type'] = $row->pc_type;
120 $this->coupons[$row->pc_pk][
'pc_value'] = $row->pc_value;
121 $this->coupons[$row->pc_pk][
'pc_from'] = $row->pc_from;
122 $this->coupons[$row->pc_pk][
'pc_till'] = $row->pc_till;
123 $this->coupons[$row->pc_pk][
'pc_from_enabled'] = $row->pc_from_enabled;
124 $this->coupons[$row->pc_pk][
'pc_till_enabled'] = $row->pc_till_enabled;
125 $this->coupons[$row->pc_pk][
'pc_uses'] = $row->pc_uses;
126 $this->coupons[$row->pc_pk][
'pc_last_change_usr_id'] = $row->pc_last_change_usr_id;
127 $this->coupons[$row->pc_pk][
'pc_last_changed'] = $row->pc_last_changed;
128 $this->coupons[$row->pc_pk][
'number_of_codes'] = count($this->
getCodesByCouponId($row->pc_pk));
138 $vendors[] = $this->user_obj->getId();
143 if ($trustees = $ptObj->getTrustees())
145 foreach ($trustees as $trustee)
147 if ((
bool) $trustee[
"perm_coupons"])
149 $vendors[] = $trustee[
"trustee_id"];
156 foreach ($vend as $v)
161 foreach ($trustees as
$t)
169 return $vendors ? $vendors : array();
175 return $this->
id = $a_id;
183 $this->coupon_user = $a_user_id;
187 return $this->coupon_user;
191 $this->title = $a_title;
199 $this->description = $a_description;
203 return $this->description;
207 $this->type = $a_type;
215 $this->value = $a_value;
223 $this->from = $a_from;
231 $this->till = $a_till;
239 $this->from_date_enabled = $a_from_date_enabled;
243 return $this->from_date_enabled;
247 $this->till_date_enabled = $a_till_date_enabled;
251 return $this->till_date_enabled;
255 $this->change_date = $a_date;
259 return $this->change_date;
263 $this->uses = $a_uses;
273 $this->search_title_type = $a_title_type;
277 return $this->search_title_type;
281 $this->search_title_value = $a_title_value;
285 return $this->search_title_value;
289 $this->search_type = $a_type;
293 return $this->search_type;
297 $this->search_from_day = $a_day;
301 return $this->search_from_day;
305 $this->search_from_month = $a_month;
309 return $this->search_from_month;
313 $this->search_from_year = $a_year;
317 return $this->search_from_year;
321 $this->search_till_day = $a_day;
325 return $this->search_till_day;
329 $this->search_till_month = $a_month;
333 return $this->search_till_month;
337 $this->search_till_year = $a_year;
341 return $this->search_till_year;
345 $this->search_from_enabled = $a_from_enabled;
349 return $this->search_from_enabled;
353 $this->search_till_enabled = $a_till_enabled;
357 return $this->search_till_enabled;
363 $this->current_coupon = $coupon;
372 $query = sprintf(
"INSERT INTO payment_coupons VALUES('', ".
373 " %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
374 $this->db->quote($this->getCouponUser()),
375 $this->db->quote($this->getTitle()),
376 $this->db->quote($this->getDescription()),
377 $this->db->quote($this->getType()),
378 $this->db->quote($this->getValue()),
379 $this->db->quote($this->getFromDate()),
380 $this->db->quote($this->getTillDate()),
381 $this->db->quote($this->getFromDateEnabled()),
382 $this->db->quote($this->getTillDateEnabled()),
383 $this->db->quote($this->getUses()),
388 $this->db->query($query);
390 return $this->db->getLastInsertId();
397 $query =
"UPDATE payment_coupons
399 pc_title = ".$this->db->quote($this->
getTitle()).
",
401 pc_type = ".$this->db->quote($this->
getType()).
",
402 pc_value = ".$this->db->quote($this->
getValue()).
",
403 pc_from = ".$this->db->quote($this->
getFromDate()).
",
404 pc_till = ".$this->db->quote($this->
getTillDate()).
",
407 pc_uses = ".$this->db->quote($this->
getUses()).
",
408 pc_last_change_usr_id = ".$this->db->quote($this->
getCouponUser()).
",
409 pc_last_changed = ".$this->db->quote($this->
getChangeDate()).
"
410 WHERE pc_pk = ".$this->db->quote($this->
getId()).
" ";
411 $this->db->query($query);
418 public function delete()
422 $query =
"DELETE FROM payment_coupons WHERE pc_pk = ".$this->db->quote($this->
getId()).
" ";
424 $this->db->query($query);
436 AND pc_pk = ".$this->db->quote($a_coupon_id).
" ";
438 $res = $this->db->query($query);
441 $this->
setId($row->pc_pk);
460 switch ($coupon[
"pc_type"])
463 return (
float) $coupon[
"pc_value"];
465 return (
float) $a_item_price * ($coupon[
"pc_value"] / 100);
474 $this->objects = array();
476 $query =
"SELECT * FROM payment_coupons_objects WHERE 1 AND pco_pc_fk = ".$this->db->quote($a_coupon_id);
478 $res = $this->db->query($query);
481 $this->objects[] = $row->ref_id;
489 $this->codes = array();
491 $query =
"SELECT payment_coupons_codes.*, COUNT(pct_pcc_fk) AS pcc_used
492 FROM payment_coupons_codes
493 LEFT JOIN payment_coupons_tracking ON pct_pcc_fk = pcc_pk
494 WHERE 1 AND pcc_pc_fk = ".$this->db->quote($a_coupon_id).
"
497 $res = $this->db->query($query);
500 $this->codes[$row->pcc_pk][
'pcc_pk'] = $row->pcc_pk;
501 $this->codes[$row->pcc_pk][
'pcc_code'] = $row->pcc_code;
502 $this->codes[$row->pcc_pk][
'pcc_used'] = $row->pcc_used;
510 $this->used_codes = array();
513 FROM payment_coupons_tracking
514 INNER JOIN payment_coupons_codes ON pcc_pk = pct_pcc_fk
516 AND pcc_pc_fk = ".$this->db->quote($a_coupon_id);
518 $res = $this->db->query($query);
521 $this->used_codes[$row->pct_pk][
'pct_pk'] = $row->pct_pk;
522 $this->used_codes[$row->pct_pk][
'pcc_code'] = $row->pcc_code;
523 $this->used_codes[$row->pct_pk][
'usr_id'] = $row->usr_id;
524 $this->used_codes[$row->pct_pk][
'pct_date'] = $row->pct_date;
525 $this->used_codes[$row->pct_pk][
'pct_ps_fk'] = $row->pct_ps_fk;
534 FROM payment_coupons_codes
535 INNER JOIN payment_coupons ON pc_pk = pcc_pc_fk
537 AND pcc_code = ".$this->db->quote($a_coupon_code).
" ";
539 $res = $this->db->query($query);
542 $coupon[
'pc_pk'] = $row->pc_pk;
543 $coupon[
'pc_title'] = $row->pc_title;
544 $coupon[
'pc_description'] = $row->pc_description;
545 $coupon[
'pc_type'] = $row->pc_type;
546 $coupon[
'pc_value'] = $row->pc_value;
547 $coupon[
'pc_type'] = $row->pc_type;
548 $coupon[
'pc_from'] = $row->pc_from;
549 $coupon[
'pc_till'] = $row->pc_till;
550 $coupon[
'pc_uses'] = $row->pc_uses;
551 $coupon[
'pcc_pk'] = $row->pcc_pk;
552 $coupon[
'pcc_code'] = $row->pcc_code;
556 $this->
setId($coupon[
'pc_pk']);
559 return $coupon ? $coupon : array();
566 if (empty($coupon))
return $this->COUPON_NOT_FOUND;
568 $current_date = date(
"Y-m-d");
569 if ($coupon[
"pc_from"] !=
"0000-00-00" && $coupon[
"pc_from_enabled"] ==
'1' &&
570 $coupon[
"pc_till"] !=
"0000-00-00" && $coupon[
"pc_till_enabled"] ==
'1'
573 if (! ($coupon[
"pc_from"] <= $current_date && $current_date <= $coupon[
"pc_till"]))
575 return $this->COUPON_OUT_OF_DATE;
578 else if ($coupon[
"pc_from"] !=
"0000-00-00" && $coupon[
"pc_from_enabled"] ==
'1')
580 if ($coupon[
"pc_from"] > $current_date)
582 return $this->COUPON_OUT_OF_DATE;
585 else if ($coupon[
"pc_till"] !=
"0000-00-00" && $coupon[
"pc_till_enabled"] ==
'1')
587 if ($coupon[
"pc_till"] < $current_date)
589 return $this->COUPON_OUT_OF_DATE;
593 if (is_numeric($coupon[
"pc_uses"]) && $coupon[
"pc_uses"] > 0)
595 $query =
"SELECT COUNT(*) AS used_coupons
596 FROM payment_coupons_tracking
597 WHERE pct_pcc_fk = ".$this->db->quote($coupon[
"pcc_pk"]).
" ";
599 $this->db->query($query);
600 $res = $this->db->query($query);
603 if ($row->used_coupons >= $coupon[
"pc_uses"])
return $this->COUPON_TOO_MUCH_USED;
606 return $this->COUPON_VALID;
613 $query =
"DELETE FROM payment_coupons_codes WHERE pcc_pk = ".$this->db->quote($a_code_id).
" ";
615 $this->db->query($query);
626 $query =
"DELETE FROM payment_coupons_codes WHERE pcc_pc_fk = ".$this->db->quote($a_coupon_id).
" ";
628 $this->db->query($query);
638 FROM payment_coupons_codes
640 AND pcc_pk = ".$this->db->quote($a_code_id).
" ";
642 $res = $this->db->query($query);
645 $code[
'pcc_pk'] = $row->pcc_pk;
646 $code[
'pcc_pc_fk'] = $row->pcc_pc_fk;
647 $code[
'pcc_code'] = $row->pcc_code;
649 return $code ? $code : array();
652 public function addCode($a_code, $a_coupon_id)
654 if ($a_code && $a_coupon_id)
656 $query = sprintf(
"INSERT INTO payment_coupons_codes VALUES('', ".
658 $this->db->quote($a_coupon_id),
659 $this->db->quote($a_code)
662 $this->db->query($query);
664 return $this->db->getLastInsertId();
676 $query = sprintf(
"INSERT INTO payment_statistic_coupons VALUES(%s, %s, %s)",
677 $this->db->quote($a_booking_id),
682 $this->db->query($query);
684 return $this->db->getLastInsertId();
695 $query =
"INSERT INTO payment_coupons_tracking "
698 .
"usr_id = ".$this->db->quote($this->user_obj->getId()).
", "
699 .
"pct_date = ".$this->db->quote(date(
"Y-m-d H:i:s")).
" ";
701 $this->db->query($query);
703 return $this->db->getLastInsertId();
716 if ($a_ref_id && is_numeric($this->
getId()))
719 FROM payment_coupons_objects
721 AND ref_id = ".$this->db->quote($a_ref_id).
"
722 AND pco_pc_fk = ".$this->db->quote($this->
getId()).
" ";
724 $res = $this->db->query($query);
726 if (
$res->numRows())
return true;
741 if ($a_ref_id && is_numeric($this->
getId()))
743 $query = sprintf(
"INSERT INTO payment_coupons_objects VALUES(%s, %s)",
744 $this->db->quote($this->getId()),
745 $this->db->quote($a_ref_id)
748 $this->db->query($query);
763 if ($a_ref_id && is_numeric($this->
getId()))
766 FROM payment_coupons_objects
768 AND ref_id = ".$this->db->quote($a_ref_id).
"
769 AND pco_pc_fk = ".$this->db->quote($this->
getId()).
" ";
771 $this->db->query($query);