From 8fbfa9da187341d892f230b30ff1d5cf85357293 Mon Sep 17 00:00:00 2001 From: Timothy Alexis Vass Date: Fri, 27 Nov 2020 13:21:11 +0000 Subject: [PATCH] Bug 27050: Allow multiple category_codes in delete_patrons.pl This patch allows multiple --category_code in delete_patrons.pl The t/db_dependent/Members.t test has 2 added testcases with multiple category_code. To test: 1) Run the test: prove -l . -v t/db_dependent/Members.t 2) This requires records in the database with at least two category codes. For example PT and ST. 3) Run the script: delete_patrons.pl --category_code PT 4) Confirm the number of patrons that would have been deleted. 5) Run the script: delete_patrons.pl --category_code PT --category-code ST 6) Confirm the number of patrons that would have been deleted. 7) Sign Off Sponsored-by: Lunds Universitetsbibliotek Signed-off-by: David Nind Signed-off-by: Marcel de Rooy Signed-off-by: Jonathan Druart --- C4/Members.pm | 9 ++++++--- misc/cronjobs/delete_patrons.pl | 15 +++++++++------ t/db_dependent/Members.t | 6 +++++- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/C4/Members.pm b/C4/Members.pm index a6760b8174..05056345e3 100644 --- a/C4/Members.pm +++ b/C4/Members.pm @@ -362,7 +362,7 @@ sub get_cardnumber_length { $borrowers = &GetBorrowersToExpunge( not_borrowed_since => $not_borrowed_since, expired_before => $expired_before, - category_code => $category_code, + category_code => \@category_code, patron_list_id => $patron_list_id, branchcode => $branchcode ); @@ -424,8 +424,11 @@ sub GetBorrowersToExpunge { push @query_params, $filterlastseen; } if ( $filtercategory ) { - $query .= " AND categorycode = ? "; - push( @query_params, $filtercategory ); + if (ref($filtercategory) ne 'ARRAY' ) { + $filtercategory = [ $filtercategory ]; + } + $query .= " AND categorycode IN (" . join(',', ('?') x @$filtercategory) . ") "; + push( @query_params, @$filtercategory ); } if ( $filterpatronlist ){ $query.=" AND patron_list_id = ? "; diff --git a/misc/cronjobs/delete_patrons.pl b/misc/cronjobs/delete_patrons.pl index f9ad0eca88..1f6bcc9a25 100755 --- a/misc/cronjobs/delete_patrons.pl +++ b/misc/cronjobs/delete_patrons.pl @@ -12,14 +12,15 @@ use Koha::Patrons; use C4::Log; my ( $help, $verbose, $not_borrowed_since, $expired_before, $last_seen, - $category_code, $branchcode, $file, $confirm ); + @category_code, $branchcode, $file, $confirm ); + 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, + 'category_code:s' => \@category_code, 'library:s' => \$branchcode, 'file:s' => \$file, 'c|confirm' => \$confirm, @@ -39,7 +40,7 @@ if ( $last_seen and not C4::Context->preference('TrackLastPatronActivity') ) { pod2usage(q{The --last_seen option cannot be used with TrackLastPatronActivity turned off}); } -unless ( $not_borrowed_since or $expired_before or $last_seen or $category_code or $branchcode or $file ) { +unless ( $not_borrowed_since or $expired_before or $last_seen or @category_code or $branchcode or $file ) { pod2usage(q{At least one filter is mandatory}); } @@ -58,13 +59,13 @@ if ($file) { } my $members; -if ( $not_borrowed_since or $expired_before or $last_seen or $category_code or $branchcode ) { +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, + category_code => \@category_code, branchcode => $branchcode, } ); @@ -143,7 +144,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] [--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] Dates should be in ISO format, e.g., 2013-07-19, and can be generated with `date -d '-3 month' --iso-8601`. @@ -178,6 +179,8 @@ The system preference TrackLastPatronActivity must be enabled to use this option Delete patrons who have this category code. +Can be used multiple times for additional category codes. + =item B<--library> Delete patrons in this library. diff --git a/t/db_dependent/Members.t b/t/db_dependent/Members.t index cac634f252..880c21c3bf 100755 --- a/t/db_dependent/Members.t +++ b/t/db_dependent/Members.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 48; +use Test::More tests => 50; use Test::MockModule; use Test::Exception; @@ -300,6 +300,8 @@ $patstodel = GetBorrowersToExpunge( {branchcode => $library3->{branchcode},patro is( scalar(@$patstodel),1,'Borrower with issue not deleted by branchcode and list'); $patstodel = GetBorrowersToExpunge( {category_code => 'CIVILIAN',patron_list_id => $list1->patron_list_id() } ); is( scalar(@$patstodel),1,'Borrower with issue not deleted by category_code and list'); +$patstodel = GetBorrowersToExpunge( {category_code => ['CIVILIAN','STAFFER'],patron_list_id => $list1->patron_list_id() } ); +is( scalar(@$patstodel),1,'Borrower with issue not deleted by multiple category_code and list'); $patstodel = GetBorrowersToExpunge( {expired_before => '2015-01-02',patron_list_id => $list1->patron_list_id() } ); is( scalar(@$patstodel),1,'Borrower with issue not deleted by expiration_date and list'); $builder->schema->resultset( 'Issue' )->delete_all; @@ -307,6 +309,8 @@ $patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id()} ok( scalar(@$patstodel)== 2,'Borrowers without issue deleted from list'); $patstodel = GetBorrowersToExpunge( {category_code => 'CIVILIAN',patron_list_id => $list1->patron_list_id() } ); is( scalar(@$patstodel),2,'Borrowers without issues deleted by category_code and list'); +$patstodel = GetBorrowersToExpunge( {category_code => ['CIVILIAN','STAFFER'],patron_list_id => $list1->patron_list_id() } ); +is( scalar(@$patstodel),2,'Borrowers without issues deleted by multiple category_code and list'); $patstodel = GetBorrowersToExpunge( {expired_before => '2015-01-02',patron_list_id => $list1->patron_list_id() } ); is( scalar(@$patstodel),2,'Borrowers without issues deleted by expiration_date and list'); $patstodel = GetBorrowersToExpunge( {not_borrowed_since => '2016-01-02', patron_list_id => $list1->patron_list_id() } ); -- 2.39.5