9 use C4::VirtualShelves;
12 my ( $help, $verbose, $not_borrowed_since, $expired_before, $category_code,
13 $branchcode, $confirm );
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,
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' )
34 unless ( $not_borrowed_since or $expired_before or $category_code or $branchcode ) {
35 pod2usage(q{At least one filter is mandatory});
39 my $members = GetBorrowersToExpunge(
41 not_borrowered_since => $not_borrowed_since,
42 expired_before => $expired_before,
43 category_code => $category_code,
44 branchcode => $branchcode,
49 say "Doing a dry run; no patron records will actually be deleted.";
50 say "Run again with --confirm to delete the records.";
53 say scalar(@$members) . " patrons to delete";
55 my $dbh = C4::Context->dbh;
56 $dbh->{RaiseError} = 1;
57 $dbh->{PrintError} = 0;
59 $dbh->{AutoCommit} = 0; # use transactions to avoid partial deletes
61 for my $member (@$members) {
62 print "Trying to delete patron $member->{borrowernumber}... "
65 my $borrowernumber = $member->{borrowernumber};
66 my $flags = C4::Members::patronflags( $member );
67 if ( my $charges = $flags->{CHARGES}{amount} ) {
68 say "Failed to delete patron $borrowernumber: patron has $charges in fines";
73 C4::Members::MoveMemberToDeleted( $borrowernumber )
77 say "Failed to delete patron $borrowernumber, cannot move it: ($@)";
82 C4::VirtualShelves::HandleDelBorrower( $borrowernumber )
86 say "Failed to delete patron $borrowernumber, error handling its lists: ($@)";
90 eval { C4::Members::DelMember( $borrowernumber ) if $confirm; };
92 say "Failed to delete patron $borrowernumber: $@)";
101 say "$deleted patrons deleted";
105 delete_patrons - This script deletes patrons
109 delete_patrons.pl [-h -v -c] --not_borrowed_since=2013-07-21 --expired_before=2013-07-21 --category_code=CAT --library=CPL
111 dates can be generated with `date -d '-3 month' "+%Y-%m-%d"`
113 Options are cumulatives.
121 Print a brief help message
123 =item B<--not_borrowed_since>
125 Delete patrons who have not borrowed since this date.
127 =item B<--expired_before>
129 Delete patrons with an account expired before this date.
131 =item B<--category_code>
133 Delete patrons who have this category code.
137 Delete patrons in this library.
139 =item B<-c|--confirm>
141 Without this flag set, this script will do nothing.
143 =item B<-v|--verbose>
151 Jonathan Druart <jonathan.druart@biblibre.com>
155 Copyright 2013 BibLibre
159 This file is part of Koha.
161 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
162 Foundation; either version 3 of the License, or (at your option) any later version.
164 You should have received a copy of the GNU General Public License along
165 with Koha; if not, write to the Free Software Foundation, Inc.,
166 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.