RentalPrice.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. class RentalPrice {
  3. private $conn;
  4. private $table_name = "rental_prices";
  5. public $id;
  6. public $item_id;
  7. public $client_id;
  8. public $start_date;
  9. public $end_date;
  10. public $daily_price;
  11. public $created_at;
  12. public $updated_at;
  13. public function __construct($db) {
  14. $this->conn = $db;
  15. }
  16. public function create() {
  17. $query = "INSERT INTO " . $this->table_name . " SET item_id=:item_id, client_id=:client_id, start_date=:start_date, end_date=:end_date, daily_price=:daily_price, created_at=:created_at, updated_at=:updated_at";
  18. $stmt = $this->conn->prepare($query);
  19. $this->item_id = htmlspecialchars(strip_tags($this->item_id));
  20. $this->client_id = htmlspecialchars(strip_tags($this->client_id));
  21. $this->start_date = htmlspecialchars(strip_tags($this->start_date));
  22. $this->end_date = htmlspecialchars(strip_tags($this->end_date));
  23. $this->daily_price = htmlspecialchars(strip_tags($this->daily_price));
  24. $this->created_at = date('Y-m-d H:i:s');
  25. $this->updated_at = date('Y-m-d H:i:s');
  26. $stmt->bindParam(":item_id", $this->item_id);
  27. $stmt->bindParam(":client_id", $this->client_id);
  28. $stmt->bindParam(":start_date", $this->start_date);
  29. $stmt->bindParam(":end_date", $this->end_date);
  30. $stmt->bindParam(":daily_price", $this->daily_price);
  31. $stmt->bindParam(":created_at", $this->created_at);
  32. $stmt->bindParam(":updated_at", $this->updated_at);
  33. if($stmt->execute()) {
  34. return true;
  35. }
  36. return false;
  37. }
  38. public function read() {
  39. $query = "SELECT * FROM " . $this->table_name . " WHERE item_id = ? ORDER BY start_date ASC";
  40. $stmt = $this->conn->prepare($query);
  41. $stmt->bindParam(1, $this->item_id);
  42. $stmt->execute();
  43. return $stmt;
  44. }
  45. public function readOne() {
  46. $query = "SELECT * FROM " . $this->table_name . " WHERE id = ? LIMIT 0,1";
  47. $stmt = $this->conn->prepare($query);
  48. $stmt->bindParam(1, $this->id);
  49. $stmt->execute();
  50. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  51. $this->item_id = $row['item_id'];
  52. $this->client_id = $row['client_id'];
  53. $this->start_date = $row['start_date'];
  54. $this->end_date = $row['end_date'];
  55. $this->daily_price = $row['daily_price'];
  56. $this->created_at = $row['created_at'];
  57. $this->updated_at = $row['updated_at'];
  58. }
  59. public function update() {
  60. $query = "UPDATE " . $this->table_name . " SET item_id=:item_id, client_id=:client_id, start_date=:start_date, end_date=:end_date, daily_price=:daily_price, updated_at=:updated_at WHERE id=:id";
  61. $stmt = $this->conn->prepare($query);
  62. $this->item_id = htmlspecialchars(strip_tags($this->item_id));
  63. $this->client_id = htmlspecialchars(strip_tags($this->client_id));
  64. $this->start_date = htmlspecialchars(strip_tags($this->start_date));
  65. $this->end_date = htmlspecialchars(strip_tags($this->end_date));
  66. $this->daily_price = htmlspecialchars(strip_tags($this->daily_price));
  67. $this->updated_at = date('Y-m-d H:i:s');
  68. $stmt->bindParam(":item_id", $this->item_id);
  69. $stmt->bindParam(":client_id", $this->client_id);
  70. $stmt->bindParam(":start_date", $this->start_date);
  71. $stmt->bindParam(":end_date", $this->end_date);
  72. $stmt->bindParam(":daily_price", $this->daily_price);
  73. $stmt->bindParam(":updated_at", $this->updated_at);
  74. $stmt->bindParam(":id", $this->id);
  75. if($stmt->execute()) {
  76. return true;
  77. }
  78. return false;
  79. }
  80. public function delete() {
  81. $query = "DELETE FROM " . $this->table_name . " WHERE id = ?";
  82. $stmt = $this->conn->prepare($query);
  83. $stmt->bindParam(1, $this->id);
  84. if($stmt->execute()) {
  85. return true;
  86. }
  87. return false;
  88. }
  89. public function getCurrentPrice() {
  90. $query = "SELECT daily_price FROM " . $this->table_name . " WHERE item_id = ? AND start_date <= CURDATE() AND end_date >= CURDATE() LIMIT 1";
  91. $stmt = $this->conn->prepare($query);
  92. $stmt->bindParam(1, $this->item_id);
  93. $stmt->execute();
  94. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  95. if($row) {
  96. return $row['daily_price'];
  97. }
  98. return null;
  99. }
  100. }
  101. ?>