From 3b46cf77c27ec332e471c04f8bdd26291f4e820d Mon Sep 17 00:00:00 2001 From: Raphael Straub Date: Wed, 24 Jul 2024 13:48:05 +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 --- C4/Members.pm | 14 +++++++++ misc/cronjobs/delete_patrons.pl | 55 +++++++++++++++++++++++---------- 2 files changed, 53 insertions(+), 16 deletions(-) diff --git a/C4/Members.pm b/C4/Members.pm index 1d93b49fa1..9ffd4c0120 100644 --- a/C4/Members.pm +++ b/C4/Members.pm @@ -251,6 +251,7 @@ sub GetBorrowersToExpunge { ? C4::Context->userenv->{branch} : ""); my $filterpatronlist = $params->{'patron_list_id'}; + my $without_restriction_types = $params->{'without_restriction_types'}; my $dbh = C4::Context->dbh; my $query = q| @@ -310,6 +311,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 7962233efa..ded05ad33a 100755 --- a/misc/cronjobs/delete_patrons.pl +++ b/misc/cronjobs/delete_patrons.pl @@ -12,20 +12,21 @@ use Koha::Patrons; use C4::Log qw( cronlogaction ); my ( $help, $verbose, $not_borrowed_since, $expired_before, $last_seen, - @category_code, $branchcode, $file, $confirm ); + @category_code, $branchcode, $file, @without_restriction_types, $confirm ); my $command_line_options = join(" ",@ARGV); 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) { @@ -46,6 +47,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; + } +} + cronlogaction({ info => $command_line_options }); my @file_members; @@ -64,11 +80,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.2