Missed test
[koha.git] / tools / cleanborrowers.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
13 #
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
17 #
18 #   Written by Antoine Farnault antoine@koha-fr.org on Nov. 2006.
19
20 # $Id$
21
22 =head1 cleanborrowers.pl
23
24 This script allows to do 2 things.
25
26 =over 2
27
28 =item * Anonymise the borrowers' issues if issue is older than a given date. see C<datefilter1>.
29
30 =item * Delete the borrowers who has not borrowered since a given date. see C<datefilter2>.
31
32 =back
33
34 =cut
35
36 use strict;
37 use CGI;
38 use C4::Auth;
39 use C4::Output;
40
41
42 use C4::Members;               # GetBorrowersWhoHavexxxBorrowed.
43 use C4::Circulation;    # AnonymiseIssueHistory.
44 use Date::Calc qw/Date_to_Days Today/;
45
46 my $cgi = new CGI;
47
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;
53
54 my $filterdate1;               # the date which filter on issue history.
55 my $filterdate2;               # the date which filter on borrowers last issue.
56
57 # getting the template
58 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
59     {
60         template_name   => "tools/cleanborrowers.tmpl",
61         query           => $cgi,
62         type            => "intranet",
63         authnotrequired => 0,
64         flagsrequired   => { tools => 1, catalogue => 1 },
65     }
66 );
67
68 if ( $params->{'step2'} ) {
69     $filterdate1 = $params->{'filterdate1'};
70     $filterdate2 = $params->{'filterdate2'};
71     my $checkbox = $params->{'checkbox'};
72
73     my $totalDel;
74     if ($checkbox eq "borrower") {
75         $filterdate1 = $params->{'filterdate1'};
76         my $membersToDelete = GetBorrowersWhoHaveNotBorrowedSince($filterdate1);
77         $totalDel = scalar @$membersToDelete;
78     }
79
80     my $totalAno;
81     if ($checkbox eq "issue") {
82         $filterdate2 = $params->{'filterdate2'};
83         my $membersToAnonymize =
84           GetBorrowersWithIssuesHistoryOlderThan($filterdate2);
85         $totalAno = scalar @$membersToAnonymize;
86     }
87
88     $template->param(
89         step2            => 1,
90         totalToDelete    => $totalDel,
91         totalToAnonymize => $totalAno,
92         filterdate1      => $filterdate1,
93         filterdate2      => $filterdate2
94     );
95
96     #writing the template
97     output_html_with_http_headers $cgi, $cookie, $template->output;
98     exit;
99 }
100
101 if ( $params->{'step3'} ) {
102     $filterdate1 = $params->{'filterdate1'};
103     $filterdate2 = $params->{'filterdate2'};
104     my $do_delete = $params->{'do_delete'};
105     my $do_anonym = $params->{'do_anonym'};
106
107     my ( $totalDel, $totalAno, $radio ) = ( 0, 0, 0 );
108     
109     # delete members
110     if ($do_delete) {
111         my $membersToDelete = GetBorrowersWhoHaveNotBorrowedSince($filterdate1);
112         $totalDel = scalar(@$membersToDelete);
113         $radio    = $params->{'radio'};
114         if ( $radio eq 'trash' ) {
115             my $i;
116             for ( $i = 0 ; $i < $totalDel ; $i++ ) {
117                 MoveMemberToDeleted( $membersToDelete->[$i]->{'borrowernumber'} );
118                 DelMember( $membersToDelete->[$i]->{'borrowernumber'} );
119             }
120         }
121         else {    # delete completly.
122             my $i;
123             for ( $i = 0 ; $i < $totalDel ; $i++ ) {
124                DelMember($membersToDelete->[$i]->{'borrowernumber'});
125             }
126         }
127         $template->param(
128             do_delete => '1',
129             TotalDel  => $totalDel
130         );
131     }
132     
133     # Anonymising all members
134     if ($do_anonym) {
135         $totalAno = AnonymiseIssueHistory($filterdate2);
136         $template->param(
137             filterdate1 => $filterdate2,
138             do_anonym   => '1',
139         );
140     }
141     
142     $template->param(
143         step3 => '1',
144         trash => ( $radio eq "trash" ) ? (1) : (0),
145     );
146
147     #writing the template
148     output_html_with_http_headers $cgi, $cookie, $template->output;
149     exit;
150 }
151
152 #default value set to the template are the 'CNIL' value.
153 my ( $year, $month, $day ) = &Today();
154 my $tmpyear  = $year - 1;
155 my $tmpmonth = $month - 3;
156 $filterdate1 = $year . "-" . $tmpmonth . "-" . $day;
157 $filterdate2 = $tmpyear . "-" . $month . "-" . $day;
158
159 $template->param(
160     step1       => '1',
161     filterdate1 => $filterdate1,
162     filterdate2 => $filterdate2
163 );
164
165 #writing the template
166 output_html_with_http_headers $cgi, $cookie, $template->output;