Bug 19289: Add existing fields to the ACQ framework
[koha.git] / Koha / ArticleRequest.pm
blobf540b802daad00eb9c1bcde07865497c4dc269c5
1 package Koha::ArticleRequest;
3 # Copyright ByWater Solutions 2015
5 # This file is part of Koha.
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.
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.
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.
20 use Modern::Perl;
22 use Carp;
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);
32 use base qw(Koha::Object);
34 =head1 NAME
36 Koha::ArticleRequest - Koha Article Request Object class
38 =head1 API
40 =head2 Class Methods
42 =cut
44 =head3 open
46 =cut
48 sub open {
49 my ($self) = @_;
51 $self->status(Koha::ArticleRequest::Status::Pending);
52 $self->SUPER::store();
53 $self->notify();
54 return $self;
57 =head3 process
59 =cut
61 sub process {
62 my ($self) = @_;
64 $self->status(Koha::ArticleRequest::Status::Processing);
65 $self->store();
66 $self->notify();
67 return $self;
70 =head3 complete
72 =cut
74 sub complete {
75 my ($self) = @_;
77 $self->status(Koha::ArticleRequest::Status::Completed);
78 $self->store();
79 $self->notify();
80 return $self;
83 =head3 cancel
85 =cut
87 sub cancel {
88 my ( $self, $notes ) = @_;
90 $self->status(Koha::ArticleRequest::Status::Canceled);
91 $self->notes($notes) if $notes;
92 $self->store();
93 $self->notify();
94 return $self;
97 =head3 notify
99 =cut
101 sub notify {
102 my ($self) = @_;
104 my $status = $self->status;
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,
124 C4::Letters::EnqueueLetter(
126 letter => $letter,
127 borrowernumber => $self->borrowernumber,
128 message_transport_type => 'email',
130 ) or warn "can't enqueue letter $letter";
134 =head3 biblio
136 Returns the Koha::Biblio object for this article request
138 =cut
140 sub biblio {
141 my ($self) = @_;
143 $self->{_biblio} ||= Koha::Biblios->find( $self->biblionumber() );
145 return $self->{_biblio};
148 =head3 item
150 Returns the Koha::Item object for this article request
152 =cut
154 sub item {
155 my ($self) = @_;
157 $self->{_item} ||= Koha::Items->find( $self->itemnumber() );
159 return $self->{_item};
162 =head3 borrower
164 Returns the Koha::Patron object for this article request
166 =cut
168 sub borrower {
169 my ($self) = @_;
171 $self->{_borrower} ||= Koha::Patrons->find( $self->borrowernumber() );
173 return $self->{_borrower};
176 =head3 branch
178 Returns the Koha::Library object for this article request
180 =cut
182 sub branch {
183 my ($self) = @_;
185 $self->{_branch} ||= Koha::Libraries->find( $self->branchcode() );
187 return $self->{_branch};
190 =head3 store
192 Override the default store behavior so that new opan requests
193 will have notifications sent.
195 =cut
197 sub store {
198 my ($self) = @_;
200 if ( $self->in_storage() ) {
201 my $now = dt_from_string();
202 $self->updated_on($now);
204 return $self->SUPER::store();
206 else {
207 $self->open();
208 return $self->SUPER::store();
212 =head3 _type
214 =cut
216 sub _type {
217 return 'ArticleRequest';
220 =head1 AUTHOR
222 Kyle M Hall <kyle@bywatersolutions.com>
224 =cut