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::Item object for this Booking
70 my $item_rs = $self->_result->item;
71 return unless $item_rs;
72 return Koha::Item->_new_from_dbic($item_rs);
77 Booking specific store method to catch booking clashes and ensure we have an item assigned
79 We assume that if an item is passed, it's bookability has already been checked. This is to allow
80 overrides in the future.
87 $self->_result->result_source->schema->txn_do(
89 if ( $self->item_id ) {
90 Koha::Exceptions::Object::FKConstraint->throw(
91 broken_fk => 'item_id',
92 value => $self->item_id,
93 ) unless ( $self->item );
95 $self->biblio_id( $self->item->biblionumber )
96 unless $self->biblio_id;
98 Koha::Exceptions::Object::FKConstraint->throw()
99 unless ( $self->biblio_id == $self->item->biblionumber );
102 Koha::Exceptions::Object::FKConstraint->throw(
103 broken_fk => 'biblio_id',
104 value => $self->biblio_id,
105 ) unless ( $self->biblio );
107 # Throw exception for item level booking clash
108 Koha::Exceptions::Booking::Clash->throw()
109 if $self->item_id && !$self->item->check_booking(
111 start_date => $self->start_date,
112 end_date => $self->end_date,
113 booking_id => $self->in_storage ? $self->booking_id : undef
117 # Throw exception for biblio level booking clash
118 Koha::Exceptions::Booking::Clash->throw()
119 if !$self->biblio->check_booking(
121 start_date => $self->start_date,
122 end_date => $self->end_date,
123 booking_id => $self->in_storage ? $self->booking_id : undef
127 # FIXME: We should be able to combine the above two functions into one
129 # Assign item at booking time
130 if ( !$self->item_id ) {
131 $self->_assign_item_for_booking;
134 $self = $self->SUPER::store;
141 =head3 _assign_item_for_booking
143 $self->_assign_item_for_booking;
145 Used internally in Koha::Booking->store to ensure we have an item assigned for the booking.
149 sub _assign_item_for_booking {
152 my $biblio = $self->biblio;
154 my $start_date = dt_from_string( $self->start_date );
155 my $end_date = dt_from_string( $self->end_date );
157 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
159 my $existing_bookings = $biblio->bookings(
163 $dtf->format_datetime($start_date),
164 $dtf->format_datetime($end_date)
169 $dtf->format_datetime($start_date),
170 $dtf->format_datetime($end_date)
174 start_date => { '<' => $dtf->format_datetime($start_date) },
175 end_date => { '>' => $dtf->format_datetime($end_date) }
181 $biblio->current_checkouts->search( { date_due => { '>=' => $dtf->format_datetime($start_date) } } );
183 my $bookable_items = $biblio->bookable_items->search(
186 '-and' => { '-not_in' => $existing_bookings->_resultset->get_column('item_id')->as_query },
187 { '-not_in' => $checkouts->_resultset->get_column('itemnumber')->as_query }
193 my $itemnumber = $bookable_items->single->itemnumber;
194 return $self->item_id($itemnumber);
197 =head3 get_items_that_can_fill
199 my $items = $bookings->get_items_that_can_fill();
201 Return the list of items that can fulfill this booking.
213 sub get_items_that_can_fill {
218 =head3 to_api_mapping
220 This method returns the mapping for representing a Koha::Booking object
229 =head2 Internal methods
241 Martin Renvoize <martin.renvoize@ptfs-europe.com>