Lines 24-33
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 base qw(Koha::Object); |
35 |
use base qw(Koha::Object); |
|
|
36 |
use List::Util qw(min); |
31 |
|
37 |
|
32 |
=head1 NAME |
38 |
=head1 NAME |
33 |
|
39 |
|
Lines 37-42
Koha::Booking - Koha Booking object class
Link Here
|
37 |
|
43 |
|
38 |
=head2 Class methods |
44 |
=head2 Class methods |
39 |
|
45 |
|
|
|
46 |
=head2 can_be_booked_in_advance |
47 |
|
48 |
$canBeBooked = &can_be_booked_in_advance($patron, $item, $branchcode) |
49 |
if ($canBeBooked->{status} eq 'OK') { #We can booked this Item in advance! } |
50 |
|
51 |
@RETURNS { status => OK }, if the Item can be booked. |
52 |
{ status => tooManyBookings, limit => $limit, rule => $rule }, if the borrower has exceeded their maximum booking amount. |
53 |
{ status => tooLongBookingPeriod, limit => $limit }, if the borrower has exceeded their maximum booking period. |
54 |
{ status => bookingPeriodNotValid }, if booking period is not valid (undef or equal to 0). |
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} || undef; |
90 |
my $bookings_per_item = $rights->{bookings_per_item} || undef; |
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 |
|
40 |
=head3 biblio |
136 |
=head3 biblio |
41 |
|
137 |
|
42 |
Returns the related Koha::Biblio object for this booking |
138 |
Returns the related Koha::Biblio object for this booking |
Lines 144-149
sub store {
Link Here
|
144 |
|
240 |
|
145 |
# FIXME: We should be able to combine the above two functions into one |
241 |
# FIXME: We should be able to combine the above two functions into one |
146 |
|
242 |
|
|
|
243 |
my $canBeBooked = can_be_booked_in_advance( $self ); |
244 |
Koha::Exceptions::Booking::Rule->throw( $canBeBooked ) if $canBeBooked->{'status'} ne "OK"; |
245 |
|
147 |
# Assign item at booking time |
246 |
# Assign item at booking time |
148 |
if ( !$self->item_id ) { |
247 |
if ( !$self->item_id ) { |
149 |
$self->_assign_item_for_booking; |
248 |
$self->_assign_item_for_booking; |