Bug 17663 - DBRev 16.06.00.048
[koha.git] / misc / cronjobs / delete_patrons.pl
index a1463b1..ee015a6 100755 (executable)
@@ -6,16 +6,18 @@ use Pod::Usage;
 use Getopt::Long;
 
 use C4::Members;
-use C4::VirtualShelves;
 use Koha::DateUtils;
+use Koha::Patrons;
+use C4::Log;
 
-my ( $help, $verbose, $not_borrowed_since, $expired_before, $category_code,
-    $branchcode, $confirm );
+my ( $help, $verbose, $not_borrowed_since, $expired_before, $last_seen,
+    $category_code, $branchcode, $confirm );
 GetOptions(
     'h|help'                 => \$help,
     'v|verbose'              => \$verbose,
     'not_borrowed_since:s'   => \$not_borrowed_since,
     'expired_before:s'       => \$expired_before,
+    'last_seen:s'            => \$last_seen,
     'category_code:s'        => \$category_code,
     'library:s'              => \$branchcode,
     'c|confirm'              => \$confirm,
@@ -31,15 +33,21 @@ $not_borrowed_since = dt_from_string( $not_borrowed_since, 'iso' )
 $expired_before = dt_from_string( $expired_before, 'iso' )
   if $expired_before;
 
-unless ( $not_borrowed_since or $expired_before or $category_code or $branchcode ) {
+if ( $last_seen and not C4::Context->preference('TrackLastPatronActivity') ) {
+    pod2usage(q{The --last_seen option cannot be used with TrackLastPatronActivity turned off});
+}
+
+unless ( $not_borrowed_since or $expired_before or $last_seen or $category_code or $branchcode ) {
     pod2usage(q{At least one filter is mandatory});
-    exit;
 }
 
+cronlogaction();
+
 my $members = GetBorrowersToExpunge(
     {
-        not_borrowered_since => $not_borrowed_since,
+        not_borrowed_since => $not_borrowed_since,
         expired_before       => $expired_before,
+        last_seen            => $last_seen,
         category_code        => $category_code,
         branchcode           => $branchcode,
     }
@@ -52,11 +60,6 @@ unless ($confirm) {
 
 say scalar(@$members) . " patrons to delete";
 
-my $dbh = C4::Context->dbh;
-$dbh->{RaiseError} = 1;
-$dbh->{PrintError} = 0;
-
-$dbh->{AutoCommit} = 0; # use transactions to avoid partial deletes
 my $deleted = 0;
 for my $member (@$members) {
     print "Trying to delete patron $member->{borrowernumber}... "
@@ -69,31 +72,20 @@ for my $member (@$members) {
         next;
     }
 
-    eval {
-        C4::Members::MoveMemberToDeleted( $borrowernumber )
-          if $confirm;
-    };
-    if ($@) {
-        say "Failed to delete patron $borrowernumber, cannot move it: ($@)";
-        $dbh->rollback;
-        next;
-    }
-    eval {
-        C4::VirtualShelves::HandleDelBorrower( $borrowernumber )
-          if $confirm;
-    };
-    if ($@) {
-        say "Failed to delete patron $borrowernumber, error handling its lists: ($@)";
-        $dbh->rollback;
-        next;
-    }
-    eval { C4::Members::DelMember( $borrowernumber ) if $confirm; };
-    if ($@) {
-        say "Failed to delete patron $borrowernumber: $@)";
-        $dbh->rollback;
-        next;
+    if ( $confirm ) {
+        my $patron = Koha::Patrons->find( $borrowernumber );
+        my $deleted = eval { $patron->move_to_deleted; };
+        if ($@ or not $deleted) {
+            say "Failed to delete patron $borrowernumber, cannot move it" . ( $@ ? ": ($@)" : "" );
+            next;
+        }
+
+        eval { $patron->delete };
+        if ($@) {
+            say "Failed to delete patron $borrowernumber: $@)";
+            next;
+        }
     }
-    $dbh->commit;
     $deleted++;
     say "OK" if $verbose;
 }
@@ -106,7 +98,7 @@ delete_patrons - This script deletes patrons
 
 =head1 SYNOPSIS
 
-delete_patrons.pl [-h|--help] [-v|--verbose] [-c|--confirm] [--not_borrowed_since=DATE] [--expired_before=DATE] [--category_code=CAT] [--library=LIBRARY]
+delete_patrons.pl [-h|--help] [-v|--verbose] [-c|--confirm] [--not_borrowed_since=DATE] [--expired_before=DATE] [--last-seen=DATE] [--category_code=CAT] [--library=LIBRARY]
 
 Dates should be in ISO format, e.g., 2013-07-19, and can be generated
 with `date -d '-3 month' "+%Y-%m-%d"`.
@@ -131,6 +123,12 @@ Delete patrons who have not borrowed since this date.
 
 Delete patrons with an account expired before this date.
 
+=item B<--last_seen>
+
+Delete patrons who have not been connected since this date.
+
+The system preference TrackLastPatronActivity must be enabled to use this option.
+
 =item B<--category_code>
 
 Delete patrons who have this category code.