Bug 27946: Make Koha::ArticleRequest->request add a fee if required
[koha.git] / Koha / ArticleRequest.pm
1 package Koha::ArticleRequest;
2
3 # Copyright ByWater Solutions 2015
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use Koha::Account::Lines;
23 use Koha::Database;
24 use Koha::Patrons;
25 use Koha::Biblios;
26 use Koha::Items;
27 use Koha::Libraries;
28 use Koha::DateUtils qw( dt_from_string );
29 use Koha::ArticleRequest::Status;
30 use Koha::Exceptions::ArticleRequest;
31
32 use base qw(Koha::Object);
33
34 =head1 NAME
35
36 Koha::ArticleRequest - Koha Article Request Object class
37
38 =head1 API
39
40 =head2 Class methods
41
42 =cut
43
44 =head3 request
45
46     $article_request->request;
47
48 Marks the article as requested. Send a notification if appropriate.
49
50 =cut
51
52 sub request {
53     my ($self) = @_;
54
55     Koha::Exceptions::ArticleRequest::LimitReached->throw(
56         error => 'Patron cannot request more articles for today'
57     ) unless $self->borrower->can_request_article;
58
59     $self->status(Koha::ArticleRequest::Status::Requested);
60
61     # Handle possible fees
62     my $debit = $self->borrower->add_article_request_fee_if_needed({ item_id => $self->itemnumber });
63     $self->debit_id( $debit->id )
64         if $debit;
65
66     $self->store();
67     $self->notify();
68     return $self;
69 }
70
71 =head3 set_pending
72
73     $article_request->set_pending;
74
75 Marks the article as pending. Send a notification if appropriate.
76
77 =cut
78
79 sub set_pending {
80     my ($self) = @_;
81
82     $self->status(Koha::ArticleRequest::Status::Pending);
83     $self->store();
84     $self->notify();
85     return $self;
86 }
87
88 =head3 process
89
90     $article_request->process;
91
92 Marks the article as in process. Send a notification if appropriate.
93
94 =cut
95
96 sub process {
97     my ($self) = @_;
98
99     $self->status(Koha::ArticleRequest::Status::Processing);
100     $self->store();
101     $self->notify();
102     return $self;
103 }
104
105 =head3 complete
106
107     $article_request->complete;
108
109 Marks the article as completed. Send a notification if appropriate.
110
111 =cut
112
113 sub complete {
114     my ($self) = @_;
115
116     $self->status(Koha::ArticleRequest::Status::Completed);
117     $self->store();
118     $self->notify();
119     return $self;
120 }
121
122 =head3 cancel
123
124     $article_request->cancel;
125
126 Marks the article as cancelled. Send a notification if appropriate.
127
128 =cut
129
130 sub cancel {
131     my ( $self, $params ) = @_;
132
133     my $cancellation_reason = $params->{cancellation_reason};
134     my $notes = $params->{notes};
135
136     $self->status(Koha::ArticleRequest::Status::Canceled);
137     $self->cancellation_reason($cancellation_reason) if $cancellation_reason;
138     $self->notes($notes) if $notes;
139     $self->store();
140     $self->notify();
141     return $self;
142 }
143
144 =head3 biblio
145
146 Returns the Koha::Biblio object for this article request
147
148 =cut
149
150 sub biblio {
151     my ($self) = @_;
152
153     $self->{_biblio} ||= Koha::Biblios->find( $self->biblionumber() );
154
155     return $self->{_biblio};
156 }
157
158 =head3 debit
159
160     my $debit = $article_request->debit;
161
162 Returns the related Koha::Account::Line object for this article request
163
164 =cut
165
166 sub debit {
167     my ($self) = @_;
168
169     my $debit_rs = $self->_result->debit;
170     return unless $debit_rs;
171
172     return Koha::Account::Line->_new_from_dbic( $debit_rs );
173 }
174
175 =head3 item
176
177 Returns the Koha::Item object for this article request
178
179 =cut
180
181 sub item {
182     my ($self) = @_;
183
184     $self->{_item} ||= Koha::Items->find( $self->itemnumber() );
185
186     return $self->{_item};
187 }
188
189 =head3 borrower
190
191 Returns the Koha::Patron object for this article request
192
193 =cut
194
195 sub borrower {
196     my ($self) = @_;
197
198     $self->{_borrower} ||= Koha::Patrons->find( $self->borrowernumber() );
199
200     return $self->{_borrower};
201 }
202
203 =head3 branch
204
205 Returns the Koha::Library object for this article request
206
207 =cut
208
209 sub branch {
210     my ($self) = @_;
211
212     $self->{_branch} ||= Koha::Libraries->find( $self->branchcode() );
213
214     return $self->{_branch};
215 }
216
217 =head3 store
218
219 Override the default store behavior so that new opac requests
220 will have notifications sent.
221
222 =cut
223
224 sub store {
225     my ($self) = @_;
226
227     if ( !$self->in_storage ) {
228         $self->created_on( dt_from_string() );
229     }
230
231     return $self->SUPER::store;
232 }
233
234 =head2 Internal methods
235
236 =head3 notify
237
238     $self->notify();
239
240 internal method to be called when changing an article request status.
241 If a letter exists for the new status, it enqueues it.
242
243 =cut
244
245 sub notify {
246     my ($self) = @_;
247
248     my $status = $self->status;
249     my $reason = $self->notes;
250     if ( !defined $reason && $self->cancellation_reason ) {
251         my $av = Koha::AuthorisedValues->search(
252             {
253                 category            => 'AR_CANCELLATION',
254                 authorised_value    => $self->cancellation_reason
255             }
256         )->next;
257         $reason = $av->lib_opac ? $av->lib_opac : $av->lib if $av;
258     }
259
260     require C4::Letters;
261     if (
262         my $letter = C4::Letters::GetPreparedLetter(
263             module                 => 'circulation',
264             letter_code            => "AR_$status", # AR_REQUESTED, AR_PENDING, AR_PROCESSING, AR_COMPLETED, AR_CANCELED
265             message_transport_type => 'email',
266             lang                   => $self->borrower->lang,
267             tables                 => {
268                 article_requests => $self->id,
269                 borrowers        => $self->borrowernumber,
270                 biblio           => $self->biblionumber,
271                 biblioitems      => $self->biblionumber,
272                 items            => $self->itemnumber,
273                 branches         => $self->branchcode,
274             },
275             substitute => {
276                 reason => $reason,
277             },
278         )
279       )
280     {
281         C4::Letters::EnqueueLetter(
282             {
283                 letter                 => $letter,
284                 borrowernumber         => $self->borrowernumber,
285                 message_transport_type => 'email',
286             }
287         ) or warn "can't enqueue letter " . $letter->{code};
288     }
289 }
290
291 =head3 _type
292
293 =cut
294
295 sub _type {
296     return 'ArticleRequest';
297 }
298
299 =head1 AUTHOR
300
301 Kyle M Hall <kyle@bywatersolutions.com>
302
303 =cut
304
305 1;