Add department-specific product pricing and API routes

Introduce functionality to set, fetch, and apply department-specific product prices. Enhanced the API with routes for superusers to manage department pricing, including endpoints to set prices, fetch prices, and retrieve department details.
This commit is contained in:
Jepp9350
2025-01-10 09:34:33 +01:00
parent ef293b2509
commit 19b9a9616d
4 changed files with 231 additions and 3 deletions
+49
View File
@@ -77,4 +77,53 @@ class departments_o extends db
$result = $db->query($sql);
return $db->fetch_assoc($result);
}
/**
* Get the price of a product in a department
* @param int $department_id
* @param int $product_id
* @param int $price
* @return void
*/
public function setDepartmentProductPrice(int $department_id, int $product_id, int $price): void
{
global $db;
// Check if the record already exists
$this->removeDepartmentProductPriceIfExist($department_id, $product_id);
// If the price is 0, return
if ($price === 0) {
return;
}
// Create a new record in the database
$sql = "INSERT INTO product_department_prices (department_id, product_id, price) VALUES ($department_id, $product_id, $price)";
$db->query($sql);
}
/**
* Remove the price of a product in a department
* @param int $department_id
* @param int $product_id
* @return void
*/
private function removeDepartmentProductPriceIfExist(int $department_id, int $product_id): void
{
global $db;
// Get the price from the database
$sql = "SELECT * FROM product_department_prices WHERE department_id = $department_id AND product_id = $product_id";
$result = $db->query($sql);
if ($result->num_rows > 0) {
// Remove the record from the database
$sql = "DELETE FROM product_department_prices WHERE department_id = $department_id AND product_id = $product_id";
$db->query($sql);
}
}
public function getDepartmentProductPrices(int $department_id): array
{
global $db;
$sql = "SELECT * FROM product_department_prices WHERE department_id = $department_id";
$result = $db->query($sql);
return $db->fetch_all($result);
}
}
+23
View File
@@ -156,4 +156,27 @@ class products_o extends db
'apply_category_discount' => (bool)$this->apply_category_discount->value()
];
}
/**
* Apply department pricing to a list of products
* @param array $products
* @param int $department_id
* @return array
*/
public function applyDepartmentPricing(array $products, int $department_id): array
{
global $db;
$department_id = $db->escape_string($department_id);
$sql = "SELECT * FROM product_department_prices WHERE department_id = $department_id";
$result = $db->query($sql);
$prices = $db->fetch_all($result);
foreach ( $products as $key => $product ) {
foreach ( $prices as $price ) {
if ($product['id'] === $price['product_id']) {
$products[$key]['price'] = $price['price'];
}
}
}
return $products;
}
}
+16 -3
View File
@@ -27,14 +27,27 @@ class productsRoute
if (isset($data['category'])) {
// Log the incident
(new logs_o())->add('products', 'global', 1, $user->id, 'LIST_PRODUCTS', 'Successfully listed products in category ' . $data['category']);
// Return the list of departments
// Return the list of products
$products = (new products_o())->listObjectsByCategory($data['category']);
// Check if the department_id is set
if (isset($data['department_id'])) {
// Apply the departments unique pricing
$products = (new products_o())->applyDepartmentPricing((array)$products, (int)$data['department_id']);
}
$response->success(
(new products_o())->listObjectsByCategory($data['category'])
$products
);
}
// Log the incident
(new logs_o())->add('products', 'global', 1, $user->id, 'LIST_PRODUCTS', 'Successfully listed products');
// Return the list of departments
// Check if the department_id is set
if (isset($data['department_id'])) {
// Return the list of products
$response->success(
(new products_o())->applyDepartmentPricing((array)(new products_o())->listObjectsWithPaginationIfSet(), (int)$data['department_id'])
);
}
// Return the list of products
$response->success(
(new products_o())->listObjectsWithPaginationIfSet()
);
+143
View File
@@ -0,0 +1,143 @@
<?php
namespace routes;
use classes\authentication;
use objects\departments_o;
use objects\logs_o;
use objects\products_o;
use traits\route_t;
class superuserDepartmentRoute
{
use route_t;
public function run(): void
{
$this->get('/superuser/department', function () {
// Require the user to be logged in
global $response;
$this->requirePermission('superuser_fetch_department');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Check if the required fields are set
if ($this->fromRequest('department_id') === null) {
$response->error('Department ID is required', 400);
}
// Check if the department id is a valid number
if (!is_numeric($this->fromRequest('department_id'))) {
$response->error('Department ID must be a number', 400);
}
if (!(new departments_o())->getDepartmentById((int)$this->fromRequest('department_id'))) {
$response->error('Department not found', 404);
}
// Log the incident
(new logs_o())->add('departments', 'global', 1, $user->id, 'SUPERUSER_FETCH_DEPARTMENT', 'Successfully fetched department');
// Return the department
$response->success(
(new departments_o())->getDepartmentById((int)$this->fromRequest('department_id'))
);
} else {
// Log the incident
(new logs_o())->add('departments', 'global', 1, 0, 'SUPERUSER_FETCH_DEPARTMENT', 'No user found, or invalid session');
// Return an error
$response->error('Invalid session', 400);
}
});
$this->post('/superuser/department/prices', function () {
// Require the user to be logged in
global $response;
$this->requirePermission('superuser_set_department_prices');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Check if the required fields are set
$data = json_decode(file_get_contents('php://input'), true);
if (!isset($data['department_id'])) {
$response->error('Department ID is required', 400);
}
if (!isset($data['price'])) {
$response->error('Price is required', 400);
}
if (!isset($data['product_id'])) {
$response->error('Product ID is required', 400);
}
// Check if the department id is a valid number
if (!is_numeric($data['department_id'])) {
$response->error('Department ID must be a number', 400);
}
// Check if the price is a valid number
if (!is_numeric($data['price'])) {
// Check if the value is an empty string
if ($data['price'] === '') {
$data['price'] = 0;
} else {
$response->error('Price must be a number', 400);
}
}
// Check if the product id is a valid number
if (!is_numeric($data['product_id'])) {
$response->error('Product ID must be a number', 400);
}
// Check if the product exists
if (!(new products_o())->getProductById((int)$data['product_id'])->exists()) {
$response->error('Product not found', 404);
}
// Check if the department exists
if (!(new departments_o())->getDepartmentById((int)$data['department_id'])) {
$response->error('Department not found', 404);
}
// Log the incident
(new logs_o())->add('departments', 'global', 1, $user->id, 'SUPERUSER_SET_DEPARTMENT_PRICES', 'Successfully set department prices');
// Set the department prices
(new departments_o())->setDepartmentProductPrice((int)$data['department_id'], (int)$data['product_id'], (int)$data['price']);
// Return the department
$response->success(
(new departments_o())->getDepartmentById((int)$data['department_id'])
);
} else {
// Log the incident
(new logs_o())->add('departments', 'global', 1, 0, 'SUPERUSER_SET_DEPARTMENT_PRICES', 'No user found, or invalid session');
// Return an error
$response->error('Invalid session', 400);
}
});
$this->get('/superuser/department/prices', function () {
// Require the user to be logged in
global $response;
$this->requirePermission('superuser_fetch_department_prices');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Check if the required fields are set
if ($this->fromRequest('department_id') === null) {
$response->error('Department ID is required', 400);
}
// Check if the department id is a valid number
if (!is_numeric($this->fromRequest('department_id'))) {
$response->error('Department ID must be a number', 400);
}
if (!(new departments_o())->getDepartmentById((int)$this->fromRequest('department_id'))) {
$response->error('Department not found', 404);
}
// Log the incident
(new logs_o())->add('departments', 'global', 1, $user->id, 'SUPERUSER_FETCH_DEPARTMENT_PRICES', 'Successfully fetched department prices');
// Return the department
$response->success(
(new departments_o())->getDepartmentProductPrices((int)$this->fromRequest('department_id'))
);
} else {
// Log the incident
(new logs_o())->add('departments', 'global', 1, 0, 'SUPERUSER_FETCH_DEPARTMENT_PRICES', 'No user found, or invalid session');
// Return an error
$response->error('Invalid session', 400);
}
});
}
}