Refactor booking creation to improve structure and validation
Replaced raw SQL with a consolidated object-based approach for adding bookings, enhancing maintainability and reducing redundancy. Adjusted form handling to include stricter type casting, additional property checks, and default fallbacks for wash certificate data. These changes improve code readability and error handling while ensuring consistent data processing.
This commit is contained in:
@@ -55,7 +55,8 @@ class book_wash_f extends form_helper_c
|
||||
// Get the user from the customer number
|
||||
$user = (new users_o())->getUserByCustomerNumber(self::getSanitizedData('customer_number'));
|
||||
// Get the department name from the department id
|
||||
$department = (new departments_o())->selectId(self::getSanitizedData('department_id'));
|
||||
$department = (new departments_o())->selectId((int)self::getSanitizedData('department_id'));
|
||||
$department->getObjectProperties();
|
||||
// Get the bookings_new object
|
||||
$bookings = new bookings_o();
|
||||
// Check if the wash type contains the interior wash
|
||||
@@ -66,6 +67,9 @@ class book_wash_f extends form_helper_c
|
||||
} else {
|
||||
$wash_certificate_email = '';
|
||||
}
|
||||
} else {
|
||||
$wash_certificate_email = '';
|
||||
$wants_wash_certificate = false;
|
||||
}
|
||||
// Create a new booking
|
||||
$bookings->add(
|
||||
@@ -80,11 +84,12 @@ class book_wash_f extends form_helper_c
|
||||
$department->id,
|
||||
(bool)self::getSanitizedData('wants_pickup'),
|
||||
self::getSanitizedData('notes'),
|
||||
$wants_wash_certificate ? 'pending' : 'cancelled',
|
||||
($wants_wash_certificate ? 'pending' : 'cancelled'),
|
||||
'',
|
||||
'pending',
|
||||
self::getSanitizedData('wash_type'),
|
||||
);
|
||||
// Ad
|
||||
// Validate the booking actually exists
|
||||
if (!$bookings->exists()) {
|
||||
throw new \Exception('Booking could not be created');
|
||||
|
||||
@@ -249,31 +249,33 @@ class bookings_o extends db
|
||||
return $departmentLegacyNames[strtolower($departmentName)] ?? 0;
|
||||
}
|
||||
|
||||
public function add(int $customer_number, string $wash_type, string $contact_email, string $reference_number, string $regNrTraekker, string $regNrTrailer, string $washCertificateEmail, string $date, string $department, int $pickup_bool, string $notes, string $washCertificateStatus, string $washCertificateUrl, string $status): void
|
||||
/**
|
||||
* Add a new booking
|
||||
* @throws Exception If the object could not be created
|
||||
*/
|
||||
public function add(int $customer_number, string $wash_type, string $contact_email, string $reference_number, string $regNrTraekker, string $regNrTrailer, string $washCertificateEmail, string $date, int $department, int $pickup_bool, string $notes, string $washCertificateStatus, string $washCertificateUrl, string $status, array $data = []): void
|
||||
{
|
||||
global $db;
|
||||
// Avoid SQL injection
|
||||
$wash_type = $db->escape_string($wash_type);
|
||||
$contact_email = $db->escape_string($contact_email);
|
||||
$reference_number = $db->escape_string($reference_number);
|
||||
$regNrTraekker = $db->escape_string($regNrTraekker);
|
||||
$regNrTrailer = $db->escape_string($regNrTrailer);
|
||||
$washCertificateEmail = $db->escape_string($washCertificateEmail);
|
||||
$date = $db->escape_string($date);
|
||||
// Parse the department name to id
|
||||
$department = $this->getDepartmentIdByLegacyName($department);
|
||||
$notes = $db->escape_string($notes);
|
||||
$washCertificateStatus = $db->escape_string($washCertificateStatus);
|
||||
$washCertificateUrl = $db->escape_string($washCertificateUrl);
|
||||
$status = $db->escape_string($status);
|
||||
// Create a new record in the database ( Replace the code, if an entry already exists )
|
||||
$sql = "INSERT INTO $this->table (customer_number, wash_type, contact_email, reference_number, regNrTraekker, regNrTrailer, washCertificateEmail, date, department, pickup_bool, notes, washCertificateStatus, washCertificateUrl, status) VALUES ($customer_number, '$wash_type', '$contact_email', '$reference_number', '$regNrTraekker', '$regNrTrailer', '$washCertificateEmail', '$date', '$department', $pickup_bool, '$notes', '$washCertificateStatus', '$washCertificateUrl', '$status')";
|
||||
$db->query($sql);
|
||||
// Clear the cache
|
||||
redis->clear_department_booking_count($department);
|
||||
|
||||
// Get the id of the new record
|
||||
$this->id = $db->insert_id();
|
||||
$tmp_id = self::add_object([
|
||||
'customer_number' => (int)$db->escape_string($customer_number),
|
||||
'wash_type' => (string)$db->escape_string($wash_type),
|
||||
'contact_email' => (string)$db->escape_string($contact_email),
|
||||
'reference_number' => (string)$db->escape_string($reference_number),
|
||||
'regNrTraekker' => (string)$db->escape_string($regNrTraekker),
|
||||
'regNrTrailer' => (string)$db->escape_string($regNrTrailer),
|
||||
'washCertificateEmail' => (string)$washCertificateEmail ? $db->escape_string($washCertificateEmail) : '',
|
||||
'date' => (string)$db->escape_string($date),
|
||||
'department' => (int)$db->escape_string($department),
|
||||
'pickup_bool' => (int)$db->escape_string($pickup_bool),
|
||||
'notes' => (string)$db->escape_string($notes),
|
||||
'washCertificateStatus' => (string)$db->escape_string($washCertificateStatus),
|
||||
'washCertificateUrl' => (string)$db->escape_string($washCertificateUrl),
|
||||
'status' => (string)$db->escape_string($status),
|
||||
'data' => json_encode($data),
|
||||
]);
|
||||
// Check if the booking was created successfully
|
||||
self::select((int)$tmp_id);
|
||||
self::requireSelected();
|
||||
}
|
||||
|
||||
public function checkUnfulfilledBookings(): void
|
||||
|
||||
Reference in New Issue
Block a user