Introduced endpoints and routes for managing draft invoices, enabling retrieval and updates. Extended object handling to include filters for deleted entries and added custom parsing for pagination results. Refactored code for improved structure and consistency.
211 lines
9.1 KiB
PHP
211 lines
9.1 KiB
PHP
<?php
|
|
|
|
namespace routes;
|
|
|
|
use classes\authentication;
|
|
use classes\response;
|
|
use objects\departments_o;
|
|
use objects\economic_module_orders;
|
|
use objects\logs_o;
|
|
use objects\orders_o;
|
|
use objects\users_o;
|
|
use traits\route_t;
|
|
|
|
class ordersRoute
|
|
{
|
|
use route_t;
|
|
|
|
public function run(): void
|
|
{
|
|
$this->get('/orders', function () {
|
|
// Require the user to be logged in
|
|
global $response;
|
|
$this->requirePermission('list_orders');
|
|
// Get the user object
|
|
$user = (new authentication())->get_user();
|
|
// Check if the request was successful
|
|
if ($user) {
|
|
// Log the incident
|
|
(new logs_o())->add('orders', 'global', 1, $user->id, 'LIST_ORDERS', 'Successfully listed orders');
|
|
// Create economic_module_orders object
|
|
$economic_module_orders = new economic_module_orders();
|
|
// Return the list of departments
|
|
$response->success(
|
|
(new orders_o())->listObjectsWithPaginationIfSet(
|
|
function ($order) {
|
|
// Add the invoice status to the order
|
|
$order['economic_invoice_module'] = (new economic_module_orders())->getByOrderId($order['id'])->asArray();
|
|
/** @var array $order */
|
|
return $order;
|
|
}
|
|
)
|
|
);
|
|
} else {
|
|
// Log the incident
|
|
(new logs_o())->add('orders', 'global', 1, 0, 'LIST_ORDERS', 'No user found, or invalid session');
|
|
// Return an error
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
});
|
|
|
|
$this->post('/orders', function () {
|
|
// Require the user to be logged in
|
|
global $response;
|
|
$this->requirePermission('add_order');
|
|
// Get the user object
|
|
$user = (new authentication())->get_user();
|
|
// Check if the request was successful
|
|
if ($user) {
|
|
// Get the post data
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
// Check if the required fields are set
|
|
$data = $this->getData($data, $response);
|
|
// Validate the department
|
|
if (!(new departments_o())->getDepartmentById((int)$data['department_id'])) {
|
|
$response->error('Department not found', 400);
|
|
}
|
|
// Make sure the customer number set is valid
|
|
$targetUser = (new users_o())->getCustomerByIdOrCustomerNumber((int)$data['customer_id']);
|
|
if (!$targetUser->exists()) {
|
|
$response->error('Customer not found', 400);
|
|
}
|
|
|
|
// Check if the user requires a reference
|
|
if ($targetUser->requiresReference() && empty($data['reference'])) {
|
|
$response->error('Reference is required by the customer', 400);
|
|
}
|
|
// Get the registration number
|
|
$reg_1 = $data['reg_1'];
|
|
// Get the registration numbers (If they are set, they 2-3 are optional)
|
|
$reg_2 = $data['reg_2'] ?? '';
|
|
$reg_3 = $data['reg_3'] ?? '';
|
|
// Create the order
|
|
$order = (new orders_o())->add((int)$data['customer_id'], $user->id, $data['reference'], $data['notes'], (int)$data['department_id'], (string)$reg_1, (string)$reg_2, (string)$reg_3);
|
|
// Log the incident
|
|
(new logs_o())->add('orders', $data['department_id'], 1, $user->id, 'ADD_ORDER', 'Successfully added an order (ID: ' . $data['department_id'] . ')');
|
|
// Return a success message, containing the orders array
|
|
$response->success($order->asArray());
|
|
} else {
|
|
// Log the incident
|
|
(new logs_o())->add('orders', 'global', 1, 0, 'ADD_ORDER', 'No user found, or invalid session');
|
|
// Return an error
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
});
|
|
|
|
$this->put('/orders', function () {
|
|
// Require the user to be logged in
|
|
global $response;
|
|
$this->requirePermission('edit_order');
|
|
// Get the user object
|
|
$user = (new authentication())->get_user();
|
|
// Check if the request was successful
|
|
if ($user) {
|
|
// Get the post data
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
// Check if the required fields are set
|
|
if (!isset($data['id'])) {
|
|
$response->error('ID is required', 400);
|
|
}
|
|
// Get the current order
|
|
$order = (new orders_o())->getOrderById((int)$data['id']);
|
|
// Check if the order exists
|
|
if (!$order->exists()) {
|
|
$response->error('Order not found', 400);
|
|
}
|
|
// If the customer ID is set, validate it
|
|
if (isset($data['customer_id'])) {
|
|
if (!(new users_o())->getCustomerByIdOrCustomerNumber((int)$data['customer_id'])->exists() || empty($data['customer_id'])) {
|
|
$response->error('Customer not found or invalid', 400);
|
|
}
|
|
$order->customer_id->set((int)$data['customer_id']);
|
|
}
|
|
// If the reference is set, validate it
|
|
if (isset($data['reference'])) {
|
|
$order->reference->set($data['reference']);
|
|
}
|
|
// If the notes are set, validate them
|
|
if (isset($data['notes'])) {
|
|
$order->notes->set($data['notes']);
|
|
}
|
|
// If the registration number is set, validate it
|
|
if (isset($data['reg_1'])) {
|
|
$order->reg_1->set($data['reg_1']);
|
|
}
|
|
// If the department ID is set, validate it
|
|
// Log the incident
|
|
(new logs_o())->add('orders', $order->department_id->value(), 1, $user->id, 'EDIT_ORDER', 'Successfully updated an order (ID: ' . $data['id'] . ')');
|
|
// Return a success message
|
|
$response->success(['message' => 'Order updated successfully']);
|
|
} else {
|
|
// Log the incident
|
|
(new logs_o())->add('orders', 'global', 1, 0, 'EDIT_ORDER', 'No user found, or invalid session');
|
|
// Return an error
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
});
|
|
|
|
$this->delete('/orders', function () {
|
|
// Require the user to be logged in
|
|
global $response;
|
|
$this->requirePermission('delete_order');
|
|
// Get the user object
|
|
$user = (new authentication())->get_user();
|
|
// Check if the request was successful
|
|
if ($user) {
|
|
// Get the payload
|
|
$id = $this->fromRequest('id');
|
|
// Check if the ID is set
|
|
if (!$id) {
|
|
$response->error('ID is required', 400);
|
|
}
|
|
// Get the current order
|
|
$order = (new orders_o())->getOrderById((int)$id);
|
|
// Check if the order exists
|
|
if (!$order->exists()) {
|
|
$response->error('Order not found', 400);
|
|
}
|
|
// Delete the order
|
|
$order->delete();
|
|
// Log the incident
|
|
(new logs_o())->add('orders', $order->department_id->value(), 1, $user->id, 'DELETE_ORDER', 'Successfully deleted an order (ID: ' . $id . ')');
|
|
// Return a success message
|
|
$response->success(['message' => 'Order deleted successfully']);
|
|
} else {
|
|
// Log the incident
|
|
(new logs_o())->add('orders', 'global', 1, 0, 'DELETE_ORDER', 'No user found, or invalid session');
|
|
// Return an error
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @param mixed $data
|
|
* @param response $response
|
|
* @return mixed
|
|
*/
|
|
private function getData(mixed $data, response $response): mixed
|
|
{
|
|
if (!isset($data['customer_id'])) {
|
|
$response->error('Customer ID is required', 400);
|
|
}
|
|
if (!isset($data['department_id'])) {
|
|
$response->error('Department ID is required', 400);
|
|
}
|
|
if (!isset($data['reference'])) {
|
|
$response->error('Reference is required', 400);
|
|
}
|
|
if (!isset($data['notes'])) {
|
|
$response->error('Notes is required', 400);
|
|
}
|
|
if (!isset($data['reg_1'])) {
|
|
$response->error('Registration number 1 is required', 400);
|
|
}
|
|
if (strlen($data['reg_1']) < 4) {
|
|
$response->error('Registration number 1 must be at least 4 characters', 400);
|
|
}
|
|
// Optional fields are not checked here, as they are optional and can be empty
|
|
return $data;
|
|
}
|
|
} |