2 #-----------------------------------
3 # Copyright 2008 LibLime
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
19 #-----------------------------------
23 longoverdue.pl cron script to set lost statuses on overdue materials.
24 Execute without options for help.
30 # find Koha's Perl modules
31 # test carefully before changing this
33 eval { require "$FindBin::Bin/../kohalib.pl" };
40 my $lost; # key=lost value, value=num days.
41 my ($charge, $verbose, $confirm);
44 'l|lost=s%' => \$lost,
45 'c|charge=s' => \$charge,
46 'confirm' => \$confirm,
47 'v|verbose' => \$verbose,
49 my $usage = << 'ENDUSAGE';
50 longoverdue.pl : This cron script set lost values on overdue items and optionally sets charges the patron's account
51 for the item's replacement price. It is designed to be run as a nightly job. The command line options that globally
52 define this behavior for this script will likely be moved into Koha's core circulation / issuing rules code in a
53 near-term release, so this script is not intended to have a long lifetime.
55 This script takes the following parameters :
57 --lost | -l This option may be used multiple times, and takes the form of n=lv ,
58 where n is num days overdue, and lv is the lost value.
60 --charge | -c This specifies what lost value triggers Koha to charge the account for the
61 lost item. Replacement costs are not charged if this is not specified.
63 --verbose | v verbose.
65 --confirm confirm. without -c, this script will report the number of affected items and
66 return without modifying any records.
68 example : $PERL5LIB/misc/cronjobs/longoverdue.pl --lost 30=2 --lost 60=1 --charge 1
69 would set LOST= 1 after 30 days, LOST= 2 after 60 days, and charge the account when setting LOST= 2 (i.e., 60 days).
70 This would be suitable for the Koha default LOST authorized values of 1 -> 'Lost' and 2 -> 'Long Overdue'
72 WARNING: Flippant use of this script could set all or most of the items in your catalog to Lost and charge your
77 if ( ! defined($lost) ) {
82 my $dbh = C4::Context->dbh();
84 #FIXME - Should add a 'system' user and get suitable userenv for it for logging, etc.
86 my $endrange = 366; # hardcoded - don't deal with anything overdue by more than this num days.
88 my @interval = sort keys %$lost;
93 # FIXME - The item is only marked returned if you supply --charge .
94 # We need a better way to handle this.
96 # FIXME - no sql should be outside the API.
98 my $query = "SELECT items.itemnumber,borrowernumber FROM issues,items WHERE items.itemnumber=issues.itemnumber AND
99 DATE_SUB( CURDATE(), INTERVAL ? DAY) > date_due AND DATE_SUB( CURDATE(), INTERVAL ? DAY ) <= date_due AND itemlost <> ? ";
100 my $sth_items = $dbh->prepare($query);
101 while ( my $startrange = shift @interval ) {
102 if( my $lostvalue = $lost->{$startrange} ) {
103 #warn "query: $query \\with\\ params: $startrange,$endrange, $lostvalue "if($verbose);
104 warn "starting range: $startrange - $endrange with lost value $lostvalue" if($verbose);
105 $sth_items->execute( $startrange,$endrange, $lostvalue );
107 while (my $row=$sth_items->fetchrow_hashref) {
108 warn "updating $row->{'itemnumber'} for borrower $row->{'borrowernumber'} to lost: $lostvalue" if($verbose);
110 ModItem({ itemlost => $lostvalue }, $row->{'biblionumber'}, $row->{'itemnumber'});
111 chargelostitem($row->{'itemnumber'}) if( $charge && $charge eq $lostvalue);
115 push @report, { range => "$startrange - $endrange",
116 lostvalue => $lostvalue,
120 $endrange = $startrange;
122 for my $range (@report) {
123 for my $var (keys %$range) {
124 warn "$var : $range->{$var}";