Bug 21063: Add "Columns settings" for ILL
[koha.git] / Koha / Illrequest.pm
index 0b4d71c..7158aa5 100644 (file)
@@ -25,13 +25,17 @@ use File::Basename qw( basename );
 use Encode qw( encode );
 use Mail::Sendmail;
 use Try::Tiny;
+use DateTime;
 
 use Koha::Database;
 use Koha::Email;
 use Koha::Exceptions::Ill;
 use Koha::Illcomments;
 use Koha::Illrequestattributes;
+use Koha::AuthorisedValue;
+use Koha::Illrequest::Logger;
 use Koha::Patron;
+use Koha::AuthorisedValues;
 
 use base qw(Koha::Object);
 
@@ -109,6 +113,30 @@ available for request.
 
 =head2 Class methods
 
+=head3 statusalias
+
+    my $statusalias = $request->statusalias;
+
+Returns a request's status alias, as a Koha::AuthorisedValue instance
+or implicit undef. This is distinct from status_alias, which only returns
+the value in the status_alias column, this method returns the entire
+AuthorisedValue object
+
+=cut
+
+sub statusalias {
+    my ( $self ) = @_;
+    return unless $self->status_alias;
+    # We can't know which result is the right one if there are multiple
+    # ILLSTATUS authorised values with the same authorised_value column value
+    # so we just use the first
+    return Koha::AuthorisedValues->search({
+        branchcode => $self->branchcode,
+        category => 'ILLSTATUS',
+        authorised_value => $self->SUPER::status_alias
+    })->next;
+}
+
 =head3 illrequestattributes
 
 =cut
@@ -131,6 +159,16 @@ sub illcomments {
     );
 }
 
+=head3 logs
+
+=cut
+
+sub logs {
+    my ( $self ) = @_;
+    my $logger = Koha::Illrequest::Logger->new;
+    return $logger->get_request_logs($self);
+}
+
 =head3 patron
 
 =cut
@@ -142,6 +180,108 @@ sub patron {
     );
 }
 
+=head3 status_alias
+
+    $Illrequest->status_alias(143);
+
+Overloaded getter/setter for status_alias,
+that only returns authorised values from the
+correct category and records the fact that the status has changed
+
+=cut
+
+sub status_alias {
+    my ($self, $new_status_alias) = @_;
+
+    my $current_status_alias = $self->SUPER::status_alias;
+
+    if ($new_status_alias) {
+        # Keep a record of the previous status before we change it,
+        # we might need it
+        $self->{previous_status} = $current_status_alias ?
+            $current_status_alias :
+            scalar $self->status;
+        # This is hackery to enable us to undefine
+        # status_alias, since we need to have an overloaded
+        # status_alias method to get us around the problem described
+        # here:
+        # https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=20581#c156
+        # We need a way of accepting implied undef, so we can nullify
+        # the status_alias column, when called from $self->status
+        my $val = $new_status_alias eq "-1" ? undef : $new_status_alias;
+        my $ret = $self->SUPER::status_alias($val);
+        my $val_to_log = $val ? $new_status_alias : scalar $self->status;
+        if ($ret) {
+            my $logger = Koha::Illrequest::Logger->new;
+            $logger->log_status_change({
+                request => $self,
+                value   => $val_to_log
+            });
+        } else {
+            delete $self->{previous_status};
+        }
+        return $ret;
+    }
+    # We can't know which result is the right one if there are multiple
+    # ILLSTATUS authorised values with the same authorised_value column value
+    # so we just use the first
+    my $alias = Koha::AuthorisedValues->search({
+        branchcode => $self->branchcode,
+        category => 'ILLSTATUS',
+        authorised_value => $self->SUPER::status_alias
+    })->next;
+    if ($alias) {
+        return $alias->authorised_value;
+    } else {
+        return;
+    }
+}
+
+=head3 status
+
+    $Illrequest->status('CANREQ');
+
+Overloaded getter/setter for request status,
+also nullifies status_alias and records the fact that the status has changed
+
+=cut
+
+sub status {
+    my ( $self, $new_status) = @_;
+
+    my $current_status = $self->SUPER::status;
+    my $current_status_alias = $self->SUPER::status_alias;
+
+    if ($new_status) {
+        # Keep a record of the previous status before we change it,
+        # we might need it
+        $self->{previous_status} = $current_status_alias ?
+            $current_status_alias :
+            $current_status;
+        my $ret = $self->SUPER::status($new_status)->store;
+        if ($current_status_alias) {
+            # This is hackery to enable us to undefine
+            # status_alias, since we need to have an overloaded
+            # status_alias method to get us around the problem described
+            # here:
+            # https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=20581#c156
+            # We need a way of passing implied undef to nullify status_alias
+            # so we pass -1, which is special cased in the overloaded setter
+            $self->status_alias("-1");
+        } else {
+            my $logger = Koha::Illrequest::Logger->new;
+            $logger->log_status_change({
+                request => $self,
+                value   => $new_status
+            });
+        }
+        delete $self->{previous_status};
+        return $ret;
+    } else {
+        return $current_status;
+    }
+}
+
 =head3 load_backend
 
 Require "Base.pm" from the relevant ILL backend.
@@ -163,7 +303,10 @@ sub load_backend {
     my $location = join "/", @raw, $backend_name, "Base.pm";    # File to load
     my $backend_class = join "::", @raw, $backend_name, "Base"; # Package name
     require $location;
-    $self->{_my_backend} = $backend_class->new({ config => $self->_config });
+    $self->{_my_backend} = $backend_class->new({
+        config => $self->_config,
+        logger => Koha::Illrequest::Logger->new
+    });
     return $self;
 }
 
@@ -489,8 +632,8 @@ Return a list of available backends.
 =cut
 
 sub available_backends {
-    my ( $self ) = @_;
-    my $backends = $self->_config->available_backends;
+    my ( $self, $reduced ) = @_;
+    my $backends = $self->_config->available_backends($reduced);
     return $backends;
 }
 
@@ -517,6 +660,7 @@ Mark a request as completed (status = COMP).
 sub mark_completed {
     my ( $self ) = @_;
     $self->status('COMP')->store;
+    $self->completed(DateTime->now)->store;
     return {
         error   => 0,
         status  => '',
@@ -527,6 +671,23 @@ sub mark_completed {
     };
 }
 
+=head2 backend_migrate
+
+Migrate a request from one backend to another.
+
+=cut
+
+sub backend_migrate {
+    my ( $self, $params ) = @_;
+
+    my $response = $self->_backend_capability('migrate',{
+            request    => $self,
+            other      => $params,
+        });
+    return $self->expandTemplate($response) if $response;
+    return $response;
+}
+
 =head2 backend_confirm
 
 Confirm a request. The backend handles setting of mandatory fields in the commit stage:
@@ -1001,6 +1162,33 @@ sub _censor {
     return $params;
 }
 
+=head3 store
+
+    $Illrequest->store;
+
+Overloaded I<store> method that, in addition to performing the 'store',
+possibly records the fact that something happened
+
+=cut
+
+sub store {
+    my ( $self, $attrs ) = @_;
+
+    my $ret = $self->SUPER::store;
+
+    $attrs->{log_origin} = 'core';
+
+    if ($ret && defined $attrs) {
+        my $logger = Koha::Illrequest::Logger->new;
+        $logger->log_maybe({
+            request => $self,
+            attrs   => $attrs
+        });
+    }
+
+    return $ret;
+}
+
 =head3 TO_JSON
 
     $json = $illrequest->TO_JSON
@@ -1018,7 +1206,6 @@ sub TO_JSON {
     my ( $self, $embed ) = @_;
 
     my $object = $self->SUPER::TO_JSON();
-    $object->{id_prefix} = $self->id_prefix;
 
     return $object;
 }