Payment.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. class Payment {
  3. private $conn;
  4. private $table_name = "payments";
  5. public $id;
  6. public $invoice_id;
  7. public $client_id;
  8. public $payment_date;
  9. public $amount;
  10. public $payment_method;
  11. public $reference_number;
  12. public $notes;
  13. public $created_at;
  14. public $updated_at;
  15. public function __construct($db) {
  16. $this->conn = $db;
  17. }
  18. public function create() {
  19. $query = "INSERT INTO " . $this->table_name . " SET invoice_id=:invoice_id, client_id=:client_id, payment_date=:payment_date, amount=:amount, payment_method=:payment_method, reference_number=:reference_number, notes=:notes, created_at=:created_at, updated_at=:updated_at";
  20. $stmt = $this->conn->prepare($query);
  21. $this->invoice_id = htmlspecialchars(strip_tags($this->invoice_id));
  22. $this->client_id = htmlspecialchars(strip_tags($this->client_id));
  23. $this->payment_date = htmlspecialchars(strip_tags($this->payment_date));
  24. $this->amount = htmlspecialchars(strip_tags($this->amount));
  25. $this->payment_method = htmlspecialchars(strip_tags($this->payment_method));
  26. $this->reference_number = htmlspecialchars(strip_tags($this->reference_number));
  27. $this->notes = htmlspecialchars(strip_tags($this->notes));
  28. $this->created_at = date('Y-m-d H:i:s');
  29. $this->updated_at = date('Y-m-d H:i:s');
  30. $stmt->bindParam(":invoice_id", $this->invoice_id);
  31. $stmt->bindParam(":client_id", $this->client_id);
  32. $stmt->bindParam(":payment_date", $this->payment_date);
  33. $stmt->bindParam(":amount", $this->amount);
  34. $stmt->bindParam(":payment_method", $this->payment_method);
  35. $stmt->bindParam(":reference_number", $this->reference_number);
  36. $stmt->bindParam(":notes", $this->notes);
  37. $stmt->bindParam(":created_at", $this->created_at);
  38. $stmt->bindParam(":updated_at", $this->updated_at);
  39. if($stmt->execute()) {
  40. return true;
  41. }
  42. return false;
  43. }
  44. public function read() {
  45. $query = "SELECT p.*, i.invoice_number, c.first_name, c.last_name, c.company_name FROM " . $this->table_name . " p LEFT JOIN invoices i ON p.invoice_id = i.id LEFT JOIN clients c ON p.client_id = c.id ORDER BY p.payment_date DESC, p.created_at DESC";
  46. $stmt = $this->conn->prepare($query);
  47. $stmt->execute();
  48. return $stmt;
  49. }
  50. public function readOne() {
  51. $query = "SELECT p.*, i.invoice_number, c.first_name, c.last_name, c.company_name FROM " . $this->table_name . " p LEFT JOIN invoices i ON p.invoice_id = i.id LEFT JOIN clients c ON p.client_id = c.id WHERE p.id = ? LIMIT 0,1";
  52. $stmt = $this->conn->prepare($query);
  53. $stmt->bindParam(1, $this->id);
  54. $stmt->execute();
  55. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  56. $this->invoice_id = $row['invoice_id'];
  57. $this->client_id = $row['client_id'];
  58. $this->payment_date = $row['payment_date'];
  59. $this->amount = $row['amount'];
  60. $this->payment_method = $row['payment_method'];
  61. $this->reference_number = $row['reference_number'];
  62. $this->notes = $row['notes'];
  63. $this->created_at = $row['created_at'];
  64. $this->updated_at = $row['updated_at'];
  65. }
  66. public function update() {
  67. $query = "UPDATE " . $this->table_name . " SET invoice_id=:invoice_id, client_id=:client_id, payment_date=:payment_date, amount=:amount, payment_method=:payment_method, reference_number=:reference_number, notes=:notes, updated_at=:updated_at WHERE id=:id";
  68. $stmt = $this->conn->prepare($query);
  69. $this->invoice_id = htmlspecialchars(strip_tags($this->invoice_id));
  70. $this->client_id = htmlspecialchars(strip_tags($this->client_id));
  71. $this->payment_date = htmlspecialchars(strip_tags($this->payment_date));
  72. $this->amount = htmlspecialchars(strip_tags($this->amount));
  73. $this->payment_method = htmlspecialchars(strip_tags($this->payment_method));
  74. $this->reference_number = htmlspecialchars(strip_tags($this->reference_number));
  75. $this->notes = htmlspecialchars(strip_tags($this->notes));
  76. $this->updated_at = date('Y-m-d H:i:s');
  77. $stmt->bindParam(":invoice_id", $this->invoice_id);
  78. $stmt->bindParam(":client_id", $this->client_id);
  79. $stmt->bindParam(":payment_date", $this->payment_date);
  80. $stmt->bindParam(":amount", $this->amount);
  81. $stmt->bindParam(":payment_method", $this->payment_method);
  82. $stmt->bindParam(":reference_number", $this->reference_number);
  83. $stmt->bindParam(":notes", $this->notes);
  84. $stmt->bindParam(":updated_at", $this->updated_at);
  85. $stmt->bindParam(":id", $this->id);
  86. if($stmt->execute()) {
  87. return true;
  88. }
  89. return false;
  90. }
  91. public function delete() {
  92. $query = "DELETE FROM " . $this->table_name . " WHERE id = ?";
  93. $stmt = $this->conn->prepare($query);
  94. $stmt->bindParam(1, $this->id);
  95. if($stmt->execute()) {
  96. return true;
  97. }
  98. return false;
  99. }
  100. public function search($search_term) {
  101. $query = "SELECT p.*, i.invoice_number, c.first_name, c.last_name, c.company_name FROM " . $this->table_name . " p LEFT JOIN invoices i ON p.invoice_id = i.id LEFT JOIN clients c ON p.client_id = c.id WHERE
  102. p.reference_number LIKE ? OR
  103. p.notes LIKE ? OR
  104. i.invoice_number LIKE ? OR
  105. c.first_name LIKE ? OR
  106. c.last_name LIKE ? OR
  107. c.company_name LIKE ?
  108. ORDER BY p.payment_date DESC, p.created_at DESC";
  109. $stmt = $this->conn->prepare($query);
  110. $search_term = "%{$search_term}%";
  111. $stmt->bindParam(1, $search_term);
  112. $stmt->bindParam(2, $search_term);
  113. $stmt->bindParam(3, $search_term);
  114. $stmt->bindParam(4, $search_term);
  115. $stmt->bindParam(5, $search_term);
  116. $stmt->bindParam(6, $search_term);
  117. $stmt->execute();
  118. return $stmt;
  119. }
  120. public function getPaymentMethodBadge() {
  121. $badges = [
  122. 'cash' => '<span style="background-color: #28a745; color: white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">Cash</span>',
  123. 'check' => '<span style="background-color: #17a2b8; color: white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">Check</span>',
  124. 'credit_card' => '<span style="background-color: #6f42c1; color: white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">Credit Card</span>',
  125. 'bank_transfer' => '<span style="background-color: #20c997; color: white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">Bank Transfer</span>',
  126. 'other' => '<span style="background-color: #6c757d; color: white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">Other</span>'
  127. ];
  128. return $badges[$this->payment_method] ?? $this->payment_method;
  129. }
  130. }
  131. ?>