Fixed bug. $sth->execute after $sti->prepare.
[koha.git] / bookcount.pl
1 #!/usr/bin/perl
2
3 #written 7/3/2002 by Finlay
4 #script to display reports
5
6 use strict;
7 use CGI;
8 use C4::Search;
9 use C4::Circulation::Circ2;
10 use C4::Output;
11
12 # get all the data ....
13 my %env;
14 my $main='#cccc99';
15 my $secondary='#ffffcc';
16
17 my $input = new CGI;
18 my $itm = $input->param('itm');
19 my $bi = $input->param('bi');
20 my $bib = $input->param('bib');
21 my $branches = getbranches(\%env);
22
23 my $idata = itemdatanum($itm);
24 my $data = bibitemdata($bi);
25
26 my $homebranch = $branches->{$idata->{'homebranch'}}->{'branchname'};
27 my $holdingbranch = $branches->{$idata->{'holdingbranch'}}->{'branchname'};
28
29 my ($lastmove, $message) = lastmove($itm);
30
31 my $lastdate;
32 my $count;
33 if (not $lastmove) {
34     $lastdate = $message;
35     $count = issuessince($itm , 0);
36 } else {
37     $lastdate = $lastmove->{'datearrived'};
38     $count = issuessince($itm ,$lastdate);
39 }
40
41
42 # make the page ... 
43 print $input->header;
44
45
46 print startpage;
47 print startmenu('report');
48 print center;
49
50 print <<"EOF";
51 <br>
52 <FONT SIZE=6><em><a href=/cgi-bin/koha/detail.pl?bib=$bib&type=intra>$data->{'title'} ($data->{'author'})</a></em></FONT><P>
53 <p>
54 <img src="/images/holder.gif" width=16 height=200 align=left>
55 <TABLE  CELLSPACING=0  CELLPADDING=5 border=1 width=440 >
56   <TR VALIGN=TOP><td  bgcolor="99cc33" background="/images/background-mem.gif">
57   <B>BARCODE $idata->{'barcode'}</b></TD>
58 </TR>
59 <TR VALIGN=TOP  >
60 <TD width=440 >
61
62 <b>Home Branch: </b> $homebranch <br>
63 <b>Current Branch: </b> $holdingbranch<br>
64 <b>Date arrived at current branch: </b> $lastdate <br>
65 <b>Number of issues since since the above date :</b> $count <br>
66
67 <table cellspacing =0 cellpadding=5 border=1 width = 440>
68 <TR><TD > <b>Branch</b></td>  <TD >   <b>No. of Issues</b></td>   <td><b>Last seen at branch</b></td></TR>
69 EOF
70
71 foreach my $branchcode (keys %$branches) {
72     my $issues = issuesat($itm, $branchcode);
73     my $date = lastseenat($itm, $branchcode);
74     my $seen = slashdate($date);
75     print << "EOF";
76 <TR><TD > <b>$branches->{$branchcode}->{'branchname'}</b></td>
77 <TD >    <b> $issues </b></td>             <td><b> $seen</b></td></TR>
78 EOF
79 }
80 print <<"EOF";
81 </table>
82 </TR>
83
84 </table>
85 EOF
86
87
88 print endmenu('report');
89 print endpage;
90
91
92 ##############################################
93 # This stuff should probably go into C4::Search
94 # database includes
95 use DBI;
96 use C4::Database;
97
98 sub itemdatanum {
99     my ($itemnumber)=@_;
100     my $dbh=C4Connect;
101     my $itm = $dbh->quote("$itemnumber");
102     my $query = "select * from items where itemnumber=$itm";
103     my $sth=$dbh->prepare($query);
104     $sth->execute;
105     my $data=$sth->fetchrow_hashref;
106     $sth->finish;
107     $dbh->disconnect;
108     return($data);
109 }
110
111 sub lastmove {
112       my ($itemnumber)=@_;
113       my $dbh=C4Connect;
114       my $var1 = $dbh->quote($itemnumber);
115       my $sth =$dbh->prepare("select max(branchtransfers.datearrived) from branchtransfers where branchtransfers.itemnumber=$var1");
116       $sth->execute;
117       my ($date) = $sth->fetchrow_array;
118       return(0, "Item has no branch transfers record") if not $date;
119       my $var2 = $dbh->quote($date);      
120       $sth=$dbh->prepare("Select * from branchtransfers where branchtransfers.itemnumber=$var1 and branchtransfers.datearrived=$var2");
121       $sth->execute;
122       my ($data) = $sth->fetchrow_hashref;
123       return(0, "Item has no branch transfers record") if not $data;
124       $sth->finish;
125       $dbh->disconnect;
126       return($data,"");
127  }
128
129 sub issuessince {
130       my ($itemnumber, $date)=@_;
131       my $dbh=C4Connect;
132       my $itm = $dbh->quote($itemnumber);
133       my $dat = $dbh->quote($date);
134       my $sth=$dbh->prepare("Select count(*) from issues where issues.itemnumber=$itm and issues.timestamp > $dat");
135       $sth->execute;
136       my $count=$sth->fetchrow_hashref;
137       $sth->finish;
138       $dbh->disconnect;
139       return($count->{'count(*)'});
140 }
141
142 sub issuesat {
143       my ($itemnumber, $brcd)=@_;
144       my $dbh=C4Connect;
145       my $itm = $dbh->quote($itemnumber);
146       my $brc = $dbh->quote($brcd);
147       my $query = "Select count(*) from issues where itemnumber=$itm and branchcode = $brc";
148       my $sth=$dbh->prepare($query);
149       $sth->execute;
150       my ($count)=$sth->fetchrow_array;
151       $sth->finish;
152       $dbh->disconnect;
153       return($count);
154 }
155
156 sub lastseenat {
157       my ($itemnumber, $brcd)=@_;
158       my $dbh=C4Connect;
159       my $itm = $dbh->quote($itemnumber);
160       my $brc = $dbh->quote($brcd);
161       my $query = "Select max(timestamp) from issues where itemnumber=$itm and branchcode = $brc";
162       my $sth=$dbh->prepare($query);
163       $sth->execute;
164       my ($date1)=$sth->fetchrow_array;
165       $sth->finish;
166       $query = "Select max(datearrived) from branchtransfers where itemnumber=$itm and tobranch = $brc";
167       my $sth=$dbh->prepare($query);
168       $sth->execute;
169       my ($date2)=$sth->fetchrow_array;
170       $sth->finish;
171       $dbh->disconnect;
172       $date2 =~ s/-//g;
173       $date2 =~ s/://g;
174       $date2 =~ s/ //g;
175       my $date;
176       if ($date1 < $date2) {
177           $date = $date2;
178       } else {
179           $date = $date1;
180       }
181       return($date);
182 }
183
184
185 #####################################################
186 # write date....
187 sub slashdate {
188     my ($date) = @_;
189     if (not $date) {
190         return "never";
191     }
192     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));
193     return "$hr:$mi  $da/$mo/$yr";
194 }