Bug 16891: Move C4::Members::MoveMemberToDeleted to Koha::Patron->move_to_deleted
[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 C4::Members;
9 use Koha::DateUtils;
10 use Koha::Patrons;
11 use C4::Log;
12
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);
25
26 if ($help) {
27     pod2usage(1);
28 }
29
30 $not_borrowed_since = dt_from_string( $not_borrowed_since, 'iso' )
31   if $not_borrowed_since;
32
33 $expired_before = dt_from_string( $expired_before, 'iso' )
34   if $expired_before;
35
36 if ( $last_seen and not C4::Context->preference('TrackLastPatronActivity') ) {
37     pod2usage(q{The --last_seen option cannot be used with TrackLastPatronActivity turned off});
38 }
39
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});
42 }
43
44 cronlogaction();
45
46 my $members = GetBorrowersToExpunge(
47     {
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,
53     }
54 );
55
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.";
59 }
60
61 say scalar(@$members) . " patrons to delete";
62
63 my $dbh = C4::Context->dbh;
64 $dbh->{RaiseError} = 1;
65 $dbh->{PrintError} = 0;
66
67 $dbh->{AutoCommit} = 0; # use transactions to avoid partial deletes
68 my $deleted = 0;
69 for my $member (@$members) {
70     print "Trying to delete patron $member->{borrowernumber}... "
71       if $verbose;
72
73     my $borrowernumber = $member->{borrowernumber};
74     my $flags = C4::Members::patronflags( $member );
75     if ( my $charges = $flags->{CHARGES}{amount} ) {
76         say "Failed to delete patron $borrowernumber: patron has $charges in fines";
77         next;
78     }
79
80     if ( $confirm ) {
81         my $deleted = eval {
82             Koha::Patrons->find( $borrowernumber )->move_to_deleted;
83         };
84         if ($@ or not $deleted) {
85             say "Failed to delete patron $borrowernumber, cannot move it" . ( $@ ? ": ($@)" : "" );
86             $dbh->rollback;
87             next;
88         }
89
90         eval {
91             C4::Members::HandleDelBorrower( $borrowernumber );
92         };
93         if ($@) {
94             say "Failed to delete patron $borrowernumber, error handling its lists: ($@)";
95             $dbh->rollback;
96             next;
97         }
98     }
99     eval { C4::Members::DelMember( $borrowernumber ) if $confirm; };
100     if ($@) {
101         say "Failed to delete patron $borrowernumber: $@)";
102         $dbh->rollback;
103         next;
104     }
105     $dbh->commit;
106     $deleted++;
107     say "OK" if $verbose;
108 }
109
110 say "$deleted patrons deleted";
111
112 =head1 NAME
113
114 delete_patrons - This script deletes patrons
115
116 =head1 SYNOPSIS
117
118 delete_patrons.pl [-h|--help] [-v|--verbose] [-c|--confirm] [--not_borrowed_since=DATE] [--expired_before=DATE] [--last-seen=DATE] [--category_code=CAT] [--library=LIBRARY]
119
120 Dates should be in ISO format, e.g., 2013-07-19, and can be generated
121 with `date -d '-3 month' "+%Y-%m-%d"`.
122
123 The options to select the patron records to delete are cumulative.  For
124 example, supplying both --expired_before and --library specifies that
125 that patron records must meet both conditions to be selected for deletion.
126
127 =head1 OPTIONS
128
129 =over
130
131 =item B<-h|--help>
132
133 Print a brief help message
134
135 =item B<--not_borrowed_since>
136
137 Delete patrons who have not borrowed since this date.
138
139 =item B<--expired_before>
140
141 Delete patrons with an account expired before this date.
142
143 =item B<--last_seen>
144
145 Delete patrons who have not been connected since this date.
146
147 The system preference TrackLastPatronActivity must be enabled to use this option.
148
149 =item B<--category_code>
150
151 Delete patrons who have this category code.
152
153 =item B<--library>
154
155 Delete patrons in this library.
156
157 =item B<-c|--confirm>
158
159 This flag must be provided in order for the script to actually
160 delete patron records.  If it is not supplied, the script will
161 only report on the patron records it would have deleted.
162
163 =item B<-v|--verbose>
164
165 Verbose mode.
166
167 =back
168
169 =head1 AUTHOR
170
171 Jonathan Druart <jonathan.druart@biblibre.com>
172
173 =head1 COPYRIGHT
174
175 Copyright 2013 BibLibre
176
177 =head1 LICENSE
178
179 This file is part of Koha.
180
181 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
182 Foundation; either version 3 of the License, or (at your option) any later version.
183
184 You should have received a copy of the GNU General Public License along
185 with Koha; if not, write to the Free Software Foundation, Inc.,
186 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
187
188 =cut