Bug 15758: [QA Follow-up] Remove onlymine from Plugin/Branches.pm
[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;
38 use C4::Output;
39 use C4::Members;        # GetBorrowersWhoHavexxxBorrowed.
40 use C4::Circulation;    # AnonymiseIssueHistory.
41 use Koha::DateUtils qw( dt_from_string output_pref );
42 use Koha::Patron::Categories;
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 my $branch = $params->{ branch } || '*';
82 $template->param( current_branch => $branch );
83 $template->param( OnlyMine => C4::Context->only_my_library );
84
85 if ( $step == 2 ) {
86
87     my %checkboxes = map { $_ => 1 } split /\0/, $params->{'checkbox'};
88
89     my $patrons_to_delete;
90     if ( $checkboxes{borrower} ) {
91         $patrons_to_delete = GetBorrowersToExpunge(
92              _get_selection_params(
93                   $not_borrowed_since,
94                   $borrower_dateexpiry,
95                   $borrower_categorycode,
96                   $patron_list_id,
97                   $branch
98              )
99         );
100     }
101     _skip_borrowers_with_nonzero_balance($patrons_to_delete);
102
103     my $members_to_anonymize;
104     if ( $checkboxes{issue} ) {
105         if ( $branch eq '*' ) {
106             $members_to_anonymize = GetBorrowersWithIssuesHistoryOlderThan($last_issue_date);
107         } else {
108             $members_to_anonymize = GetBorrowersWithIssuesHistoryOlderThan($last_issue_date, $branch);
109         }
110     }
111
112     $template->param(
113         patrons_to_delete    => $patrons_to_delete,
114         patrons_to_anonymize => $members_to_anonymize,
115         patron_list_id       => $patron_list_id
116     );
117 }
118
119 elsif ( $step == 3 ) {
120     my $do_delete = $params->{'do_delete'};
121     my $do_anonym = $params->{'do_anonym'};
122
123     my ( $totalDel, $totalAno, $radio ) = ( 0, 0, 0 );
124
125     # delete members
126     if ($do_delete) {
127         my $patrons_to_delete = GetBorrowersToExpunge(
128                 _get_selection_params(
129                     $not_borrowed_since,
130                     $borrower_dateexpiry,
131                     $borrower_categorycode,
132                     $patron_list_id,
133                     $branch
134                 )
135             );
136         _skip_borrowers_with_nonzero_balance($patrons_to_delete);
137
138         $totalDel = scalar(@$patrons_to_delete);
139         $radio    = $params->{'radio'};
140         for ( my $i = 0 ; $i < $totalDel ; $i++ ) {
141             $radio eq 'testrun' && last;
142             my $borrowernumber = $patrons_to_delete->[$i]->{'borrowernumber'};
143             $radio eq 'trash' && MoveMemberToDeleted($borrowernumber);
144             C4::Members::HandleDelBorrower($borrowernumber);
145             DelMember($borrowernumber);
146         }
147         $template->param(
148             do_delete => '1',
149             TotalDel  => $totalDel
150         );
151     }
152
153     # Anonymising all members
154     if ($do_anonym) {
155         #FIXME: anonymisation errors are not handled
156         ($totalAno,my $anonymisation_error) = AnonymiseIssueHistory($last_issue_date);
157         $template->param(
158             do_anonym   => '1',
159         );
160     }
161
162     $template->param(
163         trash => ( $radio eq "trash" ) ? (1) : (0),
164         testrun => ( $radio eq "testrun" ) ? 1: 0,
165     );
166 } else { # $step == 1
167     my @all_lists = GetPatronLists();
168     my @non_empty_lists;
169     foreach my $list (@all_lists){
170     my @patrons = $list->patron_list_patrons();
171         if( scalar @patrons ) { push(@non_empty_lists,$list) }
172     }
173     $template->param( patron_lists => [ @non_empty_lists ] );
174 }
175
176 my $patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['description']});
177
178 $template->param(
179     step                   => $step,
180     not_borrowed_since   => $not_borrowed_since,
181     borrower_dateexpiry    => $borrower_dateexpiry,
182     last_issue_date        => $last_issue_date,
183     borrower_categorycodes => $patron_categories,
184     borrower_categorycode  => $borrower_categorycode,
185 );
186
187 #writing the template
188 output_html_with_http_headers $cgi, $cookie, $template->output;
189
190 sub _skip_borrowers_with_nonzero_balance {
191     my $borrowers = shift;
192     my $balance;
193     @$borrowers = map {
194         (undef, undef, $balance) = GetMemberIssuesAndFines( $_->{borrowernumber} );
195         (defined $balance && $balance != 0) ? (): ($_);
196     } @$borrowers;
197 }
198
199 sub _get_selection_params {
200     my ($not_borrowed_since, $borrower_dateexpiry,
201         $borrower_categorycode, $patron_list_id, $branch) = @_;
202
203     my $params = {};
204     $params->{not_borrowed_since} = output_pref({
205         dt         => $not_borrowed_since,
206         dateformat => 'iso',
207         dateonly   => 1
208     }) if $not_borrowed_since;
209     $params->{expired_before} = output_pref({
210         dt         => $borrower_dateexpiry,
211         dateformat => 'iso',
212         dateonly   => 1
213     }) if $borrower_dateexpiry;
214     $params->{category_code} = $borrower_categorycode if $borrower_categorycode;
215     $params->{patron_list_id} = $patron_list_id if $patron_list_id;
216
217     if ( defined $branch and $branch ne '*' ) {
218         $params->{ branchcode } = $branch;
219     }
220
221     return $params;
222 };