ChartOfAccounts.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. class ChartOfAccounts {
  3. private $conn;
  4. private $table_name = "chart_of_accounts";
  5. public $id;
  6. public $account_number;
  7. public $account_name;
  8. public $account_type;
  9. public $parent_id;
  10. public $description;
  11. public $opening_balance;
  12. public $current_balance;
  13. public $is_active;
  14. public $created_at;
  15. public $updated_at;
  16. public function __construct($db) {
  17. $this->conn = $db;
  18. }
  19. public function create() {
  20. $query = "INSERT INTO " . $this->table_name . " SET account_number=:account_number, account_name=:account_name, account_type=:account_type, parent_id=:parent_id, description=:description, opening_balance=:opening_balance, current_balance=:current_balance, is_active=:is_active, created_at=:created_at, updated_at=:updated_at";
  21. $stmt = $this->conn->prepare($query);
  22. $this->account_number = htmlspecialchars(strip_tags($this->account_number));
  23. $this->account_name = htmlspecialchars(strip_tags($this->account_name));
  24. $this->account_type = htmlspecialchars(strip_tags($this->account_type));
  25. $this->parent_id = htmlspecialchars(strip_tags($this->parent_id));
  26. $this->description = htmlspecialchars(strip_tags($this->description));
  27. $this->opening_balance = htmlspecialchars(strip_tags($this->opening_balance));
  28. $this->current_balance = htmlspecialchars(strip_tags($this->current_balance));
  29. $this->is_active = $this->is_active ? 1 : 0;
  30. $this->created_at = date('Y-m-d H:i:s');
  31. $this->updated_at = date('Y-m-d H:i:s');
  32. $stmt->bindParam(":account_number", $this->account_number);
  33. $stmt->bindParam(":account_name", $this->account_name);
  34. $stmt->bindParam(":account_type", $this->account_type);
  35. $stmt->bindParam(":parent_id", $this->parent_id);
  36. $stmt->bindParam(":description", $this->description);
  37. $stmt->bindParam(":opening_balance", $this->opening_balance);
  38. $stmt->bindParam(":current_balance", $this->current_balance);
  39. $stmt->bindParam(":is_active", $this->is_active);
  40. $stmt->bindParam(":created_at", $this->created_at);
  41. $stmt->bindParam(":updated_at", $this->updated_at);
  42. if($stmt->execute()) {
  43. return true;
  44. }
  45. return false;
  46. }
  47. public function read() {
  48. $query = "SELECT * FROM " . $this->table_name . " WHERE is_active = TRUE ORDER BY account_type, account_number";
  49. $stmt = $this->conn->prepare($query);
  50. $stmt->execute();
  51. return $stmt;
  52. }
  53. public function readOne() {
  54. $query = "SELECT * FROM " . $this->table_name . " WHERE id = ? LIMIT 0,1";
  55. $stmt = $this->conn->prepare($query);
  56. $stmt->bindParam(1, $this->id);
  57. $stmt->execute();
  58. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  59. $this->account_number = $row['account_number'];
  60. $this->account_name = $row['account_name'];
  61. $this->account_type = $row['account_type'];
  62. $this->parent_id = $row['parent_id'];
  63. $this->description = $row['description'];
  64. $this->opening_balance = $row['opening_balance'];
  65. $this->current_balance = $row['current_balance'];
  66. $this->is_active = $row['is_active'];
  67. $this->created_at = $row['created_at'];
  68. $this->updated_at = $row['updated_at'];
  69. }
  70. public function update() {
  71. $query = "UPDATE " . $this->table_name . " SET account_number=:account_number, account_name=:account_name, account_type=:account_type, parent_id=:parent_id, description=:description, opening_balance=:opening_balance, current_balance=:current_balance, is_active=:is_active, updated_at=:updated_at WHERE id=:id";
  72. $stmt = $this->conn->prepare($query);
  73. $this->account_number = htmlspecialchars(strip_tags($this->account_number));
  74. $this->account_name = htmlspecialchars(strip_tags($this->account_name));
  75. $this->account_type = htmlspecialchars(strip_tags($this->account_type));
  76. $this->parent_id = htmlspecialchars(strip_tags($this->parent_id));
  77. $this->description = htmlspecialchars(strip_tags($this->description));
  78. $this->opening_balance = htmlspecialchars(strip_tags($this->opening_balance));
  79. $this->current_balance = htmlspecialchars(strip_tags($this->current_balance));
  80. $this->is_active = $this->is_active ? 1 : 0;
  81. $this->updated_at = date('Y-m-d H:i:s');
  82. $stmt->bindParam(":account_number", $this->account_number);
  83. $stmt->bindParam(":account_name", $this->account_name);
  84. $stmt->bindParam(":account_type", $this->account_type);
  85. $stmt->bindParam(":parent_id", $this->parent_id);
  86. $stmt->bindParam(":description", $this->description);
  87. $stmt->bindParam(":opening_balance", $this->opening_balance);
  88. $stmt->bindParam(":current_balance", $this->current_balance);
  89. $stmt->bindParam(":is_active", $this->is_active);
  90. $stmt->bindParam(":updated_at", $this->updated_at);
  91. $stmt->bindParam(":id", $this->id);
  92. if($stmt->execute()) {
  93. return true;
  94. }
  95. return false;
  96. }
  97. public function delete() {
  98. $query = "DELETE FROM " . $this->table_name . " WHERE id = ?";
  99. $stmt = $this->conn->prepare($query);
  100. $stmt->bindParam(1, $this->id);
  101. if($stmt->execute()) {
  102. return true;
  103. }
  104. return false;
  105. }
  106. public function search($search_term) {
  107. $query = "SELECT * FROM " . $this->table_name . " WHERE
  108. account_number LIKE ? OR
  109. account_name LIKE ? OR
  110. description LIKE ?
  111. ORDER BY account_type, account_number";
  112. $stmt = $this->conn->prepare($query);
  113. $search_term = "%{$search_term}%";
  114. $stmt->bindParam(1, $search_term);
  115. $stmt->bindParam(2, $search_term);
  116. $stmt->bindParam(3, $search_term);
  117. $stmt->execute();
  118. return $stmt;
  119. }
  120. public function getAccountTypeBadge() {
  121. $badges = [
  122. 'asset' => '<span style="background-color: #007bff; color: white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">Asset</span>',
  123. 'liability' => '<span style="background-color: #dc3545; color: white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">Liability</span>',
  124. 'equity' => '<span style="background-color: #28a745; color: white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">Equity</span>',
  125. 'revenue' => '<span style="background-color: #17a2b8; color: white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">Revenue</span>',
  126. 'expense' => '<span style="background-color: #ffc107; color: black; padding: 2px 6px; border-radius: 4px; font-size: 12px;">Expense</span>'
  127. ];
  128. return $badges[$this->account_type] ?? $this->account_type;
  129. }
  130. public function getAccountTypeName() {
  131. $types = [
  132. 'asset' => 'Asset',
  133. 'liability' => 'Liability',
  134. 'equity' => 'Equity',
  135. 'revenue' => 'Revenue',
  136. 'expense' => 'Expense'
  137. ];
  138. return $types[$this->account_type] ?? $this->account_type;
  139. }
  140. }
  141. ?>