From 8de21a52ce828c87dc9f24be23f5ed21eb1a5f8b Mon Sep 17 00:00:00 2001 From: Barton Chittenden Date: Mon, 31 Aug 2015 08:42:32 -0700 Subject: [PATCH] Bug 14292: Add patron category restrictions to longoverdue.pl Squashed prevoius patch set. Add command line options --category and --skip-category Convert usage() to pod2usage. (This was previous FIXME) Make borrower category handling more user friendly: Make --category and --skip-category mutally exclusive Check that categories specified by --category and --skip-category exist in categories table. Add --list-categories opttion to show available borrower categories. http://bugs.koha-community.org/show_bug.cgi?id=14292 QA Follow-up QA fixes - wording and trailing whitespaces Simplify the category list Basically we have 3 lists of categories: 1/ The full one, in the DB 2/ The one we want and list with --category 3/ The one we don't want and list with --skip-category The list of categories to process is: * 1 if none --category or --skip-category options are provided * 2 if --category is provided * 1 - 3 if --skip-category is provided (follow-up) Make --category and --skip-category case insensitive "my" variable $borrower_category masks earlier declaration in same scope "my" variable $skip_borrower_category masks earlier declaration in same scope Signed-off-by: hbraum@nekls.org Signed-off-by: Barton Chittenden Signed-off-by: Jonathan Druart Signed-off-by: Kyle M Hall --- misc/cronjobs/longoverdue.pl | 211 ++++++++++++++++++++++++++++------- 1 file changed, 169 insertions(+), 42 deletions(-) diff --git a/misc/cronjobs/longoverdue.pl b/misc/cronjobs/longoverdue.pl index 8edbcf8e5c..b73d31f47e 100755 --- a/misc/cronjobs/longoverdue.pl +++ b/misc/cronjobs/longoverdue.pl @@ -38,64 +38,161 @@ use C4::Items; use C4::Circulation qw/LostItem/; use Getopt::Long; use C4::Log; +use Pod::Usage; +use Koha::Borrowers; my $lost; # key=lost value, value=num days. my ($charge, $verbose, $confirm, $quiet); my $endrange = 366; my $mark_returned = 0; +my $borrower_category = []; +my $skip_borrower_category = []; +my $help=0; +my $man=0; +my $list_categories = 0; -GetOptions( - 'lost=s%' => \$lost, - 'c|charge=s' => \$charge, - 'confirm' => \$confirm, - 'verbose' => \$verbose, - 'quiet' => \$quiet, - 'maxdays=s' => \$endrange, - 'mark-returned' => \$mark_returned, +GetOptions( + 'lost=s%' => \$lost, + 'c|charge=s' => \$charge, + 'confirm' => \$confirm, + 'v|verbose' => \$verbose, + 'quiet' => \$quiet, + 'maxdays=s' => \$endrange, + 'mark-returned' => \$mark_returned, + 'h|help' => \$help, + 'man|manual' => \$man, + 'category=s' => $borrower_category, + 'skip-category=s' => $skip_borrower_category, + 'list-categories' => \$list_categories, ); -my $usage = << 'ENDUSAGE'; -longoverdue.pl : This cron script set lost values on overdue items and optionally sets charges the patron's account -for the item's replacement price. It is designed to be run as a nightly job. The command line options that globally -define this behavior for this script will likely be moved into Koha's core circulation / issuing rules code in a -near-term release, so this script is not intended to have a long lifetime. +if ( $man ) { + pod2usage( -verbose => 2 + -exitval => 0 + ); +} + +if ( $help ) { + pod2usage( -verbose => 1, + -exitval => 0 + ); +} + +if ( scalar @$borrower_category && scalar @$skip_borrower_category) { + pod2usage( -verbose => 1, + -message => "The options --category and --skip-category 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"; + exit 0; +} + +=head1 SYNOPSIS + + longoverdue.pl [ --help | -h | --man | --list-categories ] + 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 ] ... + [ --commit ] + + +WARNING: Flippant use of this script could set all or most of the items in your catalog to Lost and charge your + patrons for them! + +WARNING: This script is known to be faulty. It is NOT recommended to use multiple --lost options. + See http://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=2883 + +=cut + +=head1 OPTIONS This script takes the following parameters : - --lost | -l This option takes the form of n=lv, - where n is num days overdue, and lv is the lost value. See warning below. +=over 8 + +=item B<--lost | -l> + +This option takes the form of n=lv, where n is num days overdue, and lv is the lost value. See warning above. + +=item B<--charge | -c> + +This specifies what lost value triggers Koha to charge the account for the lost item. Replacement costs are not charged if this is not specified. + +=item B<--verbose | -v> + +verbose. + +=item B<--confirm> + +confirm. without this option, the script will report the number of affected items and return without modifying any records. + +=item B<--quiet> + +suppress summary output. + +=item B<--maxdays> + +Specifies the end of the range of overdue days to deal with (defaults to 366). This value is universal to all lost num days overdue passed. - --charge | -c This specifies what lost value triggers Koha to charge the account for the - lost item. Replacement costs are not charged if this is not specified. +=item B<--mark-returned> - --verbose | v verbose. +When an item is marked lost, remove it from the borrowers issued items. - --confirm confirm. without this option, the script will report the number of affected items and - return without modifying any records. +=item B<--category> - --quiet suppress summary output. +Act on the listed borrower category code (borrowers.categorycode). +Exclude all others. This may be specified multiple times to include multiple categories. +May not be used with B<--skip-category> - --maxdays Specifies the end of the range of overdue days to deal with (defaults to 366). This - value is universal to all lost num days overdue passed. +=item B<--skip-category> - --mark-returned When an item is marked lost, remove it from the borrowers issued items. +Act on all available borrower category codes, except those listed. +This may be specified multiple times, to exclude multiple categories. +May not be used with B<--category> + +=item B<--list-categories> + +List borrower categories available for use by B<--category> or +B<--skip-category>, and exit. + +=item B<--help | -h> + +Display short help message an exit. + +=item B<--man | --manual > + +Display entire manual and exit. + +=back + +=cut + +=head1 Description + +This cron script set lost values on overdue items and optionally sets charges the patron's account +for the item's replacement price. It is designed to be run as a nightly job. The command line options that globally +define this behavior for this script will likely be moved into Koha's core circulation / issuing rules code in a +near-term release, so this script is not intended to have a long lifetime. + + +=cut + +=head1 Examples - examples : $PERL5LIB/misc/cronjobs/longoverdue.pl --lost 30=1 Would set LOST=1 after 30 days (up to one year), but not charge the account. This would be suitable for the Koha default LOST authorized value of 1 -> 'Lost'. $PERL5LIB/misc/cronjobs/longoverdue.pl --lost 60=2 --charge 2 Would set LOST=2 after 60 days (up to one year), and charge the account when setting LOST=2. - This would be suitable for the Koha default LOST authorized value of 2 -> 'Long Overdue' - -WARNING: Flippant use of this script could set all or most of the items in your catalog to Lost and charge your -patrons for them! - -WARNING: This script is known to be faulty. It is NOT recommended to use multiple --lost options. - See http://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=2883 + This would be suitable for the Koha default LOST authorized value of 2 -> 'Long Overdue' -ENDUSAGE +=cut # FIXME: We need three pieces of data to operate: # ~ lower bound (number of days), @@ -105,9 +202,7 @@ ENDUSAGE # FIXME: do checks on --lost ranges to make sure they are exclusive. # FIXME: do checks on --lost ranges to make sure the authorized values exist. # FIXME: do checks on --lost ranges to make sure don't go past endrange. -# FIXME: convert to using pod2usage -# FIXME: allow --help or -h -# +# if ( ! defined($lost) ) { my $longoverdue_value = C4::Context->preference('DefaultLongOverdueLostValue'); my $longoverdue_days = C4::Context->preference('DefaultLongOverdueDays'); @@ -115,8 +210,10 @@ if ( ! defined($lost) ) { $lost->{$longoverdue_days} = $longoverdue_value; } else { - print $usage; - die "ERROR: No --lost (-l) option defined"; + pod2usage( { + -exitval => 1, + -msg => q|ERROR: No --lost (-l) option defined|, + } ); } } if ( ! defined($charge) ) { @@ -154,10 +251,36 @@ sub longoverdue_sth { return C4::Context->dbh->prepare($query); } -#FIXME - Should add a 'system' user and get suitable userenv for it for logging, etc. +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 ]; +my %category_to_process; +for my $cat ( @$borrower_category ) { + unless ( grep { /^$cat$/ } @available_categories ) { + pod2usage( + '-exitval' => 1, + '-message' => "The category $cat does not exist in the database", + ); + } + $category_to_process{$cat} = 1; +} +if ( @$skip_borrower_category ) { + for my $cat ( @$skip_borrower_category ) { + unless ( grep { /^$cat$/ } @available_categories ) { + pod2usage( + '-exitval' => 1, + '-message' => "The category $cat does not exist in the database", + ); + } + } + %category_to_process = map { $_ => 1 } @available_categories; + %category_to_process = ( %category_to_process, map { $_ => 0 } @$skip_borrower_category ); +} + +my $filter_borrower_categories = ( scalar @$borrower_category || scalar @$skip_borrower_category ); my $count; -# my @ranges = map { my @report; my $total = 0; my $i = 0; @@ -172,12 +295,16 @@ foreach my $startrange (sort keys %$lost) { my ($date1) = bounds($startrange); my ($date2) = bounds( $endrange); # print "\nRange ", ++$i, "\nDue $startrange - $endrange days ago ($date2 to $date1), lost => $lostvalue\n" if($verbose); - $verbose and + $verbose and printf "\nRange %s\nDue %3s - %3s days ago (%s to %s), lost => %s\n", ++$i, $startrange, $endrange, $date2, $date1, $lostvalue; $sth_items->execute($startrange, $endrange, $lostvalue); $count=0; - while (my $row=$sth_items->fetchrow_hashref) { + ITEM: while (my $row=$sth_items->fetchrow_hashref) { + if( $filter_borrower_categories ) { + my $category = uc Koha::Borrowers->find( $row->{borrowernumber} )->categorycode(); + next ITEM unless ( $category_to_process{ $category } ); + } 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.39.5