Bug 27944: Add missing Status module
[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 =cut
46
47 sub request {
48     my ($self) = @_;
49
50     $self->status(Koha::ArticleRequest::Status::Requested);
51     $self->SUPER::store();
52     $self->notify();
53     return $self;
54 }
55
56 =head3 open
57
58 =cut
59
60 sub open {
61     my ($self) = @_;
62
63     $self->status(Koha::ArticleRequest::Status::Pending);
64     $self->SUPER::store();
65     $self->notify();
66     return $self;
67 }
68
69 =head3 process
70
71 =cut
72
73 sub process {
74     my ($self) = @_;
75
76     $self->status(Koha::ArticleRequest::Status::Processing);
77     $self->store();
78     $self->notify();
79     return $self;
80 }
81
82 =head3 complete
83
84 =cut
85
86 sub complete {
87     my ($self) = @_;
88
89     $self->status(Koha::ArticleRequest::Status::Completed);
90     $self->store();
91     $self->notify();
92     return $self;
93 }
94
95 =head3 cancel
96
97 =cut
98
99 sub cancel {
100     my ( $self, $notes ) = @_;
101
102     $self->status(Koha::ArticleRequest::Status::Canceled);
103     $self->notes($notes) if $notes;
104     $self->store();
105     $self->notify();
106     return $self;
107 }
108
109 =head3 notify
110
111 =cut
112
113 sub notify {
114     my ($self) = @_;
115
116     my $status = $self->status;
117
118     require C4::Letters;
119     if (
120         my $letter = C4::Letters::GetPreparedLetter(
121             module                 => 'circulation',
122             letter_code            => "AR_$status", # AR_REQUESTED, AR_PENDING, AR_PROCESSING, AR_COMPLETED, AR_CANCELED
123             message_transport_type => 'email',
124             lang                   => $self->borrower->lang,
125             tables                 => {
126                 article_requests => $self->id,
127                 borrowers        => $self->borrowernumber,
128                 biblio           => $self->biblionumber,
129                 biblioitems      => $self->biblionumber,
130                 items            => $self->itemnumber,
131                 branches         => $self->branchcode,
132             },
133         )
134       )
135     {
136         C4::Letters::EnqueueLetter(
137             {
138                 letter                 => $letter,
139                 borrowernumber         => $self->borrowernumber,
140                 message_transport_type => 'email',
141             }
142         ) or warn "can't enqueue letter ". $letter->{code};
143     }
144 }
145
146 =head3 biblio
147
148 Returns the Koha::Biblio object for this article request
149
150 =cut
151
152 sub biblio {
153     my ($self) = @_;
154
155     $self->{_biblio} ||= Koha::Biblios->find( $self->biblionumber() );
156
157     return $self->{_biblio};
158 }
159
160 =head3 item
161
162 Returns the Koha::Item object for this article request
163
164 =cut
165
166 sub item {
167     my ($self) = @_;
168
169     $self->{_item} ||= Koha::Items->find( $self->itemnumber() );
170
171     return $self->{_item};
172 }
173
174 =head3 borrower
175
176 Returns the Koha::Patron object for this article request
177
178 =cut
179
180 sub borrower {
181     my ($self) = @_;
182
183     $self->{_borrower} ||= Koha::Patrons->find( $self->borrowernumber() );
184
185     return $self->{_borrower};
186 }
187
188 =head3 branch
189
190 Returns the Koha::Library object for this article request
191
192 =cut
193
194 sub branch {
195     my ($self) = @_;
196
197     $self->{_branch} ||= Koha::Libraries->find( $self->branchcode() );
198
199     return $self->{_branch};
200 }
201
202 =head3 store
203
204 Override the default store behavior so that new opac requests
205 will have notifications sent.
206
207 =cut
208
209 sub store {
210     my ($self) = @_;
211     if ( $self->in_storage ) {
212         return $self->SUPER::store;
213     } else {
214         $self->created_on( dt_from_string() );
215         return $self->request;
216     }
217 }
218
219 =head3 _type
220
221 =cut
222
223 sub _type {
224     return 'ArticleRequest';
225 }
226
227 =head1 AUTHOR
228
229 Kyle M Hall <kyle@bywatersolutions.com>
230
231 =cut
232
233 1;