Merge branch 'bug_9102' into 3.12-master
[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 =head1 cleanborrowers.pl
21
22 This script allows to do 2 things.
23
24 =over 2
25
26 =item * Anonymise the borrowers' issues if issue is older than a given date. see C<datefilter1>.
27
28 =item * Delete the borrowers who has not borrowered since a given date. see C<datefilter2>.
29
30 =back
31
32 =cut
33
34 use strict;
35
36 #use warnings; FIXME - Bug 2505
37 use CGI;
38 use C4::Auth;
39 use C4::Output;
40 use C4::Dates qw/format_date format_date_in_iso/;
41 use C4::Members;        # GetBorrowersWhoHavexxxBorrowed.
42 use C4::Circulation;    # AnonymiseIssueHistory.
43 use C4::VirtualShelves ();    #no import
44 use Date::Calc qw/Today Add_Delta_YM/;
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 my $borrower_dateexpiry;
57 my $borrower_categorycode;
58
59 # getting the template
60 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
61     {   template_name   => "tools/cleanborrowers.tmpl",
62         query           => $cgi,
63         type            => "intranet",
64         authnotrequired => 0,
65         flagsrequired   => { tools => 'delete_anonymize_patrons', 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     $borrower_dateexpiry   = format_date_in_iso( $params->{'borrower_dateexpiry'} );
73     $borrower_categorycode = $params->{'borrower_categorycode'};
74
75     my %checkboxes = map { $_ => 1 } split /\0/, $params->{'checkbox'};
76
77     my $totalDel;
78     my $membersToDelete;
79     if ( $checkboxes{borrower} ) {
80         $membersToDelete =
81           GetBorrowersToExpunge( { not_borrowered_since => $filterdate1, expired_before => $borrower_dateexpiry, category_code => $borrower_categorycode } );
82         $totalDel = scalar @$membersToDelete;
83
84     }
85     my $totalAno;
86     my $membersToAnonymize;
87     if ( $checkboxes{issue} ) {
88         $membersToAnonymize = GetBorrowersWithIssuesHistoryOlderThan($filterdate2);
89         $totalAno           = scalar @$membersToAnonymize;
90     }
91
92     $template->param(
93         step2                   => 1,
94         totalToDelete           => $totalDel,
95         totalToAnonymize        => $totalAno,
96         memberstodelete_list    => $membersToDelete,
97         memberstoanonymize_list => $membersToAnonymize,
98         filterdate1             => format_date($filterdate1),
99         filterdate2             => format_date($filterdate2),
100         borrower_dateexpiry     => $borrower_dateexpiry,
101         borrower_categorycode   => $borrower_categorycode,
102     );
103
104     ### TODO : Use GetBorrowersNamesAndLatestIssue function in order to get the borrowers to delete or anonymize.
105     output_html_with_http_headers $cgi, $cookie, $template->output;
106     exit;
107 }
108
109 if ( $params->{'step3'} ) {
110     $filterdate1           = format_date_in_iso( $params->{'filterdate1'} );
111     $filterdate2           = format_date_in_iso( $params->{'filterdate2'} );
112     $borrower_dateexpiry   = format_date_in_iso( $params->{'borrower_dateexpiry'} );
113     $borrower_categorycode = $params->{'borrower_categorycode'};
114
115     my $do_delete = $params->{'do_delete'};
116     my $do_anonym = $params->{'do_anonym'};
117
118     my ( $totalDel, $totalAno, $radio ) = ( 0, 0, 0 );
119
120     # delete members
121     if ($do_delete) {
122         my $membersToDelete =
123           GetBorrowersToExpunge( { not_borrowered_since => $filterdate1, expired_before => $borrower_dateexpiry, category_code => $borrower_categorycode } );
124         $totalDel = scalar(@$membersToDelete);
125         $radio    = $params->{'radio'};
126         if ( $radio eq 'trash' ) {
127             my $i;
128             for ( $i = 0 ; $i < $totalDel ; $i++ ) {
129                 MoveMemberToDeleted( $membersToDelete->[$i]->{'borrowernumber'} );
130                 C4::VirtualShelves::HandleDelBorrower( $membersToDelete->[$i]->{'borrowernumber'} );
131                 DelMember( $membersToDelete->[$i]->{'borrowernumber'} );
132             }
133         } else {    # delete completly.
134             my $i;
135             for ( $i = 0 ; $i < $totalDel ; $i++ ) {
136                 C4::VirtualShelves::HandleDelBorrower( $membersToDelete->[$i]->{'borrowernumber'} );
137                 DelMember( $membersToDelete->[$i]->{'borrowernumber'} );
138             }
139         }
140         $template->param(
141             do_delete => '1',
142             TotalDel  => $totalDel
143         );
144     }
145
146     # Anonymising all members
147     if ($do_anonym) {
148         $totalAno = AnonymiseIssueHistory($filterdate2);
149         $template->param(
150             filterdate1 => $filterdate2,
151             do_anonym   => '1',
152         );
153     }
154
155     $template->param(
156         step3 => '1',
157         trash => ( $radio eq "trash" ) ? (1) : (0),
158     );
159
160     #writing the template
161     output_html_with_http_headers $cgi, $cookie, $template->output;
162     exit;
163 }
164
165 $template->param(
166     step1                    => '1',
167     filterdate1              => $filterdate1,
168     filterdate2              => $filterdate2,
169     borrower_categorycodes   => GetBorrowercategoryList(),
170 );
171
172 #writing the template
173 output_html_with_http_headers $cgi, $cookie, $template->output;