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