From 602882d1cf534d83fef0f77b6547bd66c6ab14e6 Mon Sep 17 00:00:00 2001 From: Barton Chittenden Date: Mon, 31 Aug 2015 08:42:32 -0700 Subject: [PATCH] [SIGNED-OFF] Bug 14292: Add patron category restrictions to longoverdue.pl Add command line options --category and --skip-category Convert usage() to pod2usage. (This was previous FIXME) http://bugs.koha-community.org/show_bug.cgi?id=14292 Signed-off-by: hbraum@nekls.org --- misc/cronjobs/longoverdue.pl | 200 +++++++++++++++++++++++++++++++++--------- 1 file changed, 160 insertions(+), 40 deletions(-) diff --git a/misc/cronjobs/longoverdue.pl b/misc/cronjobs/longoverdue.pl index 8edbcf8..8bc1518 100755 --- a/misc/cronjobs/longoverdue.pl +++ b/misc/cronjobs/longoverdue.pl @@ -38,64 +38,135 @@ use C4::Items; use C4::Circulation qw/LostItem/; use Getopt::Long; use C4::Log; +use Pod::Usage; 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; -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, + 'verbose' => \$verbose, + 'quiet' => \$quiet, + 'maxdays=s' => \$endrange, + 'mark-returned' => \$mark_returned, + 'help' => \$help, + 'man|manual' => \$man, + 'category=s' => $borrower_category, + 'skip-category=s' => $skip_borrower_category ); -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 ); + exit; +} + +if ( $help ) { + pod2usage(1); + exit; +} + +=head1 SYNOPSIS + + longoverdue.pl [ --help | -h | --man ] + longoverdue.pl --lost | -l DAYS=LOST_CODE [ --charge | -c CHARGE_CODE ] [ --verbose | -v ] [ --quiet ] + [ --maxdays MAX_DAYS ] [ --mark-returned ] [ --category BORROWER_CATEGOERY ] ... + [ --skip-category BORROWER_CATEGOERY ] ... [ --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. + +=item B<--mark-returned> + +When an item is marked lost, remove it from the borrowers issued items. + +=item B<--category> + +Act on the listed borrower category code (borrowers.categorycode). Exclude all others. This may be specified multiple times to include multiple categories. + +=item B<--skip-category> - --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. +Act on all available borrower category codes, except those listed. This may be specified multiple times, to exclude multiple categories. - --verbose | v verbose. +=item B<--help | -h> - --confirm confirm. without this option, the script will report the number of affected items and - return without modifying any records. +Display short help message an exit. - --quiet suppress summary output. +=item B<--man | --manual > - --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. +Display entire manual and exit. - --mark-returned When an item is marked lost, remove it from the borrowers issued items. +=back + +All 'category' options will be applied before any 'skip-category' options, meaning that the only categories available to skip +are those which have already been specified on the command line. + +=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! + This would be suitable for the Koha default LOST authorized value of 2 -> 'Long Overdue' -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 - -ENDUSAGE +=cut # FIXME: We need three pieces of data to operate: # ~ lower bound (number of days), @@ -105,9 +176,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,7 +184,7 @@ if ( ! defined($lost) ) { $lost->{$longoverdue_days} = $longoverdue_value; } else { - print $usage; + pod2usage(1); die "ERROR: No --lost (-l) option defined"; } } @@ -154,10 +223,57 @@ sub longoverdue_sth { return C4::Context->dbh->prepare($query); } +# The following two functions can and should replaced by a call to +# Koha::Database. +sub borrower_categories_sth { + my $query = "select distinct categorycode from categories"; + return C4::Context->dbh->prepare($query); +} + +sub defined_borrower_categories { + my $sth = borrower_categories_sth(); + my @categories = (); + $sth->execute(); + CATEGORY: while ( my $row = $sth->fetchrow_hashref ) { + push @categories, uc( $row->{categorycode} ); + } + return @categories; +} + +# returns a hash of borrower category codes to test for long-overdue-ness. +# The key is the borrower category, upper case. The value is 1. This +# will make a fast look-up of valid borrower category codes. +sub get_borrower_categories { + my ( $borrower_category, $skip_borrower_category ) = ( @_ ); + + return () unless ( scalar @$borrower_category || scalar @$skip_borrower_category ); + + my @categories = defined_borrower_categories(); + if( @$borrower_category ) { + my %borcat = map { ( uc($_) => 1 ) } @$borrower_category; + @categories = grep { $borcat{$_} } @categories; + } + if( @$skip_borrower_category ) { + my %skip_borcat = map { ( uc($_) => 1 ) } @$skip_borrower_category; + @categories = grep { ! $skip_borcat{$_} } @categories; + } + return map { $_ => 1 } @categories; +} + +# TODO: category handling should be more user friendly: +# * Warn if a category specified using --category or +# --skip-category is not in the categories table +# * Add --list-categories argument so that you don't +# need to bring up the staff client or koha-mysql to +# find the valid borrower categories. + +my %category_to_process = get_borrower_categories( $borrower_category, $skip_borrower_category ); +my $filter_borrower_categories = ( scalar @$borrower_category || scalar @$skip_borrower_category ); + #FIXME - Should add a 'system' user and get suitable userenv for it for logging, etc. my $count; -# my @ranges = map { +# my @ranges = map { my @report; my $total = 0; my $i = 0; @@ -172,12 +288,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::Database->new()->schema->resultset('Borrower')->find( $row->{borrowernumber} )->get_column('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'}); -- 1.7.10.4