A script generating keyword clouds from Zebra Indexes
[koha.git] / misc / cronjobs / longoverdue.pl
1 #!/usr/bin/perl -w
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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 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 BEGIN {
30     # find Koha's Perl modules
31     # test carefully before changing this
32     use FindBin;
33     eval { require "$FindBin::Bin/../kohalib.pl" };
34 }
35 use C4::Context;
36 use C4::Items;
37 use C4::Accounts;
38 use Getopt::Long;
39
40 my  $lost;  #  key=lost value,  value=num days.
41 my ($charge, $verbose, $confirm);
42
43 GetOptions( 
44             'l|lost=s%'    => \$lost,
45             'c|charge=s'  => \$charge,
46             'confirm'  => \$confirm,
47             'v|verbose'  => \$verbose,
48        );
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.  
54
55 This script takes the following parameters :
56
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.
59
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.
62
63     --verbose | v       verbose.
64
65     --confirm           confirm.  without -c, this script will report the number of affected items and
66                         return without modifying any records.
67
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' 
71
72 WARNING:  Flippant use of this script could set all or most of the items in your catalog to Lost and charge your
73 patrons for them!
74
75 ENDUSAGE
76
77 if ( ! defined($lost) ) {
78     print $usage;
79     die;
80 }
81
82 my $dbh = C4::Context->dbh();
83
84 #FIXME - Should add a 'system' user and get suitable userenv for it for logging, etc.
85
86 my $endrange = 366;  # hardcoded - don't deal with anything overdue by more than this num days.
87
88 my @interval = sort keys %$lost;
89
90 my $count;
91 my @report;
92
93 # FIXME - The item is only marked returned if you supply --charge .
94 #         We need a better way to handle this.
95 #
96 # FIXME - no sql should be outside the API.
97
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 );
106         $count=0;
107         while (my $row=$sth_items->fetchrow_hashref) {
108         warn "updating $row->{'itemnumber'} for borrower $row->{'borrowernumber'} to lost: $lostvalue" if($verbose);
109             if($confirm) {
110                 ModItem({ itemlost => $lostvalue }, $row->{'biblionumber'}, $row->{'itemnumber'});
111                 chargelostitem($row->{'itemnumber'}) if( $charge && $charge eq $lostvalue);
112             }
113             $count++;
114         }
115         push @report, { range => "$startrange - $endrange",
116                         lostvalue =>  $lostvalue,
117                         count => $count,
118                      };
119     }
120     $endrange = $startrange;
121 }
122 for my $range (@report) {
123     for my $var (keys %$range) {
124         warn "$var :  $range->{$var}";
125     }
126 }
127
128
129 $sth_items->finish;
130 $dbh->disconnect;