Bug 11975: (follow-up) simplify construction of params for GetBorrowersToExpunge()
[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 under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA  02111-1307 USA
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 borrowered 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;
38 use C4::Auth;
39 use C4::Output;
40 use C4::Members;        # GetBorrowersWhoHavexxxBorrowed.
41 use C4::Circulation;    # AnonymiseIssueHistory.
42 use C4::VirtualShelves ();    #no import
43 use Koha::DateUtils qw( dt_from_string output_pref );
44 use Date::Calc qw/Today Add_Delta_YM/;
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_borrowered_since =    # the date which filter on issue history.
56   $params->{not_borrowered_since}
57   ? dt_from_string $params->{not_borrowered_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
68 my $borrower_categorycode = $params->{'borrower_categorycode'} || q{};
69
70 # getting the template
71 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
72     {   template_name   => "tools/cleanborrowers.tmpl",
73         query           => $cgi,
74         type            => "intranet",
75         authnotrequired => 0,
76         flagsrequired   => { tools => 'delete_anonymize_patrons', catalogue => 1 },
77     }
78 );
79
80 if ( $step == 2 ) {
81
82     my %checkboxes = map { $_ => 1 } split /\0/, $params->{'checkbox'};
83
84     my $totalDel;
85     my $membersToDelete;
86     if ( $checkboxes{borrower} ) {
87         $membersToDelete = GetBorrowersToExpunge(
88             _get_selection_params($not_borrowered_since, $borrower_dateexpiry, $borrower_categorycode)
89         );
90         _skip_borrowers_with_nonzero_balance( $membersToDelete );
91         $totalDel = scalar @$membersToDelete;
92
93     }
94     my $totalAno;
95     my $membersToAnonymize;
96     if ( $checkboxes{issue} ) {
97         $membersToAnonymize = GetBorrowersWithIssuesHistoryOlderThan($last_issue_date);
98         $totalAno           = scalar @$membersToAnonymize;
99     }
100
101     $template->param(
102         totalToDelete           => $totalDel,
103         totalToAnonymize        => $totalAno,
104         memberstodelete_list    => $membersToDelete,
105         memberstoanonymize_list => $membersToAnonymize,
106     );
107 }
108
109 elsif ( $step == 3 ) {
110     my $do_delete = $params->{'do_delete'};
111     my $do_anonym = $params->{'do_anonym'};
112
113     my ( $totalDel, $totalAno, $radio ) = ( 0, 0, 0 );
114
115     # delete members
116     if ($do_delete) {
117         my $membersToDelete = GetBorrowersToExpunge(
118             _get_selection_params($not_borrowered_since, $borrower_dateexpiry, $borrower_categorycode)
119         );
120         _skip_borrowers_with_nonzero_balance( $membersToDelete );
121         $totalDel = scalar(@$membersToDelete);
122         $radio    = $params->{'radio'};
123         for ( my $i = 0 ; $i < $totalDel ; $i++ ) {
124             $radio eq 'testrun' && last;
125             my $borrowernumber = $membersToDelete->[$i]->{'borrowernumber'};
126             $radio eq 'trash' && MoveMemberToDeleted( $borrowernumber );
127             C4::VirtualShelves::HandleDelBorrower( $borrowernumber );
128             DelMember( $borrowernumber );
129         }
130         $template->param(
131             do_delete => '1',
132             TotalDel  => $totalDel
133         );
134     }
135
136     # Anonymising all members
137     if ($do_anonym) {
138         #FIXME: anonymisation errors are not handled
139         ($totalAno,my $anonymisation_error) = AnonymiseIssueHistory($last_issue_date);
140         $template->param(
141             do_anonym   => '1',
142         );
143     }
144
145     $template->param(
146         trash => ( $radio eq "trash" ) ? (1) : (0),
147         testrun => ( $radio eq "testrun" ) ? 1: 0,
148     );
149 }
150
151 $template->param(
152     step                   => $step,
153     not_borrowered_since   => $not_borrowered_since,
154     borrower_dateexpiry    => $borrower_dateexpiry,
155     last_issue_date        => $last_issue_date,
156     borrower_categorycodes => GetBorrowercategoryList(),
157     borrower_categorycode  => $borrower_categorycode,
158 );
159
160 #writing the template
161 output_html_with_http_headers $cgi, $cookie, $template->output;
162
163 sub _skip_borrowers_with_nonzero_balance {
164     my $borrowers = shift;
165     my $balance;
166     @$borrowers = map {
167         (undef, undef, $balance) = GetMemberIssuesAndFines( $_->{borrowernumber} );
168         ($balance != 0) ? (): ($_);
169     } @$borrowers;
170 }
171
172 sub _get_selection_params {
173     my ($not_borrowered_since, $borrower_dateexpiry, $borrower_categorycode) = @_;
174
175     my $params = {};
176     $params->{not_borrowered_since} = output_pref({
177         dt         => $not_borrowered_since,
178         dateformat => 'iso',
179         dateonly   => 1
180     }) if $not_borrowered_since;
181     $params->{expired_before} = output_pref({
182         dt         => $borrower_dateexpiry,
183         dateformat => 'iso',
184         dateonly   => 1
185     }) if $borrower_dateexpiry;
186     $params->{borrower_categorycode} = $borrower_categorycode if $borrower_categorycode;
187
188     return $params;
189 };