|
Lines 21-28
use Modern::Perl;
Link Here
|
| 21 |
|
21 |
|
| 22 |
use Koha::Exceptions::Booking; |
22 |
use Koha::Exceptions::Booking; |
| 23 |
use Koha::DateUtils qw( dt_from_string ); |
23 |
use Koha::DateUtils qw( dt_from_string ); |
|
|
24 |
use Koha::Bookings; |
| 25 |
use Koha::CirculationRules; |
| 26 |
use Koha::Cache::Memory::Lite; |
| 27 |
|
| 28 |
use C4::Circulation; |
| 29 |
use C4::Biblio; |
| 24 |
|
30 |
|
| 25 |
use base qw(Koha::Object); |
31 |
use base qw(Koha::Object); |
|
|
32 |
use List::Util qw(min); |
| 26 |
|
33 |
|
| 27 |
=head1 NAME |
34 |
=head1 NAME |
| 28 |
|
35 |
|
|
Lines 32-37
Koha::Booking - Koha Booking object class
Link Here
|
| 32 |
|
39 |
|
| 33 |
=head2 Class methods |
40 |
=head2 Class methods |
| 34 |
|
41 |
|
|
|
42 |
=head2 can_be_booked_in_advance |
| 43 |
|
| 44 |
$canBeBooked = &can_be_booked_in_advance($patron, $item, $branchcode) |
| 45 |
if ($canBeBooked->{status} eq 'OK') { #We can booked this Item in advance! } |
| 46 |
|
| 47 |
@RETURNS { status => OK }, if the Item can be booked. |
| 48 |
{ status => tooManyBookings, limit => $limit }, if the borrower has exceeded their maximum booking amount. |
| 49 |
{ status => tooLongBookingPeriod, limit => $limit }, if the borrower has exceeded their maximum booking period. |
| 50 |
{ status => } |
| 51 |
|
| 52 |
=cut |
| 53 |
|
| 54 |
sub can_be_booked_in_advance { |
| 55 |
my ( $self, $params ) = @_; |
| 56 |
my $patron = $self->patron; |
| 57 |
my $item = $self->item; |
| 58 |
|
| 59 |
my $dbh = C4::Context->dbh; |
| 60 |
|
| 61 |
my $borrower = $patron->unblessed; |
| 62 |
|
| 63 |
if ( C4::Biblio->GetMarcFromKohaField('biblioitems.agerestriction') ) { |
| 64 |
my $biblio = $item->biblio; |
| 65 |
|
| 66 |
# Check for the age restriction |
| 67 |
my ( $ageRestriction, $daysToAgeRestriction ) = |
| 68 |
C4::Circulation::GetAgeRestriction( $biblio->biblioitem->agerestriction, $borrower ); |
| 69 |
return { status => 'ageRestricted' } if $daysToAgeRestriction && $daysToAgeRestriction > 0; |
| 70 |
} |
| 71 |
|
| 72 |
# By default for now, control branch is the item homebranch |
| 73 |
my $bookings_control_branch = $item->homebranch; |
| 74 |
|
| 75 |
# we retrieve rights |
| 76 |
my $rights = Koha::CirculationRules->get_effective_rules( |
| 77 |
{ |
| 78 |
categorycode => $borrower->{'categorycode'}, |
| 79 |
itemtype => $item->effective_itemtype, |
| 80 |
branchcode => $bookings_control_branch, |
| 81 |
rules => |
| 82 |
[ 'bookings_allowed_total', 'bookings_per_item', 'bookings_per_itemtype', 'bookings_period_length' ] |
| 83 |
} |
| 84 |
); |
| 85 |
|
| 86 |
my $bookings_allowed_total = $rights->{bookings_allowed_total} // 0; |
| 87 |
my $bookings_per_item = $rights->{bookings_per_item} // 1; |
| 88 |
my $bookings_per_itemtype = $rights->{bookings_per_itemtype} // 1; |
| 89 |
my $bookings_period_length = $rights->{bookings_period_length}; |
| 90 |
my $booking_limit; |
| 91 |
|
| 92 |
my $bookable_status = $item->bookable; |
| 93 |
|
| 94 |
if($bookable_status == 1) { |
| 95 |
$booking_limit = $bookings_per_item; |
| 96 |
} elsif (!defined($bookable_status)) { |
| 97 |
$booking_limit = $bookings_per_itemtype; |
| 98 |
} else { |
| 99 |
$booking_limit = 0; |
| 100 |
} |
| 101 |
|
| 102 |
if ( defined $bookings_allowed_total && $bookings_allowed_total ne '' ) { |
| 103 |
if ( $bookings_allowed_total == 0 ) { |
| 104 |
return { status => 'noBookingsAllowed' }; |
| 105 |
} else { |
| 106 |
$booking_limit = min( $booking_limit, $bookings_allowed_total ); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
if ( $booking_limit == 0 ) { |
| 111 |
return { status => "noBookingsAllowedOnThisItem" }; |
| 112 |
} |
| 113 |
|
| 114 |
my $total_bookings_count = Koha::Bookings->search( { patron_id => $patron->borrowernumber } )->count(); |
| 115 |
return { status => 'tooManyBookings', limit => $booking_limit } if $booking_limit <= $total_bookings_count; |
| 116 |
|
| 117 |
my $start_date = dt_from_string( $self->start_date ); |
| 118 |
my $end_date = dt_from_string( $self->end_date ); |
| 119 |
my $duration = $end_date->delta_days($start_date); |
| 120 |
|
| 121 |
my $delta_days = $duration->in_units('days'); |
| 122 |
|
| 123 |
return { status => 'tooLongBookingPeriod', limit => $bookings_period_length } if $delta_days > $bookings_period_length; |
| 124 |
|
| 125 |
return { status => 'OK' }; |
| 126 |
} |
| 127 |
|
| 35 |
=head3 biblio |
128 |
=head3 biblio |
| 36 |
|
129 |
|
| 37 |
Returns the related Koha::Biblio object for this booking |
130 |
Returns the related Koha::Biblio object for this booking |
|
Lines 126-131
sub store {
Link Here
|
| 126 |
|
219 |
|
| 127 |
# FIXME: We should be able to combine the above two functions into one |
220 |
# FIXME: We should be able to combine the above two functions into one |
| 128 |
|
221 |
|
|
|
222 |
my $canBeBooked = can_be_booked_in_advance( $self ); |
| 223 |
Koha::Exceptions::Booking::Rule->throw( $canBeBooked ) if $canBeBooked->{'status'} ne "OK"; |
| 224 |
|
| 129 |
# Assign item at booking time |
225 |
# Assign item at booking time |
| 130 |
if ( !$self->item_id ) { |
226 |
if ( !$self->item_id ) { |
| 131 |
$self->_assign_item_for_booking; |
227 |
$self->_assign_item_for_booking; |