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