2 include_once
'./payment/classes/class.ilPaymentVendors.php';
3 include_once
'./payment/classes/class.ilPaymentTrustees.php';
56 $this->vendor_view = $a_vendor_view;
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;
68 $this->coupons = array();
71 $data_types = array();
73 $query =
'SELECT * FROM payment_coupons WHERE 1 = 1 ';
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
105 OR UNIX_TIMESTAMP(pc_till) >= %s
106 AND UNIX_TIMESTAMP(pc_till) <= %s
108 OR UNIX_TIMESTAMP(pc_from) >= %s
109 AND UNIX_TIMESTAMP(pc_from) <= %s
111 OR UNIX_TIMESTAMP(pc_from) <= %s
112 AND UNIX_TIMESTAMP(pc_till) >= %s
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)
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');
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)) ';
131 '0000-00-00',
'0000-00-00');
132 array_push($data_types,
'date',
'integer',
'date',
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)) ';
140 '0000-00-00',
'0000-00-00');
141 array_push($data_types,
'date',
'integer',
'date',
149 $query .=
" AND pc_title LIKE %s ";
151 array_push($data_types,
'text');
155 $query .=
" AND pc_title LIKE %s ";
157 array_push($data_types,
'text');
163 $query .=
' AND pc_type = %s';
165 array_push($data_types,
'text');
169 if (is_array($vendors) &&
176 array_push(
$data, $vendor);
177 array_push($data_types,
'integer');
179 if($counter > 0)
$in .=
',';
189 $cnt_data = count(
$data);
190 $cnt_data_types = count($data_types);
192 if($cnt_data == 0 && $cnt_data_types == 0)
201 while(
$row = $this->db->fetchObject(
$res))
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;
226 $vendors[] = $this->user_obj->getId();
231 if ($trustees = $ptObj->getTrustees())
233 foreach ($trustees as $trustee)
235 if ((
bool) $trustee[
"perm_coupons"])
237 $vendors[] = $trustee[
"trustee_id"];
244 foreach ($vend as $v)
249 foreach ($trustees as
$t)
257 return $vendors ? $vendors : array();
263 return $this->
id = $a_id;
271 $this->coupon_user = $a_user_id;
279 $this->title = $a_title;
287 $this->description = $a_description;
295 $this->type = $a_type;
303 $this->value = $a_value;
311 $this->from = $a_from;
319 $this->till = $a_till;
327 if($a_from_date_enabled == NULL) $a_from_date_enabled = 0;
328 $this->from_date_enabled = $a_from_date_enabled;
336 if($a_till_date_enabled == NULL) $a_till_date_enabled = 0;
337 $this->till_date_enabled = $a_till_date_enabled;
345 if($a_date ==
'0000-00-00 00:00:00')
346 $this->change_date = NULLL;
348 $this->change_date = $a_date;
356 $this->uses = $a_uses;
366 $this->search_title_type = $a_title_type;
374 $this->search_title_value = $a_title_value;
382 $this->search_type = $a_type;
390 $this->search_from_day = $a_day;
399 $this->search_from_month = $a_month;
407 $this->search_from_year = $a_year;
415 $this->search_till_day = $a_day;
423 $this->search_till_month = $a_month;
431 $this->search_till_year = $a_year;
439 $this->search_from_enabled = $a_from_enabled;
447 $this->search_till_enabled = $a_till_enabled;
456 $this->current_coupon = $coupon;
465 $next_id = $this->db->nextId(
'payment_coupons');
467 $statement = $this->db->manipulateF(
'
468 INSERT INTO payment_coupons
480 pc_last_change_usr_id,
483 VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)',
519 $statement = $this->db->manipulateF(
'
520 UPDATE payment_coupons
527 pc_from_enabled = %s,
528 pc_till_enabled = %s,
530 pc_last_change_usr_id = %s,
564 public function delete()
568 $statement = $this->db->manipulateF(
'
569 DELETE FROM payment_coupons WHERE pc_pk = %s',
571 array($this->
getId()));
580 $res = $this->db->queryf(
'
581 SELECT * FROM payment_coupons
582 WHERE pc_pk = %s', array(
'integer'), array($a_coupon_id));
584 while(
$row = $this->db->fetchObject(
$res))
605 switch ($coupon[
"pc_type"])
608 return (
float) $coupon[
"pc_value"];
610 return (
float) $a_item_price * ($coupon[
"pc_value"] / 100);
619 $this->objects = array();
621 $res = $this->db->queryf(
'
622 SELECT * FROM payment_coupons_obj
623 WHERE pco_pc_fk = %s',
625 array($a_coupon_id));
627 while(
$row = $this->db->fetchObject(
$res))
629 $this->objects[] =
$row->ref_id;
637 $this->codes = array();
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
644 GROUP BY pcc_pk, payment_coupons_codes.pcc_pc_fk ,pcc_code',
646 array($a_coupon_id));
648 while(
$row = $this->db->fetchObject(
$res))
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;
660 $this->used_codes = array();
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',
667 array($a_coupon_id));
669 while(
$row = $this->db->fetchObject(
$res))
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;
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',
688 array($a_coupon_code));
691 if (is_object(
$row = $this->db->fetchObject(
$res)))
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;
707 $this->
setId($coupon[
'pc_pk']);
710 return $coupon ? $coupon : array();
717 if (empty($coupon))
return $this->COUPON_NOT_FOUND;
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'
724 if (! ($coupon[
"pc_from"] <= $current_date && $current_date <= $coupon[
"pc_till"]))
726 return $this->COUPON_OUT_OF_DATE;
729 else if ($coupon[
"pc_from"] !=
"0000-00-00" && $coupon[
"pc_from_enabled"] ==
'1')
731 if ($coupon[
"pc_from"] > $current_date)
733 return $this->COUPON_OUT_OF_DATE;
736 else if ($coupon[
"pc_till"] !=
"0000-00-00" && $coupon[
"pc_till_enabled"] ==
'1')
738 if ($coupon[
"pc_till"] < $current_date)
740 return $this->COUPON_OUT_OF_DATE;
744 if (is_numeric($coupon[
"pc_uses"]) && $coupon[
"pc_uses"] > 0)
746 $res = $this->db->queryf(
'
747 SELECT COUNT(*) used_coupons
748 FROM payment_coupons_track
749 WHERE pct_pcc_fk = %s',
751 array($coupon[
'pcc_pk']));
753 $row = $this->db->fetchObject(
$res);
755 if (
$row->used_coupons >= $coupon[
"pc_uses"])
return $this->COUPON_TOO_MUCH_USED;
758 return $this->COUPON_VALID;
765 $statement = $this->db->manipulateF(
'
766 DELETE FROM payment_coupons_codes WHERE pcc_pk = %s',
767 array(
'integer'), array($a_code_id));
778 $statement = $this->db->manipulateF(
'
779 DELETE FROM payment_coupons_codes WHERE pcc_pc_fk = %s',
780 array(
'integer'),array($a_coupon_id));
789 $res = $this->db->queryf(
'
790 SELECT * FROM payment_coupons_codes
795 while(
$row = $this->db->fetchObject(
$res))
797 $code[
'pcc_pk'] =
$row->pcc_pk;
798 $code[
'pcc_pc_fk'] =
$row->pcc_pc_fk;
799 $code[
'pcc_code'] =
$row->pcc_code;
801 return $code ? $code : array();
804 public function addCode($a_code, $a_coupon_id)
806 if ($a_code && $a_coupon_id)
808 $next_id = $this->db->nextId(
'payment_coupons_codes');
809 $statement = $this->db->manipulateF(
'
810 INSERT INTO payment_coupons_codes
816 array(
'integer',
'integer',
'text'),
817 array($next_id, $a_coupon_id, $a_code));
830 $statement = $this->db->manipulateF(
'
831 INSERT INTO payment_statistic_coup
836 array(
'integer',
'integer',
'integer'),
850 $next_id = $this->db->nextId(
'payment_coupons_track');
851 $statement = $this->db->manipulateF(
'
852 INSERT INTO payment_coupons_track
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")));
875 if ($a_ref_id && is_numeric($this->
getId()))
877 $res = $this->db->queryf(
'
878 SELECT * FROM payment_coupons_obj
881 array(
'integer',
'integer'),
882 array($a_ref_id, $this->
getId()));
884 if (
$res->numRows())
return true;
899 if ($a_ref_id && is_numeric($this->
getId()))
901 $statement = $this->db->manipulateF(
'
902 INSERT INTO payment_coupons_obj
906 array(
'integer',
'integer'),
907 array($this->
getId(), $a_ref_id));
922 if ($a_ref_id && is_numeric($this->
getId()))
924 $statement = $this->db->manipulateF(
'
925 DELETE FROM payment_coupons_obj
928 array(
'integer',
'integer'),
929 array($a_ref_id, $this->
getId()));