3 # Copyright PTFS Europe 2021
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use Koha::Exceptions::Booking;
23 use Koha::DateUtils qw( dt_from_string );
25 use base qw(Koha::Object);
29 Koha::Booking - Koha Booking object class
37 Returns the related Koha::Biblio object for this booking
44 my $biblio_rs = $self->_result->biblio;
45 return Koha::Biblio->_new_from_dbic($biblio_rs);
50 Returns the related Koha::Patron object for this booking
57 my $patron_rs = $self->_result->patron;
58 return Koha::Patron->_new_from_dbic($patron_rs);
63 Returns the related Koha::Library object for this booking
70 my $pickup_library_rs = $self->_result->pickup_library;
71 return Koha::Library->_new_from_dbic($pickup_library_rs);
76 Returns the related Koha::Item object for this Booking
83 my $item_rs = $self->_result->item;
84 return unless $item_rs;
85 return Koha::Item->_new_from_dbic($item_rs);
90 Booking specific store method to catch booking clashes and ensure we have an item assigned
92 We assume that if an item is passed, it's bookability has already been checked. This is to allow
93 overrides in the future.
100 $self->_result->result_source->schema->txn_do(
102 if ( $self->item_id ) {
103 Koha::Exceptions::Object::FKConstraint->throw(
104 broken_fk => 'item_id',
105 value => $self->item_id,
106 ) unless ( $self->item );
108 $self->biblio_id( $self->item->biblionumber )
109 unless $self->biblio_id;
111 Koha::Exceptions::Object::FKConstraint->throw()
112 unless ( $self->biblio_id == $self->item->biblionumber );
115 Koha::Exceptions::Object::FKConstraint->throw(
116 broken_fk => 'biblio_id',
117 value => $self->biblio_id,
118 ) unless ( $self->biblio );
120 # Throw exception for item level booking clash
121 Koha::Exceptions::Booking::Clash->throw()
122 if $self->item_id && !$self->item->check_booking(
124 start_date => $self->start_date,
125 end_date => $self->end_date,
126 booking_id => $self->in_storage ? $self->booking_id : undef
130 # Throw exception for biblio level booking clash
131 Koha::Exceptions::Booking::Clash->throw()
132 if !$self->biblio->check_booking(
134 start_date => $self->start_date,
135 end_date => $self->end_date,
136 booking_id => $self->in_storage ? $self->booking_id : undef
140 # FIXME: We should be able to combine the above two functions into one
142 # Assign item at booking time
143 if ( !$self->item_id ) {
144 $self->_assign_item_for_booking;
147 $self = $self->SUPER::store;
154 =head3 _assign_item_for_booking
156 $self->_assign_item_for_booking;
158 Used internally in Koha::Booking->store to ensure we have an item assigned for the booking.
162 sub _assign_item_for_booking {
165 my $biblio = $self->biblio;
167 my $start_date = dt_from_string( $self->start_date );
168 my $end_date = dt_from_string( $self->end_date );
170 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
172 my $existing_bookings = $biblio->bookings(
176 $dtf->format_datetime($start_date),
177 $dtf->format_datetime($end_date)
182 $dtf->format_datetime($start_date),
183 $dtf->format_datetime($end_date)
187 start_date => { '<' => $dtf->format_datetime($start_date) },
188 end_date => { '>' => $dtf->format_datetime($end_date) }
194 $biblio->current_checkouts->search( { date_due => { '>=' => $dtf->format_datetime($start_date) } } );
196 my $bookable_items = $biblio->bookable_items->search(
199 '-and' => { '-not_in' => $existing_bookings->_resultset->get_column('item_id')->as_query },
200 { '-not_in' => $checkouts->_resultset->get_column('itemnumber')->as_query }
206 my $itemnumber = $bookable_items->single->itemnumber;
207 return $self->item_id($itemnumber);
210 =head3 get_items_that_can_fill
212 my $items = $bookings->get_items_that_can_fill();
214 Return the list of items that can fulfill this booking.
226 sub get_items_that_can_fill {
231 =head3 to_api_mapping
233 This method returns the mapping for representing a Koha::Booking object
242 =head2 Internal methods
254 Martin Renvoize <martin.renvoize@ptfs-europe.com>