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
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #-----------------------------------
23 longoverdue.pl cron script to set lost statuses on overdue materials.
24 Execute without options for help.
31 # find Koha's Perl modules
32 # test carefully before changing this
34 eval { require "$FindBin::Bin/../kohalib.pl" };
41 my $lost; # key=lost value, value=num days.
42 my ($charge, $verbose, $confirm, $quiet);
43 my $endrange = 366; # FIXME hardcoded - don't deal with anything overdue by more than this num days.
47 'c|charge=s' => \$charge,
48 'confirm' => \$confirm,
49 'verbose' => \$verbose,
53 my $usage = << 'ENDUSAGE';
54 longoverdue.pl : This cron script set lost values on overdue items and optionally sets charges the patron's account
55 for the item's replacement price. It is designed to be run as a nightly job. The command line options that globally
56 define this behavior for this script will likely be moved into Koha's core circulation / issuing rules code in a
57 near-term release, so this script is not intended to have a long lifetime.
59 This script takes the following parameters :
61 --lost | -l This option takes the form of n=lv,
62 where n is num days overdue, and lv is the lost value. See warning below.
64 --charge | -c This specifies what lost value triggers Koha to charge the account for the
65 lost item. Replacement costs are not charged if this is not specified.
67 --verbose | v verbose.
69 --confirm confirm. without this option, the script will report the number of affected items and
70 return without modifying any records.
73 $PERL5LIB/misc/cronjobs/longoverdue.pl --lost 30=1
74 Would set LOST=1 after 30 days (up to one year), but not charge the account.
75 This would be suitable for the Koha default LOST authorized value of 1 -> 'Lost'.
77 $PERL5LIB/misc/cronjobs/longoverdue.pl --lost 60=2 --charge 1
78 Would set LOST=2 after 60 days (up to one year), and charge the account when setting LOST=2.
79 This would be suitable for the Koha default LOST authorized value of 2 -> 'Long Overdue'
81 WARNING: Flippant use of this script could set all or most of the items in your catalog to Lost and charge your
84 WARNING: This script is known to be faulty. It is NOT recommended to use multiple --lost options.
85 See http://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=2883
89 # FIXME: We need three pieces of data to operate:
90 # ~ lower bound (number of days),
91 # ~ upper bound (number of days),
93 # Right now we get only two, causing the endrange hack. This is a design-level failure.
94 # FIXME: do checks on --lost ranges to make sure they are exclusive.
95 # FIXME: do checks on --lost ranges to make sure the authorized values exist.
96 # FIXME: do checks on --lost ranges to make sure don't go past endrange.
97 # FIXME: convert to using pod2usage
98 # FIXME: allow --help or -h
100 if ( ! defined($lost) ) {
102 die "ERROR: No --lost (-l) option defined";
105 $verbose = 1; # If you're not running it for real, then the whole point is the print output.
106 print "### TEST MODE -- NO ACTIONS TAKEN ###\n";
109 # In my opinion, this line is safe SQL to have outside the API. --atz
110 our $bounds_sth = C4::Context->dbh->prepare("SELECT DATE_SUB(CURDATE(), INTERVAL ? DAY)");
113 $bounds_sth->execute(shift);
114 return $bounds_sth->fetchrow;
117 # FIXME - This sql should be inside the API.
118 sub longoverdue_sth {
120 SELECT items.itemnumber, borrowernumber, date_due
122 WHERE items.itemnumber = issues.itemnumber
123 AND DATE_SUB(CURDATE(), INTERVAL ? DAY) > date_due
124 AND DATE_SUB(CURDATE(), INTERVAL ? DAY) <= date_due
128 return C4::Context->dbh->prepare($query);
131 #FIXME - Should add a 'system' user and get suitable userenv for it for logging, etc.
139 # FIXME - The item is only marked returned if you supply --charge .
140 # We need a better way to handle this.
142 my $sth_items = longoverdue_sth();
144 foreach my $startrange (sort keys %$lost) {
145 if( my $lostvalue = $lost->{$startrange} ) {
146 my ($date1) = bounds($startrange);
147 my ($date2) = bounds( $endrange);
148 # print "\nRange ", ++$i, "\nDue $startrange - $endrange days ago ($date2 to $date1), lost => $lostvalue\n" if($verbose);
150 printf "\nRange %s\nDue %3s - %3s days ago (%s to %s), lost => %s\n", ++$i,
151 $startrange, $endrange, $date2, $date1, $lostvalue;
152 $sth_items->execute($startrange, $endrange, $lostvalue);
154 while (my $row=$sth_items->fetchrow_hashref) {
155 printf ("Due %s: item %5s from borrower %5s to lost: %s\n", $row->{date_due}, $row->{itemnumber}, $row->{borrowernumber}, $lostvalue) if($verbose);
157 ModItem({ itemlost => $lostvalue }, $row->{'biblionumber'}, $row->{'itemnumber'});
158 chargelostitem($row->{'itemnumber'}) if( $charge && $charge eq $lostvalue);
163 startrange => $startrange,
164 endrange => $endrange,
165 range => "$startrange - $endrange",
168 lostvalue => $lostvalue,
173 $endrange = $startrange;
177 my $arg = shift; # ref to array
178 my $got_items = shift || 0; # print "count" line for items
179 my @report = @$arg or return undef;
181 for my $range (@report) {
182 printf "\nRange %s\nDue %3s - %3s days ago (%s to %s), lost => %s\n", ++$i,
183 map {$range->{$_}} qw(startrange endrange date2 date1 lostvalue);
184 $got_items and printf " %4s items\n", $range->{count};
189 print "\n### LONGOVERDUE SUMMARY ###";
190 summarize (\@report, 1);
191 print "\nTOTAL: $total items\n";