Bug 17678: C4::Circulation - Replace GetIssues with Koha::Issues
[koha.git] / Koha / Hold.pm
blobba75a523b73becb58b3cf03e445d14ad8ee356c4
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;
23 use Data::Dumper qw(Dumper);
25 use C4::Context qw(preference);
26 use C4::Log;
28 use Koha::DateUtils qw(dt_from_string);
29 use Koha::Patrons;
30 use Koha::Biblios;
31 use Koha::Items;
32 use Koha::Libraries;
34 use base qw(Koha::Object);
36 =head1 NAME
38 Koha::Hold - Koha Hold object class
40 =head1 API
42 =head2 Class Methods
44 =cut
46 =head3 suspend_hold
48 my $hold = $hold->suspend_hold( $suspend_until_dt );
50 =cut
52 sub suspend_hold {
53 my ( $self, $dt ) = @_;
55 $dt = $dt ? $dt->clone()->truncate( to => 'day' ) : undef;
57 if ( $self->is_waiting ) { # We can't suspend waiting holds
58 carp "Unable to suspend waiting hold!";
59 return $self;
62 $self->suspend(1);
63 $self->suspend_until( $dt );
65 $self->store();
67 logaction( 'HOLDS', 'SUSPEND', $self->reserve_id, Dumper($self->unblessed) )
68 if C4::Context->preference('HoldsLog');
70 return $self;
73 =head3 resume
75 my $hold = $hold->resume();
77 =cut
79 sub resume {
80 my ( $self ) = @_;
82 $self->suspend(0);
83 $self->suspend_until( undef );
85 $self->store();
87 logaction( 'HOLDS', 'RESUME', $self->reserve_id, Dumper($self->unblessed) )
88 if C4::Context->preference('HoldsLog');
90 return $self;
93 =head3 delete
95 $hold->delete();
97 =cut
99 sub delete {
100 my ( $self ) = @_;
102 my $deleted = $self->SUPER::delete($self);
104 logaction( 'HOLDS', 'DELETE', $self->reserve_id, Dumper($self->unblessed) )
105 if C4::Context->preference('HoldsLog');
107 return $deleted;
110 =head3 waiting_expires_on
112 Returns a DateTime for the date a waiting holds expires on.
113 Returns undef if the system peference ReservesMaxPickUpDelay is not set.
114 Returns undef if the hold is not waiting ( found = 'W' ).
116 =cut
118 sub waiting_expires_on {
119 my ($self) = @_;
121 my $found = $self->found;
122 return unless $found && $found eq 'W';
124 my $ReservesMaxPickUpDelay = C4::Context->preference('ReservesMaxPickUpDelay');
125 return unless $ReservesMaxPickUpDelay;
127 my $dt = dt_from_string( $self->waitingdate() );
129 $dt->add( days => $ReservesMaxPickUpDelay );
131 return $dt;
134 =head3 is_found
136 Returns true if hold is a waiting or in transit
138 =cut
140 sub is_found {
141 my ($self) = @_;
143 return 0 unless $self->found();
144 return 1 if $self->found() eq 'W';
145 return 1 if $self->found() eq 'T';
148 =head3 is_waiting
150 Returns true if hold is a waiting hold
152 =cut
154 sub is_waiting {
155 my ($self) = @_;
157 my $found = $self->found;
158 return $found && $found eq 'W';
161 =head3 is_in_transit
163 Returns true if hold is a in_transit hold
165 =cut
167 sub is_in_transit {
168 my ($self) = @_;
170 return 0 unless $self->found();
171 return $self->found() eq 'T';
174 =head3 is_cancelable
176 Returns true if hold is a cancelable hold
178 Holds may be canceled if they not found, or
179 are found and waiting. A hold found but in
180 transit cannot be canceled.
182 =cut
184 sub is_cancelable {
185 my ($self) = @_;
187 return 1 unless $self->is_found();
188 return 0 if $self->is_in_transit();
189 return 1 if $self->is_waiting();
190 return 0;
193 =head3 is_at_destination
195 Returns true if hold is waiting
196 and the hold's pickup branch matches
197 the hold item's holding branch
199 =cut
201 sub is_at_destination {
202 my ($self) = @_;
204 return $self->is_waiting() && ( $self->branchcode() eq $self->item()->holdingbranch() );
207 =head3 biblio
209 Returns the related Koha::Biblio object for this hold
211 =cut
213 sub biblio {
214 my ($self) = @_;
216 $self->{_biblio} ||= Koha::Biblios->find( $self->biblionumber() );
218 return $self->{_biblio};
221 =head3 item
223 Returns the related Koha::Item object for this Hold
225 =cut
227 sub item {
228 my ($self) = @_;
230 $self->{_item} ||= Koha::Items->find( $self->itemnumber() );
232 return $self->{_item};
235 =head3 branch
237 Returns the related Koha::Library object for this Hold
239 =cut
241 sub branch {
242 my ($self) = @_;
244 $self->{_branch} ||= Koha::Libraries->find( $self->branchcode() );
246 return $self->{_branch};
249 =head3 borrower
251 Returns the related Koha::Patron object for this Hold
253 =cut
255 sub borrower {
256 my ($self) = @_;
258 $self->{_borrower} ||= Koha::Patrons->find( $self->borrowernumber() );
260 return $self->{_borrower};
263 =head3 is_suspended
265 my $bool = $hold->is_suspended();
267 =cut
269 sub is_suspended {
270 my ( $self ) = @_;
272 return $self->suspend();
275 =head3 type
277 =cut
279 sub _type {
280 return 'Reserve';
283 =head1 AUTHOR
285 Kyle M Hall <kyle@bywatersolutions.com>
287 =cut