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