accounting_categories.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. header("Access-Control-Allow-Origin: *");
  3. header("Content-Type: application/json; charset=UTF-8");
  4. header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
  5. header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  6. if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  7. exit(0);
  8. }
  9. require_once __DIR__ . '/../config/database.php';
  10. $database = new Database();
  11. $db = $database->getConnection();
  12. $request_method = $_SERVER['REQUEST_METHOD'];
  13. switch($request_method) {
  14. case 'GET':
  15. if(isset($_GET['type'])) {
  16. // Get categories by type (Tulo/Kulu)
  17. $type = $_GET['type'];
  18. $query = "SELECT category_code, category_name, category_type, vat_percentage
  19. FROM accounting_categories
  20. WHERE category_type = ? AND is_active = 1
  21. ORDER BY category_code";
  22. $stmt = $db->prepare($query);
  23. $stmt->bindParam(1, $type);
  24. $stmt->execute();
  25. $categories = [];
  26. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  27. $categories[] = [
  28. 'code' => $row['category_code'],
  29. 'name' => $row['category_name'],
  30. 'type' => $row['category_type'],
  31. 'vat_percentage' => $row['vat_percentage']
  32. ];
  33. }
  34. http_response_code(200);
  35. echo json_encode($categories);
  36. } else {
  37. // Get all categories
  38. $query = "SELECT category_code, category_name, category_type, vat_percentage
  39. FROM accounting_categories
  40. WHERE is_active = 1
  41. ORDER BY category_type, category_code";
  42. $stmt = $db->prepare($query);
  43. $stmt->execute();
  44. $categories = [];
  45. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  46. $categories[] = [
  47. 'code' => $row['category_code'],
  48. 'name' => $row['category_name'],
  49. 'type' => $row['category_type'],
  50. 'vat_percentage' => $row['vat_percentage']
  51. ];
  52. }
  53. http_response_code(200);
  54. echo json_encode($categories);
  55. }
  56. break;
  57. case 'POST':
  58. $data = json_decode(file_get_contents("php://input"));
  59. if(!empty($data->category_code) && !empty($data->category_name) && !empty($data->category_type)) {
  60. $query = "INSERT INTO accounting_categories
  61. SET category_code=:category_code,
  62. category_name=:category_name,
  63. category_type=:category_type,
  64. vat_percentage=:vat_percentage,
  65. is_active=:is_active";
  66. $stmt = $db->prepare($query);
  67. $stmt->bindParam(":category_code", $data->category_code);
  68. $stmt->bindParam(":category_name", $data->category_name);
  69. $stmt->bindParam(":category_type", $data->category_type);
  70. $stmt->bindParam(":vat_percentage", $data->vat_percentage);
  71. $stmt->bindParam(":is_active", $data->is_active ?? 1);
  72. if($stmt->execute()) {
  73. http_response_code(201);
  74. echo json_encode(array("message" => "Category was created."));
  75. } else {
  76. http_response_code(503);
  77. echo json_encode(array("message" => "Unable to create category."));
  78. }
  79. } else {
  80. http_response_code(400);
  81. echo json_encode(array("message" => "Unable to create category. Data is incomplete."));
  82. }
  83. break;
  84. case 'PUT':
  85. $data = json_decode(file_get_contents("php://input"));
  86. if(!empty($data->category_code) && !empty($data->category_name) && !empty($data->category_type)) {
  87. $query = "UPDATE accounting_categories
  88. SET category_name=:category_name,
  89. category_type=:category_type,
  90. vat_percentage=:vat_percentage,
  91. is_active=:is_active
  92. WHERE category_code=:category_code";
  93. $stmt = $db->prepare($query);
  94. $stmt->bindParam(":category_code", $data->category_code);
  95. $stmt->bindParam(":category_name", $data->category_name);
  96. $stmt->bindParam(":category_type", $data->category_type);
  97. $stmt->bindParam(":vat_percentage", $data->vat_percentage);
  98. $stmt->bindParam(":is_active", $data->is_active ?? 1);
  99. if($stmt->execute()) {
  100. http_response_code(200);
  101. echo json_encode(array("message" => "Category was updated."));
  102. } else {
  103. http_response_code(503);
  104. echo json_encode(array("message" => "Unable to update category."));
  105. }
  106. } else {
  107. http_response_code(400);
  108. echo json_encode(array("message" => "Unable to update category. Data is incomplete."));
  109. }
  110. break;
  111. case 'DELETE':
  112. if(isset($_GET['code'])) {
  113. $code = $_GET['code'];
  114. $query = "DELETE FROM accounting_categories WHERE category_code = ?";
  115. $stmt = $db->prepare($query);
  116. $stmt->bindParam(1, $code);
  117. if($stmt->execute()) {
  118. http_response_code(200);
  119. echo json_encode(array("message" => "Category was deleted."));
  120. } else {
  121. http_response_code(503);
  122. echo json_encode(array("message" => "Unable to delete category."));
  123. }
  124. } else {
  125. http_response_code(400);
  126. echo json_encode(array("message" => "Unable to delete category. Category code is missing."));
  127. }
  128. break;
  129. default:
  130. http_response_code(405);
  131. echo json_encode(array("message" => "Method not allowed."));
  132. break;
  133. }
  134. ?>