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