Bug 17963: TT syntax for notices - Prove that AR_* are compatible
[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             tables                 => {
112                 article_requests => $self->id,
113                 borrowers        => $self->borrowernumber,
114                 biblio           => $self->biblionumber,
115                 biblioitems      => $self->biblionumber,
116                 items            => $self->itemnumber,
117                 branches         => $self->branchcode,
118             },
119         )
120       )
121     {
122         C4::Letters::EnqueueLetter(
123             {
124                 letter                 => $letter,
125                 borrowernumber         => $self->borrowernumber,
126                 message_transport_type => 'email',
127             }
128         ) or warn "can't enqueue letter $letter";
129     }
130 }
131
132 =head3 biblio
133
134 Returns the Koha::Biblio object for this article request
135
136 =cut
137
138 sub biblio {
139     my ($self) = @_;
140
141     $self->{_biblio} ||= Koha::Biblios->find( $self->biblionumber() );
142
143     return $self->{_biblio};
144 }
145
146 =head3 item
147
148 Returns the Koha::Item object for this article request
149
150 =cut
151
152 sub item {
153     my ($self) = @_;
154
155     $self->{_item} ||= Koha::Items->find( $self->itemnumber() );
156
157     return $self->{_item};
158 }
159
160 =head3 borrower
161
162 Returns the Koha::Patron object for this article request
163
164 =cut
165
166 sub borrower {
167     my ($self) = @_;
168
169     $self->{_borrower} ||= Koha::Patrons->find( $self->borrowernumber() );
170
171     return $self->{_borrower};
172 }
173
174 =head3 branch
175
176 Returns the Koha::Library object for this article request
177
178 =cut
179
180 sub branch {
181     my ($self) = @_;
182
183     $self->{_branch} ||= Koha::Libraries->find( $self->branchcode() );
184
185     return $self->{_branch};
186 }
187
188 =head3 store
189
190 Override the default store behavior so that new opan requests
191 will have notifications sent.
192
193 =cut
194
195 sub store {
196     my ($self) = @_;
197
198     if ( $self->in_storage() ) {
199         my $now = dt_from_string();
200         $self->updated_on($now);
201
202         return $self->SUPER::store();
203     }
204     else {
205         $self->open();
206         return $self->SUPER::store();
207     }
208 }
209
210 =head3 _type
211
212 =cut
213
214 sub _type {
215     return 'ArticleRequest';
216 }
217
218 =head1 AUTHOR
219
220 Kyle M Hall <kyle@bywatersolutions.com>
221
222 =cut
223
224 1;