Bug 6506: When AnonymousPatron not set, deletion of issue history silently failed.
[koha.git] / misc / cronjobs / batch_anonymise.pl
1 #!/usr/bin/perl
2
3 # Copyright 2011, ByWater Solutions.
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 strict;
21 use warnings;
22 use Carp;
23
24 BEGIN {
25
26     # find Koha's Perl modules
27     # test carefully before changing this
28     use FindBin;
29     eval { require "$FindBin::Bin/../kohalib.pl" };
30 }
31
32 use C4::Context;
33 use C4::Circulation;
34 use C4::Dates;
35 use Date::Calc qw(
36   Today
37   Add_Delta_Days
38 );
39 use Getopt::Long;
40
41 sub usage {
42     print STDERR <<USAGE;
43 Usage: $0  --days DAYS  [-h|--help]
44    --days DAYS        (MANDATORY) anonymise patron history that is older than DAYS days.
45    -v --verbose       gives a little more information
46    -h --help          prints this help message, and exits, ignoring all
47                       other options
48 USAGE
49     exit $_[0];
50 }
51
52 my ( $help, $days, $verbose );
53
54 GetOptions(
55     'h|help'       => \$help,
56     'days:i'       => \$days,
57     'v|verbose'    => \$verbose,
58 ) || usage(1);
59
60 if ($help) {
61     usage(0);
62 }
63
64 if ( !$days  ) {
65     print "The days parameter is mandatory.\n\n";
66     usage(1);
67 }
68
69 my ($year,$month,$day) = Today();
70 my ($newyear,$newmonth,$newday) = Add_Delta_Days ($year,$month,$day,(-1)*$days);
71 my $formatdate = sprintf "%4d-%02d-%02d",$newyear,$newmonth,$newday;
72 $verbose and print "Checkouts before $formatdate will be anonymised.\n";
73
74 my ($rows, $err_history_not_deleted) = AnonymiseIssueHistory($formatdate);
75 carp "Anonymisation of reading history failed." if ($err_history_not_deleted);
76 $verbose and print "$rows checkouts anonymised.\n";
77
78 exit(0);