Customer.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. class Customer {
  3. private $conn;
  4. private $table_name = "clients";
  5. public $id;
  6. public $y_tunnus;
  7. public $company_name;
  8. public $first_name;
  9. public $last_name;
  10. public $email;
  11. public $phone;
  12. public $address;
  13. public $city;
  14. public $state;
  15. public $postal_code;
  16. public $country;
  17. public $notes;
  18. public $hour_price;
  19. public $created_at;
  20. public $updated_at;
  21. public function __construct($db) {
  22. $this->conn = $db;
  23. }
  24. public function create() {
  25. $query = "INSERT INTO " . $this->table_name . " SET y_tunnus=:y_tunnus, company_name=:company_name, first_name=:first_name, last_name=:last_name, email=:email, phone=:phone, address=:address, city=:city, state=:state, postal_code=:postal_code, country=:country, notes=:notes, hour_price=:hour_price, created_at=:created_at, updated_at=:updated_at";
  26. $stmt = $this->conn->prepare($query);
  27. $this->y_tunnus = htmlspecialchars(strip_tags($this->y_tunnus));
  28. $this->company_name = htmlspecialchars(strip_tags($this->company_name));
  29. $this->first_name = htmlspecialchars(strip_tags($this->first_name));
  30. $this->last_name = htmlspecialchars(strip_tags($this->last_name));
  31. $this->email = htmlspecialchars(strip_tags($this->email));
  32. $this->phone = htmlspecialchars(strip_tags($this->phone));
  33. $this->address = htmlspecialchars(strip_tags($this->address));
  34. $this->city = htmlspecialchars(strip_tags($this->city));
  35. $this->state = htmlspecialchars(strip_tags($this->state));
  36. $this->postal_code = htmlspecialchars(strip_tags($this->postal_code));
  37. $this->country = htmlspecialchars(strip_tags($this->country));
  38. $this->notes = htmlspecialchars(strip_tags($this->notes));
  39. $this->hour_price = htmlspecialchars(strip_tags($this->hour_price));
  40. $this->created_at = date('Y-m-d H:i:s');
  41. $this->updated_at = date('Y-m-d H:i:s');
  42. $stmt->bindParam(":y_tunnus", $this->y_tunnus);
  43. $stmt->bindParam(":company_name", $this->company_name);
  44. $stmt->bindParam(":first_name", $this->first_name);
  45. $stmt->bindParam(":last_name", $this->last_name);
  46. $stmt->bindParam(":email", $this->email);
  47. $stmt->bindParam(":phone", $this->phone);
  48. $stmt->bindParam(":address", $this->address);
  49. $stmt->bindParam(":city", $this->city);
  50. $stmt->bindParam(":state", $this->state);
  51. $stmt->bindParam(":postal_code", $this->postal_code);
  52. $stmt->bindParam(":country", $this->country);
  53. $stmt->bindParam(":notes", $this->notes);
  54. $stmt->bindParam(":hour_price", $this->hour_price);
  55. $stmt->bindParam(":created_at", $this->created_at);
  56. $stmt->bindParam(":updated_at", $this->updated_at);
  57. if($stmt->execute()) {
  58. return true;
  59. }
  60. return false;
  61. }
  62. public function read() {
  63. $query = "SELECT * FROM " . $this->table_name . " ORDER BY company_name, last_name, first_name";
  64. $stmt = $this->conn->prepare($query);
  65. $stmt->execute();
  66. return $stmt;
  67. }
  68. public function readOne() {
  69. $query = "SELECT * FROM " . $this->table_name . " WHERE id = ? LIMIT 0,1";
  70. $stmt = $this->conn->prepare($query);
  71. $stmt->bindParam(1, $this->id);
  72. $stmt->execute();
  73. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  74. $this->y_tunnus = $row['y_tunnus'];
  75. $this->company_name = $row['company_name'];
  76. $this->first_name = $row['first_name'];
  77. $this->last_name = $row['last_name'];
  78. $this->email = $row['email'];
  79. $this->phone = $row['phone'];
  80. $this->address = $row['address'];
  81. $this->city = $row['city'];
  82. $this->state = $row['state'];
  83. $this->postal_code = $row['postal_code'];
  84. $this->country = $row['country'];
  85. $this->notes = $row['notes'];
  86. $this->hour_price = $row['hour_price'];
  87. $this->created_at = $row['created_at'];
  88. $this->updated_at = $row['updated_at'];
  89. }
  90. public function update() {
  91. $query = "UPDATE " . $this->table_name . " SET y_tunnus=:y_tunnus, company_name=:company_name, first_name=:first_name, last_name=:last_name, email=:email, phone=:phone, address=:address, city=:city, state=:state, postal_code=:postal_code, country=:country, notes=:notes, hour_price=:hour_price, updated_at=:updated_at WHERE id=:id";
  92. $stmt = $this->conn->prepare($query);
  93. $this->y_tunnus = htmlspecialchars(strip_tags($this->y_tunnus));
  94. $this->company_name = htmlspecialchars(strip_tags($this->company_name));
  95. $this->first_name = htmlspecialchars(strip_tags($this->first_name));
  96. $this->last_name = htmlspecialchars(strip_tags($this->last_name));
  97. $this->email = htmlspecialchars(strip_tags($this->email));
  98. $this->phone = htmlspecialchars(strip_tags($this->phone));
  99. $this->address = htmlspecialchars(strip_tags($this->address));
  100. $this->city = htmlspecialchars(strip_tags($this->city));
  101. $this->state = htmlspecialchars(strip_tags($this->state));
  102. $this->postal_code = htmlspecialchars(strip_tags($this->postal_code));
  103. $this->country = htmlspecialchars(strip_tags($this->country));
  104. $this->notes = htmlspecialchars(strip_tags($this->notes));
  105. $this->hour_price = htmlspecialchars(strip_tags($this->hour_price));
  106. $this->updated_at = date('Y-m-d H:i:s');
  107. $stmt->bindParam(":y_tunnus", $this->y_tunnus);
  108. $stmt->bindParam(":company_name", $this->company_name);
  109. $stmt->bindParam(":first_name", $this->first_name);
  110. $stmt->bindParam(":last_name", $this->last_name);
  111. $stmt->bindParam(":email", $this->email);
  112. $stmt->bindParam(":phone", $this->phone);
  113. $stmt->bindParam(":address", $this->address);
  114. $stmt->bindParam(":city", $this->city);
  115. $stmt->bindParam(":state", $this->state);
  116. $stmt->bindParam(":postal_code", $this->postal_code);
  117. $stmt->bindParam(":country", $this->country);
  118. $stmt->bindParam(":notes", $this->notes);
  119. $stmt->bindParam(":hour_price", $this->hour_price);
  120. $stmt->bindParam(":updated_at", $this->updated_at);
  121. $stmt->bindParam(":id", $this->id);
  122. if($stmt->execute()) {
  123. return true;
  124. }
  125. return false;
  126. }
  127. public function delete() {
  128. $query = "DELETE FROM " . $this->table_name . " WHERE id = ?";
  129. $stmt = $this->conn->prepare($query);
  130. $stmt->bindParam(1, $this->id);
  131. if($stmt->execute()) {
  132. return true;
  133. }
  134. return false;
  135. }
  136. public function search($search_term) {
  137. $query = "SELECT * FROM " . $this->table_name . " WHERE
  138. y_tunnus LIKE ? OR
  139. company_name LIKE ? OR
  140. first_name LIKE ? OR
  141. last_name LIKE ? OR
  142. email LIKE ? OR
  143. phone LIKE ? OR
  144. address LIKE ? OR
  145. city LIKE ? OR
  146. state LIKE ? OR
  147. postal_code LIKE ? OR
  148. country LIKE ? OR
  149. notes LIKE ?
  150. ORDER BY company_name, last_name, first_name";
  151. $stmt = $this->conn->prepare($query);
  152. $search_term = "%{$search_term}%";
  153. $stmt->bindParam(1, $search_term);
  154. $stmt->bindParam(2, $search_term);
  155. $stmt->bindParam(3, $search_term);
  156. $stmt->bindParam(4, $search_term);
  157. $stmt->bindParam(5, $search_term);
  158. $stmt->bindParam(6, $search_term);
  159. $stmt->bindParam(7, $search_term);
  160. $stmt->bindParam(8, $search_term);
  161. $stmt->bindParam(9, $search_term);
  162. $stmt->execute();
  163. return $stmt;
  164. }
  165. public function getProjects($customer_id) {
  166. $query = "SELECT * FROM projects WHERE customer_id = ? ORDER BY start_date DESC, created_at DESC";
  167. $stmt = $this->conn->prepare($query);
  168. $stmt->bindParam(1, $customer_id);
  169. $stmt->execute();
  170. return $stmt;
  171. }
  172. public function getDisplayName() {
  173. if (!empty($this->company_name)) {
  174. return $this->company_name . ' - ' . $this->first_name . ' ' . $this->last_name;
  175. } else {
  176. return $this->first_name . ' ' . $this->last_name;
  177. }
  178. }
  179. public function getPrimaryContact() {
  180. $query = "SELECT * FROM contact_persons WHERE client_id = ? AND is_primary = TRUE LIMIT 1";
  181. $stmt = $this->conn->prepare($query);
  182. $stmt->bindParam(1, $this->id);
  183. $stmt->execute();
  184. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  185. return $row ? $row['first_name'] . ' ' . $row['last_name'] : 'No primary contact';
  186. }
  187. }
  188. ?>