Bug 11975: improve the batch patron deletion code
[koha_ffzg] / 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             {
89                 (
90                     $not_borrowered_since
91                     ? (
92                         not_borrowered_since => output_pref(
93                             {
94                                 dt         => $not_borrowered_since,
95                                 dateformat => 'iso',
96                                 dateonly   => 1
97                             }
98                         )
99                       )
100                     : ()
101                 ),
102                 (
103                     $borrower_dateexpiry
104                     ? (
105                         expired_before => output_pref(
106                             {
107                                 dt         => $borrower_dateexpiry,
108                                 dateformat => 'iso',
109                                 dateonly   => 1
110                             }
111                         )
112                       )
113                     : ()
114                 ),
115                 (
116                     $borrower_categorycode
117                     ? ( category_code => $borrower_categorycode )
118                     : ()
119                 ),
120             }
121         );
122         _skip_borrowers_with_nonzero_balance( $membersToDelete );
123         $totalDel = scalar @$membersToDelete;
124
125     }
126     my $totalAno;
127     my $membersToAnonymize;
128     if ( $checkboxes{issue} ) {
129         $membersToAnonymize = GetBorrowersWithIssuesHistoryOlderThan($last_issue_date);
130         $totalAno           = scalar @$membersToAnonymize;
131     }
132
133     $template->param(
134         totalToDelete           => $totalDel,
135         totalToAnonymize        => $totalAno,
136         memberstodelete_list    => $membersToDelete,
137         memberstoanonymize_list => $membersToAnonymize,
138     );
139 }
140
141 elsif ( $step == 3 ) {
142     my $do_delete = $params->{'do_delete'};
143     my $do_anonym = $params->{'do_anonym'};
144
145     my ( $totalDel, $totalAno, $radio ) = ( 0, 0, 0 );
146
147     # delete members
148     if ($do_delete) {
149         my $membersToDelete = GetBorrowersToExpunge(
150             {
151                 (
152                     $not_borrowered_since
153                     ? (
154                         not_borrowered_since => output_pref(
155                             {
156                                 dt         => $not_borrowered_since,
157                                 dateformat => 'iso'
158                             }
159                         )
160                       )
161                     : ()
162                 ),
163                 (
164                     $borrower_dateexpiry
165                     ? (
166                         expired_before => output_pref(
167                             { dt => $borrower_dateexpiry, dateformat => 'iso' }
168                         )
169                       )
170                     : ()
171                 ),
172                 (
173                     $borrower_categorycode
174                     ? ( category_code => $borrower_categorycode )
175                     : ()
176                 ),
177             }
178         );
179         _skip_borrowers_with_nonzero_balance( $membersToDelete );
180         $totalDel = scalar(@$membersToDelete);
181         $radio    = $params->{'radio'};
182         for ( my $i = 0 ; $i < $totalDel ; $i++ ) {
183             $radio eq 'testrun' && last;
184             my $borrowernumber = $membersToDelete->[$i]->{'borrowernumber'};
185             $radio eq 'trash' && MoveMemberToDeleted( $borrowernumber );
186             C4::VirtualShelves::HandleDelBorrower( $borrowernumber );
187             DelMember( $borrowernumber );
188         }
189         $template->param(
190             do_delete => '1',
191             TotalDel  => $totalDel
192         );
193     }
194
195     # Anonymising all members
196     if ($do_anonym) {
197         #FIXME: anonymisation errors are not handled
198         ($totalAno,my $anonymisation_error) = AnonymiseIssueHistory($last_issue_date);
199         $template->param(
200             do_anonym   => '1',
201         );
202     }
203
204     $template->param(
205         trash => ( $radio eq "trash" ) ? (1) : (0),
206         testrun => ( $radio eq "testrun" ) ? 1: 0,
207     );
208 }
209
210 $template->param(
211     step                   => $step,
212     not_borrowered_since   => $not_borrowered_since,
213     borrower_dateexpiry    => $borrower_dateexpiry,
214     last_issue_date        => $last_issue_date,
215     borrower_categorycodes => GetBorrowercategoryList(),
216     borrower_categorycode  => $borrower_categorycode,
217 );
218
219 #writing the template
220 output_html_with_http_headers $cgi, $cookie, $template->output;
221
222 sub _skip_borrowers_with_nonzero_balance {
223     my $borrowers = shift;
224     my $balance;
225     @$borrowers = map {
226         (undef, undef, $balance) = GetMemberIssuesAndFines( $_->{borrowernumber} );
227         ($balance != 0) ? (): ($_);
228     } @$borrowers;
229 }