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, rule => $rule }, 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 => bookingPeriodNotValid }, if booking period is not valid (undef or equal to 0). |
51 |
=cut |
52 |
|
53 |
sub can_be_booked_in_advance { |
54 |
my ( $self, $params ) = @_; |
55 |
my $patron = $self->patron; |
56 |
my $item = $self->item; |
57 |
|
58 |
my $dbh = C4::Context->dbh; |
59 |
|
60 |
my $borrower = $patron->unblessed; |
61 |
|
62 |
if ( C4::Biblio->GetMarcFromKohaField('biblioitems.agerestriction') ) { |
63 |
my $biblio = $item->biblio; |
64 |
|
65 |
# Check for the age restriction |
66 |
my ( $ageRestriction, $daysToAgeRestriction ) = |
67 |
C4::Circulation::GetAgeRestriction( $biblio->biblioitem->agerestriction, $borrower ); |
68 |
return { status => 'ageRestricted' } if $daysToAgeRestriction && $daysToAgeRestriction > 0; |
69 |
} |
70 |
|
71 |
# By default for now, control branch is the item homebranch |
72 |
my $bookings_control_branch = $item->homebranch; |
73 |
|
74 |
# we retrieve rights |
75 |
my $rights = Koha::CirculationRules->get_effective_rules( |
76 |
{ |
77 |
categorycode => $borrower->{'categorycode'}, |
78 |
itemtype => $item->effective_itemtype, |
79 |
branchcode => $bookings_control_branch, |
80 |
rules => |
81 |
[ 'bookings_allowed_total', 'bookings_per_item', 'bookings_period_length' ] |
82 |
} |
83 |
); |
84 |
|
85 |
my $bookings_allowed_total = $rights->{bookings_allowed_total} || undef; |
86 |
my $bookings_per_item = $rights->{bookings_per_item} || undef; |
87 |
my $bookings_period_length = $rights->{bookings_period_length} || 0; |
88 |
|
89 |
return { status => 'noBookingsAllowed' } if defined($bookings_allowed_total) && $bookings_allowed_total == 0; |
90 |
|
91 |
my $bookings_per_item_count = Koha::Bookings->search( { patron_id => $patron->borrowernumber, item_id => $item->itemnumber } )->count(); |
92 |
return { status => 'tooManyBookings', limit => $bookings_per_item, rule => 'bookings_per_item' } if defined($bookings_per_item) && $bookings_per_item <= $bookings_per_item_count; |
93 |
|
94 |
my $querycount = q{ |
95 |
SELECT count(*) AS count |
96 |
FROM bookings AS b |
97 |
LEFT JOIN biblioitems AS bi ON (b.biblio_id=bi.biblionumber) |
98 |
WHERE b.patron_id = ? |
99 |
AND bi.itemtype = ? |
100 |
}; |
101 |
|
102 |
my $sthcount = $dbh->prepare($querycount); |
103 |
$sthcount->execute( $patron->borrowernumber, $item->effective_itemtype ); |
104 |
my $total_bookings_count = $sthcount->fetchrow_hashref()->{count}; |
105 |
|
106 |
return { status => 'tooManyBookings', limit => $bookings_allowed_total, rule => 'bookings_allowed_total' } if if defined($bookings_allowed_total) && $bookings_allowed_total <= $total_bookings_count; |
107 |
|
108 |
my $start_date = dt_from_string( $self->start_date ); |
109 |
my $end_date = dt_from_string( $self->end_date ); |
110 |
my $duration = $end_date->delta_days($start_date); |
111 |
|
112 |
my $delta_days = $duration->in_units('days'); |
113 |
|
114 |
return { status => 'bookingPeriodNotValid'} if $bookings_period_length == 0; |
115 |
return { status => 'tooLongBookingPeriod', limit => $bookings_period_length } if $delta_days > $bookings_period_length; |
116 |
|
117 |
return { status => 'OK' }; |
118 |
} |
119 |
|
35 |
=head3 biblio |
120 |
=head3 biblio |
36 |
|
121 |
|
37 |
Returns the related Koha::Biblio object for this booking |
122 |
Returns the related Koha::Biblio object for this booking |
Lines 139-144
sub store {
Link Here
|
139 |
|
224 |
|
140 |
# FIXME: We should be able to combine the above two functions into one |
225 |
# FIXME: We should be able to combine the above two functions into one |
141 |
|
226 |
|
|
|
227 |
my $canBeBooked = can_be_booked_in_advance( $self ); |
228 |
Koha::Exceptions::Booking::Rule->throw( $canBeBooked ) if $canBeBooked->{'status'} ne "OK"; |
229 |
|
142 |
# Assign item at booking time |
230 |
# Assign item at booking time |
143 |
if ( !$self->item_id ) { |
231 |
if ( !$self->item_id ) { |
144 |
$self->_assign_item_for_booking; |
232 |
$self->_assign_item_for_booking; |