From e66adc30f2cd8d08aa2b77a513d6a360f340d0c8 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 9 Nov 2023 23:54:05 +0800 Subject: [PATCH] Add report endpoints --- CarController.php | 6 ------ ExpenseController.php | 39 ++++++++++++++++++++++++++++++++++++ SalesController.php | 46 +++++++++++++++++++++++++++++++++++++++++++ index.php | 11 +++++++++-- 4 files changed, 94 insertions(+), 8 deletions(-) diff --git a/CarController.php b/CarController.php index 0f1a082..fecebf2 100644 --- a/CarController.php +++ b/CarController.php @@ -2,12 +2,6 @@ class CarController { - - public function test() { - http_response_code(400); - echo json_encode(['Welcome to my API']); - } - public function index() { global $pdo; diff --git a/ExpenseController.php b/ExpenseController.php index d94ed59..873af52 100644 --- a/ExpenseController.php +++ b/ExpenseController.php @@ -94,6 +94,45 @@ public function destroy($params) { echo json_encode(['message' => "Successfully deleted."]); } + + public function monthlyReport() { + global $pdo; + + $data = json_decode(file_get_contents("php://input"), true); + + if (!isset($data['month'])) { + http_response_code(400); + echo json_encode(['error' => 'Invalid month']); + return; + } + + $month = $data['month']; + + // Validate that $id is a positive integer + if (!ctype_digit((string)$month) || $month <= 0) { + http_response_code(400); + echo json_encode(['error' => 'Invalid request']); + return; + } + + $stmt = $pdo->prepare("SELECT `week`, SUM(`amount`) `amount` FROM `expenses_tbl` WHERE `month` = ? GROUP BY `week`"); + $stmt->execute([$month]); + + $report = $stmt->fetchAll(PDO::FETCH_ASSOC); + + http_response_code(200); + + if (!$report) { + echo json_encode(['error' => 'No report to generate.']); + return; + } + + echo json_encode([ + "message" => "Expenses report successfully generated!", + "count" => count($report), + "data" => $report + ]); + } } ?> \ No newline at end of file diff --git a/SalesController.php b/SalesController.php index 511e6e4..7801db5 100644 --- a/SalesController.php +++ b/SalesController.php @@ -215,4 +215,50 @@ public function test($params) { "amount" => $total_amount ]); } + + public function salesReportPerItem($params) { + global $pdo; + + if (!isset($params['product_id'])) { + http_response_code(400); + echo json_encode(['error' => 'Invalid request']); + return; + } + + $product_id = $params['product_id']; + + // Validate that $id is a positive integer + if (!ctype_digit((string)$product_id) || $product_id <= 0) { + http_response_code(400); + echo json_encode(['error' => 'Invalid request']); + return; + } + + $stmt = $pdo->prepare("SELECT * FROM `products_tbl` WHERE `id` = ?"); + $stmt->execute([$product_id]); + + $product = $stmt->fetch(PDO::FETCH_ASSOC); + + if (!$product) { + http_response_code(404); + echo json_encode(['error' => 'Product not found']); + return; + } + + $stmt = $pdo->prepare("SELECT `p`.`id` `product_id`, `p`.`name` `product_name`, `p`.`category` `product_category`, COUNT(`s`.`id`) `sales_count`, SUM(`s`.`qty`) `sales_qty`, SUM(`total_amount`) `sales_amount` FROM `sales_tbl` `s` LEFT JOIN `products_tbl` `p` ON `p`.`id` = `s`.`product_id` WHERE `product_id` = ? GROUP BY `product_id`"); + $stmt->execute([$product_id]); + + $sales = $stmt->fetch(PDO::FETCH_ASSOC); + + if (!$sales) { + http_response_code(404); + echo json_encode(['error' => 'No sales found']); + return; + } + + echo json_encode([ + "message" => "Sales report successfully generated!", + "data" => $sales + ]); + } } \ No newline at end of file diff --git a/index.php b/index.php index b1c5e78..ea353ba 100644 --- a/index.php +++ b/index.php @@ -10,7 +10,11 @@ $router = new Router(); - $router->get('/', 'CarController@test'); + $router->get('/', function() { + http_response_code(200); + echo json_encode(['Welcome to my ITEC116 API']); + }); + $router->get('/cars', 'CarController@index'); $router->post('/cars', 'CarController@store'); $router->get('/cars/{id}', 'CarController@show'); @@ -24,7 +28,7 @@ $router->get('/products', 'ProductController@index'); $router->post('/products', 'ProductController@store'); - //$router->put('/products/{id}', 'ProductController@update'); + $router->put('/products/{id}', 'ProductController@update'); $router->get('/products/{id}', 'ProductController@show'); $router->get('/sales', 'SalesController@index'); @@ -32,5 +36,8 @@ $router->post('/sales', 'SalesController@withdraw'); $router->get('/sales/test/{id}', 'SalesController@test'); + $router->get('/expenses/reports/monthly', 'ExpenseController@monthlyReport'); + $router->get('/sales/reports/{product_id}', 'SalesController@salesReportPerItem'); + $router->handleRequest(); ?> \ No newline at end of file