Bug 11175: (follow-up) Don't return explicitly undef
[koha-ffzg.git] / Koha / ArticleRequest.pm
index dd7b997..be4a895 100644 (file)
@@ -19,15 +19,15 @@ package Koha::ArticleRequest;
 
 use Modern::Perl;
 
-use Carp;
 
 use Koha::Database;
 use Koha::Patrons;
 use Koha::Biblios;
 use Koha::Items;
 use Koha::Libraries;
+use Koha::DateUtils qw( dt_from_string );
 use Koha::ArticleRequest::Status;
-use Koha::DateUtils qw(dt_from_string);
+use Koha::Exceptions::ArticleRequest;
 
 use base qw(Koha::Object);
 
@@ -37,25 +37,54 @@ Koha::ArticleRequest - Koha Article Request Object class
 
 =head1 API
 
-=head2 Class Methods
+=head2 Class methods
 
 =cut
 
-=head3 open
+=head3 request
+
+    $article_request->request;
+
+Marks the article as requested. Send a notification if appropriate.
 
 =cut
 
-sub open {
+sub request {
+    my ($self) = @_;
+
+    Koha::Exceptions::ArticleRequest::LimitReached->throw(
+        error => 'Patron cannot request more articles for today'
+    ) unless $self->borrower->can_request_article;
+
+    $self->status(Koha::ArticleRequest::Status::Requested);
+    $self->store();
+    $self->notify();
+    return $self;
+}
+
+=head3 set_pending
+
+    $article_request->set_pending;
+
+Marks the article as pending. Send a notification if appropriate.
+
+=cut
+
+sub set_pending {
     my ($self) = @_;
 
     $self->status(Koha::ArticleRequest::Status::Pending);
-    $self->SUPER::store();
+    $self->store();
     $self->notify();
     return $self;
 }
 
 =head3 process
 
+    $article_request->process;
+
+Marks the article as in process. Send a notification if appropriate.
+
 =cut
 
 sub process {
@@ -69,6 +98,10 @@ sub process {
 
 =head3 complete
 
+    $article_request->complete;
+
+Marks the article as completed. Send a notification if appropriate.
+
 =cut
 
 sub complete {
@@ -82,55 +115,26 @@ sub complete {
 
 =head3 cancel
 
+    $article_request->cancel;
+
+Marks the article as cancelled. Send a notification if appropriate.
+
 =cut
 
 sub cancel {
-    my ( $self, $notes ) = @_;
+    my ( $self, $params ) = @_;
+
+    my $cancellation_reason = $params->{cancellation_reason};
+    my $notes = $params->{notes};
 
     $self->status(Koha::ArticleRequest::Status::Canceled);
+    $self->cancellation_reason($cancellation_reason) if $cancellation_reason;
     $self->notes($notes) if $notes;
     $self->store();
     $self->notify();
     return $self;
 }
 
-=head3 notify
-
-=cut
-
-sub notify {
-    my ($self) = @_;
-
-    my $status = $self->status;
-
-    require C4::Letters;
-    if (
-        my $letter = C4::Letters::GetPreparedLetter(
-            module                 => 'circulation',
-            letter_code            => "AR_$status", # AR_PENDING, AR_PROCESSING, AR_COMPLETED, AR_CANCELED
-            message_transport_type => 'email',
-            lang                   => $self->borrower->lang,
-            tables                 => {
-                article_requests => $self->id,
-                borrowers        => $self->borrowernumber,
-                biblio           => $self->biblionumber,
-                biblioitems      => $self->biblionumber,
-                items            => $self->itemnumber,
-                branches         => $self->branchcode,
-            },
-        )
-      )
-    {
-        C4::Letters::EnqueueLetter(
-            {
-                letter                 => $letter,
-                borrowernumber         => $self->borrowernumber,
-                message_transport_type => 'email',
-            }
-        ) or warn "can't enqueue letter $letter";
-    }
-}
-
 =head3 biblio
 
 Returns the Koha::Biblio object for this article request
@@ -189,7 +193,7 @@ sub branch {
 
 =head3 store
 
-Override the default store behavior so that new opan requests
+Override the default store behavior so that new opac requests
 will have notifications sent.
 
 =cut
@@ -197,15 +201,67 @@ will have notifications sent.
 sub store {
     my ($self) = @_;
 
-    if ( $self->in_storage() ) {
-        my $now = dt_from_string();
-        $self->updated_on($now);
+    if ( !$self->in_storage ) {
+        $self->created_on( dt_from_string() );
+    }
+
+    return $self->SUPER::store;
+}
+
+=head2 Internal methods
 
-        return $self->SUPER::store();
+=head3 notify
+
+    $self->notify();
+
+internal method to be called when changing an article request status.
+If a letter exists for the new status, it enqueues it.
+
+=cut
+
+sub notify {
+    my ($self) = @_;
+
+    my $status = $self->status;
+    my $reason = $self->notes;
+    if ( !defined $reason && $self->cancellation_reason ) {
+        my $av = Koha::AuthorisedValues->search(
+            {
+                category            => 'AR_CANCELLATION',
+                authorised_value    => $self->cancellation_reason
+            }
+        )->next;
+        $reason = $av->lib_opac ? $av->lib_opac : $av->lib if $av;
     }
-    else {
-        $self->open();
-        return $self->SUPER::store();
+
+    require C4::Letters;
+    if (
+        my $letter = C4::Letters::GetPreparedLetter(
+            module                 => 'circulation',
+            letter_code            => "AR_$status", # AR_REQUESTED, AR_PENDING, AR_PROCESSING, AR_COMPLETED, AR_CANCELED
+            message_transport_type => 'email',
+            lang                   => $self->borrower->lang,
+            tables                 => {
+                article_requests => $self->id,
+                borrowers        => $self->borrowernumber,
+                biblio           => $self->biblionumber,
+                biblioitems      => $self->biblionumber,
+                items            => $self->itemnumber,
+                branches         => $self->branchcode,
+            },
+            substitute => {
+                reason => $reason,
+            },
+        )
+      )
+    {
+        C4::Letters::EnqueueLetter(
+            {
+                letter                 => $letter,
+                borrowernumber         => $self->borrowernumber,
+                message_transport_type => 'email',
+            }
+        ) or warn "can't enqueue letter " . $letter->{code};
     }
 }