Refactor booking completion logic to improve wash certificate handling
- Replaced `orderWasCreatedDuringCompletion` flag with optimized checks for wash certificate attachment. - Updated method signatures to use nullable `safety_seal` parameter for consistency. - Enhanced `completeBooking` logic to prevent duplicate wash certificate creation or sending. - Added `hasWashCertificateAttached` method to streamline order checks and improve clarity. - Updated tests to cover edge cases for wash certificate attachment and email dispatch behavior.
This commit is contained in:
@@ -354,16 +354,14 @@ class order_bookings_o extends db
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function completeBooking(int $user_id, string $safety_seal = null): void
|
||||
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
|
||||
$this->createOrderBy($user_id);
|
||||
// Add order items, re-calculate the prices to be customer-specific
|
||||
$this->createOrderItemsBy($user_id);
|
||||
$orderWasCreatedDuringCompletion = true;
|
||||
}
|
||||
|
||||
if (!$this->containsWashCertificateItem()) {
|
||||
@@ -377,12 +375,14 @@ class order_bookings_o extends db
|
||||
$order->objectChanged();
|
||||
}
|
||||
|
||||
if (!$orderWasCreatedDuringCompletion) {
|
||||
if ($order->hasWashCertificateAttached()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->attachWashCertificate($user_id, $order->getSafetySealValue());
|
||||
$this->sendWashCertificateToCustomer();
|
||||
if ($order->hasWashCertificateAttached()) {
|
||||
$this->sendWashCertificateToCustomer();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -465,7 +465,7 @@ class order_bookings_o extends db
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
protected 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
|
||||
|
||||
@@ -19,9 +19,16 @@ if (!class_exists('OrderBookingsCompletionOrderDouble')) {
|
||||
$this->completed_at = new object_property('orders', -1, 'completed_at', 'timestamp', false);
|
||||
}
|
||||
|
||||
public bool $washCertificateAttached = false;
|
||||
|
||||
public function objectChanged(): void
|
||||
{
|
||||
}
|
||||
|
||||
public function hasWashCertificateAttached(): bool
|
||||
{
|
||||
return $this->washCertificateAttached;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,9 +72,10 @@ if (!class_exists('OrderBookingsCompletionDouble')) {
|
||||
return $this->linkedOrder;
|
||||
}
|
||||
|
||||
protected function attachWashCertificate(int $user_id, string $safety_seal = null): void
|
||||
protected function attachWashCertificate(int $user_id, ?string $safety_seal = null): void
|
||||
{
|
||||
$this->attachCalls++;
|
||||
$this->linkedOrder->washCertificateAttached = true;
|
||||
}
|
||||
|
||||
public function sendWashCertificateToCustomer(): void
|
||||
@@ -77,7 +85,7 @@ if (!class_exists('OrderBookingsCompletionDouble')) {
|
||||
}
|
||||
}
|
||||
|
||||
it('does not create or email a duplicate wash certificate when a booking is already linked to a pos order', function (): void {
|
||||
it('attaches and emails a wash certificate when a booking is already linked to a pos order without one', function (): void {
|
||||
$order = new OrderBookingsCompletionOrderDouble();
|
||||
$booking = new OrderBookingsCompletionDouble($order);
|
||||
$booking->order_id->set(321);
|
||||
@@ -85,6 +93,20 @@ it('does not create or email a duplicate wash certificate when a booking is alre
|
||||
|
||||
$booking->completeBooking(77, 'LINKED-SEAL');
|
||||
|
||||
expect($order->getSafetySealValue())->toBe('LINKED-SEAL');
|
||||
expect($booking->attachCalls)->toBe(1);
|
||||
expect($booking->sendCalls)->toBe(1);
|
||||
});
|
||||
|
||||
it('does not create or email a duplicate wash certificate when a linked pos order already has one', function (): void {
|
||||
$order = new OrderBookingsCompletionOrderDouble();
|
||||
$order->washCertificateAttached = true;
|
||||
$booking = new OrderBookingsCompletionDouble($order);
|
||||
$booking->order_id->set(321);
|
||||
$booking->containsWashCertificate = true;
|
||||
|
||||
$booking->completeBooking(77, 'LINKED-SEAL');
|
||||
|
||||
expect($order->getSafetySealValue())->toBe('LINKED-SEAL');
|
||||
expect($booking->attachCalls)->toBe(0);
|
||||
expect($booking->sendCalls)->toBe(0);
|
||||
|
||||
Reference in New Issue
Block a user