From f0db6584b848da0cedf5204875972599d3a6d3e0 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Wed, 7 Feb 2024 17:10:40 +0000 Subject: [PATCH] Bug 35248: Refactor - Move assign_item_for_booking This routine really belongs inside Koha::Booking rather than Koha::Biblio. This patch moves it there, renames with _ to highlight it's private nature. Test plan 1) Confirm that t/db_dependant/Koha/Booking.t still passes, specifically the 'store' test. --- Koha/Biblio.pm | 47 ----------------------------------- Koha/Booking.pm | 65 +++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 57 insertions(+), 55 deletions(-) diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index 72cbc07e8a5..31213eedae4 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -274,53 +274,6 @@ sub check_booking { return ( ( $total_bookable - $booked_count ) > 0 ) ? 1 : 0; } -=head3 assign_item_for_booking - -=cut - -sub assign_item_for_booking { - my ( $self, $params ) = @_; - - my $start_date = dt_from_string( $params->{start_date} ); - my $end_date = dt_from_string( $params->{end_date} ); - - my $dtf = Koha::Database->new->schema->storage->datetime_parser; - - my $existing_bookings = $self->bookings( - [ - start_date => { - '-between' => [ - $dtf->format_datetime($start_date), - $dtf->format_datetime($end_date) - ] - }, - end_date => { - '-between' => [ - $dtf->format_datetime($start_date), - $dtf->format_datetime($end_date) - ] - }, - { - start_date => { '<' => $dtf->format_datetime($start_date) }, - end_date => { '>' => $dtf->format_datetime($end_date) } - } - ] - ); - - my $checkouts = $self->current_checkouts->search( { date_due => { '>=' => $dtf->format_datetime($start_date) } } ); - - my $bookable_items = $self->bookable_items->search( - { - itemnumber => [ - '-and' => { '-not_in' => $existing_bookings->_resultset->get_column('item_id')->as_query }, - { '-not_in' => $checkouts->_resultset->get_column('itemnumber')->as_query } - ] - }, - { rows => 1 } - ); - return $bookable_items->single->itemnumber; -} - =head3 can_be_transferred $biblio->can_be_transferred({ to => $to_library, from => $from_library }) diff --git a/Koha/Booking.pm b/Koha/Booking.pm index 44e41ab4859..65d3122b6ee 100644 --- a/Koha/Booking.pm +++ b/Koha/Booking.pm @@ -128,14 +128,7 @@ sub store { # Assign item at booking time if ( !$self->item_id ) { - $self->item_id( - $self->biblio->assign_item_for_booking( - { - start_date => $self->start_date, - end_date => $self->end_date - } - ) - ); + $self->_assign_item_for_booking; } $self = $self->SUPER::store; @@ -145,6 +138,62 @@ sub store { return $self; } +=head3 _assign_item_for_booking + + $self->_assign_item_for_booking; + +Used internally in Koha::Booking->store to ensure we have an item assigned for the booking. + +=cut + +sub _assign_item_for_booking { + my ($self) = @_; + + my $biblio = $self->biblio; + + my $start_date = dt_from_string( $self->start_date ); + my $end_date = dt_from_string( $self->end_date ); + + my $dtf = Koha::Database->new->schema->storage->datetime_parser; + + my $existing_bookings = $biblio->bookings( + [ + start_date => { + '-between' => [ + $dtf->format_datetime($start_date), + $dtf->format_datetime($end_date) + ] + }, + end_date => { + '-between' => [ + $dtf->format_datetime($start_date), + $dtf->format_datetime($end_date) + ] + }, + { + start_date => { '<' => $dtf->format_datetime($start_date) }, + end_date => { '>' => $dtf->format_datetime($end_date) } + } + ] + ); + + my $checkouts = + $biblio->current_checkouts->search( { date_due => { '>=' => $dtf->format_datetime($start_date) } } ); + + my $bookable_items = $biblio->bookable_items->search( + { + itemnumber => [ + '-and' => { '-not_in' => $existing_bookings->_resultset->get_column('item_id')->as_query }, + { '-not_in' => $checkouts->_resultset->get_column('itemnumber')->as_query } + ] + }, + { rows => 1 } + ); + + my $itemnumber = $bookable_items->single->itemnumber; + return $self->item_id($itemnumber); +} + =head3 get_items_that_can_fill my $items = $bookings->get_items_that_can_fill(); -- 2.43.0