From 6c1a82c50ca655eb7d2ab18f6a9b2f3f084c32e1 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Fri, 16 Mar 2018 11:15:12 +0000 Subject: [PATCH] Bug 20436 - Add ability to specify itemtypes for longoverdue.pl It would be nice if we could filter long overdues based on itemtype the same way we can already filter based on borrower category code. Test Plan: 1) Set up a number of overdues of various itemtypes 2) Run longoverdue.pl with --list-itemtypes to show itemtypes 3) Run longoverdue.pl with --itemtype to verify it only operates on that itemtype 4) Run longoverdue.pl with multiple --itemtype switches, verify it only operates on those itemtypes 5) Run longoverdue.pl with --skip-itemtype to verify it does not operate on that itemtype 6) Run longoverdue.pl with multiple --skip-itemtype switches, verify that it does not operate on those itemtypes --- misc/cronjobs/longoverdue.pl | 78 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 74 insertions(+), 4 deletions(-) diff --git a/misc/cronjobs/longoverdue.pl b/misc/cronjobs/longoverdue.pl index 394000b..feb781d 100755 --- a/misc/cronjobs/longoverdue.pl +++ b/misc/cronjobs/longoverdue.pl @@ -47,23 +47,29 @@ my $endrange = 366; my $mark_returned; my $borrower_category = []; my $skip_borrower_category = []; +my $itemtype = []; +my $skip_itemtype = []; my $help=0; my $man=0; my $list_categories = 0; +my $list_itemtypes = 0; GetOptions( - 'lost=s%' => \$lost, + 'l|lost=s%' => \$lost, 'c|charge=s' => \$charge, 'confirm' => \$confirm, - 'v|verbose' => \$verbose, + 'v|verbose' => \$verbose, 'quiet' => \$quiet, 'maxdays=s' => \$endrange, 'mark-returned' => \$mark_returned, - 'h|help' => \$help, + 'h|help' => \$help, 'man|manual' => \$man, 'category=s' => $borrower_category, 'skip-category=s' => $skip_borrower_category, 'list-categories' => \$list_categories, + 'itemtype=s' => $itemtype, + 'skip-itemtype=s' => $skip_itemtype, + 'list-itemtypes' => \$list_itemtypes, ); if ( $man ) { @@ -86,9 +92,23 @@ if ( scalar @$borrower_category && scalar @$skip_borrower_category) { ); } +if ( scalar @$itemtype && scalar @$skip_itemtype) { + pod2usage( -verbose => 1, + -message => "The options --itemtype and --skip-itemtype are mually exclusive.\n" + . "Use one or the other.", + -exitval => 1 + ); +} + if ( $list_categories ) { my @categories = sort map { uc $_->[0] } @{ C4::Context->dbh->selectall_arrayref(q|SELECT categorycode FROM categories|) }; - print "\nBorrowrer Categories: " . join( " ", @categories ) . "\n\n"; + print "\nBorrower Categories: " . join( " ", @categories ) . "\n\n"; + exit 0; +} + +if ( $list_itemtypes ) { + my @itemtypes = sort map { uc $_->[0] } @{ C4::Context->dbh->selectall_arrayref(q|SELECT itemtype FROM itemtypes|) }; + print "\nItemtypes: " . join( " ", @itemtypes ) . "\n\n"; exit 0; } @@ -161,6 +181,23 @@ May not be used with B<--category> List borrower categories available for use by B<--category> or B<--skip-category>, and exit. +=item B<--itemtype> + +Act on the listed itemtype code. +Exclude all others. This may be specified multiple times to include multiple itemtypes. +May not be used with B<--skip-itemtype> + +=item B<--skip-itemtype> + +Act on all available itemtype codes, except those listed. +This may be specified multiple times, to exclude multiple itemtypes. +May not be used with B<--itemtype> + +=item B<--list-itemtypes> + +List itemtypes available for use by B<--itemtype> or +B<--skip-itemtype>, and exit. + =item B<--help | -h> Display short help message an exit. @@ -253,6 +290,7 @@ sub longoverdue_sth { } my $dbh = C4::Context->dbh; + my @available_categories = map { uc $_->[0] } @{ $dbh->selectall_arrayref(q|SELECT categorycode FROM categories|) }; $borrower_category = [ map { uc $_ } @$borrower_category ]; $skip_borrower_category = [ map { uc $_} @$skip_borrower_category ]; @@ -281,6 +319,34 @@ if ( @$skip_borrower_category ) { my $filter_borrower_categories = ( scalar @$borrower_category || scalar @$skip_borrower_category ); +my @available_itemtypes = map { uc $_->[0] } @{ $dbh->selectall_arrayref(q|SELECT itemtype FROM itemtypes|) }; +$itemtype = [ map { uc $_ } @$itemtype ]; +$skip_itemtype = [ map { uc $_} @$skip_itemtype ]; +my %itemtype_to_process; +for my $it ( @$itemtype ) { + unless ( grep { /^$it$/ } @available_itemtypes ) { + pod2usage( + '-exitval' => 1, + '-message' => "The itemtype $it does not exist in the database", + ); + } + $itemtype_to_process{$it} = 1; +} +if ( @$skip_itemtype ) { + for my $it ( @$skip_itemtype ) { + unless ( grep { /^$it$/ } @available_itemtypes ) { + pod2usage( + '-exitval' => 1, + '-message' => "The itemtype $it does not exist in the database", + ); + } + } + %itemtype_to_process = map { $_ => 1 } @available_itemtypes; + %itemtype_to_process = ( %itemtype_to_process, map { $_ => 0 } @$skip_itemtype ); +} + +my $filter_itemtypes = ( scalar @$itemtype || scalar @$skip_itemtype ); + my $count; my @report; my $total = 0; @@ -306,6 +372,10 @@ foreach my $startrange (sort keys %$lost) { my $category = uc Koha::Patrons->find( $row->{borrowernumber} )->categorycode(); next ITEM unless ( $category_to_process{ $category } ); } + if ($filter_itemtypes) { + my $it = uc Koha::Items->find( $row->{itemnumber} )->effective_itemtype(); + next ITEM unless ( $itemtype_to_process{$it} ); + } printf ("Due %s: item %5s from borrower %5s to lost: %s\n", $row->{date_due}, $row->{itemnumber}, $row->{borrowernumber}, $lostvalue) if($verbose); if($confirm) { ModItem({ itemlost => $lostvalue }, $row->{'biblionumber'}, $row->{'itemnumber'}); -- 2.10.2