Skip to content

Commit

Permalink
Merge pull request #6 from jayveeinfinity/dev/AddReportEndpoints
Browse files Browse the repository at this point in the history
Add report endpoints
  • Loading branch information
jayveeinfinity authored Nov 9, 2023
2 parents 52aa062 + e66adc3 commit 767e005
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 8 deletions.
6 changes: 0 additions & 6 deletions CarController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
39 changes: 39 additions & 0 deletions ExpenseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
]);
}
}

?>
46 changes: 46 additions & 0 deletions SalesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
]);
}
}
11 changes: 9 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -24,13 +28,16 @@

$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');
$router->post('/sales/{id}', 'SalesController@store');
$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();
?>

0 comments on commit 767e005

Please sign in to comment.