Bug 18050: DBIC schema changes
[koha.git] / tools / cleanborrowers.pl
blobc323eeb908d5730a4e469cb00a531a97b7ba7cfb
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;
40 use C4::Circulation; # AnonymiseIssueHistory.
41 use Koha::DateUtils qw( dt_from_string output_pref );
42 use Koha::Patron::Categories;
43 use Koha::Patrons;
44 use Date::Calc qw/Today Add_Delta_YM/;
45 use Koha::Patrons;
46 use Koha::List::Patron;
48 my $cgi = CGI->new;
50 # Fetch the paramater list as a hash in scalar context:
51 # * returns paramater list as tied hash ref
52 # * we can edit the values by changing the key
53 # * multivalued CGI paramaters are returned as a packaged string separated by "\0" (null)
54 my $params = $cgi->Vars;
56 my $step = $params->{step} || 1;
57 my $not_borrowed_since = # the date which filter on issue history.
58 $params->{not_borrowed_since}
59 ? dt_from_string $params->{not_borrowed_since}
60 : undef;
61 my $last_issue_date = # the date which filter on borrowers last issue.
62 $params->{last_issue_date}
63 ? dt_from_string $params->{last_issue_date}
64 : undef;
65 my $borrower_dateexpiry =
66 $params->{borrower_dateexpiry}
67 ? dt_from_string $params->{borrower_dateexpiry}
68 : undef;
69 my $borrower_lastseen =
70 $params->{borrower_lastseen}
71 ? dt_from_string $params->{borrower_lastseen}
72 : undef;
73 my $patron_list_id = $params->{patron_list_id};
75 my $borrower_categorycode = $params->{'borrower_categorycode'} || q{};
77 # getting the template
78 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
79 { template_name => "tools/cleanborrowers.tt",
80 query => $cgi,
81 type => "intranet",
82 flagsrequired => { tools => 'delete_anonymize_patrons', catalogue => 1 },
86 my $branch = $params->{ branch } || '*';
87 $template->param( current_branch => $branch );
88 $template->param( OnlyMine => C4::Context->only_my_library );
90 if ( $step == 2 ) {
92 my %checkboxes = map { $_ => 1 } split /\0/, $params->{'checkbox'};
94 my $patrons_to_delete;
95 if ( $checkboxes{borrower} ) {
96 $patrons_to_delete = GetBorrowersToExpunge(
97 _get_selection_params(
98 $not_borrowed_since,
99 $borrower_dateexpiry,
100 $borrower_lastseen,
101 $borrower_categorycode,
102 $patron_list_id,
103 $branch
107 _skip_borrowers_with_nonzero_balance($patrons_to_delete);
109 my $patrons_to_anonymize =
110 $checkboxes{issue}
111 ? $branch eq '*'
112 ? Koha::Patrons->search_patrons_to_anonymise( { before => $last_issue_date } )
113 : Koha::Patrons->search_patrons_to_anonymise( { before => $last_issue_date, library => $branch } )
114 : undef;
116 $template->param(
117 patrons_to_delete => $patrons_to_delete,
118 patrons_to_anonymize => $patrons_to_anonymize,
119 patron_list_id => $patron_list_id,
123 elsif ( $step == 3 ) {
124 my $do_delete = $params->{'do_delete'};
125 my $do_anonym = $params->{'do_anonym'};
127 my ( $totalDel, $totalAno, $radio ) = ( 0, 0, 0 );
129 # delete members
130 if ($do_delete) {
131 my $patrons_to_delete = GetBorrowersToExpunge(
132 _get_selection_params(
133 $not_borrowed_since,
134 $borrower_dateexpiry,
135 $borrower_lastseen,
136 $borrower_categorycode,
137 $patron_list_id,
138 $branch
141 _skip_borrowers_with_nonzero_balance($patrons_to_delete);
143 $totalDel = scalar(@$patrons_to_delete);
144 $radio = $params->{'radio'};
145 for ( my $i = 0 ; $i < $totalDel ; $i++ ) {
146 $radio eq 'testrun' && last;
147 my $borrowernumber = $patrons_to_delete->[$i]->{'borrowernumber'};
148 my $patron = Koha::Patrons->find($borrowernumber);
149 $radio eq 'trash' && $patron->move_to_deleted;
150 $patron->delete;
152 $template->param(
153 do_delete => '1',
154 TotalDel => $totalDel
158 # Anonymising all members
159 if ($do_anonym) {
160 #FIXME: anonymisation errors are not handled
161 my $rows = Koha::Patrons->search_patrons_to_anonymise( { before => $last_issue_date } )->anonymise_issue_history( { before => $last_issue_date } );
162 $template->param(
163 do_anonym => $rows,
167 $template->param(
168 trash => ( $radio eq "trash" ) ? (1) : (0),
169 testrun => ( $radio eq "testrun" ) ? 1: 0,
171 } else { # $step == 1
172 my @all_lists = GetPatronLists();
173 my @non_empty_lists;
174 foreach my $list (@all_lists){
175 my @patrons = $list->patron_list_patrons();
176 if( scalar @patrons ) { push(@non_empty_lists,$list) }
178 $template->param( patron_lists => [ @non_empty_lists ] );
181 my $patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['description']});
183 $template->param(
184 step => $step,
185 not_borrowed_since => $not_borrowed_since,
186 borrower_dateexpiry => $borrower_dateexpiry,
187 borrower_lastseen => $borrower_lastseen,
188 last_issue_date => $last_issue_date,
189 borrower_categorycodes => $patron_categories,
190 borrower_categorycode => $borrower_categorycode,
193 #writing the template
194 output_html_with_http_headers $cgi, $cookie, $template->output;
196 sub _skip_borrowers_with_nonzero_balance {
197 my $borrowers = shift;
198 my $balance;
199 @$borrowers = map {
200 my $patron = Koha::Patrons->find( $_->{borrowernumber} );
201 my $balance = $patron->account->balance;
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;