Bug 13918 - Add waiting expiration date to opac list of holds for user
[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 =cut
113
114 sub is_cancelable {
115     my ($self) = @_;
116
117     return ( $self->is_waiting() && !$self->is_found() )
118       || ( !$self->is_waiting() && !$self->is_in_transit() );
119 }
120
121 =head3 is_at_destination
122
123 Returns true if hold is a in_transit hold
124
125 =cut
126
127 sub is_at_destination {
128     my ($self) = @_;
129
130     return $self->is_waiting() && ( $self->branchcode() eq $self->item()->holdingbranch() );
131 }
132
133 =head3 biblio
134
135 Returns the related Koha::Biblio object for this hold
136
137 =cut
138
139 sub biblio {
140     my ($self) = @_;
141
142     $self->{_biblio} ||= Koha::Biblios->find( $self->biblionumber() );
143
144     return $self->{_biblio};
145 }
146
147 =head3 item
148
149 Returns the related Koha::Item object for this Hold
150
151 =cut
152
153 sub item {
154     my ($self) = @_;
155
156     $self->{_item} ||= Koha::Items->find( $self->itemnumber() );
157
158     return $self->{_item};
159 }
160
161 =head3 branch
162
163 Returns the related Koha::Branch object for this Hold
164
165 =cut
166
167 sub branch {
168     my ($self) = @_;
169
170     $self->{_branch} ||= Koha::Branches->find( $self->branchcode() );
171
172     return $self->{_branch};
173 }
174
175 =head3 borrower
176
177 Returns the related Koha::Borrower object for this Hold
178
179 =cut
180
181 sub borrower {
182     my ($self) = @_;
183
184     $self->{_borrower} ||= Koha::Borrowers->find( $self->borrowernumber() );
185
186     return $self->{_borrower};
187 }
188
189 =head3 type
190
191 =cut
192
193 sub type {
194     return 'Reserve';
195 }
196
197 =head1 AUTHOR
198
199 Kyle M Hall <kyle@bywatersolutions.com>
200
201 =cut
202
203 1;