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