Bug 10419: new cronjob for deleting patrons
[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
11 my ( $help, $verbose, $not_borrowered_since, $expired_before, $category_code,
12     $dryrun );
13 GetOptions(
14     'h|help'                 => \$help,
15     'v|verbose'              => \$verbose,
16     'not_borrowered_since:s' => \$not_borrowered_since,
17     'expired_before:s'       => \$expired_before,
18     'category_code:s'        => \$category_code,
19     'dry-run'                => \$dryrun,
20 ) || pod2usage(1);
21
22 if ($help) {
23     pod2usage(1);
24 }
25
26 $not_borrowered_since = dt_from_string( $not_borrowered_since, 'iso' )
27   if $not_borrowered_since;
28
29 $expired_before = dt_from_string( $expired_before, 'iso' )
30   if $expired_before;
31
32 unless ( $not_borrowered_since or $expired_before or $category_code ) {
33     pod2usage(q{At least one filter is mandatory});
34     exit;
35 }
36
37 my $members = GetBorrowersToExpunge(
38     {
39         not_borrowered_since => $not_borrowered_since,
40         expired_before       => $expired_before,
41         category_code        => $category_code,
42     }
43 );
44
45 say "I found " . scalar(@$members) . " patrons to delete";
46 for my $member (@$members) {
47     print "Trying to delete patron " . $member->{borrowernumber} . "... ";
48     eval {
49         C4::Members::MoveMemberToDeleted( $member->{borrowernumber} )
50           unless $dryrun;
51     };
52     if ($@) {
53         say "Failed, I cannot move this patron ($@)";
54         next;
55     }
56     eval { C4::Members::DelMember( $member->{borrowernumber} ) unless $dryrun; };
57     if ($@) {
58         say "Failed, I cannot delete this patron ($@)";
59         next;
60     }
61     say "OK";
62 }
63
64 =head1 NAME
65
66 delete_patrons - This script deletes patrons
67
68 =head1 SYNOPSIS
69
70 delete_patrons.pl [-h -v] --not_borrowered_since=`date -d '-3 month' "+%Y-%m-%d"` --expired_before=`date -d '-3 month' "+%Y-%m-%d"` --category_code=CAT
71
72 Options are cumulatives.
73
74 =head1 OPTIONS
75
76 =over
77
78 =item B<-h|--help>
79
80 Print a brief help message
81
82 =item B<--not_borrowered_since>
83
84 Delete patrons who have not borrowered since this date.
85
86 =item B<--expired_date>
87
88 Delete patrons with an account expired before this date.
89
90 =item B<--category_code>
91
92 Delete patrons who have this category code.
93
94 =item B<--dry-run>
95
96 Dry run mode. To use with the verbose mode.
97
98 =item B<-v|--verbose>
99
100 Verbose mode.
101
102 =back
103
104 =head1 AUTHOR
105
106 Jonathan Druart <jonathan.druart@biblibre.com>
107
108 =head1 COPYRIGHT
109
110 Copyright 2013 BibLibre
111
112 =head1 LICENSE
113
114 This file is part of Koha.
115
116 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
117 Foundation; either version 2 of the License, or (at your option) any later version.
118
119 You should have received a copy of the GNU General Public License along
120 with Koha; if not, write to the Free Software Foundation, Inc.,
121 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
122
123 =cut