installer: command-line scripts improve finding C4 modules
[koha.git] / misc / cronjobs / reservefix.pl
1 #!/usr/bin/perl -w
2 #-----------------------------------
3 # Script Name: reservefix.pl
4 # Script Version: 1.0.0
5 # Date:  2004/02/22
6 # Author:  Stephen Hedges  shedges@skemotah.com
7 # Description: fixes priority of reserves
8 #    It also e-mails a list of 'problem' reserves
9 #    to me at the library
10 # Usage: reservefix.pl.
11 # Revision History:
12 #    1.0.0  2004/02/22:  original version
13 #-----------------------------------
14
15 use strict;
16 BEGIN {
17     # find Koha's Perl modules
18     # test carefully before changing this
19     use FindBin;
20     eval { require "$FindBin::Bin/../kohalib.pl" };
21 }
22 use C4::Context;
23 use Date::Manip;
24 use Mail::Sendmail;
25
26 my $dbh   = C4::Context->dbh;
27 my $message;   # e-mail message
28 my $admin = "root@localhost"; #To
29 my @library = "root@localhost"; #From
30 #    get biblionumbers of unfilled reserves
31 my $bibnos_sth=$dbh->prepare("SELECT DISTINCT biblionumber FROM reserves WHERE cancellationdate IS NULL AND found IS NULL AND priority>0");
32 my $get_sth=$dbh->prepare("SELECT * FROM reserves WHERE biblionumber=? AND cancellationdate IS NULL AND found IS NULL ORDER BY reservedate,priority");
33 #    checking reservedate avoids overwriting legitimate duplicate reserves
34 my $put_sth=$dbh->prepare("UPDATE reserves SET priority=? WHERE biblionumber=? AND borrowernumber=? AND reservedate=?");
35 my $count_sth=$dbh->prepare("SELECT COUNT(itemnumber) FROM items WHERE biblionumber=?");
36 my $dvd_sth=$dbh->prepare("SELECT itemtype FROM biblioitems WHERE biblionumber=?");
37
38 $bibnos_sth->execute();
39
40 while (my $number=$bibnos_sth->fetchrow_arrayref) {
41     my $bibliono=$number->[0];
42
43     $get_sth->execute($bibliono);
44
45     my $priority=0;
46     while (my $data=$get_sth->fetchrow_hashref){
47         $priority++;
48         my $bibno = $data->{'biblionumber'};
49         my $borrno = $data->{'borrowernumber'};
50         my $resdate = $data->{'reservedate'};
51         if ($priority==1) {
52             my $date1 = DateCalc("today","- 60 days"); # calculate date 60 days ago
53             my $date2 = ParseDate($resdate);
54             my $flag = Date_Cmp($date2,$date1);
55             if ($flag<0) {      # date1 is later
56                 $dvd_sth->execute($bibno);
57                 while (my $itemtype=$dvd_sth->fetchrow_arrayref) {
58                     my $it = $itemtype->[0];
59                     if ($it) {
60                         if ($it ne 'DVD') {
61                             $message .= "Check $bibno\n";
62 #                           print "Check $bibno\n";
63                         }
64                     } else {
65                         $message .= "$bibno has no itemtype\n";
66 #                       print "$bibno has no itemtype\n";
67                     }
68                 }
69                 $dvd_sth->finish;
70             }
71         }
72         $put_sth->execute($priority,$bibno,$borrno,$resdate);
73         $put_sth->finish;
74     }
75
76     $count_sth->execute($bibliono);         # get item count
77     my $itemcount=$count_sth->fetchrow;
78     if (($priority/4)>$itemcount) {      # no more than 4 reserves per item
79         $dvd_sth->execute($bibliono);
80         while (my $itemtype=$dvd_sth->fetchrow_arrayref) {
81             my $it = $itemtype->[0];
82             if ($it) {
83                 if ($it ne 'DVD') {
84                     $message .= "Check $bibliono\n";
85 #                   print "Check $bibliono\n";
86                 }
87             } else {
88                 $message .= "$bibliono has no itemtype\n"
89 #               print "$bibliono has no itemtype\n";
90             }
91         }
92         $dvd_sth->finish;
93     }
94     $count_sth->finish;
95     $get_sth->finish;
96 }
97 $bibnos_sth->finish;
98 $dbh->disconnect;
99
100 my %mail = ( To      => '$admin',
101              From    => '$library',
102              Subject => 'Reserve problems',
103              Message => $message,
104             );
105 sendmail(%mail);
106