ffzg/recall_notices.pl: added --interval and --dedup
[koha.git] / ill / ill-requests.pl
index 28e8cc6..45f99c2 100755 (executable)
@@ -74,6 +74,7 @@ if ( $backends_available ) {
             csrf_token => Koha::Token->new->generate_csrf({
                 session_id => scalar $cgi->cookie('CGISESSID'),
             }),
+            ( $params->{error} ? ( error => $params->{error} ) : () ),
         );
 
     } elsif ( $op eq 'create' ) {
@@ -86,6 +87,38 @@ if ( $backends_available ) {
         );
         handle_commit_maybe($backend_result, $request);
 
+    } elsif ( $op eq 'migrate' ) {
+        # We're in the process of migrating a request
+        my $request = Koha::Illrequests->find($params->{illrequest_id});
+        my $backend_result;
+        if ( $params->{backend} ) {
+            my $new_request = Koha::Illrequest->new->load_backend( $params->{backend} );
+            $backend_result = $new_request->backend_migrate($params);
+            if ($backend_result) {
+                $template->param(
+                    whole   => $backend_result,
+                    request => $new_request
+                );
+                $request = $new_request;
+            } else {
+                # Backend failure, redirect back to illview
+                print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
+                      . '?method=illview'
+                      . '&illrequest_id='
+                      . $request->id
+                      . '&error=migrate_target' );
+                exit;
+            }
+        }
+        else {
+            $backend_result = $request->backend_migrate($params);
+            $template->param(
+                whole   => $backend_result,
+                request => $request
+            );
+        }
+        handle_commit_maybe( $backend_result, $request );
+
     } elsif ( $op eq 'confirm' ) {
         # Backend 'confirm' method
         # confirm requires a specific request, so first, find it.
@@ -129,8 +162,8 @@ if ( $backends_available ) {
                 value   => {}
             };
             $template->param(
-                whole   => $backend_result,
-                request => $request
+                whole          => $backend_result,
+                request        => $request
             );
         } else {
             # Commit:
@@ -141,6 +174,10 @@ if ( $backends_available ) {
             $request->price_paid($params->{price_paid});
             $request->notesopac($params->{notesopac});
             $request->notesstaff($params->{notesstaff});
+            my $alias = (length $params->{status_alias} > 0) ?
+                $params->{status_alias} :
+                "-1";
+            $request->status_alias($alias);
             $request->store;
             my $backend_result = {
                 error   => 0,
@@ -210,10 +247,10 @@ if ( $backends_available ) {
         }
         catch {
             my $error;
-            if ( $_->isa( 'Koha::Exceptions::Ill::NoTargetEmail' ) ) {
+            if ( ref($_) eq 'Koha::Exceptions::Ill::NoTargetEmail' ) {
                 $error = 'no_target_email';
             }
-            elsif ( $_->isa( 'Koha::Exceptions::Ill::NoLibraryEmail' ) ) {
+            elsif ( ref($_) eq 'Koha::Exceptions::Ill::NoLibraryEmail' ) {
                 $error = 'no_library_email';
             }
             else {
@@ -236,13 +273,12 @@ if ( $backends_available ) {
         my $active_filters = [];
         foreach my $filter(@{$possible_filters}) {
             if ($params->{$filter}) {
-                push @{$active_filters},
-                    { name => $filter, value => $params->{$filter}};
+                push @{$active_filters}, "$filter=$params->{$filter}";
             }
         }
         if (scalar @{$active_filters} > 0) {
             $template->param(
-                prefilters => $active_filters
+                prefilters => join(",", @{$active_filters})
             );
         }
 
@@ -287,16 +323,29 @@ output_html_with_http_headers( $cgi, $cookie, $template->output );
 
 sub handle_commit_maybe {
     my ( $backend_result, $request ) = @_;
+
     # We need to special case 'commit'
     if ( $backend_result->{stage} eq 'commit' ) {
         if ( $backend_result->{next} eq 'illview' ) {
+
             # Redirect to a view of the newly created request
-            print $cgi->redirect(
-                '/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id='.
-                $request->id
-            );
+            print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
+                  . '?method=illview'
+                  . '&illrequest_id='
+                  . $request->id );
             exit;
-        } else {
+        }
+        elsif ( $backend_result->{next} eq 'emigrate' ) {
+
+            # Redirect to a view of the newly created request
+            print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
+                  . '?method=migrate'
+                  . '&stage=emigrate'
+                  . '&illrequest_id='
+                  . $request->id );
+            exit;
+        }
+        else {
             # Redirect to a requests list view
             redirect_to_list();
         }