Bug 18478 - Unit tests
[koha.git] / Koha / Hold.pm
blobd708054c5a993fe4d7311434243cbb6cf9877c49
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 output_pref);
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 set_waiting
112 =cut
114 sub set_waiting {
115 my ( $self, $transferToDo ) = @_;
117 $self->priority(0);
119 if ($transferToDo) {
120 $self->found('T')->store();
121 return $self;
124 my $today = dt_from_string();
125 my $values = {
126 found => 'W',
127 waitingdate => $today->ymd,
130 my $requested_expiration;
131 if ($self->expirationdate) {
132 $requested_expiration = dt_from_string($self->expirationdate);
135 my $max_pickup_delay = C4::Context->preference("ReservesMaxPickUpDelay");
136 my $cancel_on_holidays = C4::Context->preference('ExpireReservesOnHolidays');
137 my $calendar = Koha::Calendar->new( branchcode => $self->branchcode );
139 my $expirationdate = $today->clone;
140 $expirationdate->add(days => $max_pickup_delay);
142 if ( C4::Context->preference("ExcludeHolidaysFromMaxPickUpDelay") ) {
143 $expirationdate = $calendar->days_forward( dt_from_string(), $max_pickup_delay );
146 # If patron's requested expiration date is prior to the
147 # calculated one, we keep the patron's one.
148 my $cmp = $requested_expiration ? DateTime->compare($requested_expiration, $expirationdate) : 0;
149 $values->{expirationdate} = $cmp == -1 ? $requested_expiration->ymd : $expirationdate->ymd;
151 $self->set($values)->store();
153 return $self;
156 =head3 is_found
158 Returns true if hold is a waiting or in transit
160 =cut
162 sub is_found {
163 my ($self) = @_;
165 return 0 unless $self->found();
166 return 1 if $self->found() eq 'W';
167 return 1 if $self->found() eq 'T';
170 =head3 is_waiting
172 Returns true if hold is a waiting hold
174 =cut
176 sub is_waiting {
177 my ($self) = @_;
179 my $found = $self->found;
180 return $found && $found eq 'W';
183 =head3 is_in_transit
185 Returns true if hold is a in_transit hold
187 =cut
189 sub is_in_transit {
190 my ($self) = @_;
192 return 0 unless $self->found();
193 return $self->found() eq 'T';
196 =head3 is_cancelable
198 Returns true if hold is a cancelable hold
200 Holds may be canceled if they not found, or
201 are found and waiting. A hold found but in
202 transit cannot be canceled.
204 =cut
206 sub is_cancelable {
207 my ($self) = @_;
209 return 1 unless $self->is_found();
210 return 0 if $self->is_in_transit();
211 return 1 if $self->is_waiting();
212 return 0;
215 =head3 is_at_destination
217 Returns true if hold is waiting
218 and the hold's pickup branch matches
219 the hold item's holding branch
221 =cut
223 sub is_at_destination {
224 my ($self) = @_;
226 return $self->is_waiting() && ( $self->branchcode() eq $self->item()->holdingbranch() );
229 =head3 biblio
231 Returns the related Koha::Biblio object for this hold
233 =cut
235 sub biblio {
236 my ($self) = @_;
238 $self->{_biblio} ||= Koha::Biblios->find( $self->biblionumber() );
240 return $self->{_biblio};
243 =head3 item
245 Returns the related Koha::Item object for this Hold
247 =cut
249 sub item {
250 my ($self) = @_;
252 $self->{_item} ||= Koha::Items->find( $self->itemnumber() );
254 return $self->{_item};
257 =head3 branch
259 Returns the related Koha::Library object for this Hold
261 =cut
263 sub branch {
264 my ($self) = @_;
266 $self->{_branch} ||= Koha::Libraries->find( $self->branchcode() );
268 return $self->{_branch};
271 =head3 borrower
273 Returns the related Koha::Patron object for this Hold
275 =cut
277 sub borrower {
278 my ($self) = @_;
280 $self->{_borrower} ||= Koha::Patrons->find( $self->borrowernumber() );
282 return $self->{_borrower};
285 =head3 is_suspended
287 my $bool = $hold->is_suspended();
289 =cut
291 sub is_suspended {
292 my ( $self ) = @_;
294 return $self->suspend();
297 =head3 type
299 =cut
301 sub _type {
302 return 'Reserve';
305 =head1 AUTHOR
307 Kyle M Hall <kyle@bywatersolutions.com>
309 =cut