|
Lines 24-35
use Koha::DateUtils qw( dt_from_string );
Link Here
|
| 24 |
use Koha::Items; |
24 |
use Koha::Items; |
| 25 |
use Koha::Patrons; |
25 |
use Koha::Patrons; |
| 26 |
use Koha::Libraries; |
26 |
use Koha::Libraries; |
|
|
27 |
use Koha::Bookings; |
| 28 |
use Koha::CirculationRules; |
| 29 |
use Koha::Cache::Memory::Lite; |
| 27 |
|
30 |
|
| 28 |
use C4::Letters; |
31 |
use C4::Letters; |
|
|
32 |
use C4::Circulation; |
| 33 |
use C4::Biblio; |
| 29 |
|
34 |
|
| 30 |
use List::Util qw(any); |
35 |
use List::Util qw(any); |
| 31 |
|
36 |
|
| 32 |
use base qw(Koha::Object); |
37 |
use base qw(Koha::Object); |
|
|
38 |
use List::Util qw(min); |
| 33 |
|
39 |
|
| 34 |
=head1 NAME |
40 |
=head1 NAME |
| 35 |
|
41 |
|
|
Lines 39-44
Koha::Booking - Koha Booking object class
Link Here
|
| 39 |
|
45 |
|
| 40 |
=head2 Class methods |
46 |
=head2 Class methods |
| 41 |
|
47 |
|
|
|
48 |
=head2 can_be_booked_in_advance |
| 49 |
|
| 50 |
$canBeBooked = &can_be_booked_in_advance($patron, $item, $branchcode) |
| 51 |
if ($canBeBooked->{status} eq 'OK') { #We can booked this Item in advance! } |
| 52 |
|
| 53 |
@RETURNS { status => OK }, if the Item can be booked. |
| 54 |
{ status => tooManyBookings, limit => $limit, rule => $rule }, if the borrower has exceeded their maximum booking amount. |
| 55 |
=cut |
| 56 |
|
| 57 |
sub can_be_booked_in_advance { |
| 58 |
my ( $self, $params ) = @_; |
| 59 |
my $patron = $self->patron; |
| 60 |
my $item = $self->item; |
| 61 |
|
| 62 |
my $dbh = C4::Context->dbh; |
| 63 |
|
| 64 |
my $borrower = $patron->unblessed; |
| 65 |
|
| 66 |
if ( C4::Biblio->GetMarcFromKohaField('biblioitems.agerestriction') ) { |
| 67 |
my $biblio = $item->biblio; |
| 68 |
|
| 69 |
# Check for the age restriction |
| 70 |
my ( $ageRestriction, $daysToAgeRestriction ) = |
| 71 |
C4::Circulation::GetAgeRestriction( $biblio->biblioitem->agerestriction, $borrower ); |
| 72 |
return { status => 'ageRestricted' } if $daysToAgeRestriction && $daysToAgeRestriction > 0; |
| 73 |
} |
| 74 |
|
| 75 |
# By default for now, control branch is the item homebranch |
| 76 |
my $bookings_control_branch = $item->homebranch; |
| 77 |
|
| 78 |
# we retrieve rights |
| 79 |
my $rights = Koha::CirculationRules->get_effective_rules( |
| 80 |
{ |
| 81 |
categorycode => $borrower->{'categorycode'}, |
| 82 |
itemtype => $item->effective_itemtype, |
| 83 |
branchcode => $bookings_control_branch, |
| 84 |
rules => |
| 85 |
[ 'bookings_allowed_total', 'bookings_per_item', 'bookings_period_length' ] |
| 86 |
} |
| 87 |
); |
| 88 |
|
| 89 |
my $bookings_allowed_total = $rights->{bookings_allowed_total}; |
| 90 |
my $bookings_per_item = $rights->{bookings_per_item}; |
| 91 |
my $bookings_period_length = $rights->{bookings_period_length} || 0; |
| 92 |
|
| 93 |
return { status => 'noBookingsAllowed' } if defined($bookings_allowed_total) && $bookings_allowed_total == 0; |
| 94 |
|
| 95 |
my $bookings_per_item_count = Koha::Bookings->search( { patron_id => $patron->borrowernumber, item_id => $item->itemnumber } )->count(); |
| 96 |
return { status => 'tooManyBookings', limit => $bookings_per_item, rule => 'bookings_per_item' } if defined($bookings_per_item) && $bookings_per_item <= $bookings_per_item_count; |
| 97 |
|
| 98 |
|
| 99 |
my $querycount; |
| 100 |
if (C4::Context->preference('item-level_itypes')) { |
| 101 |
$querycount = q{ |
| 102 |
SELECT count(*) AS count |
| 103 |
FROM bookings AS b |
| 104 |
LEFT JOIN items AS i ON (b.item_id=i.itemnumber) |
| 105 |
WHERE b.patron_id = ? |
| 106 |
AND i.itype = ? |
| 107 |
}; |
| 108 |
} else { |
| 109 |
$querycount = q{ |
| 110 |
SELECT count(*) AS count |
| 111 |
FROM bookings AS b |
| 112 |
LEFT JOIN biblioitems AS bi ON (b.biblio_id=bi.biblionumber) |
| 113 |
WHERE b.patron_id = ? |
| 114 |
AND bi.itemtype = ? |
| 115 |
}; |
| 116 |
} |
| 117 |
|
| 118 |
my $sthcount = $dbh->prepare($querycount); |
| 119 |
$sthcount->execute( $patron->borrowernumber, $item->effective_itemtype ); |
| 120 |
my $total_bookings_count = $sthcount->fetchrow_hashref()->{count}; |
| 121 |
|
| 122 |
return { status => 'tooManyBookings', limit => $bookings_allowed_total, rule => 'bookings_allowed_total' } if defined($bookings_allowed_total) && $bookings_allowed_total <= $total_bookings_count; |
| 123 |
|
| 124 |
my $start_date = dt_from_string( $self->start_date ); |
| 125 |
my $end_date = dt_from_string( $self->end_date ); |
| 126 |
my $duration = $end_date->delta_days($start_date); |
| 127 |
|
| 128 |
my $delta_days = $duration->in_units('days'); |
| 129 |
|
| 130 |
return { status => 'bookingPeriodNotValid'} if $bookings_period_length == 0; |
| 131 |
return { status => 'tooLongBookingPeriod', limit => $bookings_period_length } if $delta_days > $bookings_period_length; |
| 132 |
|
| 133 |
return { status => 'OK' }; |
| 134 |
} |
| 135 |
|
| 42 |
=head3 biblio |
136 |
=head3 biblio |
| 43 |
|
137 |
|
| 44 |
Returns the related Koha::Biblio object for this booking |
138 |
Returns the related Koha::Biblio object for this booking |
|
Lines 151-156
sub store {
Link Here
|
| 151 |
|
245 |
|
| 152 |
# FIXME: We should be able to combine the above two functions into one |
246 |
# FIXME: We should be able to combine the above two functions into one |
| 153 |
|
247 |
|
|
|
248 |
my $canBeBooked = can_be_booked_in_advance( $self ); |
| 249 |
Koha::Exceptions::Booking::Rule->throw( $canBeBooked ) if $canBeBooked->{'status'} ne "OK"; |
| 250 |
|
| 154 |
# Assign item at booking time |
251 |
# Assign item at booking time |
| 155 |
if ( !$self->item_id ) { |
252 |
if ( !$self->item_id ) { |
| 156 |
$self->_assign_item_for_booking; |
253 |
$self->_assign_item_for_booking; |