-
Notifications
You must be signed in to change notification settings - Fork 2
/
cleanborrowers.pl
executable file
·170 lines (143 loc) · 5.68 KB
/
cleanborrowers.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/perl
# This file is part of Koha.
#
# 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
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307 USA
#
# Written by Antoine Farnault antoine@koha-fr.org on Nov. 2006.
=head1 cleanborrowers.pl
This script allows to do 2 things.
=over 2
=item * Anonymise the borrowers' issues if issue is older than a given date. see C<datefilter1>.
=item * Delete the borrowers who has not borrowered since a given date. see C<datefilter2>.
=back
=cut
use strict;
#use warnings; FIXME - Bug 2505
use CGI;
use C4::Auth;
use C4::Output;
use C4::Dates qw/format_date format_date_in_iso/;
use C4::Members; # GetBorrowersWhoHavexxxBorrowed.
use C4::Circulation; # AnonymiseIssueHistory.
use C4::VirtualShelves (); #no import
use Date::Calc qw/Today Add_Delta_YM/;
my $cgi = new CGI;
# Fetch the paramater list as a hash in scalar context:
# * returns paramater list as tied hash ref
# * we can edit the values by changing the key
# * multivalued CGI paramaters are returned as a packaged string separated by "\0" (null)
my $params = $cgi->Vars;
my $filterdate1; # the date which filter on issue history.
my $filterdate2; # the date which filter on borrowers last issue.
# getting the template
my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
{
template_name => "tools/cleanborrowers.tmpl",
query => $cgi,
type => "intranet",
authnotrequired => 0,
flagsrequired => { tools => 'delete_anonymize_patrons', catalogue => 1 },
}
);
if ( $params->{'step2'} ) {
$filterdate1 = format_date_in_iso($params->{'filterdate1'});
$filterdate2 = format_date_in_iso($params->{'filterdate2'});
my %checkboxes = map { $_ => 1 } split /\0/, $params->{'checkbox'};
my $totalDel;
my $membersToDelete;
if ($checkboxes{borrower}) {
$membersToDelete = GetBorrowersWhoHaveNotBorrowedSince($filterdate1, 1);
$totalDel = scalar @$membersToDelete;
}
my $totalAno;
my $membersToAnonymize;
if ($checkboxes{issue}) {
$membersToAnonymize =
GetBorrowersWithIssuesHistoryOlderThan($filterdate2);
$totalAno = scalar @$membersToAnonymize;
}
$template->param(
step2 => 1,
totalToDelete => $totalDel,
totalToAnonymize => $totalAno,
memberstodelete_list => $membersToDelete,
memberstoanonymize_list => $membersToAnonymize,
filterdate1 => format_date($filterdate1),
filterdate2 => format_date($filterdate2),
);
### TODO : Use GetBorrowersNamesAndLatestIssue function in order to get the borrowers to delete or anonymize.
### Now, we are only using total, which is not enough imlo
#writing the template
output_html_with_http_headers $cgi, $cookie, $template->output;
exit;
}
if ( $params->{'step3'} ) {
$filterdate1 = format_date_in_iso($params->{'filterdate1'});
$filterdate2 = format_date_in_iso($params->{'filterdate2'});
my $do_delete = $params->{'do_delete'};
my $do_anonym = $params->{'do_anonym'};
my ( $totalDel, $totalAno, $radio ) = ( 0, 0, 0 );
# delete members
if ($do_delete) {
my $membersToDelete = GetBorrowersWhoHaveNotBorrowedSince($filterdate1, 1);
$totalDel = scalar(@$membersToDelete);
$radio = $params->{'radio'};
if ( $radio eq 'trash' ) {
my $i;
for ( $i = 0 ; $i < $totalDel ; $i++ ) {
MoveMemberToDeleted( $membersToDelete->[$i]->{'borrowernumber'} );
C4::VirtualShelves::HandleDelBorrower($membersToDelete->[$i]->{'borrowernumber'});
DelMember( $membersToDelete->[$i]->{'borrowernumber'} );
}
}
else { # delete completly.
my $i;
for ( $i = 0 ; $i < $totalDel ; $i++ ) {
C4::VirtualShelves::HandleDelBorrower($membersToDelete->[$i]->{'borrowernumber'});
DelMember($membersToDelete->[$i]->{'borrowernumber'});
}
}
$template->param(
do_delete => '1',
TotalDel => $totalDel
);
}
# Anonymising all members
if ($do_anonym) {
$totalAno = AnonymiseIssueHistory($filterdate2);
$template->param(
filterdate1 => $filterdate2,
do_anonym => '1',
);
}
$template->param(
step3 => '1',
trash => ( $radio eq "trash" ) ? (1) : (0),
);
#writing the template
output_html_with_http_headers $cgi, $cookie, $template->output;
exit;
}
#default value set to the template are the 'CNIL' value.
my ( $year, $month, $day ) = &Today();
$filterdate1 = format_date(sprintf("%-04.4d-%-02.2d-%02.2d", Add_Delta_YM($year, $month, $day, -1, 0)));
$filterdate2 = format_date(sprintf("%-04.4d-%-02.2d-%02.2d", Add_Delta_YM($year, $month, $day, 0, -3)));
$template->param(
step1 => '1',
filterdate1 => $filterdate1,
filterdate2 => $filterdate2,
DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
);
#writing the template
output_html_with_http_headers $cgi, $cookie, $template->output;