2f950b5c6de164c2d317958c5eef6fdbd151d94c
[koha.git] / misc / maintenance / pseudonymize_statistics.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19 use Getopt::Long qw( GetOptions );
20 use Pod::Usage   qw( pod2usage );
21
22 use Koha::DateUtils qw( dt_from_string format_sqldatetime );
23 use Koha::Script;
24 use Koha::Statistics;
25 use Koha::PseudonymizedTransactions;
26
27 use C4::Context;
28
29 my ( $help, $verbose, $before );
30 my $result = GetOptions(
31     'h|help'     => \$help,
32     'v|verbose'  => \$verbose,
33     'b|before:s' => \$before,
34 ) || pod2usage(1);
35
36 if ($help) {
37     pod2usage(0);
38 }
39
40 unless ( C4::Context->preference('Pseudonymization') ) {
41     die "The system preference for Pseudonymization is not enabled, no action will be taken";
42 }
43
44 $before //= format_sqldatetime( dt_from_string(), 'sql', undef, 1 );
45 print "Searching for statistics before $before\n" if $verbose;
46
47 my $statistics = Koha::Statistics->search( { datetime => { '<=' => $before } } );
48 print $statistics->count() . " statistics found\n" if $verbose;
49
50
51 my $existing_pseudo_stats = Koha::PseudonymizedTransactions->search( { datetime => { '<=' => $before } } )->count;
52
53 if ( $statistics->count && $existing_pseudo_stats ) {
54     print "There are "
55         . $statistics->count()
56         . " statistics found, and $existing_pseudo_stats already in the database for the date provided.\n";
57     print "You may have already run this script for the time period given.\n";
58     print "Please enter 'Y' if you would like to continue:";
59     chomp( my $continue = <> );
60     exit unless uc($continue) eq 'Y';
61 }
62
63
64 while ( my $statistic = $statistics->next ) {
65     $statistic->pseudonymize();
66 }
67
68 print $statistics->count() . " statistics pseudonymized\n" if $verbose;
69
70 =head1 NAME
71
72 pseudonymize_statistics - This script pseudonymizes statistics before a given date, or now if no date passed.
73
74 NOTE: If patrons or items have been deleted their fields cannot be saved, additionally the fields will use current
75 values as the ones from when the transaction occurred are not available.
76
77 =head1 SYNOPSIS
78
79 pseudonymize_statistics.pl [-h|--help] [-v|--verbose] [-b|--before=DATE]
80
81 =head1 OPTIONS
82
83 =over
84
85 =item B<-h|--help>
86
87 Print a brief help message
88
89 =item B<-v|--verbose>
90
91 Verbose mode.
92
93 =item B<-b|--before=DATE>
94
95 This option allows for specifying a date to pseudonmyize before. Useful if you have enabled pseudonymization and want to pseudonymize transactions before that date. If not passed all statistics before current time will be pseudonymized.
96
97 =back
98
99 =head1 AUTHOR
100
101 Nick Clemens <nick@bywatersolutions.com>
102
103 =head1 COPYRIGHT
104
105 Copyright 2023 ByWater Solutions
106
107 =head1 LICENSE
108
109 This file is part of Koha.
110
111 # Koha is free software; you can redistribute it and/or modify it
112 # under the terms of the GNU General Public License as published by
113 # the Free Software Foundation; either version 3 of the License, or
114 # (at your option) any later version.
115 #
116 # Koha is distributed in the hope that it will be useful, but
117 # WITHOUT ANY WARRANTY; without even the implied warranty of
118 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
119 # GNU General Public License for more details.
120 #
121 # You should have received a copy of the GNU General Public License
122 # along with Koha; if not, see <http://www.gnu.org/licenses>.
123
124 =cut