Add safety seal support to orders and related logic for wash certificates

- Introduced `safety_seal` column in the `orders` table.
- Updated order creation and completion logic to handle safety seal values.
- Enhanced order and booking classes to manage safety seal attachment and retrieval.
- Added tests to validate safety seal functionality in order processing.
This commit is contained in:
Jeppe Bundgaard
2026-04-14 10:51:25 +02:00
parent 98f3188a98
commit ebf7e820d5
18 changed files with 1161 additions and 151 deletions
+36 -23
View File
@@ -350,16 +350,32 @@ class order_bookings_o extends db
public function completeBooking(int $user_id, string $safety_seal = null): void
{
self::requireSelected();
$orderWasCreatedDuringCompletion = false;
if (!$this->order_id->value()) {
// Create order, if not already created
self::createOrderBy($user_id);
$this->createOrderBy($user_id);
// Add order items, re-calculate the prices to be customer-specific
self::createOrderItemsBy($user_id);
$this->createOrderItemsBy($user_id);
$orderWasCreatedDuringCompletion = true;
}
// Create a wash certificate (If applicable)
if (self::containsWashCertificateItem()) self::attachWashCertificate($user_id, $safety_seal);
// Send wash certificate
self::sendWashCertificateToCustomer();
if (!$this->containsWashCertificateItem()) {
return;
}
$order = $this->getOrder();
$normalizedSafetySeal = orders_o::normalizeSafetySealValue($safety_seal);
if ($normalizedSafetySeal !== null) {
$order->setSafetySealValue($normalizedSafetySeal);
$order->objectChanged();
}
if (!$orderWasCreatedDuringCompletion) {
return;
}
$this->attachWashCertificate($user_id, $order->getSafetySealValue());
$this->sendWashCertificateToCustomer();
}
/**
@@ -407,21 +423,18 @@ class order_bookings_o extends db
public function containsWashCertificateItem(): bool
{
self::requireSelected();
return self::containsProductId(41);
}
foreach ($this->items->value() as $item) {
$product_id = (int)($item['id'] ?? 0);
if ($product_id <= 0) {
continue;
}
/**
* @throws Exception
*/
private function containsProductId(int $productId): bool
{
self::requireSelected();
$items = $this->items->value();
foreach ($items as $item) {
if (isset($item['id']) && (int)$item['id'] == $productId) {
$product = (new products_o())->select($product_id);
if ($product->exists() && $product->isWashCertificate()) {
return true;
}
}
return false;
}
@@ -445,11 +458,11 @@ class order_bookings_o extends db
/**
* @throws Exception
*/
private function attachWashCertificate(int $user_id, string $safety_seal = null): void
protected function attachWashCertificate(int $user_id, string $safety_seal = null): void
{
self::requireSelected();
// Check if the order already has a wash certificate attached
if (self::getOrder()->hasWashCertificateAttached()) {
if ($this->getOrder()->hasWashCertificateAttached()) {
return;
}
// Get the operator name
@@ -458,7 +471,7 @@ class order_bookings_o extends db
throw new Exception('Operator not found');
}
// Generate wash certificate
self::generateWashCertificate($safety_seal, $operator->display_name->value());
$this->generateWashCertificate($safety_seal, $operator->display_name->value());
}
/**
@@ -468,7 +481,7 @@ class order_bookings_o extends db
* @throws Exception If the object is not selected
* @throws Exception If the booking already has a wash certificate
*/
public function generateWashCertificate(int|null $safety_seal = null, string|null $operator = null): void
public function generateWashCertificate(string|null $safety_seal = null, string|null $operator = null): void
{
self::requireSelected();
// Generate the wash certificate
@@ -510,7 +523,7 @@ class order_bookings_o extends db
])
->addData([
'booking_number' => $this->id,
'seal_number' => ($safety_seal ?? null),
'seal_number' => orders_o::normalizeSafetySealValue($safety_seal),
'reg_1' => $booking_array['reg_1'],
'reg_2' => $booking_array['reg_2'],
'date' => date('d-m-Y'),
@@ -556,4 +569,4 @@ class order_bookings_o extends db
return count($order_ids);
}
}
}