use C4::Members;
use Koha::DateUtils;
+use Koha::Patrons;
+use C4::Log;
-my ( $help, $verbose, $not_borrowered_since, $expired_before, $category_code,
- $confirm );
+my ( $help, $verbose, $not_borrowed_since, $expired_before, $last_seen,
+ $category_code, $branchcode, $confirm );
GetOptions(
'h|help' => \$help,
'v|verbose' => \$verbose,
- 'not_borrowered_since:s' => \$not_borrowered_since,
+ '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,
) || pod2usage(1);
pod2usage(1);
}
-$not_borrowered_since = dt_from_string( $not_borrowered_since, 'iso' )
- if $not_borrowered_since;
+$not_borrowed_since = dt_from_string( $not_borrowed_since, 'iso' )
+ if $not_borrowed_since;
$expired_before = dt_from_string( $expired_before, 'iso' )
if $expired_before;
-unless ( $not_borrowered_since or $expired_before or $category_code ) {
+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_borrowered_since,
+ not_borrowed_since => $not_borrowed_since,
expired_before => $expired_before,
+ last_seen => $last_seen,
category_code => $category_code,
+ branchcode => $branchcode,
}
);
-say scalar(@$members) . " patrons to delete";
+unless ($confirm) {
+ say "Doing a dry run; no patron records will actually be deleted.";
+ say "Run again with --confirm to delete the records.";
+}
-my $dbh = C4::Context->dbh;
-$dbh->{RaiseError} = 1;
-$dbh->{PrintError} = 0;
+say scalar(@$members) . " patrons to delete";
+my $deleted = 0;
for my $member (@$members) {
- print "Trying to delete patron " . $member->{borrowernumber} . "... ";
- eval {
- C4::Members::MoveMemberToDeleted( $member->{borrowernumber} )
- if $confirm;
- };
- if ($@) {
- say "Failed, cannot move this patron ($@)";
+ print "Trying to delete patron $member->{borrowernumber}... "
+ if $verbose;
+
+ my $borrowernumber = $member->{borrowernumber};
+ my $flags = C4::Members::patronflags( $member );
+ if ( my $charges = $flags->{CHARGES}{amount} ) {
+ say "Failed to delete patron $borrowernumber: patron has $charges in fines";
next;
}
- eval { C4::Members::DelMember( $member->{borrowernumber} ) if $confirm; };
- if ($@) {
- say "Failed, cannot delete this patron ($@)";
- 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;
+ }
}
- say "OK";
+ $deleted++;
+ say "OK" if $verbose;
}
+say "$deleted patrons deleted";
+
=head1 NAME
delete_patrons - This script deletes patrons
=head1 SYNOPSIS
-delete_patrons.pl [-h -v -c] --not_borrowered_since=2013-07-21 --expired_before=2013-07-21 --category_code=CAT
+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 can be generated with `date -d '-3 month' "+%Y-%m-%d"`
+Dates should be in ISO format, e.g., 2013-07-19, and can be generated
+with `date -d '-3 month' "+%Y-%m-%d"`.
-Options are cumulatives.
+The options to select the patron records to delete are cumulative. For
+example, supplying both --expired_before and --library specifies that
+that patron records must meet both conditions to be selected for deletion.
=head1 OPTIONS
Print a brief help message
-=item B<--not_borrowered_since>
+=item B<--not_borrowed_since>
-Delete patrons who have not borrowered since this date.
+Delete patrons who have not borrowed since this date.
-=item B<--expired_date>
+=item B<--expired_before>
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.
+=item B<--library>
+
+Delete patrons in this library.
+
=item B<-c|--confirm>
-Without this flag set, this script will do nothing.
+This flag must be provided in order for the script to actually
+delete patron records. If it is not supplied, the script will
+only report on the patron records it would have deleted.
=item B<-v|--verbose>
This file is part of Koha.
Koha is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
-Foundation; either version 2 of the License, or (at your option) any later version.
+Foundation; either version 3 of the License, or (at your option) any later version.
You should have received a copy of the GNU General Public License along
with Koha; if not, write to the Free Software Foundation, Inc.,