4 include_once 
'./Services/Payment/classes/class.ilPaymentVendors.php';
 
    5 include_once 
'./Services/Payment/classes/class.ilPaymentTrustees.php';
 
   58                 $this->vendor_view = $a_vendor_view;
 
   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;                    
 
   70                 $this->coupons = array();
 
   73                 $data_types = array();
 
   75                 $query = 
'SELECT * FROM payment_coupons WHERE 1 = 1 ';
 
   77                 if (
$_SESSION[
'pay_coupons'][
'from'][
'date'][
'd'] != 
'' &&
 
   78                         $_SESSION[
'pay_coupons'][
'from'][
'date'][
'm'] != 
'' &&
 
   79                         $_SESSION[
'pay_coupons'][
'from'][
'date'][
'y'] != 
'')
 
   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']));
 
   87                         $query .= 
'AND pc_from >= %s ';
 
   88                         $data_types[] = 
'date';
 
   91                 if (
$_SESSION[
'pay_coupons'][
'til'][
'date'][
'd'] != 
'' &&
 
   92                         $_SESSION[
'pay_coupons'][
'til'][
'date'][
'm'] != 
'' &&
 
   93                         $_SESSION[
'pay_coupons'][
'til'][
'date'][
'y'] != 
'')
 
   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';
 
  107                                 $query .= 
" AND pc_title LIKE %s ";
 
  109                                 array_push($data_types, 
'text');
 
  113                                 $query .= 
" AND pc_title LIKE %s ";
 
  115                                 array_push($data_types,
'text');                         
 
  121                         $query .= 
' AND pc_type = %s';
 
  123                         array_push($data_types, 
'text');                        
 
  127                 if (is_array($vendors) &&
 
  134                                 array_push(
$data, $vendor);
 
  135                                 array_push($data_types, 
'integer');
 
  137                                 if($counter > 0) 
$in .= 
',';
 
  147                 $cnt_data = count(
$data);
 
  148                 $cnt_data_types = count($data_types);
 
  150                 if($cnt_data == 0 && $cnt_data_types == 0)
 
  159                 while(
$row = $this->db->fetchObject(
$res))
 
  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;                                 
 
  184                 $vendors[] = $this->user_obj->getId();
 
  189                         if ($trustees = $ptObj->getTrustees())
 
  191                                 foreach ($trustees as $trustee)
 
  193                                         if ((
bool) $trustee[
"perm_coupons"])
 
  195                                                 $vendors[] = $trustee[
"trustee_id"];
 
  202                         foreach ($vend as $v)
 
  207                                         foreach ($trustees as 
$t)
 
  215                 return $vendors ? $vendors : array();
 
  221                 return $this->
id = $a_id;
 
  229                 $this->coupon_user = $a_user_id;
 
  237                 $this->title = $a_title;
 
  245                 $this->description = $a_description;
 
  253                 $this->type = $a_type;
 
  261                 $this->value = $a_value;
 
  269                 $this->from = $a_from;
 
  277                 $this->till = $a_till;
 
  285                 if($a_from_date_enabled == NULL) $a_from_date_enabled = 0;              
 
  286                 $this->from_date_enabled = $a_from_date_enabled;
 
  294                 if($a_till_date_enabled == NULL) $a_till_date_enabled = 0;
 
  295                 $this->till_date_enabled = $a_till_date_enabled;
 
  303                 if($a_date == 
'0000-00-00 00:00:00')
 
  304                         $this->change_date = NULLL;
 
  306                         $this->change_date = $a_date;
 
  314                 $this->uses = $a_uses;
 
  324                 $this->search_title_type = $a_title_type;
 
  332                 $this->search_title_value = $a_title_value;
 
  340                 $this->search_type = $a_type;
 
  348                 $this->search_from_day = $a_day;
 
  357                 $this->search_from_month = $a_month;
 
  365                 $this->search_from_year = $a_year;
 
  373                 $this->search_till_day = $a_day;
 
  381                 $this->search_till_month = $a_month;
 
  389                 $this->search_till_year = $a_year;
 
  397                 $this->search_from_enabled = $a_from_enabled;
 
  405                 $this->search_till_enabled = $a_till_enabled;
 
  414                 $this->current_coupon = $coupon;
 
  423                 $next_id = $this->db->nextId(
'payment_coupons');                
 
  425                 $statement = $this->db->manipulateF(
' 
  426                         INSERT INTO payment_coupons 
  438                                 pc_last_change_usr_id,  
  441                         VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)', 
 
  477                         $statement = $this->db->manipulateF(
' 
  478                                 UPDATE payment_coupons 
  485                                         pc_from_enabled = %s, 
  486                                         pc_till_enabled = %s, 
  488                                         pc_last_change_usr_id = %s, 
  522         public function delete()
 
  526                         $statement = $this->db->manipulateF(
' 
  527                                 DELETE FROM payment_coupons WHERE pc_pk = %s',
 
  529                                 array($this->
getId()));
 
  538                 $res = $this->db->queryf(
' 
  539                         SELECT * FROM payment_coupons 
  540                         WHERE pc_pk = %s', array(
'integer'), array($a_coupon_id));
 
  542                 while(
$row = $this->db->fetchObject(
$res))
 
  563                         switch ($coupon[
"pc_type"])
 
  566                                         return (
float) $coupon[
"pc_value"];
 
  568                                         return (
float) $a_item_price * ($coupon[
"pc_value"] / 100);                                     
 
  577                 $this->objects = array();
 
  579                 $res = $this->db->queryf(
' 
  580                         SELECT * FROM payment_coupons_obj 
  581                         WHERE pco_pc_fk = %s', 
 
  583                         array($a_coupon_id));
 
  585                 while(
$row = $this->db->fetchObject(
$res))
 
  587                         $this->objects[] = 
$row->ref_id;
 
  595                 $this->codes = array();
 
  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  
  602                         GROUP BY pcc_pk, payment_coupons_codes.pcc_pc_fk ,pcc_code', 
 
  604                         array($a_coupon_id));
 
  606                 while(
$row = $this->db->fetchObject(
$res))
 
  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; 
 
  618                 $this->used_codes = array();
 
  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', 
 
  625                         array($a_coupon_id));
 
  627                 while(
$row = $this->db->fetchObject(
$res))
 
  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; 
 
  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',   
 
  646                         array($a_coupon_code));
 
  649                 if (is_object(
$row = $this->db->fetchObject(
$res)))
 
  651                         $coupon[
'pc_pk'] = 
$row->pc_pk;                 
 
  652                         $coupon[
'pc_title'] = 
$row->pc_title;
 
  653                         $coupon[
'pc_description'] = 
$row->pc_description;
 
  654                         $coupon[
'pc_type'] = 
$row->pc_type;
 
  655                         $coupon[
'pc_value'] = 
$row->pc_value; 
 
  656                         $coupon[
'pc_type'] = 
$row->pc_type;
 
  657                         $coupon[
'pc_from'] = 
$row->pc_from;
 
  658                         $coupon[
'pc_till'] = 
$row->pc_till;
 
  659                         $coupon[
'pc_uses'] = 
$row->pc_uses;
 
  660                         $coupon[
'pcc_pk'] = 
$row->pcc_pk;
 
  661                         $coupon[
'pcc_code'] = 
$row->pcc_code;
 
  665                 $this->
setId($coupon[
'pc_pk']);
 
  668                 return $coupon ? $coupon : array();
 
  675                 if (empty($coupon)) 
return $this->COUPON_NOT_FOUND;             
 
  677                 $current_date = date(
"Y-m-d");
 
  678                 if ($coupon[
"pc_from"] != 
"0000-00-00" && $coupon[
"pc_from_enabled"] == 
'1' &&
 
  679                     $coupon[
"pc_till"] != 
"0000-00-00" && $coupon[
"pc_till_enabled"] == 
'1' 
  682                         if (! ($coupon[
"pc_from"] <= $current_date && $current_date <= $coupon[
"pc_till"]))
 
  684                                 return $this->COUPON_OUT_OF_DATE;
 
  687                 else if ($coupon[
"pc_from"] != 
"0000-00-00" && $coupon[
"pc_from_enabled"] == 
'1')
 
  689                         if ($coupon[
"pc_from"] > $current_date)
 
  691                                 return $this->COUPON_OUT_OF_DATE;
 
  694                 else if ($coupon[
"pc_till"] != 
"0000-00-00" && $coupon[
"pc_till_enabled"] == 
'1')
 
  696                         if ($coupon[
"pc_till"] < $current_date)
 
  698                                 return $this->COUPON_OUT_OF_DATE;
 
  702                 if (is_numeric($coupon[
"pc_uses"]) && $coupon[
"pc_uses"] > 0)
 
  704                         $res = $this->db->queryf(
' 
  705                                 SELECT COUNT(*) used_coupons 
  706                                 FROM payment_coupons_track                                         
  707                                 WHERE pct_pcc_fk = %s', 
 
  709                                 array($coupon[
'pcc_pk']));
 
  711                         $row = $this->db->fetchObject(
$res);
 
  713                         if (
$row->used_coupons >= $coupon[
"pc_uses"]) 
return $this->COUPON_TOO_MUCH_USED;
 
  716                 return $this->COUPON_VALID;
 
  723                         $statement = $this->db->manipulateF(
' 
  724                                 DELETE FROM payment_coupons_codes WHERE pcc_pk = %s',
 
  725                                 array(
'integer'), array($a_code_id));
 
  736                         $statement = $this->db->manipulateF(
' 
  737                                 DELETE FROM payment_coupons_codes WHERE pcc_pc_fk = %s',
 
  738                                 array(
'integer'),array($a_coupon_id));
 
  747                 $res = $this->db->queryf(
' 
  748                         SELECT * FROM payment_coupons_codes  
  753                 while(
$row = $this->db->fetchObject(
$res))
 
  755                         $code[
'pcc_pk'] = 
$row->pcc_pk;
 
  756                         $code[
'pcc_pc_fk'] = 
$row->pcc_pc_fk;
 
  757                         $code[
'pcc_code'] = 
$row->pcc_code;
 
  759                 return $code ? $code : array();
 
  762         public function addCode($a_code, $a_coupon_id)
 
  764                 if ($a_code && $a_coupon_id)
 
  766                         $next_id = $this->db->nextId(
'payment_coupons_codes');
 
  767                         $statement = $this->db->manipulateF(
' 
  768                                 INSERT INTO payment_coupons_codes 
  774                                 array(
'integer',
'integer', 
'text'),
 
  775                                 array($next_id, $a_coupon_id, $a_code));
 
  788                         $statement = $this->db->manipulateF(
' 
  789                                 INSERT INTO payment_statistic_coup  
  794                                 array(
'integer', 
'integer', 
'integer'),
 
  806                         $next_id = $this->db->nextId(
'payment_coupons_track');
 
  807                         $statement = $this->db->manipulateF(
' 
  808                                 INSERT INTO payment_coupons_track 
  814                                 VALUES (%s, %s, %s, %s)',
 
  815                                 array(
'integer',
'integer', 
'integer', 
'timestamp'),
 
  816                                 array($next_id, 
$current_coupon[
'pcc_pk'], $this->user_obj->getId(), date(
"Y-m-d H:i:s")));
 
  831                 if ($a_ref_id && is_numeric($this->
getId()))
 
  833                         $res = $this->db->queryf(
' 
  834                                 SELECT * FROM payment_coupons_obj 
  837                                 array(
'integer', 
'integer'),
 
  838                                 array($a_ref_id, $this->
getId()));
 
  840                         if (
$res->numRows()) 
return true;
 
  855                 if ($a_ref_id && is_numeric($this->
getId()))
 
  857                         $statement = $this->db->manipulateF(
' 
  858                                 INSERT INTO payment_coupons_obj 
  862                                 array(
'integer', 
'integer'),
 
  863                                 array($this->
getId(), $a_ref_id));
 
  878                 if ($a_ref_id && is_numeric($this->
getId()))
 
  880                         $statement = $this->db->manipulateF(
' 
  881                                 DELETE FROM payment_coupons_obj 
  884                                 array(
'integer', 
'integer'),
 
  885                                 array($a_ref_id, $this->
getId()));