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