Translation for 16.05.02
[koha.git] / tools / cleanborrowers.pl
blobd108d53ebd42733128212cf9d577245049f8417c
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 strict;
36 #use warnings; FIXME - Bug 2505
37 use CGI qw ( -utf8 );
38 use C4::Auth;
39 use C4::Output;
40 use C4::Members; # GetBorrowersWhoHavexxxBorrowed.
41 use C4::Circulation; # AnonymiseIssueHistory.
42 use Koha::DateUtils qw( dt_from_string output_pref );
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 $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",
74 query => $cgi,
75 type => "intranet",
76 authnotrequired => 0,
77 flagsrequired => { tools => 'delete_anonymize_patrons', catalogue => 1 },
81 if ( $step == 2 ) {
83 my %checkboxes = map { $_ => 1 } split /\0/, $params->{'checkbox'};
85 my $patrons_to_delete;
86 if ( $checkboxes{borrower} ) {
87 $patrons_to_delete = GetBorrowersToExpunge(
88 _get_selection_params(
89 $not_borrowed_since,
90 $borrower_dateexpiry,
91 $borrower_categorycode,
92 $patron_list_id,
96 _skip_borrowers_with_nonzero_balance($patrons_to_delete);
98 my $members_to_anonymize;
99 if ( $checkboxes{issue} ) {
100 $members_to_anonymize = GetBorrowersWithIssuesHistoryOlderThan($last_issue_date);
103 $template->param(
104 patrons_to_delete => $patrons_to_delete,
105 patrons_to_anonymize => $members_to_anonymize,
106 patron_list_id => $patron_list_id,
110 elsif ( $step == 3 ) {
111 my $do_delete = $params->{'do_delete'};
112 my $do_anonym = $params->{'do_anonym'};
114 my ( $totalDel, $totalAno, $radio ) = ( 0, 0, 0 );
116 # delete members
117 if ($do_delete) {
118 my $patrons_to_delete = GetBorrowersToExpunge(
119 _get_selection_params(
120 $not_borrowed_since, $borrower_dateexpiry,
121 $borrower_categorycode, $patron_list_id
124 _skip_borrowers_with_nonzero_balance($patrons_to_delete);
126 $totalDel = scalar(@$patrons_to_delete);
127 $radio = $params->{'radio'};
128 for ( my $i = 0 ; $i < $totalDel ; $i++ ) {
129 $radio eq 'testrun' && last;
130 my $borrowernumber = $patrons_to_delete->[$i]->{'borrowernumber'};
131 $radio eq 'trash' && MoveMemberToDeleted($borrowernumber);
132 C4::Members::HandleDelBorrower($borrowernumber);
133 DelMember($borrowernumber);
135 $template->param(
136 do_delete => '1',
137 TotalDel => $totalDel
141 # Anonymising all members
142 if ($do_anonym) {
143 #FIXME: anonymisation errors are not handled
144 ($totalAno,my $anonymisation_error) = AnonymiseIssueHistory($last_issue_date);
145 $template->param(
146 do_anonym => '1',
150 $template->param(
151 trash => ( $radio eq "trash" ) ? (1) : (0),
152 testrun => ( $radio eq "testrun" ) ? 1: 0,
154 } else { # $step == 1
155 my @all_lists = GetPatronLists();
156 my @non_empty_lists;
157 foreach my $list (@all_lists){
158 my @patrons = $list->patron_list_patrons();
159 if( scalar @patrons ) { push(@non_empty_lists,$list) }
161 $template->param( patron_lists => [ @non_empty_lists ] );
164 $template->param(
165 step => $step,
166 not_borrowed_since => $not_borrowed_since,
167 borrower_dateexpiry => $borrower_dateexpiry,
168 last_issue_date => $last_issue_date,
169 borrower_categorycodes => GetBorrowercategoryList(),
170 borrower_categorycode => $borrower_categorycode,
173 #writing the template
174 output_html_with_http_headers $cgi, $cookie, $template->output;
176 sub _skip_borrowers_with_nonzero_balance {
177 my $borrowers = shift;
178 my $balance;
179 @$borrowers = map {
180 (undef, undef, $balance) = GetMemberIssuesAndFines( $_->{borrowernumber} );
181 ($balance != 0) ? (): ($_);
182 } @$borrowers;
185 sub _get_selection_params {
186 my ($not_borrowed_since, $borrower_dateexpiry, $borrower_categorycode, $patron_list_id) = @_;
188 my $params = {};
189 $params->{not_borrowed_since} = output_pref({
190 dt => $not_borrowed_since,
191 dateformat => 'iso',
192 dateonly => 1
193 }) if $not_borrowed_since;
194 $params->{expired_before} = output_pref({
195 dt => $borrower_dateexpiry,
196 dateformat => 'iso',
197 dateonly => 1
198 }) if $borrower_dateexpiry;
199 $params->{category_code} = $borrower_categorycode if $borrower_categorycode;
200 $params->{patron_list_id} = $patron_list_id if $patron_list_id;
202 return $params;