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