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
84 $self->_result->result_source->schema->txn_do(
86 if ( $self->item_id ) {
87 Koha::Exceptions::Object::FKConstraint->throw(
88 broken_fk => 'item_id',
89 value => $self->item_id,
90 ) unless ( $self->item );
92 $self->biblio_id( $self->item->biblionumber )
93 unless $self->biblio_id;
95 Koha::Exceptions::Object::FKConstraint->throw()
96 unless ( $self->biblio_id == $self->item->biblionumber );
99 Koha::Exceptions::Object::FKConstraint->throw(
100 broken_fk => 'biblio_id',
101 value => $self->biblio_id,
102 ) unless ( $self->biblio );
104 # Throw exception for item level booking clash
105 Koha::Exceptions::Booking::Clash->throw()
106 if $self->item_id && !$self->item->check_booking(
108 start_date => $self->start_date,
109 end_date => $self->end_date,
110 booking_id => $self->in_storage ? $self->booking_id : undef
114 # Throw exception for biblio level booking clash
115 Koha::Exceptions::Booking::Clash->throw()
116 if !$self->biblio->check_booking(
118 start_date => $self->start_date,
119 end_date => $self->end_date,
120 booking_id => $self->in_storage ? $self->booking_id : undef
124 # Assign item at booking time
125 if ( !$self->item_id ) {
127 $self->biblio->assign_item_for_booking(
129 start_date => $self->start_date,
130 end_date => $self->end_date
136 # FIXME: We should be able to combine the above two functions into one
138 $self = $self->SUPER::store;
147 my $intersects = $booking1->intersects($booking2);
149 Returns a boolean denoting whether booking1 interfers/overlaps/clashes with booking2.
154 my ( $self, $comp ) = @_;
156 # Start date of comparison booking is after end date of this booking.
160 dt_from_string( $comp->start_date ),
161 dt_from_string( $self->end_date )
165 # End date of comparison booking is before start date of this booking.
169 dt_from_string( $comp->end_date ),
170 dt_from_string( $self->start_date )
174 # Bookings must overlap
178 =head3 get_items_that_can_fill
180 my $items = $bookings->get_items_that_can_fill();
182 Return the list of items that can fulfill this booking.
194 sub get_items_that_can_fill {
199 =head3 to_api_mapping
201 This method returns the mapping for representing a Koha::Booking object
210 =head2 Internal methods
222 Martin Renvoize <martin.renvoize@ptfs-europe.com>