Bug 36924: Remove "I don't know what to say" warnings
[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
41 use Koha::DateUtils qw( dt_from_string );
42 use Koha::Old::Checkouts;
43 use Koha::Patron::Categories;
44 use Koha::Patrons;
45 use Koha::List::Patron qw( GetPatronLists );
46
47 my $cgi = CGI->new;
48
49 # Fetch the parameter list as a hash in scalar context:
50 #  * returns parameter list as tied hash ref
51 #  * we can edit the values by changing the key
52 #  * multivalued CGI parameters are returned as a packaged string separated by "\0" (null)
53 my $params = $cgi->Vars;
54
55 my $step = $params->{step} || 1;
56 my $not_borrowed_since =    # the date which filter on issue history.
57   $params->{not_borrowed_since};
58 my $last_issue_date =         # the date which filter on borrowers last issue.
59   $params->{last_issue_date};
60 my $patron_list_id = $params->{patron_list_id};
61 my $borrower_dateexpiry = $params->{borrower_dateexpiry};
62 my $borrower_lastseen = $params->{borrower_lastseen};
63
64 my $borrower_categorycode = $params->{'borrower_categorycode'} || q{};
65
66 # getting the template
67 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
68     {   template_name   => "tools/cleanborrowers.tt",
69         query           => $cgi,
70         type            => "intranet",
71         flagsrequired   => { tools => 'delete_anonymize_patrons', catalogue => 1 },
72     }
73 );
74
75 my $branch = $params->{ branch } || '*';
76 $template->param( current_branch => $branch );
77 $template->param( OnlyMine => C4::Context->only_my_library );
78
79 if ( $step == 2 ) {
80
81     my %checkboxes = map { $_ => 1 } split /\0/, $params->{'checkbox'};
82
83     my $patrons_to_delete;
84     if ( $checkboxes{borrower} ) {
85         $patrons_to_delete = GetBorrowersToExpunge(
86              _get_selection_params(
87                   $not_borrowed_since,
88                   $borrower_dateexpiry,
89                   $borrower_lastseen,
90                   $borrower_categorycode,
91                   $patron_list_id,
92                   $branch
93              )
94         );
95     }
96     _skip_borrowers_with_nonzero_balance($patrons_to_delete);
97
98     my $patrons_to_anonymize =
99         $checkboxes{issue}
100       ? $branch eq '*'
101           ? Koha::Patrons->search_patrons_to_anonymise( { before => $last_issue_date } )
102           : Koha::Patrons->search_patrons_to_anonymise( { before => $last_issue_date, library => $branch } )
103       : undef;
104
105     $template->param(
106         patrons_to_delete    => $patrons_to_delete,
107         patrons_to_anonymize => $patrons_to_anonymize,
108         patron_list_id       => $patron_list_id,
109     );
110 }
111
112 elsif ( $step == 3 ) {
113     my $do_delete = $params->{'do_delete'};
114     my $do_anonym = $params->{'do_anonym'};
115
116     my ( $totalDel, $totalAno, $radio ) = ( 0, 0, 0 );
117
118     # delete members
119     if ($do_delete) {
120         my $patrons_to_delete = GetBorrowersToExpunge(
121                 _get_selection_params(
122                     $not_borrowed_since,
123                     $borrower_dateexpiry,
124                     $borrower_lastseen,
125                     $borrower_categorycode,
126                     $patron_list_id,
127                     $branch
128                 )
129             );
130         _skip_borrowers_with_nonzero_balance($patrons_to_delete);
131
132         $totalDel = scalar(@$patrons_to_delete);
133         $radio    = $params->{'radio'};
134         for ( my $i = 0 ; $i < $totalDel ; $i++ ) {
135             $radio eq 'testrun' && last;
136             my $borrowernumber = $patrons_to_delete->[$i]->{'borrowernumber'};
137             my $patron = Koha::Patrons->find($borrowernumber);
138             $radio eq 'trash' && $patron->move_to_deleted;
139             $patron->delete;
140         }
141         $template->param(
142             do_delete => '1',
143             TotalDel  => $totalDel
144         );
145     }
146
147     # Anonymising all members
148     if ($do_anonym) {
149         #FIXME: anonymisation errors are not handled
150         my $rows = Koha::Old::Checkouts
151                      ->filter_by_anonymizable
152                      ->filter_by_last_update({
153                          to => dt_from_string($last_issue_date), timestamp_column_name => 'returndate' })
154                      ->anonymize;
155
156         $template->param(
157             do_anonym   => $rows,
158         );
159     }
160
161     $template->param(
162         trash => ( $radio eq "trash" ) ? (1) : (0),
163         testrun => ( $radio eq "testrun" ) ? 1: 0,
164     );
165 } else { # $step == 1
166     my @all_lists = GetPatronLists();
167     my @non_empty_lists;
168     foreach my $list (@all_lists){
169     my @patrons = $list->patron_list_patrons();
170         if( scalar @patrons ) { push(@non_empty_lists,$list) }
171     }
172     $template->param( patron_lists => [ @non_empty_lists ] );
173 }
174
175 my $patron_categories = Koha::Patron::Categories->search_with_library_limits({}, {order_by => ['description']});
176
177 $template->param(
178     step                   => $step,
179     not_borrowed_since   => $not_borrowed_since,
180     borrower_dateexpiry    => $borrower_dateexpiry,
181     borrower_lastseen      => $borrower_lastseen,
182     last_issue_date        => $last_issue_date,
183     borrower_categorycodes => $patron_categories,
184     borrower_categorycode  => $borrower_categorycode,
185 );
186
187 #writing the template
188 output_html_with_http_headers $cgi, $cookie, $template->output;
189
190 sub _skip_borrowers_with_nonzero_balance {
191     my $borrowers = shift;
192     my $balance;
193     @$borrowers = map {
194         my $patron = Koha::Patrons->find( $_->{borrowernumber} );
195         my $balance = $patron->account->balance;
196         (defined $balance && $balance != 0) ? (): ($_);
197     } @$borrowers;
198 }
199
200 sub _get_selection_params {
201     my ($not_borrowed_since, $borrower_dateexpiry, $borrower_lastseen,
202         $borrower_categorycode, $patron_list_id, $branch) = @_;
203
204     my $params = {};
205     $params->{not_borrowed_since} = $not_borrowed_since;
206     $params->{expired_before} = $borrower_dateexpiry;
207     $params->{last_seen} = $borrower_lastseen;
208     $params->{category_code} = $borrower_categorycode if $borrower_categorycode;
209     $params->{patron_list_id} = $patron_list_id if $patron_list_id;
210
211     if ( defined $branch and $branch ne '*' ) {
212         $params->{ branchcode } = $branch;
213     }
214
215     return $params;
216 };