Refactor order creation to use addArray method and sanitize inputs

- Introduced `addArray` method in `orders_o` for streamlined order creation using an associative array.
- Updated `ordersRoute` to utilize `addArray`, replacing older implementation for better manageability.
- Improved input handling by trimming whitespaces from registration numbers.
- Enhanced product pricing by incorporating department-specific prices and discount percentages.
This commit is contained in:
Jeppe Bundgaard
2025-07-21 13:10:15 +02:00
parent af3b0666fb
commit 4cc270e258
2 changed files with 56 additions and 5 deletions
+21 -2
View File
@@ -147,13 +147,32 @@ class ordersRoute
// Get the registration numbers (If they are set, they 2-3 are optional)
$reg_2 = $data['reg_2'] ?? '';
$reg_3 = $data['reg_3'] ?? '';
// Strip the registration numbers of any whitespace
$reg_1 = preg_replace('/\s+/', '', $reg_1);
$reg_2 = preg_replace('/\s+/', '', $reg_2);
$reg_3 = preg_replace('/\s+/', '', $reg_3);
$new_data = [
'customer_id' => (int)$data['customer_id'],
'department_id' => (int)$data['department_id'],
'reference' => (string)$data['reference'] ?? '',
'cashier_id' => (int)$user->id, // The user who created the order
'notes' => (string)$data['notes'] ?? '',
'reg_1' => (string)$reg_1,
'reg_2' => (string)$reg_2,
'reg_3' => (string)$reg_3,
...(!empty($data['lane']) ? ['lane' => (int)$data['lane']] : []), // Optional lane
...(!empty($data['wash_id']) ? ['wash_id' => (string)$data['wash_id']] : []), // Optional wash ID
...(!empty($data['booking_id']) ? ['booking_id' => (int)$data['booking_id']] : []), // Optional booking ID
'created_at' => (string)($data['created_at'] ?? date('Y-m-d H:i:s')), // Default to current time if not set
];
// 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);
//$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);
$order = (new orders_o())->addArray($new_data);
// 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 {
} else {
// Log the incident
(new logs_o())->add('orders', 'global', 1, 0, 'ADD_ORDER', 'No user found, or invalid session');
// Return an error