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.
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>.
36 #use warnings; FIXME - Bug 2505
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/;
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
}
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
}
63 my $borrower_dateexpiry =
64 $params->{borrower_dateexpiry
}
65 ? dt_from_string
$params->{borrower_dateexpiry
}
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",
76 flagsrequired
=> { tools
=> 'delete_anonymize_patrons', catalogue
=> 1 },
82 my %checkboxes = map { $_ => 1 } split /\0/, $params->{'checkbox'};
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;
95 my $membersToAnonymize;
96 if ( $checkboxes{issue
} ) {
97 $membersToAnonymize = GetBorrowersWithIssuesHistoryOlderThan
($last_issue_date);
98 $totalAno = scalar @
$membersToAnonymize;
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 );
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 );
132 TotalDel
=> $totalDel
136 # Anonymising all members
138 #FIXME: anonymisation errors are not handled
139 ($totalAno,my $anonymisation_error) = AnonymiseIssueHistory
($last_issue_date);
146 trash
=> ( $radio eq "trash" ) ?
(1) : (0),
147 testrun
=> ( $radio eq "testrun" ) ?
1: 0,
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;
167 (undef, undef, $balance) = GetMemberIssuesAndFines
( $_->{borrowernumber
} );
168 ($balance != 0) ?
(): ($_);
172 sub _get_selection_params
{
173 my ($not_borrowered_since, $borrower_dateexpiry, $borrower_categorycode) = @_;
176 $params->{not_borrowered_since
} = output_pref
({
177 dt
=> $not_borrowered_since,
180 }) if $not_borrowered_since;
181 $params->{expired_before
} = output_pref
({
182 dt
=> $borrower_dateexpiry,
185 }) if $borrower_dateexpiry;
186 $params->{category_code
} = $borrower_categorycode if $borrower_categorycode;