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
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.
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>.
36 #use warnings; FIXME - Bug 2505
40 use C4::Dates qw/format_date format_date_in_iso/;
41 use C4::Members; # GetBorrowersWhoHavexxxBorrowed.
42 use C4::Circulation; # AnonymiseIssueHistory.
43 use C4::VirtualShelves (); #no import
44 use Date::Calc qw/Today Add_Delta_YM/;
48 # Fetch the paramater list as a hash in scalar context:
49 # * returns paramater list as tied hash ref
50 # * we can edit the values by changing the key
51 # * multivalued CGI paramaters are returned as a packaged string separated by "\0" (null)
52 my $params = $cgi->Vars;
54 my $filterdate1; # the date which filter on issue history.
55 my $filterdate2; # the date which filter on borrowers last issue.
57 # getting the template
58 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
60 template_name => "tools/cleanborrowers.tmpl",
64 flagsrequired => { tools => 'delete_anonymize_patrons', catalogue => 1 },
68 if ( $params->{'step2'} ) {
69 $filterdate1 = format_date_in_iso($params->{'filterdate1'});
70 $filterdate2 = format_date_in_iso($params->{'filterdate2'});
71 my %checkboxes = map { $_ => 1 } split /\0/, $params->{'checkbox'};
75 if ($checkboxes{borrower}) {
76 $membersToDelete = GetBorrowersWhoHaveNotBorrowedSince($filterdate1, 1);
77 $totalDel = scalar @$membersToDelete;
81 my $membersToAnonymize;
82 if ($checkboxes{issue}) {
84 GetBorrowersWithIssuesHistoryOlderThan($filterdate2);
85 $totalAno = scalar @$membersToAnonymize;
90 totalToDelete => $totalDel,
91 totalToAnonymize => $totalAno,
92 memberstodelete_list => $membersToDelete,
93 memberstoanonymize_list => $membersToAnonymize,
94 filterdate1 => format_date($filterdate1),
95 filterdate2 => format_date($filterdate2),
97 ### TODO : Use GetBorrowersNamesAndLatestIssue function in order to get the borrowers to delete or anonymize.
98 ### Now, we are only using total, which is not enough imlo
100 output_html_with_http_headers $cgi, $cookie, $template->output;
104 if ( $params->{'step3'} ) {
105 $filterdate1 = format_date_in_iso($params->{'filterdate1'});
106 $filterdate2 = format_date_in_iso($params->{'filterdate2'});
107 my $do_delete = $params->{'do_delete'};
108 my $do_anonym = $params->{'do_anonym'};
110 my ( $totalDel, $totalAno, $radio ) = ( 0, 0, 0 );
114 my $membersToDelete = GetBorrowersWhoHaveNotBorrowedSince($filterdate1, 1);
115 $totalDel = scalar(@$membersToDelete);
116 $radio = $params->{'radio'};
117 if ( $radio eq 'trash' ) {
119 for ( $i = 0 ; $i < $totalDel ; $i++ ) {
120 MoveMemberToDeleted( $membersToDelete->[$i]->{'borrowernumber'} );
121 C4::VirtualShelves::HandleDelBorrower($membersToDelete->[$i]->{'borrowernumber'});
122 DelMember( $membersToDelete->[$i]->{'borrowernumber'} );
125 else { # delete completly.
127 for ( $i = 0 ; $i < $totalDel ; $i++ ) {
128 C4::VirtualShelves::HandleDelBorrower($membersToDelete->[$i]->{'borrowernumber'});
129 DelMember($membersToDelete->[$i]->{'borrowernumber'});
134 TotalDel => $totalDel
138 # Anonymising all members
140 $totalAno = AnonymiseIssueHistory($filterdate2);
142 filterdate1 => $filterdate2,
149 trash => ( $radio eq "trash" ) ? (1) : (0),
152 #writing the template
153 output_html_with_http_headers $cgi, $cookie, $template->output;
157 #default value set to the template are the 'CNIL' value.
158 my ( $year, $month, $day ) = &Today();
159 $filterdate1 = format_date(sprintf("%-04.4d-%-02.2d-%02.2d", Add_Delta_YM($year, $month, $day, -1, 0)));
160 $filterdate2 = format_date(sprintf("%-04.4d-%-02.2d-%02.2d", Add_Delta_YM($year, $month, $day, 0, -3)));
164 filterdate1 => $filterdate1,
165 filterdate2 => $filterdate2,
166 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
169 #writing the template
170 output_html_with_http_headers $cgi, $cookie, $template->output;