X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=tools%2Fcleanborrowers.pl;h=7888b990f23b0550bd482cf3e5e809888ae7a847;hb=42e731857d53134f91ecf0099ae6b7c13393166c;hp=65ce8131e253bff2ce85e6e2012ca4947baa2230;hpb=840a907b86c054aa1c216a8cf353b1a9ef3c44fa;p=koha.git diff --git a/tools/cleanborrowers.pl b/tools/cleanborrowers.pl index 65ce8131e2..7888b990f2 100755 --- a/tools/cleanborrowers.pl +++ b/tools/cleanborrowers.pl @@ -2,18 +2,18 @@ # 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. @@ -34,13 +34,12 @@ This script allows to do 2 things. use strict; #use warnings; FIXME - Bug 2505 -use CGI; +use CGI qw ( -utf8 ); use C4::Auth; use C4::Output; -use C4::Dates qw/format_date format_date_in_iso/; use C4::Members; # GetBorrowersWhoHavexxxBorrowed. use C4::Circulation; # AnonymiseIssueHistory. -use C4::VirtualShelves (); #no import +use Koha::DateUtils qw( dt_from_string output_pref ); use Date::Calc qw/Today Add_Delta_YM/; my $cgi = new CGI; @@ -51,14 +50,25 @@ 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 $borrower_dateexpiry; -my $borrower_categorycode; +my $step = $params->{step} || 1; +my $not_borrowered_since = # the date which filter on issue history. + $params->{not_borrowered_since} + ? dt_from_string $params->{not_borrowered_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_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, @@ -66,52 +76,36 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user( } ); -if ( $params->{'step2'} ) { - $filterdate1 = format_date_in_iso( $params->{'filterdate1'} ); - $filterdate2 = format_date_in_iso( $params->{'filterdate2'} ); - $borrower_dateexpiry = format_date_in_iso( $params->{'borrower_dateexpiry'} ); - $borrower_categorycode = $params->{'borrower_categorycode'}; +if ( $step == 2 ) { my %checkboxes = map { $_ => 1 } split /\0/, $params->{'checkbox'}; my $totalDel; my $membersToDelete; if ( $checkboxes{borrower} ) { - $membersToDelete = - GetBorrowersToExpunge( { not_borrowered_since => $filterdate1, expired_before => $borrower_dateexpiry, category_code => $borrower_categorycode } ); + $membersToDelete = GetBorrowersToExpunge( + _get_selection_params($not_borrowered_since, $borrower_dateexpiry, $borrower_categorycode) + ); + _skip_borrowers_with_nonzero_balance( $membersToDelete ); $totalDel = scalar @$membersToDelete; } my $totalAno; my $membersToAnonymize; if ( $checkboxes{issue} ) { - $membersToAnonymize = GetBorrowersWithIssuesHistoryOlderThan($filterdate2); + $membersToAnonymize = GetBorrowersWithIssuesHistoryOlderThan($last_issue_date); $totalAno = scalar @$membersToAnonymize; } $template->param( - step2 => 1, totalToDelete => $totalDel, totalToAnonymize => $totalAno, memberstodelete_list => $membersToDelete, memberstoanonymize_list => $membersToAnonymize, - filterdate1 => format_date($filterdate1), - filterdate2 => format_date($filterdate2), - borrower_dateexpiry => $borrower_dateexpiry, - borrower_categorycode => $borrower_categorycode, ); - - ### TODO : Use GetBorrowersNamesAndLatestIssue function in order to get the borrowers to delete or anonymize. - 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'} ); - $borrower_dateexpiry = format_date_in_iso( $params->{'borrower_dateexpiry'} ); - $borrower_categorycode = $params->{'borrower_categorycode'}; - +elsif ( $step == 3 ) { my $do_delete = $params->{'do_delete'}; my $do_anonym = $params->{'do_anonym'}; @@ -119,23 +113,18 @@ if ( $params->{'step3'} ) { # delete members if ($do_delete) { - my $membersToDelete = - GetBorrowersToExpunge( { not_borrowered_since => $filterdate1, expired_before => $borrower_dateexpiry, category_code => $borrower_categorycode } ); + my $membersToDelete = GetBorrowersToExpunge( + _get_selection_params($not_borrowered_since, $borrower_dateexpiry, $borrower_categorycode) + ); + _skip_borrowers_with_nonzero_balance( $membersToDelete ); $totalDel = scalar(@$membersToDelete); $radio = $params->{'radio'}; - if ( $radio eq 'trash' ) { - my $i; - for ( $i = 0 ; $i < $totalDel ; $i++ ) { - MoveMemberToDeleted( $membersToDelete->[$i]->{'borrowernumber'} ); - C4::VirtualShelves::HandleDelBorrower( $membersToDelete->[$i]->{'borrowernumber'} ); - DelMember( $membersToDelete->[$i]->{'borrowernumber'} ); - } - } else { # delete completly. - my $i; - for ( $i = 0 ; $i < $totalDel ; $i++ ) { - C4::VirtualShelves::HandleDelBorrower( $membersToDelete->[$i]->{'borrowernumber'} ); - DelMember( $membersToDelete->[$i]->{'borrowernumber'} ); - } + for ( my $i = 0 ; $i < $totalDel ; $i++ ) { + $radio eq 'testrun' && last; + my $borrowernumber = $membersToDelete->[$i]->{'borrowernumber'}; + $radio eq 'trash' && MoveMemberToDeleted( $borrowernumber ); + C4::Members::HandleDelBorrower( $borrowernumber ); + DelMember( $borrowernumber ); } $template->param( do_delete => '1', @@ -145,29 +134,55 @@ if ( $params->{'step3'} ) { # Anonymising all members if ($do_anonym) { - $totalAno = AnonymiseIssueHistory($filterdate2); + #FIXME: anonymisation errors are not handled + ($totalAno,my $anonymisation_error) = AnonymiseIssueHistory($last_issue_date); $template->param( - filterdate1 => $filterdate2, do_anonym => '1', ); } $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; } $template->param( - step1 => '1', - filterdate1 => $filterdate1, - filterdate2 => $filterdate2, - borrower_categorycodes => GetBorrowercategoryList(), + step => $step, + not_borrowered_since => $not_borrowered_since, + borrower_dateexpiry => $borrower_dateexpiry, + last_issue_date => $last_issue_date, + borrower_categorycodes => GetBorrowercategoryList(), + 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 { + (undef, undef, $balance) = GetMemberIssuesAndFines( $_->{borrowernumber} ); + ($balance != 0) ? (): ($_); + } @$borrowers; +} + +sub _get_selection_params { + my ($not_borrowered_since, $borrower_dateexpiry, $borrower_categorycode) = @_; + + my $params = {}; + $params->{not_borrowered_since} = output_pref({ + dt => $not_borrowered_since, + dateformat => 'iso', + dateonly => 1 + }) if $not_borrowered_since; + $params->{expired_before} = output_pref({ + dt => $borrower_dateexpiry, + dateformat => 'iso', + dateonly => 1 + }) if $borrower_dateexpiry; + $params->{category_code} = $borrower_categorycode if $borrower_categorycode; + + return $params; +};