Bug 16699: Remove requirement from borrowernumberQueryParam
[koha.git] / tools / cleanborrowers.pl
blob4cc916910ab2991457a20b656a8a03f99bfe1ad0
1 #!/usr/bin/perl
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.
24 =over 2
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>.
30 =back
32 =cut
34 use Modern::Perl;
36 use CGI qw ( -utf8 );
37 use C4::Auth;
38 use C4::Output;
39 use C4::Members; # GetBorrowersWhoHavexxxBorrowed.
40 use C4::Circulation; # AnonymiseIssueHistory.
41 use Koha::DateUtils qw( dt_from_string output_pref );
42 use Date::Calc qw/Today Add_Delta_YM/;
43 use Koha::List::Patron;
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 $step = $params->{step} || 1;
54 my $not_borrowed_since = # the date which filter on issue history.
55 $params->{not_borrowed_since}
56 ? dt_from_string $params->{not_borrowed_since}
57 : undef;
58 my $last_issue_date = # the date which filter on borrowers last issue.
59 $params->{last_issue_date}
60 ? dt_from_string $params->{last_issue_date}
61 : undef;
62 my $borrower_dateexpiry =
63 $params->{borrower_dateexpiry}
64 ? dt_from_string $params->{borrower_dateexpiry}
65 : undef;
66 my $patron_list_id = $params->{patron_list_id};
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.tt",
73 query => $cgi,
74 type => "intranet",
75 authnotrequired => 0,
76 flagsrequired => { tools => 'delete_anonymize_patrons', catalogue => 1 },
80 my $branch = $params->{ branch } || '*';
81 $template->param( current_branch => $branch );
83 if ( $step == 2 ) {
85 my %checkboxes = map { $_ => 1 } split /\0/, $params->{'checkbox'};
87 my $patrons_to_delete;
88 if ( $checkboxes{borrower} ) {
89 $patrons_to_delete = GetBorrowersToExpunge(
90 _get_selection_params(
91 $not_borrowed_since,
92 $borrower_dateexpiry,
93 $borrower_categorycode,
94 $patron_list_id,
95 $branch
99 _skip_borrowers_with_nonzero_balance($patrons_to_delete);
101 my $members_to_anonymize;
102 if ( $checkboxes{issue} ) {
103 if ( $branch eq '*' ) {
104 $members_to_anonymize = GetBorrowersWithIssuesHistoryOlderThan($last_issue_date);
105 } else {
106 $members_to_anonymize = GetBorrowersWithIssuesHistoryOlderThan($last_issue_date, $branch);
110 $template->param(
111 patrons_to_delete => $patrons_to_delete,
112 patrons_to_anonymize => $members_to_anonymize,
113 patron_list_id => $patron_list_id
117 elsif ( $step == 3 ) {
118 my $do_delete = $params->{'do_delete'};
119 my $do_anonym = $params->{'do_anonym'};
121 my ( $totalDel, $totalAno, $radio ) = ( 0, 0, 0 );
123 # delete members
124 if ($do_delete) {
125 my $patrons_to_delete = GetBorrowersToExpunge(
126 _get_selection_params(
127 $not_borrowed_since,
128 $borrower_dateexpiry,
129 $borrower_categorycode,
130 $patron_list_id,
131 $branch
134 _skip_borrowers_with_nonzero_balance($patrons_to_delete);
136 $totalDel = scalar(@$patrons_to_delete);
137 $radio = $params->{'radio'};
138 for ( my $i = 0 ; $i < $totalDel ; $i++ ) {
139 $radio eq 'testrun' && last;
140 my $borrowernumber = $patrons_to_delete->[$i]->{'borrowernumber'};
141 $radio eq 'trash' && MoveMemberToDeleted($borrowernumber);
142 C4::Members::HandleDelBorrower($borrowernumber);
143 DelMember($borrowernumber);
145 $template->param(
146 do_delete => '1',
147 TotalDel => $totalDel
151 # Anonymising all members
152 if ($do_anonym) {
153 #FIXME: anonymisation errors are not handled
154 ($totalAno,my $anonymisation_error) = AnonymiseIssueHistory($last_issue_date);
155 $template->param(
156 do_anonym => '1',
160 $template->param(
161 trash => ( $radio eq "trash" ) ? (1) : (0),
162 testrun => ( $radio eq "testrun" ) ? 1: 0,
164 } else { # $step == 1
165 my @all_lists = GetPatronLists();
166 my @non_empty_lists;
167 foreach my $list (@all_lists){
168 my @patrons = $list->patron_list_patrons();
169 if( scalar @patrons ) { push(@non_empty_lists,$list) }
171 $template->param( patron_lists => [ @non_empty_lists ] );
174 $template->param(
175 step => $step,
176 not_borrowed_since => $not_borrowed_since,
177 borrower_dateexpiry => $borrower_dateexpiry,
178 last_issue_date => $last_issue_date,
179 borrower_categorycodes => GetBorrowercategoryList(),
180 borrower_categorycode => $borrower_categorycode,
183 #writing the template
184 output_html_with_http_headers $cgi, $cookie, $template->output;
186 sub _skip_borrowers_with_nonzero_balance {
187 my $borrowers = shift;
188 my $balance;
189 @$borrowers = map {
190 (undef, undef, $balance) = GetMemberIssuesAndFines( $_->{borrowernumber} );
191 (defined $balance && $balance != 0) ? (): ($_);
192 } @$borrowers;
195 sub _get_selection_params {
196 my ($not_borrowed_since, $borrower_dateexpiry,
197 $borrower_categorycode, $patron_list_id, $branch) = @_;
199 my $params = {};
200 $params->{not_borrowed_since} = output_pref({
201 dt => $not_borrowed_since,
202 dateformat => 'iso',
203 dateonly => 1
204 }) if $not_borrowed_since;
205 $params->{expired_before} = output_pref({
206 dt => $borrower_dateexpiry,
207 dateformat => 'iso',
208 dateonly => 1
209 }) if $borrower_dateexpiry;
210 $params->{category_code} = $borrower_categorycode if $borrower_categorycode;
211 $params->{patron_list_id} = $patron_list_id if $patron_list_id;
213 if ( defined $branch and $branch ne '*' ) {
214 $params->{ branchcode } = $branch;
217 return $params;