Removed the requirement of providing all fields when changing orders (PUT).

This commit is contained in:
Jepp9350
2024-12-17 15:34:10 +01:00
parent 20db2a2245
commit b237915194
+26 -3
View File
@@ -93,9 +93,32 @@ class ordersRoute
$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); }
$data = $this->getData($data, $response);
// Update the order
(new orders_o())->edit((int)$data['id'], $user->id, (int)$data['customer_id'], $data['reference'], $data['notes'], (int)$data['department_id']);
// 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', $data['id'], 1, $user->id, 'EDIT_ORDER', 'Successfully updated an order (ID: ' . $data['id'] . ')');
// Return a success message