Bug 11477 - Add names for librarian and borrowers in the logs
[koha.git] / tools / cleanborrowers.pl
blob0d48b99946db81f54f3d899b8f8aaad740f75bd0
1 #!/usr/bin/perl
3 # This file is part of Koha.
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.
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.
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
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;
38 use C4::Auth;
39 use C4::Output;
40 use C4::Dates qw/format_date format_date_in_iso/;
41 use C4::Members; # GetBorrowersWhoHavexxxBorrowed.
42 use C4::Circulation; # AnonymiseIssueHistory.
43 use C4::VirtualShelves (); #no import
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 $filterdate1; # the date which filter on issue history.
55 my $filterdate2; # the date which filter on borrowers last issue.
56 my $borrower_dateexpiry;
57 my $borrower_categorycode;
59 # getting the template
60 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
61 { template_name => "tools/cleanborrowers.tmpl",
62 query => $cgi,
63 type => "intranet",
64 authnotrequired => 0,
65 flagsrequired => { tools => 'delete_anonymize_patrons', catalogue => 1 },
69 if ( $params->{'step2'} ) {
70 $filterdate1 = format_date_in_iso( $params->{'filterdate1'} );
71 $filterdate2 = format_date_in_iso( $params->{'filterdate2'} );
72 $borrower_dateexpiry = format_date_in_iso( $params->{'borrower_dateexpiry'} );
73 $borrower_categorycode = $params->{'borrower_categorycode'};
75 my %checkboxes = map { $_ => 1 } split /\0/, $params->{'checkbox'};
77 my $totalDel;
78 my $membersToDelete;
79 if ( $checkboxes{borrower} ) {
80 $membersToDelete =
81 GetBorrowersToExpunge( { not_borrowered_since => $filterdate1, expired_before => $borrower_dateexpiry, category_code => $borrower_categorycode } );
82 $totalDel = scalar @$membersToDelete;
85 my $totalAno;
86 my $membersToAnonymize;
87 if ( $checkboxes{issue} ) {
88 $membersToAnonymize = GetBorrowersWithIssuesHistoryOlderThan($filterdate2);
89 $totalAno = scalar @$membersToAnonymize;
92 $template->param(
93 step2 => 1,
94 totalToDelete => $totalDel,
95 totalToAnonymize => $totalAno,
96 memberstodelete_list => $membersToDelete,
97 memberstoanonymize_list => $membersToAnonymize,
98 filterdate1 => format_date($filterdate1),
99 filterdate2 => format_date($filterdate2),
100 borrower_dateexpiry => $borrower_dateexpiry,
101 borrower_categorycode => $borrower_categorycode,
104 ### TODO : Use GetBorrowersNamesAndLatestIssue function in order to get the borrowers to delete or anonymize.
105 output_html_with_http_headers $cgi, $cookie, $template->output;
106 exit;
109 if ( $params->{'step3'} ) {
110 $filterdate1 = format_date_in_iso( $params->{'filterdate1'} );
111 $filterdate2 = format_date_in_iso( $params->{'filterdate2'} );
112 $borrower_dateexpiry = format_date_in_iso( $params->{'borrower_dateexpiry'} );
113 $borrower_categorycode = $params->{'borrower_categorycode'};
115 my $do_delete = $params->{'do_delete'};
116 my $do_anonym = $params->{'do_anonym'};
118 my ( $totalDel, $totalAno, $radio ) = ( 0, 0, 0 );
120 # delete members
121 if ($do_delete) {
122 my $membersToDelete =
123 GetBorrowersToExpunge( { not_borrowered_since => $filterdate1, expired_before => $borrower_dateexpiry, category_code => $borrower_categorycode } );
124 $totalDel = scalar(@$membersToDelete);
125 $radio = $params->{'radio'};
126 if ( $radio eq 'trash' ) {
127 my $i;
128 for ( $i = 0 ; $i < $totalDel ; $i++ ) {
129 MoveMemberToDeleted( $membersToDelete->[$i]->{'borrowernumber'} );
130 C4::VirtualShelves::HandleDelBorrower( $membersToDelete->[$i]->{'borrowernumber'} );
131 DelMember( $membersToDelete->[$i]->{'borrowernumber'} );
133 } else { # delete completly.
134 my $i;
135 for ( $i = 0 ; $i < $totalDel ; $i++ ) {
136 C4::VirtualShelves::HandleDelBorrower( $membersToDelete->[$i]->{'borrowernumber'} );
137 DelMember( $membersToDelete->[$i]->{'borrowernumber'} );
140 $template->param(
141 do_delete => '1',
142 TotalDel => $totalDel
146 # Anonymising all members
147 if ($do_anonym) {
148 #FIXME: anonymisation errors are not handled
149 ($totalAno,my $anonymisation_error) = AnonymiseIssueHistory($filterdate2);
150 $template->param(
151 filterdate1 => $filterdate2,
152 do_anonym => '1',
156 $template->param(
157 step3 => '1',
158 trash => ( $radio eq "trash" ) ? (1) : (0),
161 #writing the template
162 output_html_with_http_headers $cgi, $cookie, $template->output;
163 exit;
166 $template->param(
167 step1 => '1',
168 filterdate1 => $filterdate1,
169 filterdate2 => $filterdate2,
170 borrower_categorycodes => GetBorrowercategoryList(),
173 #writing the template
174 output_html_with_http_headers $cgi, $cookie, $template->output;