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
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.
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.
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>.
22 use Koha::Account::Lines;
28 use Koha::DateUtils qw( dt_from_string );
29 use Koha::ArticleRequest::Status;
30 use Koha::Exceptions::ArticleRequest;
32 use base qw(Koha::Object);
36 Koha::ArticleRequest - Koha Article Request Object class
46 $article_request->request;
48 Marks the article as requested. Send a notification if appropriate.
55 Koha::Exceptions::ArticleRequest::LimitReached->throw(
56 error => 'Patron cannot request more articles for today'
57 ) unless $self->borrower->can_request_article;
59 $self->status(Koha::ArticleRequest::Status::Requested);
61 # Handle possible fees
62 my $debit = $self->borrower->add_article_request_fee_if_needed({ item_id => $self->itemnumber });
63 $self->debit_id( $debit->id )
67 $self->patron->update_lastseen('article');
74 $article_request->set_pending;
76 Marks the article as pending. Send a notification if appropriate.
83 $self->status(Koha::ArticleRequest::Status::Pending);
91 $article_request->process;
93 Marks the article as in process. Send a notification if appropriate.
100 $self->status(Koha::ArticleRequest::Status::Processing);
108 $article_request->complete;
110 Marks the article as completed. Send a notification if appropriate.
117 $self->status(Koha::ArticleRequest::Status::Completed);
125 $article_request->cancel;
127 Marks the article as cancelled. Send a notification if appropriate.
132 my ( $self, $params ) = @_;
134 my $cancellation_reason = $params->{cancellation_reason};
135 my $notes = $params->{notes};
137 $self->status(Koha::ArticleRequest::Status::Canceled);
138 $self->cancellation_reason($cancellation_reason) if $cancellation_reason;
139 $self->notes($notes) if $notes;
143 my $debit = $self->debit;
147 my $account = $self->borrower->account;
149 my $total_reversible = $debit->debit_offsets->filter_by_reversible->total;
150 if ( $total_reversible ) {
152 $account->add_credit(
154 amount => abs $total_reversible,
155 interface => C4::Context->interface,
161 if ( $debit->amountoutstanding ) {
163 reduction_type => 'REFUND',
164 amount => $debit->amountoutstanding,
165 interface => C4::Context->interface,
175 Returns the Koha::Biblio object for this article request
182 my $rs = $self->_result->biblionumber;
184 return Koha::Biblio->_new_from_dbic($rs);
189 my $debit = $article_request->debit;
191 Returns the related Koha::Account::Line object for this article request
198 my $debit_rs = $self->_result->debit;
199 return unless $debit_rs;
201 return Koha::Account::Line->_new_from_dbic( $debit_rs );
206 Returns the Koha::Item object for this article request
212 my $rs = $self->_result->itemnumber;
214 return Koha::Item->_new_from_dbic($rs);
219 Returns the Koha::Patron object for this article request
225 my $rs = $self->_result->borrowernumber;
227 return Koha::Patron->_new_from_dbic($rs);
232 Returns the Koha::Patron object for this article request
238 my $rs = $self->_result->borrowernumber;
240 return Koha::Patron->_new_from_dbic($rs);
245 Returns the Koha::Library object for this article request
251 my $rs = $self->_result->branchcode;
253 return Koha::Library->_new_from_dbic($rs);
258 Override the default store behavior so that new opac requests
259 will have notifications sent.
266 if ( !$self->in_storage ) {
267 $self->created_on( dt_from_string() );
270 return $self->SUPER::store;
273 =head2 Internal methods
279 internal method to be called when changing an article request status.
280 If a letter exists for the new status, it enqueues it.
287 my $status = $self->status;
288 my $reason = $self->notes;
289 if ( !defined $reason && $self->cancellation_reason ) {
290 my $av = Koha::AuthorisedValues->search(
292 category => 'AR_CANCELLATION',
293 authorised_value => $self->cancellation_reason
296 $reason = $av->lib_opac ? $av->lib_opac : $av->lib if $av;
301 my $letter = C4::Letters::GetPreparedLetter(
302 module => 'circulation',
303 letter_code => "AR_$status", # AR_REQUESTED, AR_PENDING, AR_PROCESSING, AR_COMPLETED, AR_CANCELED
304 message_transport_type => 'email',
305 lang => $self->borrower->lang,
307 article_requests => $self->id,
308 borrowers => $self->borrowernumber,
309 biblio => $self->biblionumber,
310 biblioitems => $self->biblionumber,
311 items => $self->itemnumber,
312 branches => $self->branchcode,
320 C4::Letters::EnqueueLetter(
323 borrowernumber => $self->borrowernumber,
324 message_transport_type => 'email',
326 ) or warn "can't enqueue letter " . $letter->{code};
335 return 'ArticleRequest';
340 Kyle M Hall <kyle@bywatersolutions.com>