Bug 14056: Small punctuation error in description for deleting a holiday
[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 C4::VirtualShelves;
10 use Koha::DateUtils;
11 use C4::Log;
12
13 my ( $help, $verbose, $not_borrowed_since, $expired_before, $category_code,
14     $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     'category_code:s'        => \$category_code,
21     'library:s'              => \$branchcode,
22     'c|confirm'              => \$confirm,
23 ) || pod2usage(1);
24
25 if ($help) {
26     pod2usage(1);
27 }
28
29 $not_borrowed_since = dt_from_string( $not_borrowed_since, 'iso' )
30   if $not_borrowed_since;
31
32 $expired_before = dt_from_string( $expired_before, 'iso' )
33   if $expired_before;
34
35 unless ( $not_borrowed_since or $expired_before or $category_code or $branchcode ) {
36     pod2usage(q{At least one filter is mandatory});
37     exit;
38 }
39
40 cronlogaction();
41
42 my $members = GetBorrowersToExpunge(
43     {
44         not_borrowered_since => $not_borrowed_since,
45         expired_before       => $expired_before,
46         category_code        => $category_code,
47         branchcode           => $branchcode,
48     }
49 );
50
51 unless ($confirm) {
52     say "Doing a dry run; no patron records will actually be deleted.";
53     say "Run again with --confirm to delete the records.";
54 }
55
56 say scalar(@$members) . " patrons to delete";
57
58 my $dbh = C4::Context->dbh;
59 $dbh->{RaiseError} = 1;
60 $dbh->{PrintError} = 0;
61
62 $dbh->{AutoCommit} = 0; # use transactions to avoid partial deletes
63 my $deleted = 0;
64 for my $member (@$members) {
65     print "Trying to delete patron $member->{borrowernumber}... "
66       if $verbose;
67
68     my $borrowernumber = $member->{borrowernumber};
69     my $flags = C4::Members::patronflags( $member );
70     if ( my $charges = $flags->{CHARGES}{amount} ) {
71         say "Failed to delete patron $borrowernumber: patron has $charges in fines";
72         next;
73     }
74
75     eval {
76         C4::Members::MoveMemberToDeleted( $borrowernumber )
77           if $confirm;
78     };
79     if ($@) {
80         say "Failed to delete patron $borrowernumber, cannot move it: ($@)";
81         $dbh->rollback;
82         next;
83     }
84     eval {
85         C4::VirtualShelves::HandleDelBorrower( $borrowernumber )
86           if $confirm;
87     };
88     if ($@) {
89         say "Failed to delete patron $borrowernumber, error handling its lists: ($@)";
90         $dbh->rollback;
91         next;
92     }
93     eval { C4::Members::DelMember( $borrowernumber ) if $confirm; };
94     if ($@) {
95         say "Failed to delete patron $borrowernumber: $@)";
96         $dbh->rollback;
97         next;
98     }
99     $dbh->commit;
100     $deleted++;
101     say "OK" if $verbose;
102 }
103
104 say "$deleted patrons deleted";
105
106 =head1 NAME
107
108 delete_patrons - This script deletes patrons
109
110 =head1 SYNOPSIS
111
112 delete_patrons.pl [-h|--help] [-v|--verbose] [-c|--confirm] [--not_borrowed_since=DATE] [--expired_before=DATE] [--category_code=CAT] [--library=LIBRARY]
113
114 Dates should be in ISO format, e.g., 2013-07-19, and can be generated
115 with `date -d '-3 month' "+%Y-%m-%d"`.
116
117 The options to select the patron records to delete are cumulative.  For
118 example, supplying both --expired_before and --library specifies that
119 that patron records must meet both conditions to be selected for deletion.
120
121 =head1 OPTIONS
122
123 =over
124
125 =item B<-h|--help>
126
127 Print a brief help message
128
129 =item B<--not_borrowed_since>
130
131 Delete patrons who have not borrowed since this date.
132
133 =item B<--expired_before>
134
135 Delete patrons with an account expired before this date.
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