Bug 10419: (followup) - tidy up 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     $confirm );
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     'c|confirm'              => \$confirm,
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 scalar(@$members) . " patrons to delete";
46
47 my $dbh = C4::Context->dbh;
48 $dbh->{RaiseError} = 1;
49 $dbh->{PrintError} = 0;
50
51 for my $member (@$members) {
52     print "Trying to delete patron " . $member->{borrowernumber} . "... ";
53     eval {
54         C4::Members::MoveMemberToDeleted( $member->{borrowernumber} )
55           if $confirm;
56     };
57     if ($@) {
58         say "Failed, cannot move this patron ($@)";
59         next;
60     }
61     eval { C4::Members::DelMember( $member->{borrowernumber} ) if $confirm; };
62     if ($@) {
63         say "Failed, cannot delete this patron ($@)";
64         next;
65     }
66     say "OK";
67 }
68
69 =head1 NAME
70
71 delete_patrons - This script deletes patrons
72
73 =head1 SYNOPSIS
74
75 delete_patrons.pl [-h -v -c] --not_borrowered_since=2013-07-21 --expired_before=2013-07-21 --category_code=CAT
76
77 dates can be generated with `date -d '-3 month' "+%Y-%m-%d"`
78
79 Options are cumulatives.
80
81 =head1 OPTIONS
82
83 =over
84
85 =item B<-h|--help>
86
87 Print a brief help message
88
89 =item B<--not_borrowered_since>
90
91 Delete patrons who have not borrowered since this date.
92
93 =item B<--expired_date>
94
95 Delete patrons with an account expired before this date.
96
97 =item B<--category_code>
98
99 Delete patrons who have this category code.
100
101 =item B<-c|--confirm>
102
103 Without this flag set, this script will do nothing.
104
105 =item B<-v|--verbose>
106
107 Verbose mode.
108
109 =back
110
111 =head1 AUTHOR
112
113 Jonathan Druart <jonathan.druart@biblibre.com>
114
115 =head1 COPYRIGHT
116
117 Copyright 2013 BibLibre
118
119 =head1 LICENSE
120
121 This file is part of Koha.
122
123 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
124 Foundation; either version 2 of the License, or (at your option) any later version.
125
126 You should have received a copy of the GNU General Public License along
127 with Koha; if not, write to the Free Software Foundation, Inc.,
128 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
129
130 =cut