AccountingEntry.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. class AccountingEntry {
  3. private $conn;
  4. private $table_name = "accounting_entries";
  5. public $id;
  6. public $entry_date;
  7. public $description;
  8. public $entry_type;
  9. public $category;
  10. public $tax_free_amount;
  11. public $vat_percentage;
  12. public $vat_25_5;
  13. public $vat_14;
  14. public $vat_10;
  15. public $total_amount;
  16. public $net_amount;
  17. public $vat_amount;
  18. public $reference_number;
  19. public function __construct($db) {
  20. $this->conn = $db;
  21. }
  22. public function create() {
  23. $query = "INSERT INTO " . $this->table_name . " SET
  24. entry_date=:entry_date,
  25. description=:description,
  26. entry_type=:entry_type,
  27. category=:category,
  28. tax_free_amount=:tax_free_amount,
  29. vat_percentage=:vat_percentage,
  30. vat_25_5=:vat_25_5,
  31. vat_14=:vat_14,
  32. vat_10=:vat_10,
  33. total_amount=:total_amount,
  34. net_amount=:net_amount,
  35. vat_amount=:vat_amount,
  36. reference_number=:reference_number";
  37. $stmt = $this->conn->prepare($query);
  38. $this->entry_date = htmlspecialchars(strip_tags($this->entry_date));
  39. $this->description = htmlspecialchars(strip_tags($this->description));
  40. $this->entry_type = htmlspecialchars(strip_tags($this->entry_type));
  41. $this->category = htmlspecialchars(strip_tags($this->category));
  42. $this->tax_free_amount = htmlspecialchars(strip_tags($this->tax_free_amount));
  43. $this->vat_percentage = htmlspecialchars(strip_tags($this->vat_percentage));
  44. $this->vat_25_5 = htmlspecialchars(strip_tags($this->vat_25_5));
  45. $this->vat_14 = htmlspecialchars(strip_tags($this->vat_14));
  46. $this->vat_10 = htmlspecialchars(strip_tags($this->vat_10));
  47. $this->total_amount = htmlspecialchars(strip_tags($this->total_amount));
  48. $this->net_amount = htmlspecialchars(strip_tags($this->net_amount));
  49. $this->vat_amount = htmlspecialchars(strip_tags($this->vat_amount));
  50. $this->reference_number = htmlspecialchars(strip_tags($this->reference_number));
  51. $stmt->bindParam(":entry_date", $this->entry_date);
  52. $stmt->bindParam(":description", $this->description);
  53. $stmt->bindParam(":entry_type", $this->entry_type);
  54. $stmt->bindParam(":category", $this->category);
  55. $stmt->bindParam(":tax_free_amount", $this->tax_free_amount);
  56. $stmt->bindParam(":vat_percentage", $this->vat_percentage);
  57. $stmt->bindParam(":vat_25_5", $this->vat_25_5);
  58. $stmt->bindParam(":vat_14", $this->vat_14);
  59. $stmt->bindParam(":vat_10", $this->vat_10);
  60. $stmt->bindParam(":total_amount", $this->total_amount);
  61. $stmt->bindParam(":net_amount", $this->net_amount);
  62. $stmt->bindParam(":vat_amount", $this->vat_amount);
  63. $stmt->bindParam(":reference_number", $this->reference_number);
  64. if($stmt->execute()) {
  65. return true;
  66. }
  67. return false;
  68. }
  69. public function read() {
  70. $query = "SELECT * FROM " . $this->table_name . " ORDER BY entry_date DESC, id DESC";
  71. $stmt = $this->conn->prepare($query);
  72. $stmt->execute();
  73. return $stmt;
  74. }
  75. public function readOne() {
  76. $query = "SELECT * FROM " . $this->table_name . " WHERE id = ? LIMIT 0,1";
  77. $stmt = $this->conn->prepare($query);
  78. $stmt->bindParam(1, $this->id);
  79. $stmt->execute();
  80. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  81. $this->entry_date = $row['entry_date'];
  82. $this->description = $row['description'];
  83. $this->entry_type = $row['entry_type'];
  84. $this->category = $row['category'];
  85. $this->tax_free_amount = $row['tax_free_amount'];
  86. $this->vat_percentage = $row['vat_percentage'];
  87. $this->vat_25_5 = $row['vat_25_5'];
  88. $this->vat_14 = $row['vat_14'];
  89. $this->vat_10 = $row['vat_10'];
  90. $this->total_amount = $row['total_amount'];
  91. $this->net_amount = $row['net_amount'];
  92. $this->vat_amount = $row['vat_amount'];
  93. $this->reference_number = $row['reference_number'];
  94. }
  95. public function update() {
  96. $query = "UPDATE " . $this->table_name . " SET
  97. entry_date=:entry_date,
  98. description=:description,
  99. entry_type=:entry_type,
  100. category=:category,
  101. tax_free_amount=:tax_free_amount,
  102. vat_percentage=:vat_percentage,
  103. vat_25_5=:vat_25_5,
  104. vat_14=:vat_14,
  105. vat_10=:vat_10,
  106. total_amount=:total_amount,
  107. net_amount=:net_amount,
  108. vat_amount=:vat_amount,
  109. reference_number=:reference_number
  110. WHERE id=:id";
  111. $stmt = $this->conn->prepare($query);
  112. $this->entry_date = htmlspecialchars(strip_tags($this->entry_date));
  113. $this->description = htmlspecialchars(strip_tags($this->description));
  114. $this->entry_type = htmlspecialchars(strip_tags($this->entry_type));
  115. $this->category = htmlspecialchars(strip_tags($this->category));
  116. $this->tax_free_amount = htmlspecialchars(strip_tags($this->tax_free_amount));
  117. $this->vat_percentage = htmlspecialchars(strip_tags($this->vat_percentage));
  118. $this->vat_25_5 = htmlspecialchars(strip_tags($this->vat_25_5));
  119. $this->vat_14 = htmlspecialchars(strip_tags($this->vat_14));
  120. $this->vat_10 = htmlspecialchars(strip_tags($this->vat_10));
  121. $this->total_amount = htmlspecialchars(strip_tags($this->total_amount));
  122. $this->net_amount = htmlspecialchars(strip_tags($this->net_amount));
  123. $this->vat_amount = htmlspecialchars(strip_tags($this->vat_amount));
  124. $this->reference_number = htmlspecialchars(strip_tags($this->reference_number));
  125. $stmt->bindParam(":entry_date", $this->entry_date);
  126. $stmt->bindParam(":description", $this->description);
  127. $stmt->bindParam(":entry_type", $this->entry_type);
  128. $stmt->bindParam(":category", $this->category);
  129. $stmt->bindParam(":tax_free_amount", $this->tax_free_amount);
  130. $stmt->bindParam(":vat_percentage", $this->vat_percentage);
  131. $stmt->bindParam(":vat_25_5", $this->vat_25_5);
  132. $stmt->bindParam(":vat_14", $this->vat_14);
  133. $stmt->bindParam(":vat_10", $this->vat_10);
  134. $stmt->bindParam(":total_amount", $this->total_amount);
  135. $stmt->bindParam(":net_amount", $this->net_amount);
  136. $stmt->bindParam(":vat_amount", $this->vat_amount);
  137. $stmt->bindParam(":id", $this->id);
  138. if($stmt->execute()) {
  139. return true;
  140. }
  141. return false;
  142. }
  143. public function delete() {
  144. $query = "DELETE FROM " . $this->table_name . " WHERE id = ?";
  145. $stmt = $this->conn->prepare($query);
  146. $stmt->bindParam(1, $this->id);
  147. if($stmt->execute()) {
  148. return true;
  149. }
  150. return false;
  151. }
  152. public function search($search_term) {
  153. $query = "SELECT * FROM " . $this->table_name . "
  154. WHERE description LIKE ? OR
  155. category LIKE ? OR
  156. reference_number LIKE ?
  157. ORDER BY entry_date DESC, id DESC";
  158. $stmt = $this->conn->prepare($query);
  159. $search_term = "%{$search_term}%";
  160. $stmt->bindParam(1, $search_term);
  161. $stmt->bindParam(2, $search_term);
  162. $stmt->bindParam(3, $search_term);
  163. $stmt->execute();
  164. return $stmt;
  165. }
  166. public function getByDateRange($start_date, $end_date) {
  167. $query = "SELECT * FROM " . $this->table_name . "
  168. WHERE entry_date BETWEEN ? AND ?
  169. ORDER BY entry_date DESC, id DESC";
  170. $stmt = $this->conn->prepare($query);
  171. $stmt->bindParam(1, $start_date);
  172. $stmt->bindParam(2, $end_date);
  173. $stmt->execute();
  174. return $stmt;
  175. }
  176. public function getMonthlySummary($year, $month) {
  177. $query = "SELECT
  178. entry_type,
  179. SUM(total_amount) as total,
  180. SUM(vat_amount) as vat_total,
  181. SUM(net_amount) as net_total,
  182. COUNT(*) as count
  183. FROM " . $this->table_name . "
  184. WHERE YEAR(entry_date) = ? AND MONTH(entry_date) = ?
  185. GROUP BY entry_type";
  186. $stmt = $this->conn->prepare($query);
  187. $stmt->bindParam(1, $year);
  188. $stmt->bindParam(2, $month);
  189. $stmt->execute();
  190. return $stmt;
  191. }
  192. public function getEntryTypeBadge() {
  193. $badges = [
  194. 'Tulo' => '<span style="background-color: #28a745; color: white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">Tulo</span>',
  195. 'Kulu' => '<span style="background-color: #dc3545; color: white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">Kulu</span>'
  196. ];
  197. return $badges[$this->entry_type] ?? $this->entry_type;
  198. }
  199. public function getEntryTypeName() {
  200. $types = [
  201. 'Tulo' => 'Tulo',
  202. 'Kulu' => 'Kulu'
  203. ];
  204. return $types[$this->entry_type] ?? $this->entry_type;
  205. }
  206. }
  207. ?>