fix for 394
[koha.git] / bookcount.pl
1 #!/usr/bin/perl
2
3 # $Id$
4
5 #written 7/3/2002 by Finlay
6 #script to display reports
7
8
9 # Copyright 2000-2002 Katipo Communications
10 #
11 # This file is part of Koha.
12 #
13 # Koha is free software; you can redistribute it and/or modify it under the
14 # terms of the GNU General Public License as published by the Free Software
15 # Foundation; either version 2 of the License, or (at your option) any later
16 # version.
17 #
18 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
19 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License along with
23 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
24 # Suite 330, Boston, MA  02111-1307 USA
25
26 use strict;
27 use CGI;
28 use C4::Context;
29 use C4::Search;
30 use C4::Circulation::Circ2;
31 use C4::Output;
32 use C4::Koha;
33 use C4::Auth;
34 use HTML::Template;
35
36 # get all the data ....
37 my %env;
38 my $main='#cccc99';
39 my $secondary='#ffffcc';
40
41 my $input = new CGI;
42 my $itm = $input->param('itm');
43 my $bi = $input->param('bi');
44 my $bib = $input->param('bib');
45 my $branches = getbranches(\%env);
46
47 my $idata = itemdatanum($itm);
48 my $data = bibitemdata($bi);
49
50 my $homebranch = $branches->{$idata->{'homebranch'}}->{'branchname'};
51 my $holdingbranch = $branches->{$idata->{'holdingbranch'}}->{'branchname'};
52
53 my ($lastmove, $message) = lastmove($itm);
54
55 my $lastdate;
56 my $count;
57 if (not $lastmove) {
58     $lastdate = $message;
59     $count = issuessince($itm , 0);
60 } else {
61     $lastdate = $lastmove->{'datearrived'};
62     $count = issuessince($itm ,$lastdate);
63 }
64
65 # make the page ...
66
67 my ($template, $loggedinuser, $cookie)
68       = get_template_and_user({template_name => "bookcount.tmpl",
69                                          query => $input,
70                                          type => "intranet",
71                                          authnotrequired => 0,
72                                          flagsrequired => {borrowers => 1},
73                                          debug => 1,
74                                          });
75
76
77
78 my @branchloop;
79
80 foreach my $branchcode (keys %$branches) {
81         my %linebranch;
82     $linebranch{issues} = issuesat($itm, $branchcode);
83     my $date = lastseenat($itm, $branchcode);
84     $linebranch{seen} = slashdate($date);
85         $linebranch{branchname}=$branches->{$branchcode}->{'branchname'};
86         push(@branchloop,\%linebranch);
87 }
88
89 $template->param(       bib => $bib,
90                                                                 title => $data->{'title'},
91                                                                 author => $data->{'author'},
92                                                                 barcode => $idata->{'barcode'},
93                                                                 homebranch =>$homebranch,
94                                                                 holdingbranch => $holdingbranch,
95                                                                 lastdate =>  $lastdate,
96                                                                 count =>  $count,
97                                                                 branchloop => \@branchloop);
98
99 print "Content-Type: text/html\n\n", $template->output;
100
101 ##############################################
102 # This stuff should probably go into C4::Search
103 # database includes
104 use DBI;
105
106 sub itemdatanum {
107     my ($itemnumber)=@_;
108     my $dbh = C4::Context->dbh;
109     my $itm = $dbh->quote("$itemnumber");
110     my $query = "select * from items where itemnumber=$itm";
111     my $sth=$dbh->prepare($query);
112     $sth->execute;
113     my $data=$sth->fetchrow_hashref;
114     $sth->finish;
115     return($data);
116 }
117
118 sub lastmove {
119       my ($itemnumber)=@_;
120       my $dbh = C4::Context->dbh;
121       my $var1 = $dbh->quote($itemnumber);
122       my $sth =$dbh->prepare("select max(branchtransfers.datearrived) from branchtransfers where branchtransfers.itemnumber=$var1");
123       $sth->execute;
124       my ($date) = $sth->fetchrow_array;
125       return(0, "Item has no branch transfers record") if not $date;
126       my $var2 = $dbh->quote($date);
127       $sth=$dbh->prepare("Select * from branchtransfers where branchtransfers.itemnumber=$var1 and branchtransfers.datearrived=$var2");
128       $sth->execute;
129       my ($data) = $sth->fetchrow_hashref;
130       return(0, "Item has no branch transfers record") if not $data;
131       $sth->finish;
132       return($data,"");
133  }
134
135 sub issuessince {
136       my ($itemnumber, $date)=@_;
137       my $dbh = C4::Context->dbh;
138       my $itm = $dbh->quote($itemnumber);
139       my $dat = $dbh->quote($date);
140       my $sth=$dbh->prepare("Select count(*) from issues where issues.itemnumber=$itm and issues.timestamp > $dat");
141       $sth->execute;
142       my $count=$sth->fetchrow_hashref;
143       $sth->finish;
144       return($count->{'count(*)'});
145 }
146
147 sub issuesat {
148       my ($itemnumber, $brcd)=@_;
149       my $dbh = C4::Context->dbh;
150       my $itm = $dbh->quote($itemnumber);
151       my $brc = $dbh->quote($brcd);
152       my $query = "Select count(*) from issues where itemnumber=$itm and branchcode = $brc";
153       my $sth=$dbh->prepare($query);
154       $sth->execute;
155       my ($count)=$sth->fetchrow_array;
156       $sth->finish;
157       return($count);
158 }
159
160 sub lastseenat {
161       my ($itemnumber, $brcd)=@_;
162       my $dbh = C4::Context->dbh;
163       my $itm = $dbh->quote($itemnumber);
164       my $brc = $dbh->quote($brcd);
165       my $query = "Select max(timestamp) from issues where itemnumber=$itm and branchcode = $brc";
166       my $sth=$dbh->prepare($query);
167       $sth->execute;
168       my ($date1)=$sth->fetchrow_array;
169       $sth->finish;
170       $query = "Select max(datearrived) from branchtransfers where itemnumber=$itm and tobranch = $brc";
171       # FIXME - There's already a $sth in this scope.
172       my $sth=$dbh->prepare($query);
173       $sth->execute;
174       my ($date2)=$sth->fetchrow_array;
175       $sth->finish;
176       $date2 =~ s/-//g;
177       $date2 =~ s/://g;
178       $date2 =~ s/ //g;
179       my $date;
180       if ($date1 < $date2) {
181           $date = $date2;
182       } else {
183           $date = $date1;
184       }
185       return($date);
186 }
187
188
189 #####################################################
190 # write date....
191 sub slashdate {
192     my ($date) = @_;
193     if (not $date) {
194         return "never";
195     }
196     my ($yr, $mo, $da, $hr, $mi) = (substr($date, 0, 4), substr($date, 4, 2), substr($date, 6, 2), substr($date, 8, 2), substr($date, 10, 2));
197     return "$hr:$mi  $da/$mo/$yr";
198 }