Bug 8805 - Add a biblio-zebra-indexdefs.xsl for NORMARC
[koha.git] / tools / cleanborrowers.pl
blob0b37c20140dadca19eecc931a7cbfff7b10bb7b4
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.
21 =head1 cleanborrowers.pl
23 This script allows to do 2 things.
25 =over 2
27 =item * Anonymise the borrowers' issues if issue is older than a given date. see C<datefilter1>.
29 =item * Delete the borrowers who has not borrowered since a given date. see C<datefilter2>.
31 =back
33 =cut
35 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.
57 # getting the template
58 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
60 template_name => "tools/cleanborrowers.tmpl",
61 query => $cgi,
62 type => "intranet",
63 authnotrequired => 0,
64 flagsrequired => { tools => 'delete_anonymize_patrons', catalogue => 1 },
68 if ( $params->{'step2'} ) {
69 $filterdate1 = format_date_in_iso($params->{'filterdate1'});
70 $filterdate2 = format_date_in_iso($params->{'filterdate2'});
71 my %checkboxes = map { $_ => 1 } split /\0/, $params->{'checkbox'};
73 my $totalDel;
74 my $membersToDelete;
75 if ($checkboxes{borrower}) {
76 $membersToDelete = GetBorrowersWhoHaveNotBorrowedSince($filterdate1, 1);
77 $totalDel = scalar @$membersToDelete;
80 my $totalAno;
81 my $membersToAnonymize;
82 if ($checkboxes{issue}) {
83 $membersToAnonymize =
84 GetBorrowersWithIssuesHistoryOlderThan($filterdate2);
85 $totalAno = scalar @$membersToAnonymize;
88 $template->param(
89 step2 => 1,
90 totalToDelete => $totalDel,
91 totalToAnonymize => $totalAno,
92 memberstodelete_list => $membersToDelete,
93 memberstoanonymize_list => $membersToAnonymize,
94 filterdate1 => format_date($filterdate1),
95 filterdate2 => format_date($filterdate2),
97 ### TODO : Use GetBorrowersNamesAndLatestIssue function in order to get the borrowers to delete or anonymize.
98 ### Now, we are only using total, which is not enough imlo
99 #writing the template
100 output_html_with_http_headers $cgi, $cookie, $template->output;
101 exit;
104 if ( $params->{'step3'} ) {
105 $filterdate1 = format_date_in_iso($params->{'filterdate1'});
106 $filterdate2 = format_date_in_iso($params->{'filterdate2'});
107 my $do_delete = $params->{'do_delete'};
108 my $do_anonym = $params->{'do_anonym'};
110 my ( $totalDel, $totalAno, $radio ) = ( 0, 0, 0 );
112 # delete members
113 if ($do_delete) {
114 my $membersToDelete = GetBorrowersWhoHaveNotBorrowedSince($filterdate1, 1);
115 $totalDel = scalar(@$membersToDelete);
116 $radio = $params->{'radio'};
117 if ( $radio eq 'trash' ) {
118 my $i;
119 for ( $i = 0 ; $i < $totalDel ; $i++ ) {
120 MoveMemberToDeleted( $membersToDelete->[$i]->{'borrowernumber'} );
121 C4::VirtualShelves::HandleDelBorrower($membersToDelete->[$i]->{'borrowernumber'});
122 DelMember( $membersToDelete->[$i]->{'borrowernumber'} );
125 else { # delete completly.
126 my $i;
127 for ( $i = 0 ; $i < $totalDel ; $i++ ) {
128 C4::VirtualShelves::HandleDelBorrower($membersToDelete->[$i]->{'borrowernumber'});
129 DelMember($membersToDelete->[$i]->{'borrowernumber'});
132 $template->param(
133 do_delete => '1',
134 TotalDel => $totalDel
138 # Anonymising all members
139 if ($do_anonym) {
140 $totalAno = AnonymiseIssueHistory($filterdate2);
141 $template->param(
142 filterdate1 => $filterdate2,
143 do_anonym => '1',
147 $template->param(
148 step3 => '1',
149 trash => ( $radio eq "trash" ) ? (1) : (0),
152 #writing the template
153 output_html_with_http_headers $cgi, $cookie, $template->output;
154 exit;
157 #default value set to the template are the 'CNIL' value.
158 my ( $year, $month, $day ) = &Today();
159 $filterdate1 = format_date(sprintf("%-04.4d-%-02.2d-%02.2d", Add_Delta_YM($year, $month, $day, -1, 0)));
160 $filterdate2 = format_date(sprintf("%-04.4d-%-02.2d-%02.2d", Add_Delta_YM($year, $month, $day, 0, -3)));
162 $template->param(
163 step1 => '1',
164 filterdate1 => $filterdate1,
165 filterdate2 => $filterdate2,
166 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
169 #writing the template
170 output_html_with_http_headers $cgi, $cookie, $template->output;