Bug 23049: (QA follow-up) Correction for missed archived filter
[koha-ffzg.git] / members / merge-patrons.pl
index 8ad2cfb..b4ea721 100755 (executable)
@@ -19,6 +19,7 @@
 use Modern::Perl;
 
 use CGI qw ( -utf8 );
+use Try::Tiny;
 
 use C4::Auth;
 use C4::Output;
@@ -33,22 +34,36 @@ my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
         query           => $cgi,
         type            => "intranet",
         authnotrequired => 0,
-        flagsrequired   => { borrowers => 1 },
-        debug           => 1,
+        flagsrequired   => { borrowers => 'edit_borrowers' },
     }
 );
 
 my $action = $cgi->param('action') || 'show';
-my @ids = $cgi->multi_param('id');
+my @ids    = $cgi->multi_param('id');
 
 if ( $action eq 'show' ) {
-    my $patrons =
-      Koha::Patrons->search( { borrowernumber => { -in => \@ids } } );
+    my $patrons = Koha::Patrons->search({ borrowernumber => { -in => \@ids } });
     $template->param( patrons => $patrons );
 } elsif ( $action eq 'merge' ) {
-    my $keeper = $cgi->param('keeper');
-    my $results = Koha::Patrons->merge( { keeper => $keeper, patrons => \@ids } );
-    $template->param( results => $results );
+    my $keeper_id = $cgi->param('keeper');
+    my $results;
+
+    my $keeper = Koha::Patrons->find( $keeper_id );
+
+    if ( $keeper ) {
+        try {
+            $results = $keeper->merge_with( \@ids );
+            $template->param(
+                keeper  => $keeper,
+                results => $results
+            );
+        }
+        catch {
+            $template->param( error => $_ );
+        }
+    } else {
+        $template->param( error => 'INVALID_KEEPER' );
+    }
 }
 
 $template->param( action => $action );