Bug 17301 - Add callnumber to label-edit-batch.pl
[koha.git] / tools / cleanborrowers.pl
blob1b7e0f84d0db7f9c660c1b3d212615f67ac5fb2a
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 Koha::Patron::Categories;
43 use Date::Calc qw/Today Add_Delta_YM/;
44 use Koha::List::Patron;
46 my $cgi = new CGI;
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}
58 : undef;
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}
62 : undef;
63 my $borrower_dateexpiry =
64 $params->{borrower_dateexpiry}
65 ? dt_from_string $params->{borrower_dateexpiry}
66 : undef;
67 my $borrower_lastseen =
68 $params->{borrower_lastseen}
69 ? dt_from_string $params->{borrower_lastseen}
70 : undef;
71 my $patron_list_id = $params->{patron_list_id};
73 my $borrower_categorycode = $params->{'borrower_categorycode'} || q{};
75 # getting the template
76 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
77 { template_name => "tools/cleanborrowers.tt",
78 query => $cgi,
79 type => "intranet",
80 authnotrequired => 0,
81 flagsrequired => { tools => 'delete_anonymize_patrons', catalogue => 1 },
85 my $branch = $params->{ branch } || '*';
86 $template->param( current_branch => $branch );
87 $template->param( OnlyMine => C4::Context->only_my_library );
89 if ( $step == 2 ) {
91 my %checkboxes = map { $_ => 1 } split /\0/, $params->{'checkbox'};
93 my $patrons_to_delete;
94 if ( $checkboxes{borrower} ) {
95 $patrons_to_delete = GetBorrowersToExpunge(
96 _get_selection_params(
97 $not_borrowed_since,
98 $borrower_dateexpiry,
99 $borrower_lastseen,
100 $borrower_categorycode,
101 $patron_list_id,
102 $branch
106 _skip_borrowers_with_nonzero_balance($patrons_to_delete);
108 my $members_to_anonymize;
109 if ( $checkboxes{issue} ) {
110 if ( $branch eq '*' ) {
111 $members_to_anonymize = GetBorrowersWithIssuesHistoryOlderThan($last_issue_date);
112 } else {
113 $members_to_anonymize = GetBorrowersWithIssuesHistoryOlderThan($last_issue_date, $branch);
117 $template->param(
118 patrons_to_delete => $patrons_to_delete,
119 patrons_to_anonymize => $members_to_anonymize,
120 patron_list_id => $patron_list_id
124 elsif ( $step == 3 ) {
125 my $do_delete = $params->{'do_delete'};
126 my $do_anonym = $params->{'do_anonym'};
128 my ( $totalDel, $totalAno, $radio ) = ( 0, 0, 0 );
130 # delete members
131 if ($do_delete) {
132 my $patrons_to_delete = GetBorrowersToExpunge(
133 _get_selection_params(
134 $not_borrowed_since,
135 $borrower_dateexpiry,
136 $borrower_lastseen,
137 $borrower_categorycode,
138 $patron_list_id,
139 $branch
142 _skip_borrowers_with_nonzero_balance($patrons_to_delete);
144 $totalDel = scalar(@$patrons_to_delete);
145 $radio = $params->{'radio'};
146 for ( my $i = 0 ; $i < $totalDel ; $i++ ) {
147 $radio eq 'testrun' && last;
148 my $borrowernumber = $patrons_to_delete->[$i]->{'borrowernumber'};
149 $radio eq 'trash' && MoveMemberToDeleted($borrowernumber);
150 C4::Members::HandleDelBorrower($borrowernumber);
151 DelMember($borrowernumber);
153 $template->param(
154 do_delete => '1',
155 TotalDel => $totalDel
159 # Anonymising all members
160 if ($do_anonym) {
161 #FIXME: anonymisation errors are not handled
162 ($totalAno,my $anonymisation_error) = AnonymiseIssueHistory($last_issue_date);
163 $template->param(
164 do_anonym => '1',
168 $template->param(
169 trash => ( $radio eq "trash" ) ? (1) : (0),
170 testrun => ( $radio eq "testrun" ) ? 1: 0,
172 } else { # $step == 1
173 my @all_lists = GetPatronLists();
174 my @non_empty_lists;
175 foreach my $list (@all_lists){
176 my @patrons = $list->patron_list_patrons();
177 if( scalar @patrons ) { push(@non_empty_lists,$list) }
179 $template->param( patron_lists => [ @non_empty_lists ] );
182 my $patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['description']});
184 $template->param(
185 step => $step,
186 not_borrowed_since => $not_borrowed_since,
187 borrower_dateexpiry => $borrower_dateexpiry,
188 borrower_lastseen => $borrower_lastseen,
189 last_issue_date => $last_issue_date,
190 borrower_categorycodes => $patron_categories,
191 borrower_categorycode => $borrower_categorycode,
194 #writing the template
195 output_html_with_http_headers $cgi, $cookie, $template->output;
197 sub _skip_borrowers_with_nonzero_balance {
198 my $borrowers = shift;
199 my $balance;
200 @$borrowers = map {
201 (undef, undef, $balance) = GetMemberIssuesAndFines( $_->{borrowernumber} );
202 (defined $balance && $balance != 0) ? (): ($_);
203 } @$borrowers;
206 sub _get_selection_params {
207 my ($not_borrowed_since, $borrower_dateexpiry, $borrower_lastseen,
208 $borrower_categorycode, $patron_list_id, $branch) = @_;
210 my $params = {};
211 $params->{not_borrowed_since} = output_pref({
212 dt => $not_borrowed_since,
213 dateformat => 'iso',
214 dateonly => 1
215 }) if $not_borrowed_since;
216 $params->{expired_before} = output_pref({
217 dt => $borrower_dateexpiry,
218 dateformat => 'iso',
219 dateonly => 1
220 }) if $borrower_dateexpiry;
221 $params->{last_seen} = output_pref({
222 dt => $borrower_lastseen,
223 dateformat => 'iso',
224 dateonly => 1
225 }) if $borrower_lastseen;
226 $params->{category_code} = $borrower_categorycode if $borrower_categorycode;
227 $params->{patron_list_id} = $patron_list_id if $patron_list_id;
229 if ( defined $branch and $branch ne '*' ) {
230 $params->{ branchcode } = $branch;
233 return $params;