diff --git a/app/Models/Bookings.php b/app/Models/Bookings.php index 74971b0..7dc2ed4 100644 --- a/app/Models/Bookings.php +++ b/app/Models/Bookings.php @@ -62,9 +62,16 @@ public function primaryContact() public function getDurationAttribute() { - // Calculate the duration of the booking - $diff = $this->start_time->diff($this->end_time); - return $diff->h; + $start = $this->start_time; + $end = $this->end_time; + + // If end time is earlier than start time, add a day to end time + if ($end < $start) + { + $end = $end->addDay(); + } + + return $start->diffInHours($end); } public function events(): MorphMany diff --git a/tests/Unit/BookingOperationsTest.php b/tests/Unit/BookingOperationsTest.php index 1d966d8..01df9e8 100644 --- a/tests/Unit/BookingOperationsTest.php +++ b/tests/Unit/BookingOperationsTest.php @@ -105,6 +105,16 @@ public function test_can_calculate_duration() $this->assertEquals(4, $booking->duration); } + public function test_can_calculate_duration_after_midnight() + { + $booking = Bookings::factory()->create([ + 'start_time' => '21:00:00', + 'end_time' => '01:00:00', + ]); + + $this->assertEquals(4, $booking->duration); + } + public function test_can_determine_if_booking_is_paid() { // Create a booking with a price of 1000