Bug 13918 [QA Followup] - Unit Tests
[koha.git] / Koha / Hold.pm
1 package Koha::Hold;
2
3 # Copyright ByWater Solutions 2014
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
15 #
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.
19
20 use Modern::Perl;
21
22 use Carp;
23
24 use C4::Context qw(preference);
25 use Koha::DateUtils qw(dt_from_string);
26
27 use Koha::Borrowers;
28 use Koha::Biblios;
29 use Koha::Branches;
30 use Koha::Items;
31
32 use base qw(Koha::Object);
33
34 =head1 NAME
35
36 Koha::Hold - Koha Hold object class
37
38 =head1 API
39
40 =head2 Class Methods
41
42 =cut
43
44 =head3 waiting_expires_on
45
46 Returns a DateTime for the date a waiting holds expires on.
47 Returns undef if the system peference ReservesMaxPickUpDelay is not set.
48 Returns undef if the hold is not waiting ( found = 'W' ).
49
50 =cut
51
52 sub waiting_expires_on {
53     my ($self) = @_;
54
55     my $found = $self->found;
56     return unless $found && $found eq 'W';
57
58     my $ReservesMaxPickUpDelay = C4::Context->preference('ReservesMaxPickUpDelay');
59     return unless $ReservesMaxPickUpDelay;
60
61     my $dt = dt_from_string( $self->waitingdate() );
62
63     $dt->add( days => $ReservesMaxPickUpDelay );
64
65     return $dt;
66 }
67
68 =head3 is_found
69
70 Returns true if hold is a waiting or in transit
71
72 =cut
73
74 sub is_found {
75     my ($self) = @_;
76
77     return 0 unless $self->found();
78     return 1 if $self->found() eq 'W';
79     return 1 if $self->found() eq 'T';
80 }
81
82 =head3 is_waiting
83
84 Returns true if hold is a waiting hold
85
86 =cut
87
88 sub is_waiting {
89     my ($self) = @_;
90
91     my $found = $self->found;
92     return $found && $found eq 'W';
93 }
94
95 =head3 is_in_transit
96
97 Returns true if hold is a in_transit hold
98
99 =cut
100
101 sub is_in_transit {
102     my ($self) = @_;
103
104     return 0 unless $self->found();
105     return $self->found() eq 'T';
106 }
107
108 =head3 is_cancelable
109
110 Returns true if hold is a cancelable hold
111
112 Holds may be canceled if they not found, or
113 are found and waiting. A hold found but in
114 transit cannot be canceled.
115
116 =cut
117
118 sub is_cancelable {
119     my ($self) = @_;
120
121     return 1 unless $self->is_found();
122     return 0 if $self->is_in_transit();
123     return 1 if $self->is_waiting();
124     return 0;
125 }
126
127 =head3 is_at_destination
128
129 Returns true if hold is waiting
130 and the hold's pickup branch matches
131 the hold item's holding branch
132
133 =cut
134
135 sub is_at_destination {
136     my ($self) = @_;
137
138     return $self->is_waiting() && ( $self->branchcode() eq $self->item()->holdingbranch() );
139 }
140
141 =head3 biblio
142
143 Returns the related Koha::Biblio object for this hold
144
145 =cut
146
147 sub biblio {
148     my ($self) = @_;
149
150     $self->{_biblio} ||= Koha::Biblios->find( $self->biblionumber() );
151
152     return $self->{_biblio};
153 }
154
155 =head3 item
156
157 Returns the related Koha::Item object for this Hold
158
159 =cut
160
161 sub item {
162     my ($self) = @_;
163
164     $self->{_item} ||= Koha::Items->find( $self->itemnumber() );
165
166     return $self->{_item};
167 }
168
169 =head3 branch
170
171 Returns the related Koha::Branch object for this Hold
172
173 =cut
174
175 sub branch {
176     my ($self) = @_;
177
178     $self->{_branch} ||= Koha::Branches->find( $self->branchcode() );
179
180     return $self->{_branch};
181 }
182
183 =head3 borrower
184
185 Returns the related Koha::Borrower object for this Hold
186
187 =cut
188
189 sub borrower {
190     my ($self) = @_;
191
192     $self->{_borrower} ||= Koha::Borrowers->find( $self->borrowernumber() );
193
194     return $self->{_borrower};
195 }
196
197 =head3 type
198
199 =cut
200
201 sub type {
202     return 'Reserve';
203 }
204
205 =head1 AUTHOR
206
207 Kyle M Hall <kyle@bywatersolutions.com>
208
209 =cut
210
211 1;