Bug 15098: Itemtype description missing from facets for itypes in a search group
[koha.git] / tools / cleanborrowers.pl
blobd1fc01c8572535b480c103d1fdf6cfdb1cb10284
1 #!/usr/bin/perl
3 # This file is part of Koha.
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.
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.
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>.
18 # Written by Antoine Farnault antoine@koha-fr.org on Nov. 2006.
20 =head1 cleanborrowers.pl
22 This script allows to do 2 things.
24 =over 2
26 =item * Anonymise the borrowers' issues if issue is older than a given date. see C<datefilter1>.
28 =item * Delete the borrowers who has not borrowered since a given date. see C<datefilter2>.
30 =back
32 =cut
34 use strict;
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 C4::VirtualShelves (); #no import
43 use Koha::DateUtils qw( dt_from_string output_pref );
44 use Date::Calc qw/Today Add_Delta_YM/;
46 my $cgi = new CGI;
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;
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;
68 my $borrower_categorycode = $params->{'borrower_categorycode'} || q{};
70 # getting the template
71 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
72 { template_name => "tools/cleanborrowers.tt",
73 query => $cgi,
74 type => "intranet",
75 authnotrequired => 0,
76 flagsrequired => { tools => 'delete_anonymize_patrons', catalogue => 1 },
80 if ( $step == 2 ) {
82 my %checkboxes = map { $_ => 1 } split /\0/, $params->{'checkbox'};
84 my $totalDel;
85 my $membersToDelete;
86 if ( $checkboxes{borrower} ) {
87 $membersToDelete = GetBorrowersToExpunge(
88 _get_selection_params($not_borrowered_since, $borrower_dateexpiry, $borrower_categorycode)
90 _skip_borrowers_with_nonzero_balance( $membersToDelete );
91 $totalDel = scalar @$membersToDelete;
94 my $totalAno;
95 my $membersToAnonymize;
96 if ( $checkboxes{issue} ) {
97 $membersToAnonymize = GetBorrowersWithIssuesHistoryOlderThan($last_issue_date);
98 $totalAno = scalar @$membersToAnonymize;
101 $template->param(
102 totalToDelete => $totalDel,
103 totalToAnonymize => $totalAno,
104 memberstodelete_list => $membersToDelete,
105 memberstoanonymize_list => $membersToAnonymize,
109 elsif ( $step == 3 ) {
110 my $do_delete = $params->{'do_delete'};
111 my $do_anonym = $params->{'do_anonym'};
113 my ( $totalDel, $totalAno, $radio ) = ( 0, 0, 0 );
115 # delete members
116 if ($do_delete) {
117 my $membersToDelete = GetBorrowersToExpunge(
118 _get_selection_params($not_borrowered_since, $borrower_dateexpiry, $borrower_categorycode)
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 );
130 $template->param(
131 do_delete => '1',
132 TotalDel => $totalDel
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',
145 $template->param(
146 trash => ( $radio eq "trash" ) ? (1) : (0),
147 testrun => ( $radio eq "testrun" ) ? 1: 0,
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,
160 #writing the template
161 output_html_with_http_headers $cgi, $cookie, $template->output;
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;
172 sub _get_selection_params {
173 my ($not_borrowered_since, $borrower_dateexpiry, $borrower_categorycode) = @_;
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->{category_code} = $borrower_categorycode if $borrower_categorycode;
188 return $params;