X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=tools%2Fcleanborrowers.pl;h=3040b1d777483ce286856bcbc0aa8161e4722f2a;hb=75e6c204a4b04c95bb73d1d6c5307fe6d71a4148;hp=c6ecac549fe75f7190ae9c54df44271c92a3e801;hpb=2e0e56a016f4829b479ec260435e185400b72a57;p=koha.git diff --git a/tools/cleanborrowers.pl b/tools/cleanborrowers.pl index c6ecac549f..3040b1d777 100755 --- a/tools/cleanborrowers.pl +++ b/tools/cleanborrowers.pl @@ -2,22 +2,21 @@ # This file is part of Koha. # -# Koha is free software; you can redistribute it and/or modify it under the -# terms of the GNU General Public License as published by the Free Software -# Foundation; either version 2 of the License, or (at your option) any later -# version. +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. # -# Koha is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR -# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. # -# You should have received a copy of the GNU General Public License along with -# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place, -# Suite 330, Boston, MA 02111-1307 USA +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . # # Written by Antoine Farnault antoine@koha-fr.org on Nov. 2006. - =head1 cleanborrowers.pl This script allows to do 2 things. @@ -26,20 +25,25 @@ This script allows to do 2 things. =item * Anonymise the borrowers' issues if issue is older than a given date. see C. -=item * Delete the borrowers who has not borrowered since a given date. see C. +=item * Delete the borrowers who has not borrowed since a given date. see C. =back =cut -use strict; -use CGI; +use Modern::Perl; + +use CGI qw ( -utf8 ); use C4::Auth; use C4::Output; -use C4::Dates; -use C4::Members; # GetBorrowersWhoHavexxxBorrowed. +use C4::Members; use C4::Circulation; # AnonymiseIssueHistory. -use Date::Calc qw/Date_to_Days Today/; +use Koha::DateUtils qw( dt_from_string output_pref ); +use Koha::Patron::Categories; +use Koha::Patrons; +use Date::Calc qw/Today Add_Delta_YM/; +use Koha::Patrons; +use Koha::List::Patron; my $cgi = new CGI; @@ -49,115 +53,183 @@ my $cgi = new CGI; # * multivalued CGI paramaters are returned as a packaged string separated by "\0" (null) my $params = $cgi->Vars; -my $filterdate1; # the date which filter on issue history. -my $filterdate2; # the date which filter on borrowers last issue. +my $step = $params->{step} || 1; +my $not_borrowed_since = # the date which filter on issue history. + $params->{not_borrowed_since} + ? dt_from_string $params->{not_borrowed_since} + : undef; +my $last_issue_date = # the date which filter on borrowers last issue. + $params->{last_issue_date} + ? dt_from_string $params->{last_issue_date} + : undef; +my $borrower_dateexpiry = + $params->{borrower_dateexpiry} + ? dt_from_string $params->{borrower_dateexpiry} + : undef; +my $borrower_lastseen = + $params->{borrower_lastseen} + ? dt_from_string $params->{borrower_lastseen} + : undef; +my $patron_list_id = $params->{patron_list_id}; + +my $borrower_categorycode = $params->{'borrower_categorycode'} || q{}; # getting the template my ( $template, $loggedinuser, $cookie ) = get_template_and_user( - { - template_name => "tools/cleanborrowers.tmpl", + { template_name => "tools/cleanborrowers.tt", query => $cgi, type => "intranet", authnotrequired => 0, - flagsrequired => { tools => 1, catalogue => 1 }, + flagsrequired => { tools => 'delete_anonymize_patrons', catalogue => 1 }, } ); -if ( $params->{'step2'} ) { - $filterdate1 = format_date_in_iso($params->{'filterdate1'}); - $filterdate2 = format_date_in_iso($params->{'filterdate2'}); - my $checkbox = $params->{'checkbox'}; - - my $totalDel; - if ($checkbox eq "borrower") { - my $membersToDelete = GetBorrowersWhoHaveNotBorrowedSince($filterdate1); - $totalDel = scalar @$membersToDelete; +my $branch = $params->{ branch } || '*'; +$template->param( current_branch => $branch ); +$template->param( OnlyMine => C4::Context->only_my_library ); + +if ( $step == 2 ) { + + my %checkboxes = map { $_ => 1 } split /\0/, $params->{'checkbox'}; + + my $patrons_to_delete; + if ( $checkboxes{borrower} ) { + $patrons_to_delete = GetBorrowersToExpunge( + _get_selection_params( + $not_borrowed_since, + $borrower_dateexpiry, + $borrower_lastseen, + $borrower_categorycode, + $patron_list_id, + $branch + ) + ); } + _skip_borrowers_with_nonzero_balance($patrons_to_delete); - my $totalAno; - if ($checkbox eq "issue") { - my $membersToAnonymize = - GetBorrowersWithIssuesHistoryOlderThan($filterdate2); - $totalAno = scalar @$membersToAnonymize; - } + my $patrons_to_anonymize = + $checkboxes{issue} + ? $branch eq '*' + ? Koha::Patrons->search_patrons_to_anonymise( { before => $last_issue_date } ) + : Koha::Patrons->search_patrons_to_anonymise( { before => $last_issue_date, library => $branch } ) + : undef; $template->param( - step2 => 1, - totalToDelete => $totalDel, - totalToAnonymize => $totalAno, - filterdate1 => format_date($filterdate1), - filterdate2 => format_date($filterdate2), + patrons_to_delete => $patrons_to_delete, + patrons_to_anonymize => $patrons_to_anonymize, + patron_list_id => $patron_list_id, ); - - #writing the template - output_html_with_http_headers $cgi, $cookie, $template->output; - exit; } -if ( $params->{'step3'} ) { - $filterdate1 = format_date_in_iso($params->{'filterdate1'}); - $filterdate2 = format_date_in_iso($params->{'filterdate2'}); +elsif ( $step == 3 ) { my $do_delete = $params->{'do_delete'}; my $do_anonym = $params->{'do_anonym'}; my ( $totalDel, $totalAno, $radio ) = ( 0, 0, 0 ); - + # delete members if ($do_delete) { - my $membersToDelete = GetBorrowersWhoHaveNotBorrowedSince($filterdate1); - $totalDel = scalar(@$membersToDelete); + my $patrons_to_delete = GetBorrowersToExpunge( + _get_selection_params( + $not_borrowed_since, + $borrower_dateexpiry, + $borrower_lastseen, + $borrower_categorycode, + $patron_list_id, + $branch + ) + ); + _skip_borrowers_with_nonzero_balance($patrons_to_delete); + + $totalDel = scalar(@$patrons_to_delete); $radio = $params->{'radio'}; - if ( $radio eq 'trash' ) { - my $i; - for ( $i = 0 ; $i < $totalDel ; $i++ ) { - MoveMemberToDeleted( $membersToDelete->[$i]->{'borrowernumber'} ); - DelMember( $membersToDelete->[$i]->{'borrowernumber'} ); - } - } - else { # delete completly. - my $i; - for ( $i = 0 ; $i < $totalDel ; $i++ ) { - DelMember($membersToDelete->[$i]->{'borrowernumber'}); - } + for ( my $i = 0 ; $i < $totalDel ; $i++ ) { + $radio eq 'testrun' && last; + my $borrowernumber = $patrons_to_delete->[$i]->{'borrowernumber'}; + my $patron = Koha::Patrons->find($borrowernumber); + $radio eq 'trash' && $patron->move_to_deleted; + $patron->delete; } $template->param( do_delete => '1', TotalDel => $totalDel ); } - + # Anonymising all members if ($do_anonym) { - $totalAno = AnonymiseIssueHistory($filterdate2); + #FIXME: anonymisation errors are not handled + my $rows = Koha::Patrons->search_patrons_to_anonymise( { before => $last_issue_date } )->anonymise_issue_history( { before => $last_issue_date } ); $template->param( - filterdate1 => $filterdate2, - do_anonym => '1', + do_anonym => $rows, ); } - + $template->param( - step3 => '1', trash => ( $radio eq "trash" ) ? (1) : (0), + testrun => ( $radio eq "testrun" ) ? 1: 0, ); - - #writing the template - output_html_with_http_headers $cgi, $cookie, $template->output; - exit; +} else { # $step == 1 + my @all_lists = GetPatronLists(); + my @non_empty_lists; + foreach my $list (@all_lists){ + my @patrons = $list->patron_list_patrons(); + if( scalar @patrons ) { push(@non_empty_lists,$list) } + } + $template->param( patron_lists => [ @non_empty_lists ] ); } -#default value set to the template are the 'CNIL' value. -my ( $year, $month, $day ) = &Today(); -my $tmpyear = $year - 1; -my $tmpmonth = $month - 3; -$filterdate1 = format_date($tmpyear . "-" . $month . "-" . $day); -$filterdate2 = format_date($year . "-" . $tmpmonth . "-" . $day); +my $patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['description']}); $template->param( - step1 => '1', - filterdate1 => $filterdate1, - filterdate2 => $filterdate2, - DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(), + step => $step, + not_borrowed_since => $not_borrowed_since, + borrower_dateexpiry => $borrower_dateexpiry, + borrower_lastseen => $borrower_lastseen, + last_issue_date => $last_issue_date, + borrower_categorycodes => $patron_categories, + borrower_categorycode => $borrower_categorycode, ); #writing the template output_html_with_http_headers $cgi, $cookie, $template->output; + +sub _skip_borrowers_with_nonzero_balance { + my $borrowers = shift; + my $balance; + @$borrowers = map { + my $patron = Koha::Patrons->find( $_->{borrowernumber} ); + my $balance = $patron->account->balance; + (defined $balance && $balance != 0) ? (): ($_); + } @$borrowers; +} + +sub _get_selection_params { + my ($not_borrowed_since, $borrower_dateexpiry, $borrower_lastseen, + $borrower_categorycode, $patron_list_id, $branch) = @_; + + my $params = {}; + $params->{not_borrowed_since} = output_pref({ + dt => $not_borrowed_since, + dateformat => 'iso', + dateonly => 1 + }) if $not_borrowed_since; + $params->{expired_before} = output_pref({ + dt => $borrower_dateexpiry, + dateformat => 'iso', + dateonly => 1 + }) if $borrower_dateexpiry; + $params->{last_seen} = output_pref({ + dt => $borrower_lastseen, + dateformat => 'iso', + dateonly => 1 + }) if $borrower_lastseen; + $params->{category_code} = $borrower_categorycode if $borrower_categorycode; + $params->{patron_list_id} = $patron_list_id if $patron_list_id; + + if ( defined $branch and $branch ne '*' ) { + $params->{ branchcode } = $branch; + } + + return $params; +};