Bug 20600: Add filtering of ILL requests in list
[koha.git] / misc / cronjobs / delete_patrons.pl
blobcb59465df49860a05f9dabd4e414f7dac5a66d77
1 #!/usr/bin/perl
3 use Modern::Perl;
5 use Pod::Usage;
6 use Getopt::Long;
8 use C4::Members;
9 use Koha::DateUtils;
10 use Koha::Patrons;
11 use C4::Log;
13 my ( $help, $verbose, $not_borrowed_since, $expired_before, $last_seen,
14 $category_code, $branchcode, $confirm );
15 GetOptions(
16 'h|help' => \$help,
17 'v|verbose' => \$verbose,
18 'not_borrowed_since:s' => \$not_borrowed_since,
19 'expired_before:s' => \$expired_before,
20 'last_seen:s' => \$last_seen,
21 'category_code:s' => \$category_code,
22 'library:s' => \$branchcode,
23 'c|confirm' => \$confirm,
24 ) || pod2usage(1);
26 if ($help) {
27 pod2usage(1);
30 $not_borrowed_since = dt_from_string( $not_borrowed_since, 'iso' )
31 if $not_borrowed_since;
33 $expired_before = dt_from_string( $expired_before, 'iso' )
34 if $expired_before;
36 if ( $last_seen and not C4::Context->preference('TrackLastPatronActivity') ) {
37 pod2usage(q{The --last_seen option cannot be used with TrackLastPatronActivity turned off});
40 unless ( $not_borrowed_since or $expired_before or $last_seen or $category_code or $branchcode ) {
41 pod2usage(q{At least one filter is mandatory});
44 cronlogaction();
46 my $members = GetBorrowersToExpunge(
48 not_borrowed_since => $not_borrowed_since,
49 expired_before => $expired_before,
50 last_seen => $last_seen,
51 category_code => $category_code,
52 branchcode => $branchcode,
56 unless ($confirm) {
57 say "Doing a dry run; no patron records will actually be deleted.";
58 say "Run again with --confirm to delete the records.";
61 say scalar(@$members) . " patrons to delete";
63 my $deleted = 0;
64 for my $member (@$members) {
65 print "Trying to delete patron $member->{borrowernumber}... "
66 if $verbose;
68 my $borrowernumber = $member->{borrowernumber};
69 my $patron = Koha::Patrons->find( $borrowernumber );
70 unless ( $patron ) {
71 say "Patron with borrowernumber $borrowernumber does not exist";
72 next;
74 if ( my $charges = $patron->account->non_issues_charges ) { # And what if we owe to this patron?
75 say "Failed to delete patron $borrowernumber: patron has $charges in fines";
76 next;
79 if ( $confirm ) {
80 my $deleted = eval { $patron->move_to_deleted; };
81 if ($@ or not $deleted) {
82 say "Failed to delete patron $borrowernumber, cannot move it" . ( $@ ? ": ($@)" : "" );
83 next;
86 eval { $patron->delete };
87 if ($@) {
88 say "Failed to delete patron $borrowernumber: $@)";
89 next;
92 $deleted++;
93 say "OK" if $verbose;
96 say "$deleted patrons deleted";
98 =head1 NAME
100 delete_patrons - This script deletes patrons
102 =head1 SYNOPSIS
104 delete_patrons.pl [-h|--help] [-v|--verbose] [-c|--confirm] [--not_borrowed_since=DATE] [--expired_before=DATE] [--last-seen=DATE] [--category_code=CAT] [--library=LIBRARY]
106 Dates should be in ISO format, e.g., 2013-07-19, and can be generated
107 with `date -d '-3 month' --iso-8601`.
109 The options to select the patron records to delete are cumulative. For
110 example, supplying both --expired_before and --library specifies that
111 that patron records must meet both conditions to be selected for deletion.
113 =head1 OPTIONS
115 =over
117 =item B<-h|--help>
119 Print a brief help message
121 =item B<--not_borrowed_since>
123 Delete patrons who have not borrowed since this date.
125 =item B<--expired_before>
127 Delete patrons with an account expired before this date.
129 =item B<--last_seen>
131 Delete patrons who have not been connected since this date.
133 The system preference TrackLastPatronActivity must be enabled to use this option.
135 =item B<--category_code>
137 Delete patrons who have this category code.
139 =item B<--library>
141 Delete patrons in this library.
143 =item B<-c|--confirm>
145 This flag must be provided in order for the script to actually
146 delete patron records. If it is not supplied, the script will
147 only report on the patron records it would have deleted.
149 =item B<-v|--verbose>
151 Verbose mode.
153 =back
155 =head1 AUTHOR
157 Jonathan Druart <jonathan.druart@biblibre.com>
159 =head1 COPYRIGHT
161 Copyright 2013 BibLibre
163 =head1 LICENSE
165 This file is part of Koha.
167 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
168 Foundation; either version 3 of the License, or (at your option) any later version.
170 You should have received a copy of the GNU General Public License along
171 with Koha; if not, write to the Free Software Foundation, Inc.,
172 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
174 =cut