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