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) &&
 
  132                         foreach($vendors as $vendor)
 
  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();
 
  185                 if (ilPaymentVendors::_isVendor($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 = NULL;
 
  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));
 
  650                 if (is_object(
$row = $this->db->fetchObject(
$res)))
 
  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;
 
  666                 $this->
setId($coupon[
'pc_pk']);
 
  669                 return $coupon ? $coupon : array();
 
  676                 if (empty($coupon)) 
return $this->COUPON_NOT_FOUND;             
 
  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' 
  683                         if (! ($coupon[
"pc_from"] <= $current_date && $current_date <= $coupon[
"pc_till"]))
 
  685                                 return $this->COUPON_OUT_OF_DATE;
 
  688                 else if ($coupon[
"pc_from"] != 
"0000-00-00" && $coupon[
"pc_from_enabled"] == 
'1')
 
  690                         if ($coupon[
"pc_from"] > $current_date)
 
  692                                 return $this->COUPON_OUT_OF_DATE;
 
  695                 else if ($coupon[
"pc_till"] != 
"0000-00-00" && $coupon[
"pc_till_enabled"] == 
'1')
 
  697                         if ($coupon[
"pc_till"] < $current_date)
 
  699                                 return $this->COUPON_OUT_OF_DATE;
 
  703                 if (is_numeric($coupon[
"pc_uses"]) && $coupon[
"pc_uses"] > 0)
 
  705                         $res = $this->db->queryf(
' 
  706                                 SELECT COUNT(*) used_coupons 
  707                                 FROM payment_coupons_track                                         
  708                                 WHERE pct_pcc_fk = %s', 
 
  710                                 array($coupon[
'pcc_pk']));
 
  712                         $row = $this->db->fetchObject(
$res);
 
  714                         if (
$row->used_coupons >= $coupon[
"pc_uses"]) 
return $this->COUPON_TOO_MUCH_USED;
 
  717                 return $this->COUPON_VALID;
 
  724                         $statement = $this->db->manipulateF(
' 
  725                                 DELETE FROM payment_coupons_codes WHERE pcc_pk = %s',
 
  726                                 array(
'integer'), array($a_code_id));
 
  737                         $statement = $this->db->manipulateF(
' 
  738                                 DELETE FROM payment_coupons_codes WHERE pcc_pc_fk = %s',
 
  739                                 array(
'integer'),array($a_coupon_id));
 
  748                 $res = $this->db->queryf(
' 
  749                         SELECT * FROM payment_coupons_codes  
  756                 while(
$row = $this->db->fetchObject(
$res))
 
  758                         $code[
'pcc_pk'] = 
$row->pcc_pk;
 
  759                         $code[
'pcc_pc_fk'] = 
$row->pcc_pc_fk;
 
  760                         $code[
'pcc_code'] = 
$row->pcc_code;
 
  762                 return $code ? $code : array();
 
  765         public function addCode($a_code, $a_coupon_id)
 
  767                 if ($a_code && $a_coupon_id)
 
  769                         $next_id = $this->db->nextId(
'payment_coupons_codes');
 
  770                         $statement = $this->db->manipulateF(
' 
  771                                 INSERT INTO payment_coupons_codes 
  777                                 array(
'integer',
'integer', 
'text'),
 
  778                                 array($next_id, $a_coupon_id, $a_code));
 
  791                         $statement = $this->db->manipulateF(
' 
  792                                 INSERT INTO payment_statistic_coup  
  797                                 array(
'integer', 
'integer', 
'integer'),
 
  809                         $next_id = $this->db->nextId(
'payment_coupons_track');
 
  810                         $statement = $this->db->manipulateF(
' 
  811                                 INSERT INTO payment_coupons_track 
  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")));
 
  834                 if ($a_ref_id && is_numeric($this->
getId()))
 
  836                         $res = $this->db->queryf(
' 
  837                                 SELECT * FROM payment_coupons_obj 
  840                                 array(
'integer', 
'integer'),
 
  841                                 array($a_ref_id, $this->
getId()));
 
  843                         if (
$res->numRows()) 
return true;
 
  858                 if ($a_ref_id && is_numeric($this->
getId()))
 
  860                         $statement = $this->db->manipulateF(
' 
  861                                 INSERT INTO payment_coupons_obj 
  865                                 array(
'integer', 
'integer'),
 
  866                                 array($this->
getId(), $a_ref_id));
 
  881                 if ($a_ref_id && is_numeric($this->
getId()))
 
  883                         $statement = $this->db->manipulateF(
' 
  884                                 DELETE FROM payment_coupons_obj 
  887                                 array(
'integer', 
'integer'),
 
  888                                 array($a_ref_id, $this->
getId()));
 
  903                 $res =  $ilDB->queryF(
' 
  905                         FROM payment_coupons_codes 
  906                         WHERE pcc_pc_fk = %s',
 
  907                         array(
'integer'),array($a_pc_pk));                      
 
  910                 while(
$row = $ilDB->fetchAssoc(
$res))
 
  912                         $code_ids[] = 
$row[
'pcc_pk'];
 
  916                         DELETE FROM payment_coupons_track 
  917                         WHERE '. $ilDB->in(
'pct_pcc_fk', $code_ids, 
false, 
'integer'));
 
  920                         DELETE FROM payment_statistic_coup 
  921                         WHERE '.$ilDB->in(
'psc_pcc_fk', $code_ids, 
false, 
'integer'));
 
  924                         DELETE FROM payment_coupons 
  926                         array(
'integer'),array($a_pc_pk));                              
 
  929                         DELETE FROM payment_coupons_obj  
  930                         WHERE pco_pc_fk = %s',
 
  931                         array(
'integer'),array($a_pc_pk));                      
 
  934                         DELETE FROM payment_coupons_codes 
  936                         array(
'integer'),array($a_pc_pk));                              
 
  944                 $res = $ilDB->queryF(
'SELECT pc_title FROM payment_coupons WHERE pc_pk = %s',
 
  945                                 array(
'integer'), array($a_coupon_id));
 
  948                 return $row[
'pc_title'];