fixing permissions on scripts
[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 lib '/usr/local/koha/intranet/modules/';
16
17 use strict;
18 use C4::Context;
19 use Date::Manip;
20 use Mail::Sendmail;
21
22 my $dbh   = C4::Context->dbh;
23 my $message;   # e-mail message
24 my $admin = "root@localhost"; #To
25 my @library = "root@localhost"; #From
26 #    get biblionumbers of unfilled reserves
27 my $bibnos_sth=$dbh->prepare("SELECT DISTINCT biblionumber FROM reserves WHERE cancellationdate IS NULL AND found IS NULL AND priority>0");
28 my $get_sth=$dbh->prepare("SELECT * FROM reserves WHERE biblionumber=? AND cancellationdate IS NULL AND found IS NULL ORDER BY reservedate,priority");
29 #    checking reservedate avoids overwriting legitimate duplicate reserves
30 my $put_sth=$dbh->prepare("UPDATE reserves SET priority=? WHERE biblionumber=? AND borrowernumber=? AND reservedate=?");
31 my $count_sth=$dbh->prepare("SELECT COUNT(itemnumber) FROM items WHERE biblionumber=?");
32 my $dvd_sth=$dbh->prepare("SELECT itemtype FROM biblioitems WHERE biblionumber=?");
33
34 $bibnos_sth->execute();
35
36 while (my $number=$bibnos_sth->fetchrow_arrayref) {
37     my $bibliono=$number->[0];
38
39     $get_sth->execute($bibliono);
40
41     my $priority=0;
42     while (my $data=$get_sth->fetchrow_hashref){
43         $priority++;
44         my $bibno = $data->{'biblionumber'};
45         my $borrno = $data->{'borrowernumber'};
46         my $resdate = $data->{'reservedate'};
47         if ($priority==1) {
48             my $date1 = DateCalc("today","- 60 days"); # calculate date 60 days ago
49             my $date2 = ParseDate($resdate);
50             my $flag = Date_Cmp($date2,$date1);
51             if ($flag<0) {      # date1 is later
52                 $dvd_sth->execute($bibno);
53                 while (my $itemtype=$dvd_sth->fetchrow_arrayref) {
54                     my $it = $itemtype->[0];
55                     if ($it) {
56                         if ($it ne 'DVD') {
57                             $message .= "Check $bibno\n";
58 #                           print "Check $bibno\n";
59                         }
60                     } else {
61                         $message .= "$bibno has no itemtype\n";
62 #                       print "$bibno has no itemtype\n";
63                     }
64                 }
65                 $dvd_sth->finish;
66             }
67         }
68         $put_sth->execute($priority,$bibno,$borrno,$resdate);
69         $put_sth->finish;
70     }
71
72     $count_sth->execute($bibliono);         # get item count
73     my $itemcount=$count_sth->fetchrow;
74     if (($priority/4)>$itemcount) {      # no more than 4 reserves per item
75         $dvd_sth->execute($bibliono);
76         while (my $itemtype=$dvd_sth->fetchrow_arrayref) {
77             my $it = $itemtype->[0];
78             if ($it) {
79                 if ($it ne 'DVD') {
80                     $message .= "Check $bibliono\n";
81 #                   print "Check $bibliono\n";
82                 }
83             } else {
84                 $message .= "$bibliono has no itemtype\n"
85 #               print "$bibliono has no itemtype\n";
86             }
87         }
88         $dvd_sth->finish;
89     }
90     $count_sth->finish;
91     $get_sth->finish;
92 }
93 $bibnos_sth->finish;
94 $dbh->disconnect;
95
96 my %mail = ( To      => '$admin',
97              From    => '$library',
98              Subject => 'Reserve problems',
99              Message => $message,
100             );
101 sendmail(%mail);
102