invoices.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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/Invoice.php';
  11. require_once __DIR__ . '/../models/InvoiceItem.php';
  12. $database = new Database();
  13. $db = $database->getConnection();
  14. $invoice = new Invoice($db);
  15. $invoiceItem = new InvoiceItem($db);
  16. $request_method = $_SERVER['REQUEST_METHOD'];
  17. switch($request_method) {
  18. case 'GET':
  19. if(isset($_GET['id'])) {
  20. $invoice->id = $_GET['id'];
  21. $invoice->readOne();
  22. if($invoice->invoice_number != null) {
  23. $invoice_arr = array(
  24. "id" => $invoice->id,
  25. "client_id" => $invoice->client_id,
  26. "invoice_number" => $invoice->invoice_number,
  27. "issue_date" => $invoice->issue_date,
  28. "due_date" => $invoice->due_date,
  29. "status" => $invoice->status,
  30. "subtotal" => $invoice->subtotal,
  31. "tax_amount" => $invoice->tax_amount,
  32. "total_amount" => $invoice->total_amount,
  33. "notes" => $invoice->notes,
  34. "created_at" => $invoice->created_at,
  35. "updated_at" => $invoice->updated_at,
  36. "client_name" => $invoice->getClientName()
  37. );
  38. $invoice_arr['items'] = $invoice->getInvoiceItems($invoice->id);
  39. $invoice_arr['payments'] = $invoice->getPayments($invoice->id);
  40. http_response_code(200);
  41. echo json_encode($invoice_arr);
  42. } else {
  43. http_response_code(404);
  44. echo json_encode(array("message" => "Invoice not found."));
  45. }
  46. } elseif(isset($_GET['client_id'])) {
  47. $invoice->client_id = $_GET['client_id'];
  48. $stmt = $invoice->read();
  49. $num = $stmt->rowCount();
  50. if($num > 0) {
  51. $invoices_arr = array();
  52. $invoices_arr["records"] = array();
  53. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  54. extract($row);
  55. $invoice_item = array(
  56. "id" => $id,
  57. "client_id" => $client_id,
  58. "invoice_number" => $invoice_number,
  59. "issue_date" => $issue_date,
  60. "due_date" => $due_date,
  61. "status" => $status,
  62. "subtotal" => $subtotal,
  63. "tax_amount" => $tax_amount,
  64. "total_amount" => $total_amount,
  65. "notes" => $notes,
  66. "created_at" => $created_at,
  67. "updated_at" => $updated_at,
  68. "client_name" => $client_name
  69. );
  70. array_push($invoices_arr["records"], $invoice_item);
  71. }
  72. http_response_code(200);
  73. echo json_encode($invoices_arr);
  74. } else {
  75. http_response_code(200);
  76. echo json_encode(array("records" => array()));
  77. }
  78. } else {
  79. $stmt = $invoice->read();
  80. $num = $stmt->rowCount();
  81. if($num > 0) {
  82. $invoices_arr = array();
  83. $invoices_arr["records"] = array();
  84. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  85. extract($row);
  86. $invoice_item = array(
  87. "id" => $id,
  88. "client_id" => $client_id,
  89. "invoice_number" => $invoice_number,
  90. "issue_date" => $issue_date,
  91. "due_date" => $due_date,
  92. "status" => $status,
  93. "subtotal" => $subtotal,
  94. "tax_amount" => $tax_amount,
  95. "total_amount" => $total_amount,
  96. "notes" => $notes,
  97. "created_at" => $created_at,
  98. "updated_at" => $updated_at,
  99. "client_name" => $client_name
  100. );
  101. array_push($invoices_arr["records"], $invoice_item);
  102. }
  103. http_response_code(200);
  104. echo json_encode($invoices_arr);
  105. } else {
  106. http_response_code(200);
  107. echo json_encode(array("records" => array()));
  108. }
  109. }
  110. break;
  111. case 'POST':
  112. $data = json_decode(file_get_contents("php://input"));
  113. if(!empty($data->client_id) && !empty($data->invoice_number) && !empty($data->issue_date) && !empty($data->due_date)) {
  114. $invoice->client_id = $data->client_id;
  115. $invoice->invoice_number = $data->invoice_number;
  116. $invoice->issue_date = $data->issue_date;
  117. $invoice->due_date = $data->due_date;
  118. $invoice->status = $data->status ?? 'draft';
  119. $invoice->subtotal = $data->subtotal ?? 0;
  120. $invoice->tax_amount = $data->tax_amount ?? 0;
  121. $invoice->total_amount = $data->total_amount ?? 0;
  122. $invoice->notes = $data->notes ?? '';
  123. if($invoice->create()) {
  124. http_response_code(201);
  125. echo json_encode(array("message" => "Invoice was created."));
  126. } else {
  127. http_response_code(503);
  128. echo json_encode(array("message" => "Unable to create invoice."));
  129. }
  130. } else {
  131. http_response_code(400);
  132. echo json_encode(array("message" => "Unable to create invoice. Data is incomplete."));
  133. }
  134. break;
  135. case 'PUT':
  136. $data = json_decode(file_get_contents("php://input"));
  137. if(!empty($data->id) && !empty($data->client_id) && !empty($data->invoice_number) && !empty($data->issue_date) && !empty($data->due_date)) {
  138. $invoice->id = $data->id;
  139. $invoice->client_id = $data->client_id;
  140. $invoice->invoice_number = $data->invoice_number;
  141. $invoice->issue_date = $data->issue_date;
  142. $invoice->due_date = $data->due_date;
  143. $invoice->status = $data->status;
  144. $invoice->subtotal = $data->subtotal ?? 0;
  145. $invoice->tax_amount = $data->tax_amount ?? 0;
  146. $invoice->total_amount = $data->total_amount ?? 0;
  147. $invoice->notes = $data->notes ?? '';
  148. if($invoice->update()) {
  149. http_response_code(200);
  150. echo json_encode(array("message" => "Invoice was updated."));
  151. } else {
  152. http_response_code(503);
  153. echo json_encode(array("message" => "Unable to update invoice."));
  154. }
  155. } else {
  156. http_response_code(400);
  157. echo json_encode(array("message" => "Unable to update invoice. Data is incomplete."));
  158. }
  159. break;
  160. case 'DELETE':
  161. if(isset($_GET['id'])) {
  162. $invoice->id = $_GET['id'];
  163. if($invoice->delete()) {
  164. http_response_code(200);
  165. echo json_encode(array("message" => "Invoice was deleted."));
  166. } else {
  167. http_response_code(503);
  168. echo json_encode(array("message" => "Unable to delete invoice."));
  169. }
  170. } else {
  171. http_response_code(400);
  172. echo json_encode(array("message" => "Unable to delete invoice. ID is missing."));
  173. }
  174. break;
  175. default:
  176. http_response_code(405);
  177. echo json_encode(array("message" => "Method not allowed."));
  178. break;
  179. }
  180. ?>