3 # This file is part of Koha.
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.
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.
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>.
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 borrowed since a given date. see C<datefilter2>.
39 use C4::Members; # GetBorrowersWhoHavexxxBorrowed.
40 use C4::Circulation; # AnonymiseIssueHistory.
41 use Koha::DateUtils qw( dt_from_string output_pref );
42 use Koha::Patron::Categories;
43 use Date::Calc qw/Today Add_Delta_YM/;
44 use Koha::List::Patron;
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_borrowed_since = # the date which filter on issue history.
56 $params->{not_borrowed_since}
57 ? dt_from_string $params->{not_borrowed_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}
67 my $patron_list_id = $params->{patron_list_id};
69 my $borrower_categorycode = $params->{'borrower_categorycode'} || q{};
71 # getting the template
72 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
73 { template_name => "tools/cleanborrowers.tt",
77 flagsrequired => { tools => 'delete_anonymize_patrons', catalogue => 1 },
81 my $branch = $params->{ branch } || '*';
82 $template->param( current_branch => $branch );
86 my %checkboxes = map { $_ => 1 } split /\0/, $params->{'checkbox'};
88 my $patrons_to_delete;
89 if ( $checkboxes{borrower} ) {
90 $patrons_to_delete = GetBorrowersToExpunge(
91 _get_selection_params(
94 $borrower_categorycode,
100 _skip_borrowers_with_nonzero_balance($patrons_to_delete);
102 my $members_to_anonymize;
103 if ( $checkboxes{issue} ) {
104 if ( $branch eq '*' ) {
105 $members_to_anonymize = GetBorrowersWithIssuesHistoryOlderThan($last_issue_date);
107 $members_to_anonymize = GetBorrowersWithIssuesHistoryOlderThan($last_issue_date, $branch);
112 patrons_to_delete => $patrons_to_delete,
113 patrons_to_anonymize => $members_to_anonymize,
114 patron_list_id => $patron_list_id
118 elsif ( $step == 3 ) {
119 my $do_delete = $params->{'do_delete'};
120 my $do_anonym = $params->{'do_anonym'};
122 my ( $totalDel, $totalAno, $radio ) = ( 0, 0, 0 );
126 my $patrons_to_delete = GetBorrowersToExpunge(
127 _get_selection_params(
129 $borrower_dateexpiry,
130 $borrower_categorycode,
135 _skip_borrowers_with_nonzero_balance($patrons_to_delete);
137 $totalDel = scalar(@$patrons_to_delete);
138 $radio = $params->{'radio'};
139 for ( my $i = 0 ; $i < $totalDel ; $i++ ) {
140 $radio eq 'testrun' && last;
141 my $borrowernumber = $patrons_to_delete->[$i]->{'borrowernumber'};
142 $radio eq 'trash' && MoveMemberToDeleted($borrowernumber);
143 C4::Members::HandleDelBorrower($borrowernumber);
144 DelMember($borrowernumber);
148 TotalDel => $totalDel
152 # Anonymising all members
154 #FIXME: anonymisation errors are not handled
155 ($totalAno,my $anonymisation_error) = AnonymiseIssueHistory($last_issue_date);
162 trash => ( $radio eq "trash" ) ? (1) : (0),
163 testrun => ( $radio eq "testrun" ) ? 1: 0,
165 } else { # $step == 1
166 my @all_lists = GetPatronLists();
168 foreach my $list (@all_lists){
169 my @patrons = $list->patron_list_patrons();
170 if( scalar @patrons ) { push(@non_empty_lists,$list) }
172 $template->param( patron_lists => [ @non_empty_lists ] );
175 my $patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['description']});
179 not_borrowed_since => $not_borrowed_since,
180 borrower_dateexpiry => $borrower_dateexpiry,
181 last_issue_date => $last_issue_date,
182 borrower_categorycodes => $patron_categories,
183 borrower_categorycode => $borrower_categorycode,
186 #writing the template
187 output_html_with_http_headers $cgi, $cookie, $template->output;
189 sub _skip_borrowers_with_nonzero_balance {
190 my $borrowers = shift;
193 (undef, undef, $balance) = GetMemberIssuesAndFines( $_->{borrowernumber} );
194 (defined $balance && $balance != 0) ? (): ($_);
198 sub _get_selection_params {
199 my ($not_borrowed_since, $borrower_dateexpiry,
200 $borrower_categorycode, $patron_list_id, $branch) = @_;
203 $params->{not_borrowed_since} = output_pref({
204 dt => $not_borrowed_since,
207 }) if $not_borrowed_since;
208 $params->{expired_before} = output_pref({
209 dt => $borrower_dateexpiry,
212 }) if $borrower_dateexpiry;
213 $params->{category_code} = $borrower_categorycode if $borrower_categorycode;
214 $params->{patron_list_id} = $patron_list_id if $patron_list_id;
216 if ( defined $branch and $branch ne '*' ) {
217 $params->{ branchcode } = $branch;