Bug 25078: Fix indentation and style for additional descriptions
[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::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   ? dt_from_string $params->{not_borrowed_since}
57   : undef;
58 my $last_issue_date =         # the date which filter on borrowers last issue.
59   $params->{last_issue_date}
60   ? dt_from_string $params->{last_issue_date}
61   : undef;
62 my $borrower_dateexpiry =
63   $params->{borrower_dateexpiry}
64   ? dt_from_string $params->{borrower_dateexpiry}
65   : undef;
66 my $borrower_lastseen =
67   $params->{borrower_lastseen}
68   ? dt_from_string $params->{borrower_lastseen}
69   : undef;
70 my $patron_list_id = $params->{patron_list_id};
71
72 my $borrower_categorycode = $params->{'borrower_categorycode'} || q{};
73
74 # getting the template
75 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
76     {   template_name   => "tools/cleanborrowers.tt",
77         query           => $cgi,
78         type            => "intranet",
79         flagsrequired   => { tools => 'delete_anonymize_patrons', catalogue => 1 },
80     }
81 );
82
83 my $branch = $params->{ branch } || '*';
84 $template->param( current_branch => $branch );
85 $template->param( OnlyMine => C4::Context->only_my_library );
86
87 if ( $step == 2 ) {
88
89     my %checkboxes = map { $_ => 1 } split /\0/, $params->{'checkbox'};
90
91     my $patrons_to_delete;
92     if ( $checkboxes{borrower} ) {
93         $patrons_to_delete = GetBorrowersToExpunge(
94              _get_selection_params(
95                   $not_borrowed_since,
96                   $borrower_dateexpiry,
97                   $borrower_lastseen,
98                   $borrower_categorycode,
99                   $patron_list_id,
100                   $branch
101              )
102         );
103     }
104     _skip_borrowers_with_nonzero_balance($patrons_to_delete);
105
106     my $patrons_to_anonymize =
107         $checkboxes{issue}
108       ? $branch eq '*'
109           ? Koha::Patrons->search_patrons_to_anonymise( { before => $last_issue_date } )
110           : Koha::Patrons->search_patrons_to_anonymise( { before => $last_issue_date, library => $branch } )
111       : undef;
112
113     $template->param(
114         patrons_to_delete    => $patrons_to_delete,
115         patrons_to_anonymize => $patrons_to_anonymize,
116         patron_list_id       => $patron_list_id,
117     );
118 }
119
120 elsif ( $step == 3 ) {
121     my $do_delete = $params->{'do_delete'};
122     my $do_anonym = $params->{'do_anonym'};
123
124     my ( $totalDel, $totalAno, $radio ) = ( 0, 0, 0 );
125
126     # delete members
127     if ($do_delete) {
128         my $patrons_to_delete = GetBorrowersToExpunge(
129                 _get_selection_params(
130                     $not_borrowed_since,
131                     $borrower_dateexpiry,
132                     $borrower_lastseen,
133                     $borrower_categorycode,
134                     $patron_list_id,
135                     $branch
136                 )
137             );
138         _skip_borrowers_with_nonzero_balance($patrons_to_delete);
139
140         $totalDel = scalar(@$patrons_to_delete);
141         $radio    = $params->{'radio'};
142         for ( my $i = 0 ; $i < $totalDel ; $i++ ) {
143             $radio eq 'testrun' && last;
144             my $borrowernumber = $patrons_to_delete->[$i]->{'borrowernumber'};
145             my $patron = Koha::Patrons->find($borrowernumber);
146             $radio eq 'trash' && $patron->move_to_deleted;
147             $patron->delete;
148         }
149         $template->param(
150             do_delete => '1',
151             TotalDel  => $totalDel
152         );
153     }
154
155     # Anonymising all members
156     if ($do_anonym) {
157         #FIXME: anonymisation errors are not handled
158         my $rows = Koha::Patrons->search_patrons_to_anonymise( { before => $last_issue_date } )->anonymise_issue_history( { before => $last_issue_date } );
159         $template->param(
160             do_anonym   => $rows,
161         );
162     }
163
164     $template->param(
165         trash => ( $radio eq "trash" ) ? (1) : (0),
166         testrun => ( $radio eq "testrun" ) ? 1: 0,
167     );
168 } else { # $step == 1
169     my @all_lists = GetPatronLists();
170     my @non_empty_lists;
171     foreach my $list (@all_lists){
172     my @patrons = $list->patron_list_patrons();
173         if( scalar @patrons ) { push(@non_empty_lists,$list) }
174     }
175     $template->param( patron_lists => [ @non_empty_lists ] );
176 }
177
178 my $patron_categories = Koha::Patron::Categories->search_with_library_limits({}, {order_by => ['description']});
179
180 $template->param(
181     step                   => $step,
182     not_borrowed_since   => $not_borrowed_since,
183     borrower_dateexpiry    => $borrower_dateexpiry,
184     borrower_lastseen      => $borrower_lastseen,
185     last_issue_date        => $last_issue_date,
186     borrower_categorycodes => $patron_categories,
187     borrower_categorycode  => $borrower_categorycode,
188 );
189
190 #writing the template
191 output_html_with_http_headers $cgi, $cookie, $template->output;
192
193 sub _skip_borrowers_with_nonzero_balance {
194     my $borrowers = shift;
195     my $balance;
196     @$borrowers = map {
197         my $patron = Koha::Patrons->find( $_->{borrowernumber} );
198         my $balance = $patron->account->balance;
199         (defined $balance && $balance != 0) ? (): ($_);
200     } @$borrowers;
201 }
202
203 sub _get_selection_params {
204     my ($not_borrowed_since, $borrower_dateexpiry, $borrower_lastseen,
205         $borrower_categorycode, $patron_list_id, $branch) = @_;
206
207     my $params = {};
208     $params->{not_borrowed_since} = output_pref({
209         dt         => $not_borrowed_since,
210         dateformat => 'iso',
211         dateonly   => 1
212     }) if $not_borrowed_since;
213     $params->{expired_before} = output_pref({
214         dt         => $borrower_dateexpiry,
215         dateformat => 'iso',
216         dateonly   => 1
217     }) if $borrower_dateexpiry;
218     $params->{last_seen} = output_pref({
219         dt         => $borrower_lastseen,
220         dateformat => 'iso',
221         dateonly   => 1
222     }) if $borrower_lastseen;
223     $params->{category_code} = $borrower_categorycode if $borrower_categorycode;
224     $params->{patron_list_id} = $patron_list_id if $patron_list_id;
225
226     if ( defined $branch and $branch ne '*' ) {
227         $params->{ branchcode } = $branch;
228     }
229
230     return $params;
231 };