warn on attempts to add duplicate item barcodes during batch import
[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::Date;
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 = format_date_in_iso($params->{'filterdate1'});
70     $filterdate2 = format_date_in_iso($params->{'filterdate2'});
71     my $checkbox = $params->{'checkbox'};
72
73     my $totalDel;
74     if ($checkbox eq "borrower") {
75         my $membersToDelete = GetBorrowersWhoHaveNotBorrowedSince($filterdate1);
76         $totalDel = scalar @$membersToDelete;
77     }
78
79     my $totalAno;
80     if ($checkbox eq "issue") {
81         my $membersToAnonymize =
82           GetBorrowersWithIssuesHistoryOlderThan($filterdate2);
83         $totalAno = scalar @$membersToAnonymize;
84     }
85
86     $template->param(
87         step2            => 1,
88         totalToDelete    => $totalDel,
89         totalToAnonymize => $totalAno,
90         filterdate1      => format_date($filterdate1),
91         filterdate2      => format_date($filterdate2),
92     );
93
94     #writing the template
95     output_html_with_http_headers $cgi, $cookie, $template->output;
96     exit;
97 }
98
99 if ( $params->{'step3'} ) {
100     $filterdate1 = format_date_in_iso($params->{'filterdate1'});
101     $filterdate2 = format_date_in_iso($params->{'filterdate2'});
102     my $do_delete = $params->{'do_delete'};
103     my $do_anonym = $params->{'do_anonym'};
104
105     my ( $totalDel, $totalAno, $radio ) = ( 0, 0, 0 );
106     
107     # delete members
108     if ($do_delete) {
109         my $membersToDelete = GetBorrowersWhoHaveNotBorrowedSince($filterdate1);
110         $totalDel = scalar(@$membersToDelete);
111         $radio    = $params->{'radio'};
112         if ( $radio eq 'trash' ) {
113             my $i;
114             for ( $i = 0 ; $i < $totalDel ; $i++ ) {
115                 MoveMemberToDeleted( $membersToDelete->[$i]->{'borrowernumber'} );
116                 DelMember( $membersToDelete->[$i]->{'borrowernumber'} );
117             }
118         }
119         else {    # delete completly.
120             my $i;
121             for ( $i = 0 ; $i < $totalDel ; $i++ ) {
122                DelMember($membersToDelete->[$i]->{'borrowernumber'});
123             }
124         }
125         $template->param(
126             do_delete => '1',
127             TotalDel  => $totalDel
128         );
129     }
130     
131     # Anonymising all members
132     if ($do_anonym) {
133         $totalAno = AnonymiseIssueHistory($filterdate2);
134         $template->param(
135             filterdate1 => $filterdate2,
136             do_anonym   => '1',
137         );
138     }
139     
140     $template->param(
141         step3 => '1',
142         trash => ( $radio eq "trash" ) ? (1) : (0),
143     );
144
145     #writing the template
146     output_html_with_http_headers $cgi, $cookie, $template->output;
147     exit;
148 }
149
150 #default value set to the template are the 'CNIL' value.
151 my ( $year, $month, $day ) = &Today();
152 my $tmpyear  = $year - 1;
153 my $tmpmonth = $month - 3;
154 $filterdate1 = format_date($tmpyear . "-" . $month . "-" . $day);
155 $filterdate2 = format_date($year . "-" . $tmpmonth . "-" . $day);
156
157 $template->param(
158     step1       => '1',
159     filterdate1 => $filterdate1,
160     filterdate2 => $filterdate2,
161     DHTMLcalendar_dateformat => get_date_format_string_for_DHTMLcalendar(),
162 );
163
164 #writing the template
165 output_html_with_http_headers $cgi, $cookie, $template->output;