Bug 22770: DBRev 19.06.00.004
[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->SUPER::store();
53     $self->notify();
54     return $self;
55 }
56
57 =head3 process
58
59 =cut
60
61 sub process {
62     my ($self) = @_;
63
64     $self->status(Koha::ArticleRequest::Status::Processing);
65     $self->store();
66     $self->notify();
67     return $self;
68 }
69
70 =head3 complete
71
72 =cut
73
74 sub complete {
75     my ($self) = @_;
76
77     $self->status(Koha::ArticleRequest::Status::Completed);
78     $self->store();
79     $self->notify();
80     return $self;
81 }
82
83 =head3 cancel
84
85 =cut
86
87 sub cancel {
88     my ( $self, $notes ) = @_;
89
90     $self->status(Koha::ArticleRequest::Status::Canceled);
91     $self->notes($notes) if $notes;
92     $self->store();
93     $self->notify();
94     return $self;
95 }
96
97 =head3 notify
98
99 =cut
100
101 sub notify {
102     my ($self) = @_;
103
104     my $status = $self->status;
105
106     require C4::Letters;
107     if (
108         my $letter = C4::Letters::GetPreparedLetter(
109             module                 => 'circulation',
110             letter_code            => "AR_$status", # AR_PENDING, AR_PROCESSING, AR_COMPLETED, AR_CANCELED
111             message_transport_type => 'email',
112             lang                   => $self->borrower->lang,
113             tables                 => {
114                 article_requests => $self->id,
115                 borrowers        => $self->borrowernumber,
116                 biblio           => $self->biblionumber,
117                 biblioitems      => $self->biblionumber,
118                 items            => $self->itemnumber,
119                 branches         => $self->branchcode,
120             },
121         )
122       )
123     {
124         C4::Letters::EnqueueLetter(
125             {
126                 letter                 => $letter,
127                 borrowernumber         => $self->borrowernumber,
128                 message_transport_type => 'email',
129             }
130         ) or warn "can't enqueue letter $letter";
131     }
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 opan requests
193 will have notifications sent.
194
195 =cut
196
197 sub store {
198     my ($self) = @_;
199
200     if ( $self->in_storage() ) {
201         my $now = dt_from_string();
202         $self->updated_on($now);
203
204         return $self->SUPER::store();
205     }
206     else {
207         $self->open();
208         return $self->SUPER::store();
209     }
210 }
211
212 =head3 _type
213
214 =cut
215
216 sub _type {
217     return 'ArticleRequest';
218 }
219
220 =head1 AUTHOR
221
222 Kyle M Hall <kyle@bywatersolutions.com>
223
224 =cut
225
226 1;