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