From da10ce85e34d8f96dfb0ba87f4042f8faca5d7d4 Mon Sep 17 00:00:00 2001 From: Michael Hafen Date: Fri, 16 Feb 2024 15:27:01 -0700 Subject: [PATCH] Bug 9596: Add library and skip-library options to cronjobs/longoverdue.pl Content-Type: text/plain; charset="utf-8" This script doesn't seem to be included in cron files by default. This change is to allow script parameters to effect only certain libraries. This allows the script to be added multiple times to a cron file with different settings for different libraries. Test plan: 1. apply patch 2. identify two books at different libraries the same number of days overdue. 3. run the longoverdue.pl script specifying one of the libraries in the --library command line parameter. i.e. koha-shell -c 'misc/cronjobs/longoverdue.pl --library branch_code --lost 60=2 --maxdays=61 --confirm' instance_name 4. observe that the book at the specified library has been or would be affected by the script while the other book is not. Bug 9596: (QA follow-up) Add some clarifying comments Bug 9596: (QA follow-up) Only find a given patron once --- misc/cronjobs/longoverdue.pl | 74 +++++++++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 2 deletions(-) diff --git a/misc/cronjobs/longoverdue.pl b/misc/cronjobs/longoverdue.pl index aa0c2ff253..cce95b086d 100755 --- a/misc/cronjobs/longoverdue.pl +++ b/misc/cronjobs/longoverdue.pl @@ -45,6 +45,8 @@ my $endrange = 366; my $mark_returned; my $borrower_category = []; my $skip_borrower_category = []; +my @branches; +my @skip_branches; my $itemtype = []; my $skip_itemtype = []; my $help=0; @@ -68,6 +70,8 @@ GetOptions( 'category=s' => $borrower_category, 'skip-category=s' => $skip_borrower_category, 'list-categories' => \$list_categories, + 'library=s' => \@branches, + 'skip-library=s' => \@skip_branches, 'itemtype=s' => $itemtype, 'skip-itemtype=s' => $skip_itemtype, 'list-itemtypes' => \$list_itemtypes, @@ -94,6 +98,14 @@ if ( scalar @$borrower_category && scalar @$skip_borrower_category) { ); } +if ( scalar @branches && scalar @skip_branches ) { + pod2usage( + -verbose => 1, + -message => "The options --library and --skip-library are mutually exclusive.\n" . "Use one or the other.", + -exitval => 1 + ); +} + if ( scalar @$itemtype && scalar @$skip_itemtype) { pod2usage( -verbose => 1, -message => "The options --itemtype and --skip-itemtype are mutually exclusive.\n" @@ -121,6 +133,7 @@ if ( $list_itemtypes ) { longoverdue.pl --lost | -l DAYS=LOST_CODE [ --charge | -c CHARGE_CODE ] [ --verbose | -v ] [ --quiet ] [ --maxdays MAX_DAYS ] [ --mark-returned ] [ --category BORROWER_CATEGORY ] ... [ --skip-category BORROWER_CATEGORY ] ... + [ --library LIBRARY_CODE ] [ --skip-library LIBRARY_CODE ] ... [ --skip-lost-value LOST_VALUE [ --skip-lost-value LOST_VALUE ] ] [ --commit ] @@ -187,6 +200,16 @@ If not provided, the value of the system preference 'DefaultLongOverdueSkipPatro List borrower categories available for use by B<--category> or B<--skip-category>, and exit. +=item B<--library> + +Act on the listed library codes. Exclude all others. This may be specified multiple times to include multiple libraries. Which libraries are selected follows the CircControl system preference. +May not be used with B<--skip-library> + +=item B<--skip-library> + +Act on all library codes except the ones listed. This may be specified multiple times to exclude multiple libraries. Which libraries are excluded follows the CircControl system preference. +May not be used with B<--library> + =item B<--itemtype> Act on the listed itemtype code. @@ -332,6 +355,7 @@ sub longoverdue_sth { } my $dbh = C4::Context->dbh; +my $circ_control_pref = C4::Context->preference('CircControl'); my @available_categories = Koha::Patron::Categories->search()->get_column('categorycode'); $borrower_category = [ map { uc $_ } @$borrower_category ]; @@ -361,6 +385,36 @@ if ( @$skip_borrower_category ) { my $filter_borrower_categories = ( scalar @$borrower_category || scalar @$skip_borrower_category ); +my @available_branches = Koha::Libraries->search()->get_column('branchcode'); +my %branches_to_process; +# If --library was used, validate any branchcode passed in and mark them as branches to use +for my $lib (@branches) { + unless ( grep { $_ eq $lib } @available_branches ) { + pod2usage( + '-exitval' => 1, + '-message' => "The library $lib does not exist in the database", + ); + } + $branches_to_process{$lib} = 1; +} +# If --skip-library was used, validate any branchcode passed in and mark them as branches to *not* use +if (@skip_branches) { + for my $lib (@skip_branches) { + unless ( grep { $_ eq $lib } @available_branches ) { + pod2usage( + '-exitval' => 1, + '-message' => "The library $lib does not exist in the database", + ); + } + } + %branches_to_process = map { $_ => 1 } @available_branches; + # The mapped 0 values here will overwrite the corrosponding mapped 1 values + # where the 0 values exist + %branches_to_process = ( %branches_to_process, map { $_ => 0 } @skip_branches ); +} + +my $filter_branches = ( scalar @branches || scalar @skip_branches ); + my @available_itemtypes = Koha::ItemTypes->search()->get_column('itemtype'); $itemtype = [ map { uc $_ } @$itemtype ]; $skip_itemtype = [ map { uc $_} @$skip_itemtype ]; @@ -410,10 +464,26 @@ foreach my $startrange (sort keys %$lost) { $sth_items->execute($startrange, $endrange, $lostvalue); $count=0; ITEM: while (my $row=$sth_items->fetchrow_hashref) { + my $patron; if( $filter_borrower_categories ) { - my $category = uc Koha::Patrons->find( $row->{borrowernumber} )->categorycode(); + $patron ||= Koha::Patrons->find( $row->{borrowernumber} ); + my $category = uc $patron->categorycode(); next ITEM unless ( $category_to_process{ $category } ); } + if ($filter_branches) { + my $lib; + for ($circ_control_pref) { + if ( $_ eq 'PatronLibrary' ) { + $patron ||= Koha::Patrons->find( $row->{borrowernumber} ); + $lib = $patron->branchcode(); + } elsif ( $_ eq 'PickupLibrary' ) { + $lib = C4::Context->userenv->{'branch'}; + } else { # ( $_ eq 'ItemHomeLibrary' ) + $lib = Koha::Items->find( $row->{itemnumber} )->homebranch(); + } + } + next ITEM unless ( $branches_to_process{$lib} ); + } if ($filter_itemtypes) { my $it = uc Koha::Items->find( $row->{itemnumber} )->effective_itemtype(); next ITEM unless ( $itemtype_to_process{$it} ); @@ -425,7 +495,7 @@ foreach my $startrange (sort keys %$lost) { if ( $charge && $charge eq $lostvalue ) { LostItem( $row->{'itemnumber'}, 'cronjob', $mark_returned ); } elsif ( $mark_returned ) { - my $patron = Koha::Patrons->find( $row->{borrowernumber} ); + $patron ||= Koha::Patrons->find( $row->{borrowernumber} ); MarkIssueReturned($row->{borrowernumber},$row->{itemnumber},undef,$patron->privacy) } } -- 2.34.1