Bug 27945: Add limit article request feature
[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
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     $self->SUPER::store();
61     $self->notify();
62     return $self;
63 }
64
65 =head3 set_pending
66
67     $article_request->set_pending;
68
69 Marks the article as pending. Send a notification if appropriate.
70
71 =cut
72
73 sub set_pending {
74     my ($self) = @_;
75
76     $self->status(Koha::ArticleRequest::Status::Pending);
77     $self->SUPER::store();
78     $self->notify();
79     return $self;
80 }
81
82 =head3 process
83
84     $article_request->process;
85
86 Marks the article as in process. Send a notification if appropriate.
87
88 =cut
89
90 sub process {
91     my ($self) = @_;
92
93     $self->status(Koha::ArticleRequest::Status::Processing);
94     $self->store();
95     $self->notify();
96     return $self;
97 }
98
99 =head3 complete
100
101     $article_request->complete;
102
103 Marks the article as completed. Send a notification if appropriate.
104
105 =cut
106
107 sub complete {
108     my ($self) = @_;
109
110     $self->status(Koha::ArticleRequest::Status::Completed);
111     $self->store();
112     $self->notify();
113     return $self;
114 }
115
116 =head3 cancel
117
118     $article_request->cancel;
119
120 Marks the article as cancelled. Send a notification if appropriate.
121
122 =cut
123
124 sub cancel {
125     my ( $self, $notes ) = @_;
126
127     $self->status(Koha::ArticleRequest::Status::Canceled);
128     $self->notes($notes) if $notes;
129     $self->store();
130     $self->notify();
131     return $self;
132 }
133
134 =head3 biblio
135
136 Returns the Koha::Biblio object for this article request
137
138 =cut
139
140 sub biblio {
141     my ($self) = @_;
142
143     $self->{_biblio} ||= Koha::Biblios->find( $self->biblionumber() );
144
145     return $self->{_biblio};
146 }
147
148 =head3 item
149
150 Returns the Koha::Item object for this article request
151
152 =cut
153
154 sub item {
155     my ($self) = @_;
156
157     $self->{_item} ||= Koha::Items->find( $self->itemnumber() );
158
159     return $self->{_item};
160 }
161
162 =head3 borrower
163
164 Returns the Koha::Patron object for this article request
165
166 =cut
167
168 sub borrower {
169     my ($self) = @_;
170
171     $self->{_borrower} ||= Koha::Patrons->find( $self->borrowernumber() );
172
173     return $self->{_borrower};
174 }
175
176 =head3 branch
177
178 Returns the Koha::Library object for this article request
179
180 =cut
181
182 sub branch {
183     my ($self) = @_;
184
185     $self->{_branch} ||= Koha::Libraries->find( $self->branchcode() );
186
187     return $self->{_branch};
188 }
189
190 =head3 store
191
192 Override the default store behavior so that new opac requests
193 will have notifications sent.
194
195 =cut
196
197 sub store {
198     my ($self) = @_;
199     if ( $self->in_storage ) {
200         return $self->SUPER::store;
201     } else {
202         $self->created_on( dt_from_string() );
203         return $self->request;
204     }
205 }
206
207 =head2 Internal methods
208
209 =head3 notify
210
211     $self->notify();
212
213 internal method to be called when changing an article request status.
214 If a letter exists for the new status, it enqueues it.
215
216 =cut
217
218 sub notify {
219     my ($self) = @_;
220
221     my $status = $self->status;
222
223     require C4::Letters;
224     if (
225         my $letter = C4::Letters::GetPreparedLetter(
226             module                 => 'circulation',
227             letter_code            => "AR_$status", # AR_REQUESTED, AR_PENDING, AR_PROCESSING, AR_COMPLETED, AR_CANCELED
228             message_transport_type => 'email',
229             lang                   => $self->borrower->lang,
230             tables                 => {
231                 article_requests => $self->id,
232                 borrowers        => $self->borrowernumber,
233                 biblio           => $self->biblionumber,
234                 biblioitems      => $self->biblionumber,
235                 items            => $self->itemnumber,
236                 branches         => $self->branchcode,
237             },
238         )
239       )
240     {
241         C4::Letters::EnqueueLetter(
242             {
243                 letter                 => $letter,
244                 borrowernumber         => $self->borrowernumber,
245                 message_transport_type => 'email',
246             }
247         ) or warn "can't enqueue letter ". $letter->{code};
248     }
249 }
250
251 =head3 _type
252
253 =cut
254
255 sub _type {
256     return 'ArticleRequest';
257 }
258
259 =head1 AUTHOR
260
261 Kyle M Hall <kyle@bywatersolutions.com>
262
263 =cut
264
265 1;