AccountTransaction.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. class AccountTransaction {
  3. private $conn;
  4. private $table_name = "account_transactions";
  5. public $id;
  6. public $journal_entry_id;
  7. public $account_id;
  8. public $debit_amount;
  9. public $credit_amount;
  10. public $description;
  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 journal_entry_id=:journal_entry_id, account_id=:account_id, debit_amount=:debit_amount, credit_amount=:credit_amount, description=:description, created_at=:created_at, updated_at=:updated_at";
  18. $stmt = $this->conn->prepare($query);
  19. $this->journal_entry_id = htmlspecialchars(strip_tags($this->journal_entry_id));
  20. $this->account_id = htmlspecialchars(strip_tags($this->account_id));
  21. $this->debit_amount = htmlspecialchars(strip_tags($this->debit_amount));
  22. $this->credit_amount = htmlspecialchars(strip_tags($this->credit_amount));
  23. $this->description = htmlspecialchars(strip_tags($this->description));
  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(":journal_entry_id", $this->journal_entry_id);
  27. $stmt->bindParam(":account_id", $this->account_id);
  28. $stmt->bindParam(":debit_amount", $this->debit_amount);
  29. $stmt->bindParam(":credit_amount", $this->credit_amount);
  30. $stmt->bindParam(":description", $this->description);
  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 at.*, coa.account_name, coa.account_type, je.entry_number, je.entry_date FROM " . $this->table_name . " at LEFT JOIN chart_of_accounts coa ON at.account_id = coa.id LEFT JOIN journal_entries je ON at.journal_entry_id = je.id ORDER BY je.entry_date DESC, je.created_at DESC, at.id DESC";
  40. $stmt = $this->conn->prepare($query);
  41. $stmt->execute();
  42. return $stmt;
  43. }
  44. public function readOne() {
  45. $query = "SELECT at.*, coa.account_name, coa.account_type, je.entry_number, je.entry_date FROM " . $this->table_name . " at LEFT JOIN chart_of_accounts coa ON at.account_id = coa.id LEFT JOIN journal_entries je ON at.journal_entry_id = je.id WHERE at.id = ? LIMIT 0,1";
  46. $stmt = $this->conn->prepare($query);
  47. $stmt->bindParam(1, $this->id);
  48. $stmt->execute();
  49. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  50. $this->journal_entry_id = $row['journal_entry_id'];
  51. $this->account_id = $row['account_id'];
  52. $this->debit_amount = $row['debit_amount'];
  53. $this->credit_amount = $row['credit_amount'];
  54. $this->description = $row['description'];
  55. $this->created_at = $row['created_at'];
  56. $this->updated_at = $row['updated_at'];
  57. }
  58. public function update() {
  59. $query = "UPDATE " . $this->table_name . " SET journal_entry_id=:journal_entry_id, account_id=:account_id, debit_amount=:debit_amount, credit_amount=:credit_amount, description=:description, updated_at=:updated_at WHERE id=:id";
  60. $stmt = $this->conn->prepare($query);
  61. $this->journal_entry_id = htmlspecialchars(strip_tags($this->journal_entry_id));
  62. $this->account_id = htmlspecialchars(strip_tags($this->account_id));
  63. $this->debit_amount = htmlspecialchars(strip_tags($this->debit_amount));
  64. $this->credit_amount = htmlspecialchars(strip_tags($this->credit_amount));
  65. $this->description = htmlspecialchars(strip_tags($this->description));
  66. $this->updated_at = date('Y-m-d H:i:s');
  67. $stmt->bindParam(":journal_entry_id", $this->journal_entry_id);
  68. $stmt->bindParam(":account_id", $this->account_id);
  69. $stmt->bindParam(":debit_amount", $this->debit_amount);
  70. $stmt->bindParam(":credit_amount", $this->credit_amount);
  71. $stmt->bindParam(":description", $this->description);
  72. $stmt->bindParam(":updated_at", $this->updated_at);
  73. $stmt->bindParam(":id", $this->id);
  74. if($stmt->execute()) {
  75. return true;
  76. }
  77. return false;
  78. }
  79. public function delete() {
  80. $query = "DELETE FROM " . $this->table_name . " WHERE id = ?";
  81. $stmt = $this->conn->prepare($query);
  82. $stmt->bindParam(1, $this->id);
  83. if($stmt->execute()) {
  84. return true;
  85. }
  86. return false;
  87. }
  88. public function search($search_term) {
  89. $query = "SELECT at.*, coa.account_name, coa.account_type, je.entry_number, je.entry_date FROM " . $this->table_name . " at LEFT JOIN chart_of_accounts coa ON at.account_id = coa.id LEFT JOIN journal_entries je ON at.journal_entry_id = je.id WHERE
  90. at.description LIKE ? OR
  91. coa.account_name LIKE ? OR
  92. je.entry_number LIKE ? OR
  93. je.entry_date LIKE ?
  94. ORDER BY je.entry_date DESC, je.created_at DESC, at.id DESC";
  95. $stmt = $this->conn->prepare($query);
  96. $search_term = "%{$search_term}%";
  97. $stmt->bindParam(1, $search_term);
  98. $stmt->bindParam(2, $search_term);
  99. $stmt->bindParam(3, $search_term);
  100. $stmt->bindParam(4, $search_term);
  101. $stmt->execute();
  102. return $stmt;
  103. }
  104. public function getBalance($account_id) {
  105. $query = "SELECT SUM(debit_amount) - SUM(credit_amount) as balance FROM " . $this->table_name . " WHERE account_id = ?";
  106. $stmt = $this->conn->prepare($query);
  107. $stmt->bindParam(1, $account_id);
  108. $stmt->execute();
  109. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  110. return $row['balance'] ?? 0;
  111. }
  112. public function getTransactionType() {
  113. if ($this->debit_amount > 0 && $this->credit_amount == 0) {
  114. return '<span style="background-color: #dc3545; color: white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">Debit</span>';
  115. } elseif ($this->debit_amount == 0 && $this->credit_amount > 0) {
  116. return '<span style="background-color: #28a745; color: white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">Credit</span>';
  117. } else {
  118. return '<span style="background-color: #ffc107; color: black; padding: 2px 6px; border-radius: 4px; font-size: 12px;">Mixed</span>';
  119. }
  120. }
  121. public function getAmount() {
  122. if ($this->debit_amount > 0) {
  123. return $this->debit_amount;
  124. } else {
  125. return $this->credit_amount;
  126. }
  127. }
  128. }
  129. ?>