Independent-branchify MARC export. Add option to remove all holdings except your...
[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/Today Add_Delta_YM/;
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 => 'delete_anonymize_patrons', 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 %checkboxes = map { $_ => 1 } split /\0/, $params->{'checkbox'};
70
71     my $totalDel;
72     my $membersToDelete;
73     if ($checkboxes{borrower}) {
74         $membersToDelete = GetBorrowersWhoHaveNotBorrowedSince($filterdate1);
75         $totalDel = scalar @$membersToDelete;
76             
77     }
78     my $totalAno;
79     my $membersToAnonymize;
80     if ($checkboxes{issue}) {
81         $membersToAnonymize =
82           GetBorrowersWithIssuesHistoryOlderThan($filterdate2);
83         $totalAno = scalar @$membersToAnonymize;
84     }
85
86     $template->param(
87         step2            => 1,
88         totalToDelete    => $totalDel,
89         totalToAnonymize => $totalAno,
90         memberstodelete_list => $membersToDelete,    
91         memberstoanonymize_list => $membersToAnonymize,    
92         filterdate1      => format_date($filterdate1),
93         filterdate2      => format_date($filterdate2),
94     );
95 ### TODO : Use GetBorrowersNamesAndLatestIssue function in order to get the borrowers to delete or anonymize.
96 ### Now, we are only using total, which is not enough imlo
97     #writing the template
98     output_html_with_http_headers $cgi, $cookie, $template->output;
99     exit;
100 }
101
102 if ( $params->{'step3'} ) {
103     $filterdate1 = format_date_in_iso($params->{'filterdate1'});
104     $filterdate2 = format_date_in_iso($params->{'filterdate2'});
105     my $do_delete = $params->{'do_delete'};
106     my $do_anonym = $params->{'do_anonym'};
107
108     my ( $totalDel, $totalAno, $radio ) = ( 0, 0, 0 );
109     
110     # delete members
111     if ($do_delete) {
112         my $membersToDelete = GetBorrowersWhoHaveNotBorrowedSince($filterdate1);
113         $totalDel = scalar(@$membersToDelete);
114         $radio    = $params->{'radio'};
115         if ( $radio eq 'trash' ) {
116             my $i;
117             for ( $i = 0 ; $i < $totalDel ; $i++ ) {
118                 MoveMemberToDeleted( $membersToDelete->[$i]->{'borrowernumber'} );
119                 DelMember( $membersToDelete->[$i]->{'borrowernumber'} );
120             }
121         }
122         else {    # delete completly.
123             my $i;
124             for ( $i = 0 ; $i < $totalDel ; $i++ ) {
125                DelMember($membersToDelete->[$i]->{'borrowernumber'});
126             }
127         }
128         $template->param(
129             do_delete => '1',
130             TotalDel  => $totalDel
131         );
132     }
133     
134     # Anonymising all members
135     if ($do_anonym) {
136         $totalAno = AnonymiseIssueHistory($filterdate2);
137         $template->param(
138             filterdate1 => $filterdate2,
139             do_anonym   => '1',
140         );
141     }
142     
143     $template->param(
144         step3 => '1',
145         trash => ( $radio eq "trash" ) ? (1) : (0),
146     );
147
148     #writing the template
149     output_html_with_http_headers $cgi, $cookie, $template->output;
150     exit;
151 }
152
153 #default value set to the template are the 'CNIL' value.
154 my ( $year, $month, $day ) = &Today();
155 $filterdate1 = format_date(sprintf("%-04.4d-%-02.2d-%02.2d", Add_Delta_YM($year, $month, $day, -1, 0)));
156 $filterdate2 = format_date(sprintf("%-04.4d-%-02.2d-%02.2d", Add_Delta_YM($year, $month, $day, 0, -3)));
157
158 $template->param(
159     step1       => '1',
160     filterdate1 => $filterdate1,
161     filterdate2 => $filterdate2,
162     DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
163 );
164
165 #writing the template
166 output_html_with_http_headers $cgi, $cookie, $template->output;