b88b9b6fc9f2fdda294be3d44f5927ce36e5329e
[koha-equinox.git] / misc / cronjobs / delete_expired_opac_registrations.pl
1 #!/usr/bin/perl
2
3 # Copyright 2009-2010 Kyle Hall
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use Modern::Perl;
21 use Getopt::Long;
22
23 BEGIN {
24
25     # find Koha's Perl modules
26     # test carefully before changing this
27     use FindBin;
28     eval { my $lib = "$FindBin::Bin/../kohalib.pl"; require $lib };
29 }
30
31 use C4::Context;
32 use C4::Members qw/ DelMember /;
33
34 my $help;
35 my $confirm;
36 my $verbose;
37
38 GetOptions(
39     'h|help'    => \$help,
40     'c|confirm' => \$confirm,
41     'v|verbose' => \$verbose,
42 );
43 my $usage = << 'ENDUSAGE';
44
45 This script removes confirmed OPAC based patron registrations
46 that have not been changed from the patron category specified
47 in the system preference PatronSelfRegistrationDefaultCategory
48 within the required time period.
49
50 NOTE: If you do not use self registration, do NOT run this script.
51 If you use self registration, but you do not use a temporary
52 category for new registrations, do NOT run this script too.
53
54 This script has the following parameters :
55     -c --confirm: Without this flag set, this script will do nothing.
56     -h --help:    Print this message.
57     -v --verbose: Print number of removed patrons.
58
59 ENDUSAGE
60
61 if ( $help || !$confirm ) {
62     print $usage;
63     exit;
64 }
65
66 # Delete accounts that haven't been upgraded from the 'temporary' category code
67 my $delay =
68   C4::Context->preference('PatronSelfRegistrationExpireTemporaryAccountsDelay');
69 my $category_code =
70   C4::Context->preference('PatronSelfRegistrationDefaultCategory');
71
72 die "PatronSelfRegistrationExpireTemporaryAccountsDelay and PatronSelfRegistrationDefaultCategory should be filled to use this script!"
73     if not $category_code or not defined $delay or $delay eq q||;
74
75 my $query = "
76     SELECT borrowernumber
77     FROM borrowers
78     WHERE
79         categorycode = ?
80       AND
81         DATEDIFF( NOW(), dateenrolled ) > ?
82 ";
83
84 my $dbh = C4::Context->dbh;
85 my $sth = $dbh->prepare($query);
86 $sth->execute( $category_code, $delay );
87
88 my $cnt=0;
89 while ( my ($borrowernumber) = $sth->fetchrow_array() ) {
90     DelMember($borrowernumber);
91     $cnt++;
92 }
93 print "Removed $cnt expired self-registered borrowers in category $category_code\n" if $verbose;