Bug 8337: Small typo in usage statement for longoverdue.pl
[koha.git] / misc / cronjobs / longoverdue.pl
1 #!/usr/bin/perl
2 #-----------------------------------
3 # Copyright 2008 LibLime
4 #
5 # This file is part of Koha.
6 #
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
10 # version.
11 #
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.
15 #
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 #-----------------------------------
20
21 =head1 NAME
22
23 longoverdue.pl  cron script to set lost statuses on overdue materials.
24                 Execute without options for help.
25
26 =cut
27
28 use strict;
29 use warnings;
30 BEGIN {
31     # find Koha's Perl modules
32     # test carefully before changing this
33     use FindBin;
34     eval { require "$FindBin::Bin/../kohalib.pl" };
35 }
36 use C4::Context;
37 use C4::Items;
38 use C4::Circulation qw/LostItem/;
39 use Getopt::Long;
40
41 my  $lost;  #  key=lost value,  value=num days.
42 my ($charge, $verbose, $confirm, $quiet);
43 my $endrange = 366;
44 my $mark_returned = 0;
45
46 GetOptions( 
47     'lost=s%'       => \$lost,
48     'c|charge=s'    => \$charge,
49     'confirm'       => \$confirm,
50     'verbose'       => \$verbose,
51     'quiet'         => \$quiet,
52     'maxdays=s'     => \$endrange,
53     'mark-returned' => \$mark_returned,
54 );
55
56 my $usage = << 'ENDUSAGE';
57 longoverdue.pl : This cron script set lost values on overdue items and optionally sets charges the patron's account
58 for the item's replacement price.  It is designed to be run as a nightly job.  The command line options that globally
59 define this behavior for this script  will likely be moved into Koha's core circulation / issuing rules code in a 
60 near-term release, so this script is not intended to have a long lifetime.  
61
62 This script takes the following parameters :
63
64     --lost | -l         This option takes the form of n=lv,
65                         where n is num days overdue, and lv is the lost value.  See warning below.
66
67     --charge | -c       This specifies what lost value triggers Koha to charge the account for the
68                         lost item.  Replacement costs are not charged if this is not specified.
69
70     --verbose | v       verbose.
71
72     --confirm           confirm.  without this option, the script will report the number of affected items and
73                         return without modifying any records.
74
75     --quiet             suppress summary output.
76
77     --maxdays           Specifies the end of the range of overdue days to deal with (defaults to 366).  This
78                         value is universal to all lost num days overdue passed.
79
80     --mark-returned     When an item is marked lost, remove it from the borrowers issued items.
81
82   examples :
83   $PERL5LIB/misc/cronjobs/longoverdue.pl --lost 30=1
84     Would set LOST=1 after 30 days (up to one year), but not charge the account.
85     This would be suitable for the Koha default LOST authorized value of 1 -> 'Lost'.
86
87   $PERL5LIB/misc/cronjobs/longoverdue.pl --lost 60=2 --charge 2
88     Would set LOST=2 after 60 days (up to one year), and charge the account when setting LOST=2.
89     This would be suitable for the Koha default LOST authorized value of 2 -> 'Long Overdue' 
90
91 WARNING:  Flippant use of this script could set all or most of the items in your catalog to Lost and charge your
92 patrons for them!
93
94 WARNING:  This script is known to be faulty.  It is NOT recommended to use multiple --lost options.
95           See http://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=2883
96
97 ENDUSAGE
98
99 # FIXME: We need three pieces of data to operate:
100 #         ~ lower bound (number of days),
101 #         ~ upper bound (number of days),
102 #         ~ new lost value.
103 #        Right now we get only two, causing the endrange hack.  This is a design-level failure.
104 # FIXME: do checks on --lost ranges to make sure they are exclusive.
105 # FIXME: do checks on --lost ranges to make sure the authorized values exist.
106 # FIXME: do checks on --lost ranges to make sure don't go past endrange.
107 # FIXME: convert to using pod2usage
108 # FIXME: allow --help or -h
109
110 if ( ! defined($lost) ) {
111     my $longoverdue_value = C4::Context->preference('DefaultLongOverdueLostValue');
112     my $longoverdue_days = C4::Context->preference('DefaultLongOverdueDays');
113     if(defined($longoverdue_value) and defined($longoverdue_days) and $longoverdue_value ne '' and $longoverdue_days ne '' and $longoverdue_days >= 0) {
114         $lost->{$longoverdue_days} = $longoverdue_value;
115     }
116     else {
117         print $usage;
118         die "ERROR: No --lost (-l) option defined";
119     }
120 }
121 if ( ! defined($charge) ) {
122     my $charge_value = C4::Context->preference('DefaultLongOverdueChargeValue');
123     if(defined($charge_value) and $charge_value ne '') {
124         $charge = $charge_value;
125     }
126 }
127 unless ($confirm) {
128     $verbose = 1;     # If you're not running it for real, then the whole point is the print output.
129     print "### TEST MODE -- NO ACTIONS TAKEN ###\n";
130 }
131
132 # In my opinion, this line is safe SQL to have outside the API. --atz
133 our $bounds_sth = C4::Context->dbh->prepare("SELECT DATE_SUB(CURDATE(), INTERVAL ? DAY)");
134
135 sub bounds ($) {
136     $bounds_sth->execute(shift);
137     return $bounds_sth->fetchrow;
138 }
139
140 # FIXME - This sql should be inside the API.
141 sub longoverdue_sth {
142     my $query = "
143     SELECT items.itemnumber, borrowernumber, date_due
144       FROM issues, items
145      WHERE items.itemnumber = issues.itemnumber
146       AND  DATE_SUB(CURDATE(), INTERVAL ? DAY)  > date_due
147       AND  DATE_SUB(CURDATE(), INTERVAL ? DAY) <= date_due
148       AND  itemlost <> ?
149      ORDER BY date_due
150     ";
151     return C4::Context->dbh->prepare($query);
152 }
153
154 #FIXME - Should add a 'system' user and get suitable userenv for it for logging, etc.
155
156 my $count;
157 # my @ranges = map { 
158 my @report;
159 my $total = 0;
160 my $i = 0;
161
162 # FIXME - The item is only marked returned if you supply --charge .
163 #         We need a better way to handle this.
164 #
165 my $sth_items = longoverdue_sth();
166
167 foreach my $startrange (sort keys %$lost) {
168     if( my $lostvalue = $lost->{$startrange} ) {
169         my ($date1) = bounds($startrange);
170         my ($date2) = bounds(  $endrange);
171         # print "\nRange ", ++$i, "\nDue $startrange - $endrange days ago ($date2 to $date1), lost => $lostvalue\n" if($verbose);
172         $verbose and 
173             printf "\nRange %s\nDue %3s - %3s days ago (%s to %s), lost => %s\n", ++$i,
174             $startrange, $endrange, $date2, $date1, $lostvalue;
175         $sth_items->execute($startrange, $endrange, $lostvalue);
176         $count=0;
177         while (my $row=$sth_items->fetchrow_hashref) {
178             printf ("Due %s: item %5s from borrower %5s to lost: %s\n", $row->{date_due}, $row->{itemnumber}, $row->{borrowernumber}, $lostvalue) if($verbose);
179             if($confirm) {
180                 ModItem({ itemlost => $lostvalue }, $row->{'biblionumber'}, $row->{'itemnumber'});
181                 LostItem($row->{'itemnumber'}, $mark_returned) if( $charge && $charge eq $lostvalue);
182             }
183             $count++;
184         }
185         push @report, {
186            startrange => $startrange,
187              endrange => $endrange,
188                 range => "$startrange - $endrange",
189                 date1 => $date1,
190                 date2 => $date2,
191             lostvalue => $lostvalue,
192                 count => $count,
193         };
194         $total += $count;
195     }
196     $endrange = $startrange;
197 }
198
199 sub summarize ($$) {
200     my $arg = shift;    # ref to array
201     my $got_items = shift || 0;     # print "count" line for items
202     my @report = @$arg or return undef;
203     my $i = 0;
204     for my $range (@report) {
205         printf "\nRange %s\nDue %3s - %3s days ago (%s to %s), lost => %s\n", ++$i,
206             map {$range->{$_}} qw(startrange endrange date2 date1 lostvalue);
207         $got_items and printf "  %4s items\n", $range->{count};
208     }
209 }
210
211 if (!$quiet){
212     print "\n### LONGOVERDUE SUMMARY ###";
213     summarize (\@report, 1);
214     print "\nTOTAL: $total items\n";
215 }