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