diff --git a/C4/Circulation.pm b/C4/Circulation.pm index 54c8e4782c..c53e799dde 100644 --- a/C4/Circulation.pm +++ b/C4/Circulation.pm @@ -1768,14 +1768,29 @@ sub AddIssue { # and SwitchOnSiteCheckouts is enabled this converts it to a regular checkout $issue = Koha::Checkouts->find( { itemnumber => $item_object->itemnumber } ); - #if this checkout is a booking mark it as completed - if ( - my $booking = $item_object->find_booking( - { checkout_date => $issuedate, due_date => $datedue, patron_id => $patron->borrowernumber } - ) - ) - { - $booking->status('completed')->store; + # If this checkout relates to a booking, handle booking status appropriately + if ( my $booking = $item_object->find_booking( { checkout_date => $issuedate, due_date => $datedue } ) ) { + if ( $booking->patron_id == $patron->borrowernumber ) { + + # Patron's own booking - mark as completed + $booking->status('completed')->store; + } else { + + # Another patron's booking - only cancel if checkout period overlaps with actual booking period + my $booking_start = dt_from_string( $booking->start_date ); + my $booking_end = dt_from_string( $booking->end_date ); + + # Check if checkout period overlaps with actual booking period (not just lead/trail) + if ( ( $issuedate >= $booking_start && $issuedate <= $booking_end ) + || ( $datedue >= $booking_start && $datedue <= $booking_end ) + || ( $issuedate <= $booking_start && $datedue >= $booking_end ) ) + { + # Checkout overlaps with actual booking period - cancel the booking + $booking->status('cancelled')->store; + } + + # If only in lead/trail period, do nothing - librarian has made informed decision + } } if ($issue) {