chart_of_accounts.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 '../config/database.php';
  10. require_once '../models/ChartOfAccounts.php';
  11. $database = new Database();
  12. $db = $database->getConnection();
  13. $account = new ChartOfAccounts($db);
  14. $request_method = $_SERVER['REQUEST_METHOD'];
  15. switch($request_method) {
  16. case 'GET':
  17. if(isset($_GET['id'])) {
  18. $account->id = $_GET['id'];
  19. $account->readOne();
  20. if($account->account_number != null) {
  21. $account_arr = array(
  22. "id" => $account->id,
  23. "account_number" => $account->account_number,
  24. "account_name" => $account->account_name,
  25. "account_type" => $account->account_type,
  26. "parent_id" => $account->parent_id,
  27. "description" => $account->description,
  28. "opening_balance" => $account->opening_balance,
  29. "current_balance" => $account->current_balance,
  30. "is_active" => $account->is_active,
  31. "created_at" => $account->created_at,
  32. "updated_at" => $account->updated_at,
  33. "account_type_name" => $account->getAccountTypeName()
  34. );
  35. http_response_code(200);
  36. echo json_encode($account_arr);
  37. } else {
  38. http_response_code(404);
  39. echo json_encode(array("message" => "Account not found."));
  40. }
  41. } else {
  42. $stmt = $account->read();
  43. $num = $stmt->rowCount();
  44. if($num > 0) {
  45. $accounts_arr = array();
  46. $accounts_arr["records"] = array();
  47. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  48. extract($row);
  49. $account_item = array(
  50. "id" => $id,
  51. "account_number" => $account_number,
  52. "account_name" => $account_name,
  53. "account_type" => $account_type,
  54. "parent_id" => $parent_id,
  55. "description" => $description,
  56. "opening_balance" => $opening_balance,
  57. "current_balance" => $current_balance,
  58. "is_active" => $is_active,
  59. "created_at" => $created_at,
  60. "updated_at" => $updated_at,
  61. "account_type_name" => $account->getAccountTypeName()
  62. );
  63. array_push($accounts_arr["records"], $account_item);
  64. }
  65. http_response_code(200);
  66. echo json_encode($accounts_arr);
  67. } else {
  68. http_response_code(200);
  69. echo json_encode(array("records" => array()));
  70. }
  71. }
  72. break;
  73. case 'POST':
  74. $data = json_decode(file_get_contents("php://input"));
  75. if(!empty($data->account_name)) {
  76. $account->account_number = $data->account_number ?? '';
  77. $account->account_name = $data->account_name;
  78. $account->account_type = $data->account_type ?? 'asset';
  79. $account->parent_id = $data->parent_id ?? null;
  80. $account->description = $data->description ?? '';
  81. $account->opening_balance = $data->opening_balance ?? 0;
  82. $account->current_balance = $data->current_balance ?? 0;
  83. $account->is_active = $data->is_active ?? true;
  84. if($account->create()) {
  85. http_response_code(201);
  86. echo json_encode(array("message" => "Account was created."));
  87. } else {
  88. http_response_code(503);
  89. echo json_encode(array("message" => "Unable to create account."));
  90. }
  91. } else {
  92. http_response_code(400);
  93. echo json_encode(array("message" => "Unable to create account. Account name is required."));
  94. }
  95. break;
  96. case 'PUT':
  97. $data = json_decode(file_get_contents("php://input"));
  98. if(!empty($data->id) && !empty($data->account_name)) {
  99. $account->id = $data->id;
  100. $account->account_number = $data->account_number ?? '';
  101. $account->account_name = $data->account_name;
  102. $account->account_type = $data->account_type ?? 'asset';
  103. $account->parent_id = $data->parent_id ?? null;
  104. $account->description = $data->description ?? '';
  105. $account->opening_balance = $data->opening_balance ?? 0;
  106. $account->current_balance = $data->current_balance ?? 0;
  107. $account->is_active = $data->is_active ?? true;
  108. if($account->update()) {
  109. http_response_code(200);
  110. echo json_encode(array("message" => "Account was updated."));
  111. } else {
  112. http_response_code(503);
  113. echo json_encode(array("message" => "Unable to update account."));
  114. }
  115. } else {
  116. http_response_code(400);
  117. echo json_encode(array("message" => "Unable to update account. Data is incomplete."));
  118. }
  119. break;
  120. case 'DELETE':
  121. if(isset($_GET['id'])) {
  122. $account->id = $_GET['id'];
  123. if($account->delete()) {
  124. http_response_code(200);
  125. echo json_encode(array("message" => "Account was deleted."));
  126. } else {
  127. http_response_code(503);
  128. echo json_encode(array("message" => "Unable to delete account."));
  129. }
  130. } else {
  131. http_response_code(400);
  132. echo json_encode(array("message" => "Unable to delete account. ID is missing."));
  133. }
  134. break;
  135. default:
  136. http_response_code(405);
  137. echo json_encode(array("message" => "Method not allowed."));
  138. break;
  139. }
  140. ?>