From 8e4963d97cd6a18b84b7ffe6332d95e2396d0b3e Mon Sep 17 00:00:00 2001 From: Michael Hafen Date: Fri, 16 Feb 2024 15:27:01 -0700 Subject: [PATCH] Bug 9596: Add branch and skip-branch options to cronjobs/longoverdue.pl MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This script doesn't seem to be included in cron files by default. This change is to allow script parameters to effect only certain branches. This allows the script to be added multiple times to a cron file with different settings for different branches. 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. i.e. koha-shell -c 'misc/cronjobs/longoverdue.pl --branch branch_code --lost 60=2 --maxdays=61 --confirm' instance_name 4. observe that the book at the specified branch has been or would be affected by the script while the other book is not. Signed-off-by: Tadeusz „tadzik” Sośnierz Signed-off-by: Kyle M Hall Signed-off-by: Katrin Fischer --- misc/cronjobs/longoverdue.pl | 63 ++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/misc/cronjobs/longoverdue.pl b/misc/cronjobs/longoverdue.pl index aa0c2ff253..ca2a7832d0 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 ] @@ -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<--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. @@ -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,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 ]; @@ -414,6 +464,19 @@ 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.39.5