From 2d949f821cbcf61a7f1a57793d6426df3494945e Mon Sep 17 00:00:00 2001 From: Michael Hafen Date: Mon, 12 Feb 2024 17:16:17 -0700 Subject: [PATCH] Bug 9596 - Add branch and skip-branch options to cronjobs/longoverdue.pl Content-Type: text/plain; charset="utf-8" Test plan: 1. apply patch 2. identify two books at different branches the same number of days overdue. 3. run the longoverdue.pl script specifying one of the branches in the --branch command line parameter. 4. observe that the book at the specified branch has been or would be affected by the script while the other book is not. --- misc/cronjobs/longoverdue.pl | 65 ++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/misc/cronjobs/longoverdue.pl b/misc/cronjobs/longoverdue.pl index 2d313b77b2..7d9c03a55e 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, + 'branch=s' => \@branches, + 'skip-branch=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 --branch and --skip-branch 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 ] ... + [ --branch BRANCH_CODE ] [ --skip-branch BRANCH_CODE ] ... [ --skip-lost-value LOST_VALUE [ --skip-lost-value LOST_VALUE ] ] [ --commit ] @@ -185,6 +198,16 @@ May not be used with B<--category> List borrower categories available for use by B<--category> or B<--skip-category>, and exit. +=item B<--branch> + +Act on the listed branch codes. Exclude all others. This may be specified multiple times to include multiple branches. Which branches are selected follows the CircControl system preference. +May not be used with B<--skip-branch> + +=item B<--skip-branch> + +Act on all branch codes except the ones listed. This may be specified multiple times to exclude multiple branches. Which branches are excluded follows the CircControl system preference. +May not be used with B<--branch> + =item B<--itemtype> Act on the listed itemtype code. @@ -315,6 +338,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 ]; @@ -344,6 +368,32 @@ 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; +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_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; + %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 ]; @@ -397,6 +447,21 @@ foreach my $startrange (sort keys %$lost) { my $category = uc Koha::Patrons->find( $row->{borrowernumber} )->categorycode(); next ITEM unless ( $category_to_process{ $category } ); } + if ($filter_branches) { + my $lib; + for ($circ_control_pref) { + if ( $_ eq 'PatronLibrary' ) { + $lib = Koha::Patrons->find( $row->{borrowernumber} )->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} ); -- 2.34.1