Bug 16601: Update of italian MARC21 files
[koha.git] / misc / cronjobs / delete_patrons.pl
bloba76d1ddf2b3f5263a1fbf744e65ef746ae8d8233
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 C4::Log;
12 my ( $help, $verbose, $not_borrowed_since, $expired_before, $category_code,
13 $branchcode, $confirm );
14 GetOptions(
15 'h|help' => \$help,
16 'v|verbose' => \$verbose,
17 'not_borrowed_since:s' => \$not_borrowed_since,
18 'expired_before:s' => \$expired_before,
19 'category_code:s' => \$category_code,
20 'library:s' => \$branchcode,
21 'c|confirm' => \$confirm,
22 ) || pod2usage(1);
24 if ($help) {
25 pod2usage(1);
28 $not_borrowed_since = dt_from_string( $not_borrowed_since, 'iso' )
29 if $not_borrowed_since;
31 $expired_before = dt_from_string( $expired_before, 'iso' )
32 if $expired_before;
34 unless ( $not_borrowed_since or $expired_before or $category_code or $branchcode ) {
35 pod2usage(q{At least one filter is mandatory});
36 exit;
39 cronlogaction();
41 my $members = GetBorrowersToExpunge(
43 not_borrowed_since => $not_borrowed_since,
44 expired_before => $expired_before,
45 category_code => $category_code,
46 branchcode => $branchcode,
50 unless ($confirm) {
51 say "Doing a dry run; no patron records will actually be deleted.";
52 say "Run again with --confirm to delete the records.";
55 say scalar(@$members) . " patrons to delete";
57 my $dbh = C4::Context->dbh;
58 $dbh->{RaiseError} = 1;
59 $dbh->{PrintError} = 0;
61 $dbh->{AutoCommit} = 0; # use transactions to avoid partial deletes
62 my $deleted = 0;
63 for my $member (@$members) {
64 print "Trying to delete patron $member->{borrowernumber}... "
65 if $verbose;
67 my $borrowernumber = $member->{borrowernumber};
68 my $flags = C4::Members::patronflags( $member );
69 if ( my $charges = $flags->{CHARGES}{amount} ) {
70 say "Failed to delete patron $borrowernumber: patron has $charges in fines";
71 next;
74 eval {
75 C4::Members::MoveMemberToDeleted( $borrowernumber )
76 if $confirm;
78 if ($@) {
79 say "Failed to delete patron $borrowernumber, cannot move it: ($@)";
80 $dbh->rollback;
81 next;
83 eval {
84 C4::Members::HandleDelBorrower( $borrowernumber )
85 if $confirm;
87 if ($@) {
88 say "Failed to delete patron $borrowernumber, error handling its lists: ($@)";
89 $dbh->rollback;
90 next;
92 eval { C4::Members::DelMember( $borrowernumber ) if $confirm; };
93 if ($@) {
94 say "Failed to delete patron $borrowernumber: $@)";
95 $dbh->rollback;
96 next;
98 $dbh->commit;
99 $deleted++;
100 say "OK" if $verbose;
103 say "$deleted patrons deleted";
105 =head1 NAME
107 delete_patrons - This script deletes patrons
109 =head1 SYNOPSIS
111 delete_patrons.pl [-h|--help] [-v|--verbose] [-c|--confirm] [--not_borrowed_since=DATE] [--expired_before=DATE] [--category_code=CAT] [--library=LIBRARY]
113 Dates should be in ISO format, e.g., 2013-07-19, and can be generated
114 with `date -d '-3 month' "+%Y-%m-%d"`.
116 The options to select the patron records to delete are cumulative. For
117 example, supplying both --expired_before and --library specifies that
118 that patron records must meet both conditions to be selected for deletion.
120 =head1 OPTIONS
122 =over
124 =item B<-h|--help>
126 Print a brief help message
128 =item B<--not_borrowed_since>
130 Delete patrons who have not borrowed since this date.
132 =item B<--expired_before>
134 Delete patrons with an account expired before this date.
136 =item B<--category_code>
138 Delete patrons who have this category code.
140 =item B<--library>
142 Delete patrons in this library.
144 =item B<-c|--confirm>
146 This flag must be provided in order for the script to actually
147 delete patron records. If it is not supplied, the script will
148 only report on the patron records it would have deleted.
150 =item B<-v|--verbose>
152 Verbose mode.
154 =back
156 =head1 AUTHOR
158 Jonathan Druart <jonathan.druart@biblibre.com>
160 =head1 COPYRIGHT
162 Copyright 2013 BibLibre
164 =head1 LICENSE
166 This file is part of Koha.
168 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
169 Foundation; either version 3 of the License, or (at your option) any later version.
171 You should have received a copy of the GNU General Public License along
172 with Koha; if not, write to the Free Software Foundation, Inc.,
173 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
175 =cut