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.
20 =head1 cleanborrowers.pl
22 This script allows to do 2 things.
26 =item * Anonymise the borrowers' issues if issue is older than a given date. see C<datefilter1>.
28 =item * Delete the borrowers who has not borrowered since a given date. see C<datefilter2>.
36 #use warnings; FIXME - Bug 2505
40 use C4::Members; # GetBorrowersWhoHavexxxBorrowed.
41 use C4::Circulation; # AnonymiseIssueHistory.
42 use C4::VirtualShelves (); #no import
43 use Koha::DateUtils qw( dt_from_string output_pref );
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 $step = $params->{step} || 1;
55 my $not_borrowered_since = # the date which filter on issue history.
56 $params->{not_borrowered_since}
57 ? dt_from_string $params->{not_borrowered_since}
59 my $last_issue_date = # the date which filter on borrowers last issue.
60 $params->{last_issue_date}
61 ? dt_from_string $params->{last_issue_date}
63 my $borrower_dateexpiry =
64 $params->{borrower_dateexpiry}
65 ? dt_from_string $params->{borrower_dateexpiry}
68 my $borrower_categorycode = $params->{'borrower_categorycode'} || q{};
70 # getting the template
71 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
72 { template_name => "tools/cleanborrowers.tmpl",
76 flagsrequired => { tools => 'delete_anonymize_patrons', catalogue => 1 },
82 my %checkboxes = map { $_ => 1 } split /\0/, $params->{'checkbox'};
86 if ( $checkboxes{borrower} ) {
87 $membersToDelete = GetBorrowersToExpunge(
92 not_borrowered_since => output_pref(
94 dt => $not_borrowered_since,
105 expired_before => output_pref(
107 dt => $borrower_dateexpiry,
116 $borrower_categorycode
117 ? ( category_code => $borrower_categorycode )
122 _skip_borrowers_with_nonzero_balance( $membersToDelete );
123 $totalDel = scalar @$membersToDelete;
127 my $membersToAnonymize;
128 if ( $checkboxes{issue} ) {
129 $membersToAnonymize = GetBorrowersWithIssuesHistoryOlderThan($last_issue_date);
130 $totalAno = scalar @$membersToAnonymize;
134 totalToDelete => $totalDel,
135 totalToAnonymize => $totalAno,
136 memberstodelete_list => $membersToDelete,
137 memberstoanonymize_list => $membersToAnonymize,
141 elsif ( $step == 3 ) {
142 my $do_delete = $params->{'do_delete'};
143 my $do_anonym = $params->{'do_anonym'};
145 my ( $totalDel, $totalAno, $radio ) = ( 0, 0, 0 );
149 my $membersToDelete = GetBorrowersToExpunge(
152 $not_borrowered_since
154 not_borrowered_since => output_pref(
156 dt => $not_borrowered_since,
166 expired_before => output_pref(
167 { dt => $borrower_dateexpiry, dateformat => 'iso' }
173 $borrower_categorycode
174 ? ( category_code => $borrower_categorycode )
179 _skip_borrowers_with_nonzero_balance( $membersToDelete );
180 $totalDel = scalar(@$membersToDelete);
181 $radio = $params->{'radio'};
182 for ( my $i = 0 ; $i < $totalDel ; $i++ ) {
183 $radio eq 'testrun' && last;
184 my $borrowernumber = $membersToDelete->[$i]->{'borrowernumber'};
185 $radio eq 'trash' && MoveMemberToDeleted( $borrowernumber );
186 C4::VirtualShelves::HandleDelBorrower( $borrowernumber );
187 DelMember( $borrowernumber );
191 TotalDel => $totalDel
195 # Anonymising all members
197 #FIXME: anonymisation errors are not handled
198 ($totalAno,my $anonymisation_error) = AnonymiseIssueHistory($last_issue_date);
205 trash => ( $radio eq "trash" ) ? (1) : (0),
206 testrun => ( $radio eq "testrun" ) ? 1: 0,
212 not_borrowered_since => $not_borrowered_since,
213 borrower_dateexpiry => $borrower_dateexpiry,
214 last_issue_date => $last_issue_date,
215 borrower_categorycodes => GetBorrowercategoryList(),
216 borrower_categorycode => $borrower_categorycode,
219 #writing the template
220 output_html_with_http_headers $cgi, $cookie, $template->output;
222 sub _skip_borrowers_with_nonzero_balance {
223 my $borrowers = shift;
226 (undef, undef, $balance) = GetMemberIssuesAndFines( $_->{borrowernumber} );
227 ($balance != 0) ? (): ($_);