JournalEntry.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. class JournalEntry {
  3. private $conn;
  4. private $table_name = "journal_entries";
  5. public $id;
  6. public $entry_number;
  7. public $entry_date;
  8. public $description;
  9. public $reference_number;
  10. public $created_at;
  11. public $updated_at;
  12. public function __construct($db) {
  13. $this->conn = $db;
  14. }
  15. public function create() {
  16. $query = "INSERT INTO " . $this->table_name . " SET entry_number=:entry_number, entry_date=:entry_date, description=:description, reference_number=:reference_number, created_at=:created_at, updated_at=:updated_at";
  17. $stmt = $this->conn->prepare($query);
  18. $this->entry_number = htmlspecialchars(strip_tags($this->entry_number));
  19. $this->entry_date = htmlspecialchars(strip_tags($this->entry_date));
  20. $this->description = htmlspecialchars(strip_tags($this->description));
  21. $this->reference_number = htmlspecialchars(strip_tags($this->reference_number));
  22. $this->created_at = date('Y-m-d H:i:s');
  23. $this->updated_at = date('Y-m-d H:i:s');
  24. $stmt->bindParam(":entry_number", $this->entry_number);
  25. $stmt->bindParam(":entry_date", $this->entry_date);
  26. $stmt->bindParam(":description", $this->description);
  27. $stmt->bindParam(":reference_number", $this->reference_number);
  28. $stmt->bindParam(":created_at", $this->created_at);
  29. $stmt->bindParam(":updated_at", $this->updated_at);
  30. if($stmt->execute()) {
  31. return true;
  32. }
  33. return false;
  34. }
  35. public function read() {
  36. $query = "SELECT * FROM " . $this->table_name . " ORDER BY entry_date DESC, created_at DESC";
  37. $stmt = $this->conn->prepare($query);
  38. $stmt->execute();
  39. return $stmt;
  40. }
  41. public function readOne() {
  42. $query = "SELECT * FROM " . $this->table_name . " WHERE id = ? LIMIT 0,1";
  43. $stmt = $this->conn->prepare($query);
  44. $stmt->bindParam(1, $this->id);
  45. $stmt->execute();
  46. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  47. $this->entry_number = $row['entry_number'];
  48. $this->entry_date = $row['entry_date'];
  49. $this->description = $row['description'];
  50. $this->reference_number = $row['reference_number'];
  51. $this->created_at = $row['created_at'];
  52. $this->updated_at = $row['updated_at'];
  53. }
  54. public function update() {
  55. $query = "UPDATE " . $this->table_name . " SET entry_number=:entry_number, entry_date=:entry_date, description=:description, reference_number=:reference_number, updated_at=:updated_at WHERE id=:id";
  56. $stmt = $this->conn->prepare($query);
  57. $this->entry_number = htmlspecialchars(strip_tags($this->entry_number));
  58. $this->entry_date = htmlspecialchars(strip_tags($this->entry_date));
  59. $this->description = htmlspecialchars(strip_tags($this->description));
  60. $this->reference_number = htmlspecialchars(strip_tags($this->reference_number));
  61. $this->updated_at = date('Y-m-d H:i:s');
  62. $stmt->bindParam(":entry_number", $this->entry_number);
  63. $stmt->bindParam(":entry_date", $this->entry_date);
  64. $stmt->bindParam(":description", $this->description);
  65. $stmt->bindParam(":reference_number", $this->reference_number);
  66. $stmt->bindParam(":updated_at", $this->updated_at);
  67. $stmt->bindParam(":id", $this->id);
  68. if($stmt->execute()) {
  69. return true;
  70. }
  71. return false;
  72. }
  73. public function delete() {
  74. $query = "DELETE FROM " . $this->table_name . " WHERE id = ?";
  75. $stmt = $this->conn->prepare($query);
  76. $stmt->bindParam(1, $this->id);
  77. if($stmt->execute()) {
  78. return true;
  79. }
  80. return false;
  81. }
  82. public function search($search_term) {
  83. $query = "SELECT * FROM " . $this->table_name . " WHERE
  84. entry_number LIKE ? OR
  85. description LIKE ? OR
  86. reference_number LIKE ?
  87. ORDER BY entry_date DESC, created_at DESC";
  88. $stmt = $this->conn->prepare($query);
  89. $search_term = "%{$search_term}%";
  90. $stmt->bindParam(1, $search_term);
  91. $stmt->bindParam(2, $search_term);
  92. $stmt->bindParam(3, $search_term);
  93. $stmt->execute();
  94. return $stmt;
  95. }
  96. public function getTransactions($entry_id) {
  97. $query = "SELECT at.*, coa.account_name, coa.account_type FROM account_transactions at LEFT JOIN chart_of_accounts coa ON at.account_id = coa.id WHERE at.journal_entry_id = ? ORDER BY at.id";
  98. $stmt = $this->conn->prepare($query);
  99. $stmt->bindParam(1, $entry_id);
  100. $stmt->execute();
  101. return $stmt;
  102. }
  103. public function generateEntryNumber() {
  104. $query = "SELECT COUNT(*) as count FROM " . $this->table_name . " WHERE YEAR(entry_date) = YEAR(CURRENT_DATE)";
  105. $stmt = $this->conn->prepare($query);
  106. $stmt->execute();
  107. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  108. $count = $row['count'] + 1;
  109. return date('Y') . '-' . str_pad($count, 4, '0', STR_PAD_LEFT);
  110. }
  111. public function validateEntry() {
  112. $query = "SELECT SUM(debit_amount) as total_debit, SUM(credit_amount) as total_credit FROM account_transactions WHERE journal_entry_id = ?";
  113. $stmt = $this->conn->prepare($query);
  114. $stmt->bindParam(1, $this->id);
  115. $stmt->execute();
  116. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  117. $total_debit = $row['total_debit'];
  118. $total_credit = $row['total_credit'];
  119. return abs($total_debit - $total_credit) < 0.01; // Allow for small rounding differences
  120. }
  121. }
  122. ?>