From caddeff96db94c580d323863a9377acb4d44e339 Mon Sep 17 00:00:00 2001 From: Raphael Straub Date: Tue, 11 Mar 2025 09:25:25 +0000 Subject: [PATCH] Bug 37418: delete_patrons.pl: Exclude patrons with restrictions Add the option "--without_restriction_type" so that delete_patrons.pl does not delete patrons with given restriction types. To test: 1) Run misc/cronjobs/delete_patrons.pl --not_borrowed_since 2010-01-01 and choose a borrowernumber that would be deleted. 2) In your browser go to /cgi-bin/koha/members/moremember.pl?borrowernumber= and add a manual restriction to this patron. 3) Apply the patch. 4) Run misc/cronjobs/delete_patrons.pl --not_borrowed_since 2010-01-01 \ --without_restriction_type MANUAL 5) Check that the patron with the manual restriction would not be deleted. Sponsored-by: Karlsruhe Institute of Technology (KIT) Signed-off-by: Roman Dolny Signed-off-by: Thomas Klausner --- C4/Members.pm | 16 ++++++++- misc/cronjobs/delete_patrons.pl | 57 +++++++++++++++++++++++---------- 2 files changed, 55 insertions(+), 18 deletions(-) diff --git a/C4/Members.pm b/C4/Members.pm index 06365485b3..dab47be321 100644 --- a/C4/Members.pm +++ b/C4/Members.pm @@ -248,7 +248,8 @@ sub GetBorrowersToExpunge { ? C4::Context->userenv->{branch} : "" ); - my $filterpatronlist = $params->{'patron_list_id'}; + my $filterpatronlist = $params->{'patron_list_id'}; + my $without_restriction_types = $params->{'without_restriction_types'}; my $dbh = C4::Context->dbh; my $query = q| @@ -308,6 +309,19 @@ sub GetBorrowersToExpunge { $query .= " AND ( latestissue < ? OR latestissue IS NULL ) "; push @query_params, $filterdate; } + if ( ref($without_restriction_types) ne 'ARRAY' ) { + $without_restriction_types = [$without_restriction_types]; + } + if ( @$without_restriction_types > 0 ) { + $query .= q| + AND NOT EXISTS ( + SELECT 1 + FROM borrower_debarments + WHERE borrower_debarments.borrowernumber = xxx.borrowernumber + AND borrower_debarments.type IN (| + . join( ',', ('?') x @$without_restriction_types ) . "))"; + push @query_params, @$without_restriction_types; + } if ( my $anonymous_patron = C4::Context->preference("AnonymousPatron") ) { $query .= q{ AND borrowernumber != ? }; diff --git a/misc/cronjobs/delete_patrons.pl b/misc/cronjobs/delete_patrons.pl index ab18450f17..3820e26385 100755 --- a/misc/cronjobs/delete_patrons.pl +++ b/misc/cronjobs/delete_patrons.pl @@ -12,23 +12,24 @@ use Koha::Patrons; use C4::Log qw( cronlogaction ); my ( - $help, $verbose, $not_borrowed_since, $expired_before, $last_seen, - @category_code, $branchcode, $file, $confirm + $help, $verbose, $not_borrowed_since, $expired_before, $last_seen, + @category_code, $branchcode, $file, @without_restriction_types, $confirm ); my $command_line_options = join( " ", @ARGV ); cronlogaction( { info => $command_line_options } ); GetOptions( - 'h|help' => \$help, - 'v|verbose' => \$verbose, - 'not_borrowed_since:s' => \$not_borrowed_since, - 'expired_before:s' => \$expired_before, - 'last_seen:s' => \$last_seen, - 'category_code:s' => \@category_code, - 'library:s' => \$branchcode, - 'file:s' => \$file, - 'c|confirm' => \$confirm, + 'h|help' => \$help, + 'v|verbose' => \$verbose, + 'not_borrowed_since:s' => \$not_borrowed_since, + 'expired_before:s' => \$expired_before, + 'last_seen:s' => \$last_seen, + 'category_code:s' => \@category_code, + 'library:s' => \$branchcode, + 'file:s' => \$file, + 'without_restriction_type:s' => \@without_restriction_types, + 'c|confirm' => \$confirm, ) || pod2usage(1); if ($help) { @@ -49,6 +50,21 @@ unless ( $not_borrowed_since or $expired_before or $last_seen or @category_code pod2usage(q{At least one filter is mandatory}); } +if ( @without_restriction_types > 0 ) { + my %restriction_types; + @restriction_types{ map { $_->code() } Koha::Patron::Restriction::Types->search()->as_list } = (); + + my @invalid_restriction_types; + foreach my $restriction_type (@without_restriction_types) { + if ( !exists $restriction_types{$restriction_type} ) { + push @invalid_restriction_types, $restriction_type; + } + } + if ( @invalid_restriction_types > 0 ) { + die 'Invalid restriction type(s): ' . join ', ', @invalid_restriction_types; + } +} + my @file_members; if ($file) { open( my $fh, '<:encoding(UTF-8)', $file ) or die "Could not open file $file' $!"; @@ -65,11 +81,12 @@ my $members; if ( $not_borrowed_since or $expired_before or $last_seen or @category_code or $branchcode ) { $members = GetBorrowersToExpunge( { - not_borrowed_since => $not_borrowed_since, - expired_before => $expired_before, - last_seen => $last_seen, - category_code => \@category_code, - branchcode => $branchcode, + not_borrowed_since => $not_borrowed_since, + expired_before => $expired_before, + last_seen => $last_seen, + category_code => \@category_code, + branchcode => $branchcode, + without_restriction_types => \@without_restriction_types } ); } @@ -149,7 +166,7 @@ delete_patrons - This script deletes patrons =head1 SYNOPSIS -delete_patrons.pl [-h|--help] [-v|--verbose] [-c|--confirm] [--not_borrowed_since=DATE] [--expired_before=DATE] [--last-seen=DATE] [--category_code=CAT] [--category_code=CAT ...] [--library=LIBRARY] [--file=FILE] +delete_patrons.pl [-h|--help] [-v|--verbose] [-c|--confirm] [--not_borrowed_since=DATE] [--expired_before=DATE] [--last-seen=DATE] [--category_code=CAT] [--category_code=CAT ...] [--library=LIBRARY] [--file=FILE] [--without_restriction_type=TYPE] [--without_restriction_type=TYPE ...] Dates should be in ISO format, e.g., 2013-07-19, and can be generated with `date -d '-3 month' --iso-8601`. @@ -202,6 +219,12 @@ Delete patrons in this library. Delete patrons whose borrower numbers are in this file. If other criteria are defined it will only delete those in the file that match those criteria. +=item B<--without_restriction_type> + +Delete patrons who DO NOT have a debarment with this restriction type. + +Can be used multiple times for additional restriction types. + =item B<-c|--confirm> This flag must be provided in order for the script to actually -- 2.39.5