Bug 12032: disable automatic URL conversion by TinyMCE in news
[koha.git] / tools / cleanborrowers.pl
blob9c1977b96ba9982670f070fbb4e536c121dbaf27
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 _skip_borrowers_with_nonzero_balance( $membersToDelete );
83 $totalDel = scalar @$membersToDelete;
86 my $totalAno;
87 my $membersToAnonymize;
88 if ( $checkboxes{issue} ) {
89 $membersToAnonymize = GetBorrowersWithIssuesHistoryOlderThan($filterdate2);
90 $totalAno = scalar @$membersToAnonymize;
93 $template->param(
94 step2 => 1,
95 totalToDelete => $totalDel,
96 totalToAnonymize => $totalAno,
97 memberstodelete_list => $membersToDelete,
98 memberstoanonymize_list => $membersToAnonymize,
99 filterdate1 => format_date($filterdate1),
100 filterdate2 => format_date($filterdate2),
101 borrower_dateexpiry => format_date($borrower_dateexpiry),
102 borrower_categorycode => $borrower_categorycode,
105 ### TODO : Use GetBorrowersNamesAndLatestIssue function in order to get the borrowers to delete or anonymize.
106 output_html_with_http_headers $cgi, $cookie, $template->output;
107 exit;
110 if ( $params->{'step3'} ) {
111 $filterdate1 = format_date_in_iso( $params->{'filterdate1'} );
112 $filterdate2 = format_date_in_iso( $params->{'filterdate2'} );
113 $borrower_dateexpiry = format_date_in_iso( $params->{'borrower_dateexpiry'} );
114 $borrower_categorycode = $params->{'borrower_categorycode'};
116 my $do_delete = $params->{'do_delete'};
117 my $do_anonym = $params->{'do_anonym'};
119 my ( $totalDel, $totalAno, $radio ) = ( 0, 0, 0 );
121 # delete members
122 if ($do_delete) {
123 my $membersToDelete =
124 GetBorrowersToExpunge( { not_borrowered_since => $filterdate1, expired_before => $borrower_dateexpiry, category_code => $borrower_categorycode } );
125 _skip_borrowers_with_nonzero_balance( $membersToDelete );
126 $totalDel = scalar(@$membersToDelete);
127 $radio = $params->{'radio'};
128 for ( my $i = 0 ; $i < $totalDel ; $i++ ) {
129 $radio eq 'testrun' && last;
130 my $borrowernumber = $membersToDelete->[$i]->{'borrowernumber'};
131 $radio eq 'trash' && MoveMemberToDeleted( $borrowernumber );
132 C4::VirtualShelves::HandleDelBorrower( $borrowernumber );
133 DelMember( $borrowernumber );
135 $template->param(
136 do_delete => '1',
137 TotalDel => $totalDel
141 # Anonymising all members
142 if ($do_anonym) {
143 #FIXME: anonymisation errors are not handled
144 ($totalAno,my $anonymisation_error) = AnonymiseIssueHistory($filterdate2);
145 $template->param(
146 filterdate1 => $filterdate2,
147 do_anonym => '1',
151 $template->param(
152 step3 => '1',
153 trash => ( $radio eq "trash" ) ? (1) : (0),
154 testrun => ( $radio eq "testrun" ) ? 1: 0,
157 #writing the template
158 output_html_with_http_headers $cgi, $cookie, $template->output;
159 exit;
162 $template->param(
163 step1 => '1',
164 filterdate1 => $filterdate1,
165 filterdate2 => $filterdate2,
166 borrower_categorycodes => GetBorrowercategoryList(),
169 #writing the template
170 output_html_with_http_headers $cgi, $cookie, $template->output;
172 sub _skip_borrowers_with_nonzero_balance {
173 my $borrowers = shift;
174 my $balance;
175 @$borrowers = map {
176 (undef, undef, $balance) = GetMemberIssuesAndFines( $_->{borrowernumber} );
177 ($balance != 0) ? (): ($_);
178 } @$borrowers;