Bug 17660: #adv is considered as an ad by adblock
[koha.git] / Koha / Hold.pm
blobf4d541a6e6efd3eac62ba4a9c110275df8db6593
1 package Koha::Hold;
3 # Copyright ByWater Solutions 2014
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use Modern::Perl;
22 use Carp;
24 use C4::Context qw(preference);
26 use Koha::DateUtils qw(dt_from_string);
27 use Koha::Patrons;
28 use Koha::Biblios;
29 use Koha::Items;
30 use Koha::Libraries;
32 use base qw(Koha::Object);
34 =head1 NAME
36 Koha::Hold - Koha Hold object class
38 =head1 API
40 =head2 Class Methods
42 =cut
44 =head3 suspend_hold
46 my $hold = $hold->suspend_hold( $suspend_until_dt );
48 =cut
50 sub suspend_hold {
51 my ( $self, $dt ) = @_;
53 $dt = $dt ? $dt->clone()->truncate( to => 'day' ) : undef;
55 if ( $self->is_waiting ) { # We can't suspend waiting holds
56 carp "Unable to suspend waiting hold!";
57 return $self;
60 $self->suspend(1);
61 $self->suspend_until( $dt );
63 $self->store();
65 return $self;
68 =head3 resume
70 my $hold = $hold->resume();
72 =cut
74 sub resume {
75 my ( $self ) = @_;
77 $self->suspend(0);
78 $self->suspend_until( undef );
80 $self->store();
82 return $self;
85 =head3 waiting_expires_on
87 Returns a DateTime for the date a waiting holds expires on.
88 Returns undef if the system peference ReservesMaxPickUpDelay is not set.
89 Returns undef if the hold is not waiting ( found = 'W' ).
91 =cut
93 sub waiting_expires_on {
94 my ($self) = @_;
96 my $found = $self->found;
97 return unless $found && $found eq 'W';
99 my $ReservesMaxPickUpDelay = C4::Context->preference('ReservesMaxPickUpDelay');
100 return unless $ReservesMaxPickUpDelay;
102 my $dt = dt_from_string( $self->waitingdate() );
104 $dt->add( days => $ReservesMaxPickUpDelay );
106 return $dt;
109 =head3 is_found
111 Returns true if hold is a waiting or in transit
113 =cut
115 sub is_found {
116 my ($self) = @_;
118 return 0 unless $self->found();
119 return 1 if $self->found() eq 'W';
120 return 1 if $self->found() eq 'T';
123 =head3 is_waiting
125 Returns true if hold is a waiting hold
127 =cut
129 sub is_waiting {
130 my ($self) = @_;
132 my $found = $self->found;
133 return $found && $found eq 'W';
136 =head3 is_in_transit
138 Returns true if hold is a in_transit hold
140 =cut
142 sub is_in_transit {
143 my ($self) = @_;
145 return 0 unless $self->found();
146 return $self->found() eq 'T';
149 =head3 is_cancelable
151 Returns true if hold is a cancelable hold
153 Holds may be canceled if they not found, or
154 are found and waiting. A hold found but in
155 transit cannot be canceled.
157 =cut
159 sub is_cancelable {
160 my ($self) = @_;
162 return 1 unless $self->is_found();
163 return 0 if $self->is_in_transit();
164 return 1 if $self->is_waiting();
165 return 0;
168 =head3 is_at_destination
170 Returns true if hold is waiting
171 and the hold's pickup branch matches
172 the hold item's holding branch
174 =cut
176 sub is_at_destination {
177 my ($self) = @_;
179 return $self->is_waiting() && ( $self->branchcode() eq $self->item()->holdingbranch() );
182 =head3 biblio
184 Returns the related Koha::Biblio object for this hold
186 =cut
188 sub biblio {
189 my ($self) = @_;
191 $self->{_biblio} ||= Koha::Biblios->find( $self->biblionumber() );
193 return $self->{_biblio};
196 =head3 item
198 Returns the related Koha::Item object for this Hold
200 =cut
202 sub item {
203 my ($self) = @_;
205 $self->{_item} ||= Koha::Items->find( $self->itemnumber() );
207 return $self->{_item};
210 =head3 branch
212 Returns the related Koha::Library object for this Hold
214 =cut
216 sub branch {
217 my ($self) = @_;
219 $self->{_branch} ||= Koha::Libraries->find( $self->branchcode() );
221 return $self->{_branch};
224 =head3 borrower
226 Returns the related Koha::Patron object for this Hold
228 =cut
230 sub borrower {
231 my ($self) = @_;
233 $self->{_borrower} ||= Koha::Patrons->find( $self->borrowernumber() );
235 return $self->{_borrower};
238 =head3 is_suspended
240 my $bool = $hold->is_suspended();
242 =cut
244 sub is_suspended {
245 my ( $self ) = @_;
247 return $self->suspend();
250 =head3 type
252 =cut
254 sub _type {
255 return 'Reserve';
258 =head1 AUTHOR
260 Kyle M Hall <kyle@bywatersolutions.com>
262 =cut