00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00031 include_once './payment/classes/class.ilPaymentVendors.php';
00032 include_once './payment/classes/class.ilPaymentTrustees.php';
00033
00034
00035 class ilPaymentBookings
00036 {
00037
00038
00039
00040 var $user_id = null;
00041 var $db = null;
00042
00043 var $bookings = array();
00044
00045 var $booking_id = null;
00046 var $payed = null;
00047 var $access = null;
00048 var $admin_view = false;
00049
00050
00051
00052
00053 function ilPaymentBookings($a_user_id = '',$a_admin_view = false)
00054 {
00055 global $ilDB;
00056
00057 $this->admin_view = $a_admin_view;
00058 $this->user_id = $a_user_id;
00059 $this->db =& $ilDB;
00060
00061 if($a_user_id)
00062 {
00063 $this->__read();
00064 }
00065 }
00066
00067
00068 function setBookingId($a_booking_id)
00069 {
00070 return $this->booking_id = $a_booking_id;
00071 }
00072 function getBookingId()
00073 {
00074 return $this->booking_id;
00075 }
00076 function setTransaction($a_transaction)
00077 {
00078 $this->transaction = $a_transaction;
00079 }
00080 function getTransaction()
00081 {
00082 return $this->transaction;
00083 }
00084 function setPobjectId($a_pobject_id)
00085 {
00086 $this->pobject_id = $a_pobject_id;
00087 }
00088 function getPobjectId()
00089 {
00090 return $this->pobject_id;
00091 }
00092 function setCustomerId($a_customer_id)
00093 {
00094 $this->customer_id = $a_customer_id;
00095 }
00096 function getCustomerId()
00097 {
00098 return $this->customer_id;
00099 }
00100 function setVendorId($a_vendor_id)
00101 {
00102 $this->vendor_id = $a_vendor_id;
00103 }
00104 function getVendorId()
00105 {
00106 return $this->vendor_id;
00107 }
00108 function setPayMethod($a_pay_method)
00109 {
00110 $this->pay_method = $a_pay_method;
00111 }
00112 function getPayMethod()
00113 {
00114 return $this->pay_method;
00115 }
00116 function setOrderDate($a_order_date)
00117 {
00118 $this->order_date = $a_order_date;
00119 }
00120 function getOrderDate()
00121 {
00122 return $this->order_date;
00123 }
00124 function setDuration($a_duration)
00125 {
00126 $this->duration = $a_duration;
00127 }
00128 function getDuration()
00129 {
00130 return $this->duration;
00131 }
00132 function setPrice($a_price)
00133 {
00134 $this->price = $a_price;
00135 }
00136 function getPrice()
00137 {
00138 return $this->price;
00139 }
00140 function setDiscount($a_discount)
00141 {
00142 $this->discount = $a_discount;
00143 }
00144 function getDiscount()
00145 {
00146 return $this->discount;
00147 }
00148 function setPayed($a_payed)
00149 {
00150 $this->payed = $a_payed;
00151 }
00152 function getPayedStatus()
00153 {
00154 return $this->payed;
00155 }
00156 function setAccess($a_access)
00157 {
00158 $this->access = $a_access;
00159 }
00160 function getAccessStatus()
00161 {
00162 return $this->access;
00163 }
00164 function setVoucher($a_voucher)
00165 {
00166 $this->voucher = $a_voucher;
00167 }
00168 function getVoucher()
00169 {
00170 return $this->voucher;
00171 }
00172 function setTransactionExtern($a_transaction_extern)
00173 {
00174 $this->transaction_extern = $a_transaction_extern;
00175 }
00176 function getTransactionExtern()
00177 {
00178 return $this->transaction_extern;
00179 }
00180
00181 function add()
00182 {
00183 $query = sprintf("INSERT INTO payment_statistic VALUES('',".
00184 "'%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')",
00185 $this->getTransaction(),
00186 $this->getPobjectId(),
00187 $this->getCustomerId(),
00188 $this->getVendorId(),
00189 $this->getPayMethod(),
00190 $this->getOrderDate(),
00191 $this->getDuration(),
00192 $this->getPrice(),
00193 $this->getDiscount(),
00194 $this->getPayedStatus(),
00195 $this->getAccessStatus(),
00196 $this->getVoucher(),
00197 $this->getTransactionExtern());
00198
00199 $this->db->query($query);
00200
00201 return $this->db->getLastInsertId();
00202 }
00203
00204 function update()
00205 {
00206 if($this->getBookingId())
00207 {
00208 $query = "UPDATE payment_statistic ".
00209 "SET payed = '".(int) $this->getPayedStatus()."', ".
00210 "access = '".(int) $this->getAccessStatus()."' ".
00211 "WHERE booking_id = '".$this->getBookingId()."'";
00212 $this->db->query($query);
00213
00214 return true;
00215 }
00216 return false;
00217 }
00218
00219 function delete()
00220 {
00221 if($this->getBookingId())
00222 {
00223 $query = "DELETE FROM payment_statistic ".
00224 "WHERE booking_id = '".$this->getBookingId()."'";
00225
00226 $this->db->query($query);
00227
00228 return true;
00229 }
00230 return false;
00231 }
00232
00233 function getBookingsOfCustomer($a_usr_id)
00234 {
00235 $query = 'SELECT * FROM payment_statistic as ps, payment_objects as po '.
00236 "WHERE ps.pobject_id = po.pobject_id ".
00237 "AND customer_id = '".$a_usr_id."' ".
00238 "ORDER BY order_date DESC";
00239
00240 $res = $this->db->query($query);
00241 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00242 {
00243 $booking[$row->booking_id]['booking_id'] = $row->booking_id;
00244 $booking[$row->booking_id]['transaction'] = $row->transaction;
00245 $booking[$row->booking_id]['pobject_id'] = $row->pobject_id;
00246 $booking[$row->booking_id]['customer_id'] = $row->customer_id;
00247 $booking[$row->booking_id]['order_date'] = $row->order_date;
00248 $booking[$row->booking_id]['duration'] = $row->duration;
00249 $booking[$row->booking_id]['price'] = $row->price;
00250 $booking[$row->booking_id]['discount'] = $row->discount;
00251 $booking[$row->booking_id]['payed'] = $row->payed;
00252 $booking[$row->booking_id]['access'] = $row->access;
00253 $booking[$row->booking_id]['ref_id'] = $row->ref_id;
00254 $booking[$row->booking_id]['status'] = $row->status;
00255 $booking[$row->booking_id]['pay_method'] = $row->pay_method;
00256 $booking[$row->booking_id]['vendor_id'] = $row->vendor_id;
00257 $booking[$row->booking_id]['b_vendor_id'] = $row->b_vendor_id;
00258 $booking[$row->booking_id]['b_pay_method'] = $row->b_pay_method;
00259 $booking[$row->booking_id]['voucher'] = $row->voucher;
00260 $booking[$row->booking_id]['transaction_extern'] = $row->transaction_extern;
00261 }
00262 return $booking ? $booking : array();
00263 }
00264
00265 function getBookings()
00266 {
00267 return $this->bookings ? $this->bookings : array();
00268 }
00269
00270 function getBooking($a_booking_id)
00271 {
00272 $query = 'SELECT * FROM payment_statistic as ps, payment_objects as po '.
00273 "WHERE ps.pobject_id = po.pobject_id ".
00274 "AND booking_id = '".$a_booking_id."'";
00275
00276 $res = $this->db->query($query);
00277 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00278 {
00279 $booking['booking_id'] = $row->booking_id;
00280 $booking['transaction'] = $row->transaction;
00281 $booking['pobject_id'] = $row->pobject_id;
00282 $booking['customer_id'] = $row->customer_id;
00283 $booking['order_date'] = $row->order_date;
00284 $booking['duration'] = $row->duration;
00285 $booking['price'] = $row->price;
00286 $booking['discount'] = $row->discount;
00287 $booking['payed'] = $row->payed;
00288 $booking['access'] = $row->access;
00289 $booking['ref_id'] = $row->ref_id;
00290 $booking['status'] = $row->status;
00291 $booking['pay_method'] = $row->pay_method;
00292 $booking['vendor_id'] = $row->vendor_id;
00293 $booking['b_vendor_id'] = $row->b_vendor_id;
00294 $booking['b_pay_method'] = $row->b_pay_method;
00295 $booking['voucher'] = $row->voucher;
00296 $booking['transaction_extern'] = $row->transaction_extern;
00297 }
00298 return $booking ? $booking : array();
00299 }
00300
00301
00302
00303 function _getCountBookingsByVendor($a_vendor_id)
00304 {
00305 global $ilDB;
00306
00307 $query = "SELECT COUNT(booking_id) AS bid FROM payment_statistic ".
00308 "WHERE b_vendor_id = '".$a_vendor_id."'";
00309
00310 $res = $ilDB->query($query);
00311 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00312 {
00313 return $row->bid;
00314 }
00315 return 0;
00316 }
00317
00318 function _getCountBookingsByCustomer($a_vendor_id)
00319 {
00320 global $ilDB;
00321
00322 $query = "SELECT COUNT(booking_id) AS bid FROM payment_statistic ".
00323 "WHERE customer_id = '".$a_vendor_id."'";
00324
00325 $res = $ilDB->query($query);
00326 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00327 {
00328 return $row->bid;
00329 }
00330 return 0;
00331 }
00332 function _getCountBookingsByObject($a_pobject_id)
00333 {
00334 global $ilDB;
00335
00336 $query = "SELECT COUNT(booking_id) AS bid FROM payment_statistic ".
00337 "WHERE pobject_id = '".$a_pobject_id."'";
00338
00339 $res = $ilDB->query($query);
00340 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00341 {
00342 return $row->bid;
00343 }
00344 return 0;
00345 }
00346
00347 function _hasAccess($a_pobject_id,$a_user_id = 0)
00348 {
00349 global $ilDB,$ilias;
00350
00351 $usr_id = $a_user_id ? $a_user_id : $ilias->account->getId();
00352
00353 $query = "SELECT * FROM payment_statistic ".
00354 "WHERE pobject_id = '".$a_pobject_id."' ".
00355 "AND customer_id = '".$usr_id."' ".
00356 "AND payed = '1' ".
00357 "AND access = '1'";
00358
00359 $res = $ilDB->query($query);
00360 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00361 {
00362 $orderDateYear = date("Y", $row->order_date);
00363 $orderDateMonth = date("m", $row->order_date);
00364 $orderDateDay = date("d", $row->order_date);
00365 $orderDateHour = date("H", $row->order_date);
00366 $orderDateMinute = date("i", $row->order_date);
00367 $orderDateSecond = date("s", $row->order_date);
00368 if (($orderDateMonth + $row->duration) > 12)
00369 {
00370 $years = floor(($orderDateMonth + $row->duration) / 12);
00371 $months = ($orderDateMonth + $row->duration) - (12 * $years);
00372 $orderDateYear += $years;
00373 $orderDateMonth = $months;
00374 }
00375 else
00376 {
00377 $orderDateMonth += $row->duration;
00378 }
00379 $startDate = date("Y-m-d H:i:s", $row->order_date);
00380 $endDate = date("Y-m-d H:i:s", mktime($orderDateHour, $orderDateMinute, $orderDateSecond, $orderDateMonth, $orderDateDay, $orderDateYear));
00381 if (date("Y-m-d H:i:s") >= $startDate &&
00382 date("Y-m-d H:i:s") <= $endDate)
00383 {
00384 return true;
00385 }
00386 }
00387 return false;
00388 }
00389
00390 function _getActivation($a_pobject_id,$a_user_id = 0)
00391 {
00392 global $ilDB,$ilias;
00393
00394 $usr_id = $a_user_id ? $a_user_id : $ilias->account->getId();
00395
00396 $query = "SELECT * FROM payment_statistic ".
00397 "WHERE pobject_id = '".$a_pobject_id."' ".
00398 "AND customer_id = '".$usr_id."' ".
00399 "AND payed = '1' ".
00400 "AND access = '1'";
00401
00402 $res = $ilDB->query($query);
00403 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00404 {
00405 $orderDateYear = date("Y", $row->order_date);
00406 $orderDateMonth = date("m", $row->order_date);
00407 $orderDateDay = date("d", $row->order_date);
00408 $orderDateHour = date("H", $row->order_date);
00409 $orderDateMinute = date("i", $row->order_date);
00410 $orderDateSecond = date("s", $row->order_date);
00411 if (($orderDateMonth + $row->duration) > 12)
00412 {
00413 $years = floor(($orderDateMonth + $row->duration) / 12);
00414 $months = ($orderDateMonth + $row->duration) - (12 * $years);
00415 $orderDateYear += $years;
00416 $orderDateMonth = $months;
00417 }
00418 else
00419 {
00420 $orderDateMonth += $row->duration;
00421 }
00422 $startDate = date("Y-m-d H:i:s", $row->order_date);
00423 $endDate = date("Y-m-d H:i:s", mktime($orderDateHour, $orderDateMinute, $orderDateSecond, $orderDateMonth, $orderDateDay, $orderDateYear));
00424 if (date("Y-m-d H:i:s") >= $startDate &&
00425 date("Y-m-d H:i:s") <= $endDate)
00426 {
00427 $activation = array(
00428 "activation_start" => $row->order_date,
00429 "activation_end" => mktime($orderDateHour, $orderDateMinute, $orderDateSecond, $orderDateMonth, $orderDateDay, $orderDateYear)
00430 );
00431 return $activation;
00432 }
00433 }
00434 return false;
00435 }
00436
00437 function _getCountBookingsByPayMethod($a_pm)
00438 {
00439 switch($a_pm)
00440 {
00441 case 'pm_bill':
00442 $query = "SELECT COUNT(booking_id) AS bid FROM payment_statistic ".
00443 "WHERE pay_method = '1'";
00444
00445 $res = $ilDB->query($query);
00446 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00447 {
00448 return $row->bid;
00449 }
00450 return 0;
00451
00452 case 'pm_bmf':
00453 $query = "SELECT COUNT(booking_id) AS bid FROM payment_statistic ".
00454 "WHERE pay_method = '2'";
00455
00456 $res = $ilDB->query($query);
00457 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00458 {
00459 return $row->bid;
00460 }
00461 return 0;
00462
00463 case 'pm_paypal':
00464 $query = "SELECT COUNT(booking_id) AS bid FROM payment_statistic ".
00465 "WHERE pay_method = '3'";
00466
00467 $res = $ilDB->query($query);
00468 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00469 {
00470 return $row->bid;
00471 }
00472 return 0;
00473
00474 default:
00475 return 0;
00476 }
00477 }
00478
00479
00480
00481 function __read()
00482 {
00483 $query = 'SELECT * FROM payment_statistic as ps, payment_objects as po';
00484 if ($_SESSION["pay_statistics"]["customer"] or $_SESSION['pay_statistics']['vendor'])
00485 {
00486 $query .= ', usr_data as ud';
00487 }
00488 $query .= " WHERE ps.pobject_id = po.pobject_id ";
00489
00490 if ($_SESSION["pay_statistics"]["transaction_value"] != "")
00491 {
00492 if ($_SESSION["pay_statistics"]["transaction_type"] == 0)
00493 {
00494 $query .= "AND transaction_extern LIKE '" . $_SESSION["pay_statistics"]["transaction_value"] . "%' ";
00495 }
00496 else if ($_SESSION["pay_statistics"]["transaction_type"] == 1)
00497 {
00498 $query .= "AND transaction_extern LIKE '%" . $_SESSION["pay_statistics"]["transaction_value"] . "' ";
00499 }
00500 }
00501 if ($_SESSION["pay_statistics"]["customer"] != "")
00502 {
00503 $query .= "AND ud.login LIKE '%" . $_SESSION["pay_statistics"]["customer"] . "%' " .
00504 "AND ud.usr_id = ps.customer_id ";
00505 }
00506 if ($_SESSION["pay_statistics"]["from"]["day"] != "" &&
00507 $_SESSION["pay_statistics"]["from"]["month"] != "" &&
00508 $_SESSION["pay_statistics"]["from"]["year"] != "")
00509 {
00510 $from = mktime(0, 0, 0, $_SESSION["pay_statistics"]["from"]["month"],
00511 $_SESSION["pay_statistics"]["from"]["day"], $_SESSION["pay_statistics"]["from"]["year"]);
00512 $query .= "AND order_date >= '" . $from . "' ";
00513 }
00514 if ($_SESSION["pay_statistics"]["til"]["day"] != "" &&
00515 $_SESSION["pay_statistics"]["til"]["month"] != "" &&
00516 $_SESSION["pay_statistics"]["til"]["year"] != "")
00517 {
00518 $til = mktime(23, 59, 59, $_SESSION["pay_statistics"]["til"]["month"],
00519 $_SESSION["pay_statistics"]["til"]["day"], $_SESSION["pay_statistics"]["til"]["year"]);
00520 $query .= "AND order_date <= '" . $til . "' ";
00521 }
00522 if ($_SESSION["pay_statistics"]["payed"] == "0" ||
00523 $_SESSION["pay_statistics"]["payed"] == "1")
00524 {
00525 $query .= "AND payed = '" . $_SESSION["pay_statistics"]["payed"] . "' ";
00526 }
00527 if ($_SESSION["pay_statistics"]["access"] == "0" ||
00528 $_SESSION["pay_statistics"]["access"] == "1")
00529 {
00530 $query .= "AND access = '" . $_SESSION["pay_statistics"]["access"] . "' ";
00531 }
00532 if ($_SESSION["pay_statistics"]["pay_method"] == "1" ||
00533 $_SESSION["pay_statistics"]["pay_method"] == "2" ||
00534 $_SESSION["pay_statistics"]["pay_method"] == "3")
00535 {
00536 $query .= "AND b_pay_method = '" . $_SESSION["pay_statistics"]["pay_method"] . "' ";
00537 }
00538
00539 if(!$this->admin_view)
00540 {
00541 $vendors = $this->__getVendorIds();
00542 if (is_array($vendors) &&
00543 count($vendors) > 0)
00544 {
00545 $in = 'ps.b_vendor_id IN (';
00546 $in .= implode(',',$vendors);
00547 $in .= ')';
00548
00549 $query .= "AND ".$in." ";
00550 }
00551 }
00552 else
00553 {
00554 if($_SESSION['pay_statistics']['vendor'])
00555 {
00556 $query .= "AND ud.login LIKE '%" . $_SESSION["pay_statistics"]["vendor"] . "%' " .
00557 "AND ud.usr_id = ps.b_vendor_id ";
00558 }
00559 }
00560 $query .= "ORDER BY order_date DESC";
00561
00562 $res = $this->db->query($query);
00563 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00564 {
00565 $this->bookings[$row->booking_id]['booking_id'] = $row->booking_id;
00566 $this->bookings[$row->booking_id]['transaction'] = $row->transaction;
00567 $this->bookings[$row->booking_id]['pobject_id'] = $row->pobject_id;
00568 $this->bookings[$row->booking_id]['customer_id'] = $row->customer_id;
00569 $this->bookings[$row->booking_id]['order_date'] = $row->order_date;
00570 $this->bookings[$row->booking_id]['duration'] = $row->duration;
00571 $this->bookings[$row->booking_id]['price'] = $row->price;
00572 $this->bookings[$row->booking_id]['discount'] = $row->discount;
00573 $this->bookings[$row->booking_id]['payed'] = $row->payed;
00574 $this->bookings[$row->booking_id]['access'] = $row->access;
00575 $this->bookings[$row->booking_id]['ref_id'] = $row->ref_id;
00576 $this->bookings[$row->booking_id]['status'] = $row->status;
00577 $this->bookings[$row->booking_id]['pay_method'] = $row->pay_method;
00578 $this->bookings[$row->booking_id]['vendor_id'] = $row->vendor_id;
00579 $this->bookings[$row->booking_id]['b_vendor_id'] = $row->b_vendor_id;
00580 $this->bookings[$row->booking_id]['b_pay_method'] = $row->b_pay_method;
00581 $this->bookings[$row->booking_id]['voucher'] = $row->voucher;
00582 $this->bookings[$row->booking_id]['transaction_extern'] = $row->transaction_extern;
00583 }
00584
00585 }
00586
00587 function __getVendorIds()
00588 {
00589 if(ilPaymentVendors::_isVendor($this->user_id))
00590 {
00591 $vendors[] = $this->user_id;
00592 }
00593 if($vend = ilPaymentTrustees::_getVendorsForObjects($this->user_id))
00594 {
00595 foreach($vend as $v)
00596 {
00597 if(ilPaymentTrustees::_hasStatisticPermissionByVendor($this->user_id,$v))
00598 {
00599 $vendors[] = $v;
00600 #vendors = array_merge($vendors,$v);
00601 }
00602 }
00603 }
00604 return $vendors ? $vendors : array();
00605 }
00606
00607
00608 }
00609 ?>