Add customer fixed pricing routes and object

Introduce routes for managing customer fixed pricing, including retrieval, creation, and deletion. Create `customer_fixed_pricing_o` object to handle database operations for fixed pricing data. This change enables structured handling of fixed pricing-related logic and functionality.
This commit is contained in:
Jepp9350
2025-05-05 13:36:04 +02:00
parent 8d2fe8d75d
commit 91d215ffcc
2 changed files with 270 additions and 0 deletions
@@ -0,0 +1,149 @@
<?php
namespace routes;
use classes\authentication;
use objects\customer_fixed_pricing_o;
use objects\logs_o;
use objects\users_o;
use traits\route_t;
class customerFixedPricingRoute
{
use route_t;
public function run(): void
{
$this->get('/customer/pricing/fixed', function () {
// Require the user to be logged in
global $response;
self::requirePermission('get_customer_fixed_pricing');
$user = (new authentication())->get_user();
if (!$user) {
(new logs_o())->add('customer_fixed_pricing', 'global', 0, $user->id, 'CUSTOMER_GET_FIXED_PRICING', 'User not logged in');
$response->error('Invalid session', 400);
}
// Require the customer number parameter
self::requireParameters(['customer_number']);
// Check if the customer number is valid
$customer_number = (int)self::getParameter('customer_number');
self::requireMinLength('customer_number', 1);
self::requireMaxLength('customer_number', 255);
self::requireType($customer_number, self::type_int());
self::requireMinValue($customer_number, 1);
// Check if the customer number exists
$customer = (new users_o())->getUserByCustomerNumber((int)$customer_number);
if (!$customer->exists()) {
(new logs_o())->add('customer_fixed_pricing', 'global', 0, $user->id, 'CUSTOMER_GET_FIXED_PRICING', 'Customer not found');
$response->error('Customer not found', 400);
}
// Get the fixed price
$customer_fixed_pricing_o = new customer_fixed_pricing_o();
$fixed_pricing_object_id = $customer_fixed_pricing_o->getIdByCustomerNumber((int)$customer_number);
if (empty($fixed_pricing_object_id)) {
// Return "Not found"
$response->error('This customer does not have a fixed price', 400);
}
// Select the object
$customer_fixed_pricing_o->select((int)$fixed_pricing_object_id);
// Log the action
(new logs_o())->add('customer_fixed_pricing', 'global', 0, $user->id, 'CUSTOMER_GET_FIXED_PRICING', 'Fixed price retrieved');
// Return success
$response->success($customer_fixed_pricing_o->asArray());
},
[
'get_customer_fixed_pricing' => 'Get a fixed price for a customer',
]
);
$this->post('/customer/pricing/fixed', function () {
// Require the user to be logged in
global $response;
self::requirePermission('add_customer_fixed_pricing');
$user = (new authentication())->get_user();
if (!$user) {
(new logs_o())->add('customer_fixed_pricing', 'global', 0, $user->id, 'CUSTOMER_ADD_FIXED_PRICING', 'User not logged in');
$response->error('Invalid session', 400);
}
// Require the customer number, price and description parameters
self::requireParameters(['customer_number', 'price', 'description']);
// Check if the customer number is valid
$customer_number = (int)self::getParameter('customer_number');
self::requireMinLength('customer_number', 1);
self::requireMaxLength('customer_number', 255);
self::requireType($customer_number, self::type_int());
self::requireMinValue($customer_number, 1);
// Check if the price is valid
$price = (int)self::getParameter('price');
self::requireMinLength('price', 1);
self::requireMaxLength('price', 255);
self::requireType($price, self::type_int());
self::requireMinValue($price, 1);
// Check if the description is valid
$description = (string)self::getParameter('description');
self::requireMinLength('description', 1);
self::requireMaxLength('description', 255);
self::requireType($description, self::type_string());
// Check if the customer number exists
$customer = (new users_o())->getUserByCustomerNumber((int)$customer_number);
if (!$customer->exists()) {
(new logs_o())->add('customer_fixed_pricing', 'global', 0, $user->id, 'CUSTOMER_ADD_FIXED_PRICING', 'Customer not found');
$response->error('Customer not found', 400);
}
// Check if the customer already has a fixed price
$customer_fixed_pricing_o = new customer_fixed_pricing_o();
$fixed_pricing_object_id = $customer_fixed_pricing_o->getIdByCustomerNumber((int)$customer_number);
if (!empty($fixed_pricing_object_id)) {
// Return "Already exists"
$response->error('This customer already has a fixed price', 400);
}
// Add the fixed price
$customer_fixed_pricing_o->add((int)$customer_number, (int)$price, (string)$description);
// Log the action
(new logs_o())->add('customer_fixed_pricing', 'global', 0, $user->id, 'CUSTOMER_ADD_FIXED_PRICING', 'Fixed price added');
// Return success
$response->success($customer_fixed_pricing_o->asArray());
},
[
'add_customer_fixed_pricing' => 'Add a fixed price for a customer (This is a global action, should only be permitted superusers)',
]
);
$this->delete('/customer/pricing/fixed', function () {
// Require the user to be logged in
global $response;
self::requirePermission('delete_customer_fixed_pricing');
$user = (new authentication())->get_user();
if (!$user) {
(new logs_o())->add('customer_fixed_pricing', 'global', 0, $user->id, 'CUSTOMER_DELETE_FIXED_PRICING', 'User not logged in');
$response->error('Invalid session', 400);
}
// Require the customer number parameter
self::requireParameters(['customer_number']);
// Check if the customer number is valid
$customer_number = (int)self::getParameter('customer_number');
self::requireMinLength('customer_number', 1);
self::requireMaxLength('customer_number', 255);
self::requireType($customer_number, self::type_int());
self::requireMinValue($customer_number, 1);
// Check if the customer number exists
$customer = (new users_o())->getUserByCustomerNumber((int)$customer_number);
if (!$customer->exists()) {
(new logs_o())->add('customer_fixed_pricing', 'global', 0, $user->id, 'CUSTOMER_DELETE_FIXED_PRICING', 'Customer not found');
$response->error('Customer not found', 400);
}
// Check if the customer already has a fixed price
$customer_fixed_pricing_o = new customer_fixed_pricing_o();
$fixed_pricing_object = $customer_fixed_pricing_o->selectByCustomerNumber((int)$customer_number);
// Delete the fixed price
$fixed_pricing_object->delete();
// Log the action
(new logs_o())->add('customer_fixed_pricing', 'global', 0, $user->id, 'CUSTOMER_DELETE_FIXED_PRICING', 'Fixed price deleted');
// Return success
$response->success('Fixed price deleted');
},
[
'delete_customer_fixed_pricing' => 'Delete a fixed price for a customer (This is a global action, should only be permitted superusers)',
]
);
}
}