Bug 28759: limit accessibility for "Manage API keys"
[koha-ffzg.git] / tools / cleanborrowers.pl
index ceb5722..add68a2 100755 (executable)
@@ -34,17 +34,15 @@ This script allows to do 2 things.
 use Modern::Perl;
 
 use CGI qw ( -utf8 );
-use C4::Auth;
-use C4::Output;
-use C4::Members;        # GetBorrowersWhoHavexxxBorrowed.
-use C4::Circulation;    # AnonymiseIssueHistory.
+use C4::Auth qw( get_template_and_user );
+use C4::Output qw( output_html_with_http_headers );
+use C4::Members qw( GetBorrowersToExpunge );
 use Koha::DateUtils qw( dt_from_string output_pref );
 use Koha::Patron::Categories;
 use Koha::Patrons;
-use Date::Calc qw/Today Add_Delta_YM/;
-use Koha::List::Patron;
+use Koha::List::Patron qw( GetPatronLists );
 
-my $cgi = new CGI;
+my $cgi = CGI->new;
 
 # Fetch the paramater list as a hash in scalar context:
 #  * returns paramater list as tied hash ref
@@ -78,7 +76,6 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
     {   template_name   => "tools/cleanborrowers.tt",
         query           => $cgi,
         type            => "intranet",
-        authnotrequired => 0,
         flagsrequired   => { tools => 'delete_anonymize_patrons', catalogue => 1 },
     }
 );
@@ -106,19 +103,17 @@ if ( $step == 2 ) {
     }
     _skip_borrowers_with_nonzero_balance($patrons_to_delete);
 
-    my $members_to_anonymize;
-    if ( $checkboxes{issue} ) {
-        if ( $branch eq '*' ) {
-            $members_to_anonymize = GetBorrowersWithIssuesHistoryOlderThan($last_issue_date);
-        } else {
-            $members_to_anonymize = GetBorrowersWithIssuesHistoryOlderThan($last_issue_date, $branch);
-        }
-    }
+    my $patrons_to_anonymize =
+        $checkboxes{issue}
+      ? $branch eq '*'
+          ? Koha::Patrons->search_patrons_to_anonymise( { before => $last_issue_date } )
+          : Koha::Patrons->search_patrons_to_anonymise( { before => $last_issue_date, library => $branch } )
+      : undef;
 
     $template->param(
         patrons_to_delete    => $patrons_to_delete,
-        patrons_to_anonymize => $members_to_anonymize,
-        patron_list_id       => $patron_list_id
+        patrons_to_anonymize => $patrons_to_anonymize,
+        patron_list_id       => $patron_list_id,
     );
 }
 
@@ -147,9 +142,9 @@ elsif ( $step == 3 ) {
         for ( my $i = 0 ; $i < $totalDel ; $i++ ) {
             $radio eq 'testrun' && last;
             my $borrowernumber = $patrons_to_delete->[$i]->{'borrowernumber'};
-            $radio eq 'trash' && Koha::Patrons->find($borrowernumber)->move_to_deleted;
-            C4::Members::HandleDelBorrower($borrowernumber);
-            DelMember($borrowernumber);
+            my $patron = Koha::Patrons->find($borrowernumber);
+            $radio eq 'trash' && $patron->move_to_deleted;
+            $patron->delete;
         }
         $template->param(
             do_delete => '1',
@@ -160,9 +155,9 @@ elsif ( $step == 3 ) {
     # Anonymising all members
     if ($do_anonym) {
         #FIXME: anonymisation errors are not handled
-        ($totalAno,my $anonymisation_error) = AnonymiseIssueHistory($last_issue_date);
+        my $rows = Koha::Patrons->search_patrons_to_anonymise( { before => $last_issue_date } )->anonymise_issue_history( { before => $last_issue_date } );
         $template->param(
-            do_anonym   => '1',
+            do_anonym   => $rows,
         );
     }
 
@@ -180,7 +175,7 @@ elsif ( $step == 3 ) {
     $template->param( patron_lists => [ @non_empty_lists ] );
 }
 
-my $patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['description']});
+my $patron_categories = Koha::Patron::Categories->search_with_library_limits({}, {order_by => ['description']});
 
 $template->param(
     step                   => $step,
@@ -199,7 +194,8 @@ sub _skip_borrowers_with_nonzero_balance {
     my $borrowers = shift;
     my $balance;
     @$borrowers = map {
-        (undef, undef, $balance) = GetMemberIssuesAndFines( $_->{borrowernumber} );
+        my $patron = Koha::Patrons->find( $_->{borrowernumber} );
+        my $balance = $patron->account->balance;
         (defined $balance && $balance != 0) ? (): ($_);
     } @$borrowers;
 }