Bug 16212 - Generate initial swagger.min.json file
[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
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
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 borrowed since a given date. see C<datefilter2>.
29
30 =back
31
32 =cut
33
34 use Modern::Perl;
35
36 use CGI qw ( -utf8 );
37 use C4::Auth;
38 use C4::Output;
39 use C4::Members;        # GetBorrowersWhoHavexxxBorrowed.
40 use C4::Circulation;    # AnonymiseIssueHistory.
41 use Koha::DateUtils qw( dt_from_string output_pref );
42 use Date::Calc qw/Today Add_Delta_YM/;
43 use Koha::List::Patron;
44
45 my $cgi = new CGI;
46
47 # Fetch the paramater list as a hash in scalar context:
48 #  * returns paramater list as tied hash ref
49 #  * we can edit the values by changing the key
50 #  * multivalued CGI paramaters are returned as a packaged string separated by "\0" (null)
51 my $params = $cgi->Vars;
52
53 my $step = $params->{step} || 1;
54 my $not_borrowed_since =    # the date which filter on issue history.
55   $params->{not_borrowed_since}
56   ? dt_from_string $params->{not_borrowed_since}
57   : undef;
58 my $last_issue_date =         # the date which filter on borrowers last issue.
59   $params->{last_issue_date}
60   ? dt_from_string $params->{last_issue_date}
61   : undef;
62 my $borrower_dateexpiry =
63   $params->{borrower_dateexpiry}
64   ? dt_from_string $params->{borrower_dateexpiry}
65   : undef;
66 my $patron_list_id = $params->{patron_list_id};
67
68 my $borrower_categorycode = $params->{'borrower_categorycode'} || q{};
69
70 # getting the template
71 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
72     {   template_name   => "tools/cleanborrowers.tt",
73         query           => $cgi,
74         type            => "intranet",
75         authnotrequired => 0,
76         flagsrequired   => { tools => 'delete_anonymize_patrons', catalogue => 1 },
77     }
78 );
79
80 my $branch = $params->{ branch } || '*';
81 $template->param( current_branch => $branch );
82
83 if ( $step == 2 ) {
84
85     my %checkboxes = map { $_ => 1 } split /\0/, $params->{'checkbox'};
86
87     my $patrons_to_delete;
88     if ( $checkboxes{borrower} ) {
89         $patrons_to_delete = GetBorrowersToExpunge(
90              _get_selection_params(
91                   $not_borrowed_since,
92                   $borrower_dateexpiry,
93                   $borrower_categorycode,
94                   $patron_list_id,
95                   $branch
96              )
97         );
98     }
99     _skip_borrowers_with_nonzero_balance($patrons_to_delete);
100
101     my $members_to_anonymize;
102     if ( $checkboxes{issue} ) {
103         if ( $branch eq '*' ) {
104             $members_to_anonymize = GetBorrowersWithIssuesHistoryOlderThan($last_issue_date);
105         } else {
106             $members_to_anonymize = GetBorrowersWithIssuesHistoryOlderThan($last_issue_date, $branch);
107         }
108     }
109
110     $template->param(
111         patrons_to_delete    => $patrons_to_delete,
112         patrons_to_anonymize => $members_to_anonymize,
113         patron_list_id       => $patron_list_id
114     );
115 }
116
117 elsif ( $step == 3 ) {
118     my $do_delete = $params->{'do_delete'};
119     my $do_anonym = $params->{'do_anonym'};
120
121     my ( $totalDel, $totalAno, $radio ) = ( 0, 0, 0 );
122
123     # delete members
124     if ($do_delete) {
125         my $patrons_to_delete = GetBorrowersToExpunge(
126                 _get_selection_params(
127                     $not_borrowed_since,
128                     $borrower_dateexpiry,
129                     $borrower_categorycode,
130                     $patron_list_id,
131                     $branch
132                 )
133             );
134         _skip_borrowers_with_nonzero_balance($patrons_to_delete);
135
136         $totalDel = scalar(@$patrons_to_delete);
137         $radio    = $params->{'radio'};
138         for ( my $i = 0 ; $i < $totalDel ; $i++ ) {
139             $radio eq 'testrun' && last;
140             my $borrowernumber = $patrons_to_delete->[$i]->{'borrowernumber'};
141             $radio eq 'trash' && MoveMemberToDeleted($borrowernumber);
142             C4::Members::HandleDelBorrower($borrowernumber);
143             DelMember($borrowernumber);
144         }
145         $template->param(
146             do_delete => '1',
147             TotalDel  => $totalDel
148         );
149     }
150
151     # Anonymising all members
152     if ($do_anonym) {
153         #FIXME: anonymisation errors are not handled
154         ($totalAno,my $anonymisation_error) = AnonymiseIssueHistory($last_issue_date);
155         $template->param(
156             do_anonym   => '1',
157         );
158     }
159
160     $template->param(
161         trash => ( $radio eq "trash" ) ? (1) : (0),
162         testrun => ( $radio eq "testrun" ) ? 1: 0,
163     );
164 } else { # $step == 1
165     my @all_lists = GetPatronLists();
166     my @non_empty_lists;
167     foreach my $list (@all_lists){
168     my @patrons = $list->patron_list_patrons();
169         if( scalar @patrons ) { push(@non_empty_lists,$list) }
170     }
171     $template->param( patron_lists => [ @non_empty_lists ] );
172 }
173
174 $template->param(
175     step                   => $step,
176     not_borrowed_since   => $not_borrowed_since,
177     borrower_dateexpiry    => $borrower_dateexpiry,
178     last_issue_date        => $last_issue_date,
179     borrower_categorycodes => GetBorrowercategoryList(),
180     borrower_categorycode  => $borrower_categorycode,
181 );
182
183 #writing the template
184 output_html_with_http_headers $cgi, $cookie, $template->output;
185
186 sub _skip_borrowers_with_nonzero_balance {
187     my $borrowers = shift;
188     my $balance;
189     @$borrowers = map {
190         (undef, undef, $balance) = GetMemberIssuesAndFines( $_->{borrowernumber} );
191         (defined $balance && $balance != 0) ? (): ($_);
192     } @$borrowers;
193 }
194
195 sub _get_selection_params {
196     my ($not_borrowed_since, $borrower_dateexpiry,
197         $borrower_categorycode, $patron_list_id, $branch) = @_;
198
199     my $params = {};
200     $params->{not_borrowed_since} = output_pref({
201         dt         => $not_borrowed_since,
202         dateformat => 'iso',
203         dateonly   => 1
204     }) if $not_borrowed_since;
205     $params->{expired_before} = output_pref({
206         dt         => $borrower_dateexpiry,
207         dateformat => 'iso',
208         dateonly   => 1
209     }) if $borrower_dateexpiry;
210     $params->{category_code} = $borrower_categorycode if $borrower_categorycode;
211     $params->{patron_list_id} = $patron_list_id if $patron_list_id;
212
213     if ( defined $branch and $branch ne '*' ) {
214         $params->{ branchcode } = $branch;
215     }
216
217     return $params;
218 };