WorkHour.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. class WorkHour {
  3. private $conn;
  4. private $table_name = 'work_hours';
  5. public $id;
  6. public $task_id;
  7. public $user_id;
  8. public $date;
  9. public $hours;
  10. public $description;
  11. public $rate;
  12. public $total_amount;
  13. public $created_at;
  14. public $updated_at;
  15. public function __construct($db) {
  16. $this->conn = $db;
  17. }
  18. public function create() {
  19. // Auto-fetch client hour price if rate is not provided or empty
  20. if (!$this->rate || $this->rate === '') {
  21. $this->rate = $this->getClientHourPrice();
  22. }
  23. // Calculate total amount if rate is provided and greater than 0
  24. if ($this->rate && $this->rate > 0) {
  25. $this->total_amount = $this->hours * $this->rate;
  26. } else {
  27. $this->rate = null;
  28. $this->total_amount = null;
  29. }
  30. $query = "INSERT INTO " . $this->table_name . "
  31. SET task_id=:task_id, user_id=:user_id, date=:date, hours=:hours,
  32. description=:description, rate=:rate, total_amount=:total_amount,
  33. created_at=:created_at, updated_at=:updated_at";
  34. $stmt = $this->conn->prepare($query);
  35. $this->task_id = htmlspecialchars(strip_tags($this->task_id));
  36. $this->user_id = htmlspecialchars(strip_tags($this->user_id));
  37. $this->date = htmlspecialchars(strip_tags($this->date));
  38. $this->hours = htmlspecialchars(strip_tags($this->hours));
  39. $this->description = htmlspecialchars(strip_tags($this->description));
  40. $this->rate = $this->rate ? htmlspecialchars(strip_tags($this->rate)) : null;
  41. $this->total_amount = $this->total_amount ? htmlspecialchars(strip_tags($this->total_amount)) : null;
  42. $this->created_at = date('Y-m-d H:i:s');
  43. $this->updated_at = date('Y-m-d H:i:s');
  44. $stmt->bindParam(":task_id", $this->task_id);
  45. $stmt->bindParam(":user_id", $this->user_id);
  46. $stmt->bindParam(":date", $this->date);
  47. $stmt->bindParam(":hours", $this->hours);
  48. $stmt->bindParam(":description", $this->description);
  49. $stmt->bindParam(":rate", $this->rate);
  50. $stmt->bindParam(":total_amount", $this->total_amount);
  51. $stmt->bindParam(":created_at", $this->created_at);
  52. $stmt->bindParam(":updated_at", $this->updated_at);
  53. if($stmt->execute()) {
  54. return true;
  55. }
  56. return false;
  57. }
  58. public function read() {
  59. $query = "SELECT wh.*, t.title as task_title, u.first_name, u.last_name
  60. FROM " . $this->table_name . " wh
  61. LEFT JOIN tasks t ON wh.task_id = t.id
  62. LEFT JOIN users u ON wh.user_id = u.id
  63. ORDER BY wh.date DESC, wh.created_at DESC";
  64. $stmt = $this->conn->prepare($query);
  65. $stmt->execute();
  66. return $stmt;
  67. }
  68. public function readByTask($task_id) {
  69. $query = "SELECT wh.*, u.first_name, u.last_name, c.hour_price as client_hour_price,
  70. c.company_name as client_name, c.first_name as client_first_name, c.last_name as client_last_name
  71. FROM " . $this->table_name . " wh
  72. LEFT JOIN users u ON wh.user_id = u.id
  73. LEFT JOIN tasks t ON wh.task_id = t.id
  74. LEFT JOIN projects p ON t.project_id = p.id
  75. LEFT JOIN clients c ON p.customer_id = c.id
  76. WHERE wh.task_id = ?
  77. ORDER BY wh.date DESC, wh.created_at DESC";
  78. $stmt = $this->conn->prepare($query);
  79. $stmt->bindParam(1, $task_id);
  80. $stmt->execute();
  81. return $stmt;
  82. }
  83. public function readOne() {
  84. $query = "SELECT wh.*, t.title as task_title, u.first_name, u.last_name
  85. FROM " . $this->table_name . " wh
  86. LEFT JOIN tasks t ON wh.task_id = t.id
  87. LEFT JOIN users u ON wh.user_id = u.id
  88. WHERE wh.id = ? LIMIT 0,1";
  89. $stmt = $this->conn->prepare($query);
  90. $stmt->bindParam(1, $this->id);
  91. $stmt->execute();
  92. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  93. $this->task_id = $row['task_id'];
  94. $this->user_id = $row['user_id'];
  95. $this->date = $row['date'];
  96. $this->hours = $row['hours'];
  97. $this->description = $row['description'];
  98. $this->rate = $row['rate'];
  99. $this->total_amount = $row['total_amount'];
  100. $this->created_at = $row['created_at'];
  101. $this->updated_at = $row['updated_at'];
  102. }
  103. public function update() {
  104. $query = "UPDATE " . $this->table_name . "
  105. SET task_id=:task_id, user_id=:user_id, date=:date, hours=:hours,
  106. description=:description, rate=:rate, total_amount=:total_amount,
  107. updated_at=:updated_at
  108. WHERE id=:id";
  109. $stmt = $this->conn->prepare($query);
  110. $this->task_id = htmlspecialchars(strip_tags($this->task_id));
  111. $this->user_id = htmlspecialchars(strip_tags($this->user_id));
  112. $this->date = htmlspecialchars(strip_tags($this->date));
  113. $this->hours = htmlspecialchars(strip_tags($this->hours));
  114. $this->description = htmlspecialchars(strip_tags($this->description));
  115. $this->rate = htmlspecialchars(strip_tags($this->rate));
  116. $this->total_amount = htmlspecialchars(strip_tags($this->total_amount));
  117. $this->updated_at = date('Y-m-d H:i:s');
  118. $stmt->bindParam(":task_id", $this->task_id);
  119. $stmt->bindParam(":user_id", $this->user_id);
  120. $stmt->bindParam(":date", $this->date);
  121. $stmt->bindParam(":hours", $this->hours);
  122. $stmt->bindParam(":description", $this->description);
  123. $stmt->bindParam(":rate", $this->rate);
  124. $stmt->bindParam(":total_amount", $this->total_amount);
  125. $stmt->bindParam(":updated_at", $this->updated_at);
  126. $stmt->bindParam(":id", $this->id);
  127. if($stmt->execute()) {
  128. return true;
  129. }
  130. return false;
  131. }
  132. public function delete() {
  133. $query = "DELETE FROM " . $this->table_name . " WHERE id = ?";
  134. $stmt = $this->conn->prepare($query);
  135. $stmt->bindParam(1, $this->id);
  136. if($stmt->execute()) {
  137. return true;
  138. }
  139. return false;
  140. }
  141. public function getTotalHoursByTask($task_id) {
  142. $query = "SELECT SUM(hours) as total_hours, COUNT(*) as entries
  143. FROM " . $this->table_name . "
  144. WHERE task_id = ?";
  145. $stmt = $this->conn->prepare($query);
  146. $stmt->bindParam(1, $task_id);
  147. $stmt->execute();
  148. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  149. return [
  150. 'total_hours' => $row['total_hours'] || 0,
  151. 'entries' => $row['entries'] || 0
  152. ];
  153. }
  154. public function getTotalHoursByUser($user_id, $start_date = null, $end_date = null) {
  155. $query = "SELECT SUM(wh.hours) as total_hours, COUNT(*) as entries
  156. FROM " . $this->table_name . " wh
  157. WHERE wh.user_id = ?";
  158. if ($start_date && $end_date) {
  159. $query .= " AND wh.date BETWEEN ? AND ?";
  160. }
  161. $stmt = $this->conn->prepare($query);
  162. $stmt->bindParam(1, $user_id);
  163. if ($start_date && $end_date) {
  164. $stmt->bindParam(2, $start_date);
  165. $stmt->bindParam(3, $end_date);
  166. }
  167. $stmt->execute();
  168. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  169. return [
  170. 'total_hours' => $row['total_hours'] || 0,
  171. 'entries' => $row['entries'] || 0
  172. ];
  173. }
  174. public function getClientHourPrice() {
  175. $query = "SELECT c.hour_price
  176. FROM " . $this->table_name . " wh
  177. LEFT JOIN tasks t ON wh.task_id = t.id
  178. LEFT JOIN projects p ON t.project_id = p.id
  179. LEFT JOIN clients c ON p.customer_id = c.id
  180. WHERE wh.task_id = ? AND c.hour_price IS NOT NULL AND c.hour_price > 0
  181. LIMIT 1";
  182. $stmt = $this->conn->prepare($query);
  183. $stmt->bindParam(1, $this->task_id);
  184. $stmt->execute();
  185. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  186. return $row ? $row['hour_price'] : 0;
  187. }
  188. }
  189. ?>