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