00001 <?php
00002 include_once './payment/classes/class.ilPaymentVendors.php';
00003 include_once './payment/classes/class.ilPaymentTrustees.php';
00004
00012 class ilPaymentCoupons
00013 {
00014 private $db = null;
00015
00016 private $user_obj = null;
00017 private $vendor_view = false;
00018
00019 private $coupons = array();
00020 private $current_coupon = array();
00021 private $codes = array();
00022 private $used_codes = array();
00023 private $objects = array();
00024
00025 public function ilPaymentCoupons(&$user_obj, $a_vendor_view = false)
00026 {
00027 global $ilDB;
00028
00029 $this->db =& $ilDB;
00030 $this->vendor_view = $a_vendor_view;
00031 $this->user_obj =& $user_obj;
00032
00033 $this->COUPON_VALID = 0;
00034 $this->COUPON_OUT_OF_DATE = 1;
00035 $this->COUPON_TOO_MUCH_USED = 2;
00036 $this->COUPON_NOT_FOUND = 3;
00037 }
00038
00039 public function getCoupons()
00040 {
00041 $this->coupons = array();
00042
00043 $query = "SELECT * FROM payment_coupons WHERE 1 ";
00044
00045 if ($this->getSearchFromDay() != "" &&
00046 $this->getSearchFromMonth() != "" &&
00047 $this->getSearchFromYear() != "" &&
00048 $this->getSearchFromDateEnabled()
00049 )
00050 {
00051 $from = mktime(0, 0, 0, $this->getSearchFromMonth(), $this->getSearchFromDay(), $this->getSearchFromYear());
00052 }
00053
00054 if ($this->getSearchTillDay() != "" &&
00055 $this->getSearchTillMonth() != "" &&
00056 $this->getSearchTillYear() != "" &&
00057 $this->getSearchTillDateEnabled()
00058 )
00059 {
00060 $till = mktime(23, 59, 59, $this->getSearchTillMonth(), $this->getSearchTillDay(), $this->getSearchTillYear());
00061 }
00062
00063 if ($from && $till)
00064 {
00065 $query .= " AND ((pc_from != '0000-00-00' AND pc_till != '0000-00-00' AND pc_from_enabled = '1' AND pc_till_enabled = '1' AND
00066 (UNIX_TIMESTAMP(pc_from) >= " . $this->db->quote($from). " AND UNIX_TIMESTAMP(pc_till) <= " . $this->db->quote($till). "
00067 OR UNIX_TIMESTAMP(pc_till) >= " . $this->db->quote($from). " AND UNIX_TIMESTAMP(pc_till) <= " . $this->db->quote($till). "
00068 OR UNIX_TIMESTAMP(pc_from) >= " . $this->db->quote($from). " AND UNIX_TIMESTAMP(pc_from) <= " . $this->db->quote($till). "
00069 OR UNIX_TIMESTAMP(pc_from) <= " . $this->db->quote($from). " AND UNIX_TIMESTAMP(pc_till) >= " . $this->db->quote($till). "
00070 ))
00071 OR (pc_from != '0000-00-00' AND pc_from_enabled = '1' AND UNIX_TIMESTAMP(pc_from) <= " . $this->db->quote($till). ")
00072 OR (pc_till != '0000-00-00' AND pc_till_enabled = '1' AND UNIX_TIMESTAMP(pc_till) >= " . $this->db->quote($from). ")
00073 )";
00074 }
00075 else if ($from)
00076 {
00077 $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')) ";
00078 }
00079 else if ($till)
00080 {
00081 $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')) ";
00082 }
00083
00084 if ($this->getSearchTitleValue() != "")
00085 {
00086 if ($this->getSearchTitleType() == 0)
00087 {
00088 $query .= " AND pc_title LIKE '" . $this->getSearchTitleValue() . "%' ";
00089 }
00090 else if ($this->getSearchTitleType() == 1)
00091 {
00092 $query .= " AND pc_title LIKE '%" . $this->getSearchTitleValue() . "' ";
00093 }
00094 }
00095
00096 if ($this->getSearchType() != "")
00097 {
00098 $query .= " AND pc_type = " . $this->db->quote($this->getSearchType()) . " ";
00099 }
00100
00101 $vendors = $this->getVendorIds();
00102 if (is_array($vendors) &&
00103 count($vendors) > 0)
00104 {
00105 $in = 'usr_id IN (';
00106 $in .= implode(',',$vendors);
00107 $in .= ')';
00108
00109 $query .= " AND ".$in." ";
00110 }
00111
00112 $res = $this->db->query($query);
00113 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00114 {
00115 $this->coupons[$row->pc_pk]['pc_pk'] = $row->pc_pk;
00116 $this->coupons[$row->pc_pk]['usr_id'] = $row->usr_id;
00117 $this->coupons[$row->pc_pk]['pc_title'] = $row->pc_title;
00118 $this->coupons[$row->pc_pk]['pc_description'] = $row->pc_description;
00119 $this->coupons[$row->pc_pk]['pc_type'] = $row->pc_type;
00120 $this->coupons[$row->pc_pk]['pc_value'] = $row->pc_value;
00121 $this->coupons[$row->pc_pk]['pc_from'] = $row->pc_from;
00122 $this->coupons[$row->pc_pk]['pc_till'] = $row->pc_till;
00123 $this->coupons[$row->pc_pk]['pc_from_enabled'] = $row->pc_from_enabled;
00124 $this->coupons[$row->pc_pk]['pc_till_enabled'] = $row->pc_till_enabled;
00125 $this->coupons[$row->pc_pk]['pc_uses'] = $row->pc_uses;
00126 $this->coupons[$row->pc_pk]['pc_last_change_usr_id'] = $row->pc_last_change_usr_id;
00127 $this->coupons[$row->pc_pk]['pc_last_changed'] = $row->pc_last_changed;
00128 $this->coupons[$row->pc_pk]['number_of_codes'] = count($this->getCodesByCouponId($row->pc_pk));
00129 $this->coupons[$row->pc_pk]['usage_of_codes'] = count($this->getUsedCouponsByCouponId($row->pc_pk));
00130 $this->coupons[$row->pc_pk]['objects'] = $this->getObjectsByCouponId($row->pc_pk);
00131 }
00132
00133 return $this->coupons;
00134 }
00135
00136 private function getVendorIds()
00137 {
00138 $vendors[] = $this->user_obj->getId();
00139 if (ilPaymentVendors::_isVendor($this->user_obj->getId()))
00140 {
00141 $ptObj = new ilPaymentTrustees($this->user_obj);
00142
00143 if ($trustees = $ptObj->getTrustees())
00144 {
00145 foreach ($trustees as $trustee)
00146 {
00147 if ((bool) $trustee["perm_coupons"])
00148 {
00149 $vendors[] = $trustee["trustee_id"];
00150 }
00151 }
00152 }
00153 }
00154 if ($vend = ilPaymentTrustees::_getVendorsForCouponsByTrusteeId($this->user_obj->getId()))
00155 {
00156 foreach ($vend as $v)
00157 {
00158 $vendors[] = $v;
00159 if ($trustees = ilPaymentTrustees::_getTrusteesForCouponsByVendorId($v))
00160 {
00161 foreach ($trustees as $t)
00162 {
00163 $vendors[] = $t;
00164 }
00165 }
00166 }
00167 }
00168
00169 return $vendors ? $vendors : array();
00170 }
00171
00172
00173 public function setId($a_id)
00174 {
00175 return $this->id = $a_id;
00176 }
00177 public function getId()
00178 {
00179 return $this->id;
00180 }
00181 public function setCouponUser($a_user_id)
00182 {
00183 $this->coupon_user = $a_user_id;
00184 }
00185 public function getCouponUser()
00186 {
00187 return $this->coupon_user;
00188 }
00189 public function setTitle($a_title)
00190 {
00191 $this->title = $a_title;
00192 }
00193 public function getTitle()
00194 {
00195 return $this->title;
00196 }
00197 public function setDescription($a_description)
00198 {
00199 $this->description = $a_description;
00200 }
00201 public function getDescription()
00202 {
00203 return $this->description;
00204 }
00205 public function setType($a_type)
00206 {
00207 $this->type = $a_type;
00208 }
00209 public function getType()
00210 {
00211 return $this->type;
00212 }
00213 public function setValue($a_value)
00214 {
00215 $this->value = $a_value;
00216 }
00217 public function getValue()
00218 {
00219 return $this->value;
00220 }
00221 public function setFromDate($a_from)
00222 {
00223 $this->from = $a_from;
00224 }
00225 public function getFromDate()
00226 {
00227 return $this->from;
00228 }
00229 public function setTillDate($a_till)
00230 {
00231 $this->till = $a_till;
00232 }
00233 public function getTillDate()
00234 {
00235 return $this->till;
00236 }
00237 public function setFromDateEnabled($a_from_date_enabled = 0)
00238 {
00239 $this->from_date_enabled = $a_from_date_enabled;
00240 }
00241 public function getFromDateEnabled()
00242 {
00243 return $this->from_date_enabled;
00244 }
00245 public function setTillDateEnabled($a_till_date_enabled = 0)
00246 {
00247 $this->till_date_enabled = $a_till_date_enabled;
00248 }
00249 public function getTillDateEnabled()
00250 {
00251 return $this->till_date_enabled;
00252 }
00253 public function setChangeDate($a_date)
00254 {
00255 $this->change_date = $a_date;
00256 }
00257 public function getChangeDate()
00258 {
00259 return $this->change_date;
00260 }
00261 public function setUses($a_uses)
00262 {
00263 $this->uses = $a_uses;
00264 }
00265 public function getUses()
00266 {
00267 return $this->uses;
00268 }
00269
00270
00271 public function setSearchTitleType($a_title_type)
00272 {
00273 $this->search_title_type = $a_title_type;
00274 }
00275 public function getSearchTitleType()
00276 {
00277 return $this->search_title_type;
00278 }
00279 public function setSearchTitleValue($a_title_value)
00280 {
00281 $this->search_title_value = $a_title_value;
00282 }
00283 public function getSearchTitleValue()
00284 {
00285 return $this->search_title_value;
00286 }
00287 public function setSearchType($a_type)
00288 {
00289 $this->search_type = $a_type;
00290 }
00291 public function getSearchType()
00292 {
00293 return $this->search_type;
00294 }
00295 public function setSearchFromDay($a_day)
00296 {
00297 $this->search_from_day = $a_day;
00298 }
00299 public function getSearchFromDay()
00300 {
00301 return $this->search_from_day;
00302 }
00303 public function setSearchFromMonth($a_month)
00304 {
00305 $this->search_from_month = $a_month;
00306 }
00307 public function getSearchFromMonth()
00308 {
00309 return $this->search_from_month;
00310 }
00311 public function setSearchFromYear($a_year)
00312 {
00313 $this->search_from_year = $a_year;
00314 }
00315 public function getSearchFromYear()
00316 {
00317 return $this->search_from_year;
00318 }
00319 public function setSearchTillDay($a_day)
00320 {
00321 $this->search_till_day = $a_day;
00322 }
00323 public function getSearchTillDay()
00324 {
00325 return $this->search_till_day;
00326 }
00327 public function setSearchTillMonth($a_month)
00328 {
00329 $this->search_till_month = $a_month;
00330 }
00331 public function getSearchTillMonth()
00332 {
00333 return $this->search_till_month;
00334 }
00335 public function setSearchTillYear($a_year)
00336 {
00337 $this->search_till_year = $a_year;
00338 }
00339 public function getSearchTillYear()
00340 {
00341 return $this->search_till_year;
00342 }
00343 public function setSearchFromDateEnabled($a_from_enabled)
00344 {
00345 $this->search_from_enabled = $a_from_enabled;
00346 }
00347 public function getSearchFromDateEnabled()
00348 {
00349 return $this->search_from_enabled;
00350 }
00351 public function setSearchTillDateEnabled($a_till_enabled)
00352 {
00353 $this->search_till_enabled = $a_till_enabled;
00354 }
00355 public function getSearchTillDateEnabled()
00356 {
00357 return $this->search_till_enabled;
00358 }
00359
00360
00361 public function setCurrentCoupon($coupon = array())
00362 {
00363 $this->current_coupon = $coupon;
00364 }
00365 public function getCurrentCoupon()
00366 {
00367 return $this->current_coupon;
00368 }
00369
00370 public function add()
00371 {
00372 $query = sprintf("INSERT INTO payment_coupons VALUES('', ".
00373 " %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
00374 $this->db->quote($this->getCouponUser()),
00375 $this->db->quote($this->getTitle()),
00376 $this->db->quote($this->getDescription()),
00377 $this->db->quote($this->getType()),
00378 $this->db->quote($this->getValue()),
00379 $this->db->quote($this->getFromDate()),
00380 $this->db->quote($this->getTillDate()),
00381 $this->db->quote($this->getFromDateEnabled()),
00382 $this->db->quote($this->getTillDateEnabled()),
00383 $this->db->quote($this->getUses()),
00384 "''",
00385 "''"
00386 );
00387
00388 $this->db->query($query);
00389
00390 return $this->db->getLastInsertId();
00391 }
00392
00393 public function update()
00394 {
00395 if ($this->getId())
00396 {
00397 $query = "UPDATE payment_coupons
00398 SET
00399 pc_title = ".$this->db->quote($this->getTitle()).",
00400 pc_description = ".$this->db->quote($this->getDescription()).",
00401 pc_type = ".$this->db->quote($this->getType()).",
00402 pc_value = ".$this->db->quote($this->getValue()).",
00403 pc_from = ".$this->db->quote($this->getFromDate()).",
00404 pc_till = ".$this->db->quote($this->getTillDate()).",
00405 pc_from_enabled = ".$this->db->quote($this->getFromDateEnabled()).",
00406 pc_till_enabled = ".$this->db->quote($this->getTillDateEnabled()).",
00407 pc_uses = ".$this->db->quote($this->getUses()).",
00408 pc_last_change_usr_id = ".$this->db->quote($this->getCouponUser()).",
00409 pc_last_changed = ".$this->db->quote($this->getChangeDate())."
00410 WHERE pc_pk = ".$this->db->quote($this->getId())." ";
00411 $this->db->query($query);
00412
00413 return true;
00414 }
00415 return false;
00416 }
00417
00418 public function delete()
00419 {
00420 if ($this->getId())
00421 {
00422 $query = "DELETE FROM payment_coupons WHERE pc_pk = ".$this->db->quote($this->getId())." ";
00423
00424 $this->db->query($query);
00425
00426 return true;
00427 }
00428 return false;
00429 }
00430
00431 public function getCouponById($a_coupon_id)
00432 {
00433 $query = "SELECT *
00434 FROM payment_coupons
00435 WHERE 1
00436 AND pc_pk = ".$this->db->quote($a_coupon_id)." ";
00437
00438 $res = $this->db->query($query);
00439 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00440 {
00441 $this->setId($row->pc_pk);
00442 $this->setCouponUser($row->usr_id);
00443 $this->setTitle($row->pc_title);
00444 $this->setDescription($row->pc_description);
00445 $this->setType($row->pc_type);
00446 $this->setValue($row->pc_value);
00447 $this->setFromDate($row->pc_from);
00448 $this->setTillDate($row->pc_till);
00449 $this->setFromDateEnabled($row->pc_from_enabled);
00450 $this->setTillDateEnabled($row->pc_till_enabled);
00451 $this->setUses($row->pc_uses);
00452 $this->setChangeDate(date("Y-m-h H:i:s"));
00453 }
00454 }
00455
00456 public function getCouponBonus($a_item_price)
00457 {
00458 if (is_array($coupon = $this->getCurrentCoupon()))
00459 {
00460 switch ($coupon["pc_type"])
00461 {
00462 case "fix":
00463 return (float) $coupon["pc_value"];
00464 case "percent":
00465 return (float) $a_item_price * ($coupon["pc_value"] / 100);
00466 }
00467 }
00468
00469 return 0;
00470 }
00471
00472 public function getObjectsByCouponId($a_coupon_id)
00473 {
00474 $this->objects = array();
00475
00476 $query = "SELECT * FROM payment_coupons_objects WHERE 1 AND pco_pc_fk = ".$this->db->quote($a_coupon_id);
00477
00478 $res = $this->db->query($query);
00479 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00480 {
00481 $this->objects[] = $row->ref_id;
00482 }
00483
00484 return $this->objects;
00485 }
00486
00487 public function getCodesByCouponId($a_coupon_id)
00488 {
00489 $this->codes = array();
00490
00491 $query = "SELECT payment_coupons_codes.*, COUNT(pct_pcc_fk) AS pcc_used
00492 FROM payment_coupons_codes
00493 LEFT JOIN payment_coupons_tracking ON pct_pcc_fk = pcc_pk
00494 WHERE 1 AND pcc_pc_fk = ".$this->db->quote($a_coupon_id)."
00495 GROUP BY pcc_pk";
00496
00497 $res = $this->db->query($query);
00498 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00499 {
00500 $this->codes[$row->pcc_pk]['pcc_pk'] = $row->pcc_pk;
00501 $this->codes[$row->pcc_pk]['pcc_code'] = $row->pcc_code;
00502 $this->codes[$row->pcc_pk]['pcc_used'] = $row->pcc_used;
00503 }
00504
00505 return $this->codes;
00506 }
00507
00508 public function getUsedCouponsByCouponId($a_coupon_id)
00509 {
00510 $this->used_codes = array();
00511
00512 $query = "SELECT *
00513 FROM payment_coupons_tracking
00514 INNER JOIN payment_coupons_codes ON pcc_pk = pct_pcc_fk
00515 WHERE 1
00516 AND pcc_pc_fk = ".$this->db->quote($a_coupon_id);
00517
00518 $res = $this->db->query($query);
00519 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00520 {
00521 $this->used_codes[$row->pct_pk]['pct_pk'] = $row->pct_pk;
00522 $this->used_codes[$row->pct_pk]['pcc_code'] = $row->pcc_code;
00523 $this->used_codes[$row->pct_pk]['usr_id'] = $row->usr_id;
00524 $this->used_codes[$row->pct_pk]['pct_date'] = $row->pct_date;
00525 $this->used_codes[$row->pct_pk]['pct_ps_fk'] = $row->pct_ps_fk;
00526 }
00527
00528 return $this->used_codes;
00529 }
00530
00531 public function getCouponByCode($a_coupon_code)
00532 {
00533 $query = "SELECT *
00534 FROM payment_coupons_codes
00535 INNER JOIN payment_coupons ON pc_pk = pcc_pc_fk
00536 WHERE 1
00537 AND pcc_code = ".$this->db->quote($a_coupon_code). " ";
00538
00539 $res = $this->db->query($query);
00540 if (is_object($row = $res->fetchRow(DB_FETCHMODE_OBJECT)))
00541 {
00542 $coupon['pc_pk'] = $row->pc_pk;
00543 $coupon['pc_title'] = $row->pc_title;
00544 $coupon['pc_description'] = $row->pc_description;
00545 $coupon['pc_type'] = $row->pc_type;
00546 $coupon['pc_value'] = $row->pc_value;
00547 $coupon['pc_type'] = $row->pc_type;
00548 $coupon['pc_from'] = $row->pc_from;
00549 $coupon['pc_till'] = $row->pc_till;
00550 $coupon['pc_uses'] = $row->pc_uses;
00551 $coupon['pcc_pk'] = $row->pcc_pk;
00552 $coupon['pcc_code'] = $row->pcc_code;
00553 $coupon['objects'] = $this->getObjectsByCouponId($row->pc_pk);
00554 }
00555
00556 $this->setId($coupon['pc_pk']);
00557 $this->setCurrentCoupon($coupon);
00558
00559 return $coupon ? $coupon : array();
00560 }
00561
00562 public function checkCouponValidity()
00563 {
00564 $coupon = $this->getCurrentCoupon();
00565
00566 if (empty($coupon)) return $this->COUPON_NOT_FOUND;
00567
00568 $current_date = date("Y-m-d");
00569 if ($coupon["pc_from"] != "0000-00-00" && $coupon["pc_from_enabled"] == '1' &&
00570 $coupon["pc_till"] != "0000-00-00" && $coupon["pc_till_enabled"] == '1'
00571 )
00572 {
00573 if (! ($coupon["pc_from"] <= $current_date && $current_date <= $coupon["pc_till"]))
00574 {
00575 return $this->COUPON_OUT_OF_DATE;
00576 }
00577 }
00578 else if ($coupon["pc_from"] != "0000-00-00" && $coupon["pc_from_enabled"] == '1')
00579 {
00580 if ($coupon["pc_from"] > $current_date)
00581 {
00582 return $this->COUPON_OUT_OF_DATE;
00583 }
00584 }
00585 else if ($coupon["pc_till"] != "0000-00-00" && $coupon["pc_till_enabled"] == '1')
00586 {
00587 if ($coupon["pc_till"] < $current_date)
00588 {
00589 return $this->COUPON_OUT_OF_DATE;
00590 }
00591 }
00592
00593 if (is_numeric($coupon["pc_uses"]) && $coupon["pc_uses"] > 0)
00594 {
00595 $query = "SELECT COUNT(*) AS used_coupons
00596 FROM payment_coupons_tracking
00597 WHERE pct_pcc_fk = ".$this->db->quote($coupon["pcc_pk"])." ";
00598
00599 $this->db->query($query);
00600 $res = $this->db->query($query);
00601 $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
00602
00603 if ($row->used_coupons >= $coupon["pc_uses"]) return $this->COUPON_TOO_MUCH_USED;
00604 }
00605
00606 return $this->COUPON_VALID;
00607 }
00608
00609 public function deleteCode($a_code_id)
00610 {
00611 if ($a_code_id)
00612 {
00613 $query = "DELETE FROM payment_coupons_codes WHERE pcc_pk = ".$this->db->quote($a_code_id)." ";
00614
00615 $this->db->query($query);
00616
00617 return true;
00618 }
00619 return false;
00620 }
00621
00622 public function deleteAllCodesByCouponId($a_coupon_id)
00623 {
00624 if ($a_coupon_id)
00625 {
00626 $query = "DELETE FROM payment_coupons_codes WHERE pcc_pc_fk = ".$this->db->quote($a_coupon_id)." ";
00627
00628 $this->db->query($query);
00629
00630 return true;
00631 }
00632 return false;
00633 }
00634
00635 public function getCode($a_code_id)
00636 {
00637 $query = "SELECT *
00638 FROM payment_coupons_codes
00639 WHERE 1
00640 AND pcc_pk = ".$this->db->quote($a_code_id)." ";
00641
00642 $res = $this->db->query($query);
00643 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00644 {
00645 $code['pcc_pk'] = $row->pcc_pk;
00646 $code['pcc_pc_fk'] = $row->pcc_pc_fk;
00647 $code['pcc_code'] = $row->pcc_code;
00648 }
00649 return $code ? $code : array();
00650 }
00651
00652 public function addCode($a_code, $a_coupon_id)
00653 {
00654 if ($a_code && $a_coupon_id)
00655 {
00656 $query = sprintf("INSERT INTO payment_coupons_codes VALUES('', ".
00657 " %s, %s)",
00658 $this->db->quote($a_coupon_id),
00659 $this->db->quote($a_code)
00660 );
00661
00662 $this->db->query($query);
00663
00664 return $this->db->getLastInsertId();
00665 }
00666 return false;
00667 }
00668
00669 public function addCouponForBookingId($a_booking_id)
00670 {
00671 $current_coupon = $this->getCurrentCoupon();
00672
00673 if ($a_booking_id && is_array($current_coupon))
00674 {
00675
00676 $query = sprintf("INSERT INTO payment_statistic_coupons VALUES(%s, %s, %s)",
00677 $this->db->quote($a_booking_id),
00678 $this->db->quote($current_coupon["pc_pk"]),
00679 $this->db->quote($current_coupon["pcc_pk"])
00680 );
00681
00682 $this->db->query($query);
00683
00684 return $this->db->getLastInsertId();
00685 }
00686 return false;
00687 }
00688
00689 public function addTracking()
00690 {
00691 $current_coupon = $this->getCurrentCoupon();
00692
00693 if (is_array($current_coupon))
00694 {
00695 $query = "INSERT INTO payment_coupons_tracking "
00696 ."SET "
00697 ."pct_pcc_fk = ".$this->db->quote($current_coupon["pcc_pk"]). ", "
00698 ."usr_id = ".$this->db->quote($this->user_obj->getId()). ", "
00699 ."pct_date = ".$this->db->quote(date("Y-m-d H:i:s")). " ";
00700
00701 $this->db->query($query);
00702
00703 return $this->db->getLastInsertId();
00704 }
00705 return false;
00706 }
00707
00714 public function isObjectAssignedToCoupon($a_ref_id)
00715 {
00716 if ($a_ref_id && is_numeric($this->getId()))
00717 {
00718 $query = "SELECT *
00719 FROM payment_coupons_objects
00720 WHERE 1
00721 AND ref_id = ".$this->db->quote($a_ref_id)."
00722 AND pco_pc_fk = ".$this->db->quote($this->getId())." ";
00723
00724 $res = $this->db->query($query);
00725
00726 if ($res->numRows()) return true;
00727
00728 return false;
00729 }
00730 return false;
00731 }
00732
00739 public function assignObjectToCoupon($a_ref_id)
00740 {
00741 if ($a_ref_id && is_numeric($this->getId()))
00742 {
00743 $query = sprintf("INSERT INTO payment_coupons_objects VALUES(%s, %s)",
00744 $this->db->quote($this->getId()),
00745 $this->db->quote($a_ref_id)
00746 );
00747
00748 $this->db->query($query);
00749
00750 return true;
00751 }
00752 return false;
00753 }
00754
00761 public function unassignObjectFromCoupon($a_ref_id)
00762 {
00763 if ($a_ref_id && is_numeric($this->getId()))
00764 {
00765 $query = "DELETE
00766 FROM payment_coupons_objects
00767 WHERE 1
00768 AND ref_id = ".$this->db->quote($a_ref_id)."
00769 AND pco_pc_fk = ".$this->db->quote($this->getId())." ";
00770
00771 $this->db->query($query);
00772
00773 return true;
00774 }
00775 return false;
00776 }
00777
00778 }
00779 ?>