Bug 30128: DBrev 21.12.00.017
[koha.git] / tools / cleanborrowers.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17 #
18 #   Written by Antoine Farnault antoine@koha-fr.org on Nov. 2006.
19
20 =head1 cleanborrowers.pl
21
22 This script allows to do 2 things.
23
24 =over 2
25
26 =item * Anonymise the borrowers' issues if issue is older than a given date. see C<datefilter1>.
27
28 =item * Delete the borrowers who has not borrowed since a given date. see C<datefilter2>.
29
30 =back
31
32 =cut
33
34 use Modern::Perl;
35
36 use CGI qw ( -utf8 );
37 use C4::Auth qw( get_template_and_user );
38 use C4::Output qw( output_html_with_http_headers );
39 use C4::Members qw( GetBorrowersToExpunge );
40 use Koha::DateUtils qw( dt_from_string output_pref );
41 use Koha::Old::Checkouts;
42 use Koha::Patron::Categories;
43 use Koha::Patrons;
44 use Koha::List::Patron qw( GetPatronLists );
45
46 my $cgi = CGI->new;
47
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;
53
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};
72
73 my $borrower_categorycode = $params->{'borrower_categorycode'} || q{};
74
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         flagsrequired   => { tools => 'delete_anonymize_patrons', catalogue => 1 },
81     }
82 );
83
84 my $branch = $params->{ branch } || '*';
85 $template->param( current_branch => $branch );
86 $template->param( OnlyMine => C4::Context->only_my_library );
87
88 if ( $step == 2 ) {
89
90     my %checkboxes = map { $_ => 1 } split /\0/, $params->{'checkbox'};
91
92     my $patrons_to_delete;
93     if ( $checkboxes{borrower} ) {
94         $patrons_to_delete = GetBorrowersToExpunge(
95              _get_selection_params(
96                   $not_borrowed_since,
97                   $borrower_dateexpiry,
98                   $borrower_lastseen,
99                   $borrower_categorycode,
100                   $patron_list_id,
101                   $branch
102              )
103         );
104     }
105     _skip_borrowers_with_nonzero_balance($patrons_to_delete);
106
107     my $patrons_to_anonymize =
108         $checkboxes{issue}
109       ? $branch eq '*'
110           ? Koha::Patrons->search_patrons_to_anonymise( { before => $last_issue_date } )
111           : Koha::Patrons->search_patrons_to_anonymise( { before => $last_issue_date, library => $branch } )
112       : undef;
113
114     $template->param(
115         patrons_to_delete    => $patrons_to_delete,
116         patrons_to_anonymize => $patrons_to_anonymize,
117         patron_list_id       => $patron_list_id,
118     );
119 }
120
121 elsif ( $step == 3 ) {
122     my $do_delete = $params->{'do_delete'};
123     my $do_anonym = $params->{'do_anonym'};
124
125     my ( $totalDel, $totalAno, $radio ) = ( 0, 0, 0 );
126
127     # delete members
128     if ($do_delete) {
129         my $patrons_to_delete = GetBorrowersToExpunge(
130                 _get_selection_params(
131                     $not_borrowed_since,
132                     $borrower_dateexpiry,
133                     $borrower_lastseen,
134                     $borrower_categorycode,
135                     $patron_list_id,
136                     $branch
137                 )
138             );
139         _skip_borrowers_with_nonzero_balance($patrons_to_delete);
140
141         $totalDel = scalar(@$patrons_to_delete);
142         $radio    = $params->{'radio'};
143         for ( my $i = 0 ; $i < $totalDel ; $i++ ) {
144             $radio eq 'testrun' && last;
145             my $borrowernumber = $patrons_to_delete->[$i]->{'borrowernumber'};
146             my $patron = Koha::Patrons->find($borrowernumber);
147             $radio eq 'trash' && $patron->move_to_deleted;
148             $patron->delete;
149         }
150         $template->param(
151             do_delete => '1',
152             TotalDel  => $totalDel
153         );
154     }
155
156     # Anonymising all members
157     if ($do_anonym) {
158         #FIXME: anonymisation errors are not handled
159         my $rows = Koha::Old::Checkouts
160                      ->filter_by_anonymizable
161                      ->filter_by_last_update({
162                          to => $last_issue_date, timestamp_column_name => 'returndate' })
163                      ->anonymize;
164
165         $template->param(
166             do_anonym   => $rows,
167         );
168     }
169
170     $template->param(
171         trash => ( $radio eq "trash" ) ? (1) : (0),
172         testrun => ( $radio eq "testrun" ) ? 1: 0,
173     );
174 } else { # $step == 1
175     my @all_lists = GetPatronLists();
176     my @non_empty_lists;
177     foreach my $list (@all_lists){
178     my @patrons = $list->patron_list_patrons();
179         if( scalar @patrons ) { push(@non_empty_lists,$list) }
180     }
181     $template->param( patron_lists => [ @non_empty_lists ] );
182 }
183
184 my $patron_categories = Koha::Patron::Categories->search_with_library_limits({}, {order_by => ['description']});
185
186 $template->param(
187     step                   => $step,
188     not_borrowed_since   => $not_borrowed_since,
189     borrower_dateexpiry    => $borrower_dateexpiry,
190     borrower_lastseen      => $borrower_lastseen,
191     last_issue_date        => $last_issue_date,
192     borrower_categorycodes => $patron_categories,
193     borrower_categorycode  => $borrower_categorycode,
194 );
195
196 #writing the template
197 output_html_with_http_headers $cgi, $cookie, $template->output;
198
199 sub _skip_borrowers_with_nonzero_balance {
200     my $borrowers = shift;
201     my $balance;
202     @$borrowers = map {
203         my $patron = Koha::Patrons->find( $_->{borrowernumber} );
204         my $balance = $patron->account->balance;
205         (defined $balance && $balance != 0) ? (): ($_);
206     } @$borrowers;
207 }
208
209 sub _get_selection_params {
210     my ($not_borrowed_since, $borrower_dateexpiry, $borrower_lastseen,
211         $borrower_categorycode, $patron_list_id, $branch) = @_;
212
213     my $params = {};
214     $params->{not_borrowed_since} = output_pref({
215         dt         => $not_borrowed_since,
216         dateformat => 'iso',
217         dateonly   => 1
218     }) if $not_borrowed_since;
219     $params->{expired_before} = output_pref({
220         dt         => $borrower_dateexpiry,
221         dateformat => 'iso',
222         dateonly   => 1
223     }) if $borrower_dateexpiry;
224     $params->{last_seen} = output_pref({
225         dt         => $borrower_lastseen,
226         dateformat => 'iso',
227         dateonly   => 1
228     }) if $borrower_lastseen;
229     $params->{category_code} = $borrower_categorycode if $borrower_categorycode;
230     $params->{patron_list_id} = $patron_list_id if $patron_list_id;
231
232     if ( defined $branch and $branch ne '*' ) {
233         $params->{ branchcode } = $branch;
234     }
235
236     return $params;
237 };