ChartOfAccounts.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 $vat_percentage;
  14. public $is_active;
  15. public $created_at;
  16. public $updated_at;
  17. public function __construct($db) {
  18. $this->conn = $db;
  19. }
  20. public function create() {
  21. $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, vat_percentage=:vat_percentage, is_active=:is_active, created_at=:created_at, updated_at=:updated_at";
  22. $stmt = $this->conn->prepare($query);
  23. $this->account_number = htmlspecialchars(strip_tags($this->account_number));
  24. $this->account_name = htmlspecialchars(strip_tags($this->account_name));
  25. $this->account_type = htmlspecialchars(strip_tags($this->account_type));
  26. $this->parent_id = htmlspecialchars(strip_tags($this->parent_id));
  27. $this->description = htmlspecialchars(strip_tags($this->description));
  28. $this->opening_balance = htmlspecialchars(strip_tags($this->opening_balance));
  29. $this->current_balance = htmlspecialchars(strip_tags($this->current_balance));
  30. $this->vat_percentage = htmlspecialchars(strip_tags($this->vat_percentage));
  31. $this->is_active = $this->is_active ? 1 : 0;
  32. $this->created_at = date('Y-m-d H:i:s');
  33. $this->updated_at = date('Y-m-d H:i:s');
  34. $stmt->bindParam(":account_number", $this->account_number);
  35. $stmt->bindParam(":account_name", $this->account_name);
  36. $stmt->bindParam(":account_type", $this->account_type);
  37. $stmt->bindParam(":parent_id", $this->parent_id);
  38. $stmt->bindParam(":description", $this->description);
  39. $stmt->bindParam(":opening_balance", $this->opening_balance);
  40. $stmt->bindParam(":current_balance", $this->current_balance);
  41. $stmt->bindParam(":vat_percentage", $this->vat_percentage);
  42. $stmt->bindParam(":is_active", $this->is_active);
  43. $stmt->bindParam(":created_at", $this->created_at);
  44. $stmt->bindParam(":updated_at", $this->updated_at);
  45. if($stmt->execute()) {
  46. return true;
  47. }
  48. return false;
  49. }
  50. public function read() {
  51. $query = "SELECT * FROM " . $this->table_name . " WHERE is_active = TRUE ORDER BY account_type, account_number";
  52. $stmt = $this->conn->prepare($query);
  53. $stmt->execute();
  54. return $stmt;
  55. }
  56. public function readOne() {
  57. $query = "SELECT * FROM " . $this->table_name . " WHERE id = ? LIMIT 0,1";
  58. $stmt = $this->conn->prepare($query);
  59. $stmt->bindParam(1, $this->id);
  60. $stmt->execute();
  61. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  62. $this->account_number = $row['account_number'];
  63. $this->account_name = $row['account_name'];
  64. $this->account_type = $row['account_type'];
  65. $this->parent_id = $row['parent_id'];
  66. $this->description = $row['description'];
  67. $this->opening_balance = $row['opening_balance'];
  68. $this->current_balance = $row['current_balance'];
  69. $this->vat_percentage = $row['vat_percentage'];
  70. $this->is_active = $row['is_active'];
  71. $this->created_at = $row['created_at'];
  72. $this->updated_at = $row['updated_at'];
  73. }
  74. public function update() {
  75. $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, vat_percentage=:vat_percentage, is_active=:is_active, updated_at=:updated_at WHERE id=:id";
  76. $stmt = $this->conn->prepare($query);
  77. $this->account_number = htmlspecialchars(strip_tags($this->account_number));
  78. $this->account_name = htmlspecialchars(strip_tags($this->account_name));
  79. $this->account_type = htmlspecialchars(strip_tags($this->account_type));
  80. $this->parent_id = htmlspecialchars(strip_tags($this->parent_id));
  81. $this->description = htmlspecialchars(strip_tags($this->description));
  82. $this->opening_balance = htmlspecialchars(strip_tags($this->opening_balance));
  83. $this->current_balance = htmlspecialchars(strip_tags($this->current_balance));
  84. $this->vat_percentage = htmlspecialchars(strip_tags($this->vat_percentage));
  85. $this->is_active = $this->is_active ? 1 : 0;
  86. $this->updated_at = date('Y-m-d H:i:s');
  87. $stmt->bindParam(":account_number", $this->account_number);
  88. $stmt->bindParam(":account_name", $this->account_name);
  89. $stmt->bindParam(":account_type", $this->account_type);
  90. $stmt->bindParam(":parent_id", $this->parent_id);
  91. $stmt->bindParam(":description", $this->description);
  92. $stmt->bindParam(":opening_balance", $this->opening_balance);
  93. $stmt->bindParam(":current_balance", $this->current_balance);
  94. $stmt->bindParam(":vat_percentage", $this->vat_percentage);
  95. $stmt->bindParam(":is_active", $this->is_active);
  96. $stmt->bindParam(":updated_at", $this->updated_at);
  97. $stmt->bindParam(":id", $this->id);
  98. if($stmt->execute()) {
  99. return true;
  100. }
  101. return false;
  102. }
  103. public function delete() {
  104. $query = "DELETE FROM " . $this->table_name . " WHERE id = ?";
  105. $stmt = $this->conn->prepare($query);
  106. $stmt->bindParam(1, $this->id);
  107. if($stmt->execute()) {
  108. return true;
  109. }
  110. return false;
  111. }
  112. public function search($search_term) {
  113. $query = "SELECT * FROM " . $this->table_name . " WHERE
  114. account_number LIKE ? OR
  115. account_name LIKE ? OR
  116. description LIKE ?
  117. ORDER BY account_type, account_number";
  118. $stmt = $this->conn->prepare($query);
  119. $search_term = "%{$search_term}%";
  120. $stmt->bindParam(1, $search_term);
  121. $stmt->bindParam(2, $search_term);
  122. $stmt->bindParam(3, $search_term);
  123. $stmt->execute();
  124. return $stmt;
  125. }
  126. public function getAccountTypeBadge() {
  127. $badges = [
  128. 'asset' => '<span style="background-color: #007bff; color: white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">Vasta-omaisuus</span>',
  129. 'liability' => '<span style="background-color: #dc3545; color: white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">Velat</span>',
  130. 'equity' => '<span style="background-color: #28a745; color: white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">Oma pääoma</span>',
  131. 'revenue' => '<span style="background-color: #17a2b8; color: white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">Tuotot</span>',
  132. 'expense' => '<span style="background-color: #ffc107; color: black; padding: 2px 6px; border-radius: 4px; font-size: 12px;">Kulut</span>'
  133. ];
  134. return $badges[$this->account_type] ?? $this->account_type;
  135. }
  136. public function getAccountTypeName() {
  137. $types = [
  138. 'asset' => 'Vasta-omaisuus',
  139. 'liability' => 'Velat',
  140. 'equity' => 'Oma pääoma',
  141. 'revenue' => 'Tuotot',
  142. 'expense' => 'Kulut'
  143. ];
  144. return $types[$this->account_type] ?? $this->account_type;
  145. }
  146. public function getAccountCategory() {
  147. $categories = [
  148. 'asset' => '1000-1999',
  149. 'liability' => '2000-2999',
  150. 'equity' => '3000-3999',
  151. 'revenue' => '4000-4999',
  152. 'expense' => '5000-5999'
  153. ];
  154. return $categories[$this->account_type] ?? $this->account_type;
  155. }
  156. }
  157. ?>