Bug 9259: Ability to delete a staged file once it has been cleaned
[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 strict;
35
36 #use warnings; FIXME - Bug 2505
37 use CGI qw ( -utf8 );
38 use C4::Auth;
39 use C4::Output;
40 use C4::Members;        # GetBorrowersWhoHavexxxBorrowed.
41 use C4::Circulation;    # AnonymiseIssueHistory.
42 use Koha::DateUtils qw( dt_from_string output_pref );
43 use Date::Calc qw/Today Add_Delta_YM/;
44 use Koha::List::Patron;
45
46 my $cgi = new CGI;
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 $patron_list_id = $params->{patron_list_id};
68
69 my $borrower_categorycode = $params->{'borrower_categorycode'} || q{};
70
71 # getting the template
72 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
73     {   template_name   => "tools/cleanborrowers.tt",
74         query           => $cgi,
75         type            => "intranet",
76         authnotrequired => 0,
77         flagsrequired   => { tools => 'delete_anonymize_patrons', catalogue => 1 },
78     }
79 );
80
81 if ( $step == 2 ) {
82
83     my %checkboxes = map { $_ => 1 } split /\0/, $params->{'checkbox'};
84
85     my $patrons_to_delete;
86     if ( $checkboxes{borrower} ) {
87         $patrons_to_delete = GetBorrowersToExpunge(
88              _get_selection_params(
89                   $not_borrowed_since,
90                   $borrower_dateexpiry,
91                   $borrower_categorycode,
92                   $patron_list_id,
93              )
94         );
95     }
96     _skip_borrowers_with_nonzero_balance($patrons_to_delete);
97
98     my $members_to_anonymize;
99     if ( $checkboxes{issue} ) {
100         $members_to_anonymize = GetBorrowersWithIssuesHistoryOlderThan($last_issue_date);
101     }
102
103     $template->param(
104         patrons_to_delete    => $patrons_to_delete,
105         patrons_to_anonymize => $members_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, $borrower_dateexpiry,
121                     $borrower_categorycode, $patron_list_id
122                 )
123             );
124         _skip_borrowers_with_nonzero_balance($patrons_to_delete);
125
126         $totalDel = scalar(@$patrons_to_delete);
127         $radio    = $params->{'radio'};
128         for ( my $i = 0 ; $i < $totalDel ; $i++ ) {
129             $radio eq 'testrun' && last;
130             my $borrowernumber = $patrons_to_delete->[$i]->{'borrowernumber'};
131             $radio eq 'trash' && MoveMemberToDeleted($borrowernumber);
132             C4::Members::HandleDelBorrower($borrowernumber);
133             DelMember($borrowernumber);
134         }
135         $template->param(
136             do_delete => '1',
137             TotalDel  => $totalDel
138         );
139     }
140
141     # Anonymising all members
142     if ($do_anonym) {
143         #FIXME: anonymisation errors are not handled
144         ($totalAno,my $anonymisation_error) = AnonymiseIssueHistory($last_issue_date);
145         $template->param(
146             do_anonym   => '1',
147         );
148     }
149
150     $template->param(
151         trash => ( $radio eq "trash" ) ? (1) : (0),
152         testrun => ( $radio eq "testrun" ) ? 1: 0,
153     );
154 } else { # $step == 1
155     my @all_lists = GetPatronLists();
156     my @non_empty_lists;
157     foreach my $list (@all_lists){
158     my @patrons = $list->patron_list_patrons();
159         if( scalar @patrons ) { push(@non_empty_lists,$list) }
160     }
161     $template->param( patron_lists => [ @non_empty_lists ] );
162 }
163
164 $template->param(
165     step                   => $step,
166     not_borrowed_since   => $not_borrowed_since,
167     borrower_dateexpiry    => $borrower_dateexpiry,
168     last_issue_date        => $last_issue_date,
169     borrower_categorycodes => GetBorrowercategoryList(),
170     borrower_categorycode  => $borrower_categorycode,
171 );
172
173 #writing the template
174 output_html_with_http_headers $cgi, $cookie, $template->output;
175
176 sub _skip_borrowers_with_nonzero_balance {
177     my $borrowers = shift;
178     my $balance;
179     @$borrowers = map {
180         (undef, undef, $balance) = GetMemberIssuesAndFines( $_->{borrowernumber} );
181         ($balance != 0) ? (): ($_);
182     } @$borrowers;
183 }
184
185 sub _get_selection_params {
186     my ($not_borrowed_since, $borrower_dateexpiry, $borrower_categorycode, $patron_list_id) = @_;
187
188     my $params = {};
189     $params->{not_borrowed_since} = output_pref({
190         dt         => $not_borrowed_since,
191         dateformat => 'iso',
192         dateonly   => 1
193     }) if $not_borrowed_since;
194     $params->{expired_before} = output_pref({
195         dt         => $borrower_dateexpiry,
196         dateformat => 'iso',
197         dateonly   => 1
198     }) if $borrower_dateexpiry;
199     $params->{category_code} = $borrower_categorycode if $borrower_categorycode;
200     $params->{patron_list_id} = $patron_list_id if $patron_list_id;
201
202     return $params;
203 };