chart_of_accounts.php 6.6 KB

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