*** empty log message ***
[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 HTML::Template;
33
34 # get all the data ....
35 my %env;
36 my $main='#cccc99';
37 my $secondary='#ffffcc';
38
39 my $input = new CGI;
40 my $itm = $input->param('itm');
41 my $bi = $input->param('bi');
42 my $bib = $input->param('bib');
43 my $branches = getbranches(\%env);
44
45 my $idata = itemdatanum($itm);
46 my $data = bibitemdata($bi);
47
48 my $homebranch = $branches->{$idata->{'homebranch'}}->{'branchname'};
49 my $holdingbranch = $branches->{$idata->{'holdingbranch'}}->{'branchname'};
50
51 my ($lastmove, $message) = lastmove($itm);
52
53 my $lastdate;
54 my $count;
55 if (not $lastmove) {
56     $lastdate = $message;
57     $count = issuessince($itm , 0);
58 } else {
59     $lastdate = $lastmove->{'datearrived'};
60     $count = issuessince($itm ,$lastdate);
61 }
62
63 # make the page ...
64 my $template = gettemplate("bookcount.tmpl");
65
66 my @branchloop;
67
68 foreach my $branchcode (keys %$branches) {
69         my %linebranch;
70     $linebranch{issues} = issuesat($itm, $branchcode);
71     my $date = lastseenat($itm, $branchcode);
72     $linebranch{seen} = slashdate($date);
73         $linebranch{branchname}=$branches->{$branchcode}->{'branchname'};
74         push(@branchloop,\%linebranch);
75 }
76
77 $template->param(       bib => $bib,
78                                                                 title => $data->{'title'},
79                                                                 author => $data->{'author'},
80                                                                 barcode => $idata->{'barcode'},
81                                                                 homebranch =>$homebranch,
82                                                                 holdingbranch => $holdingbranch,
83                                                                 lastdate =>  $lastdate,
84                                                                 count =>  $count,
85                                                                 branchloop => \@branchloop);
86
87 print "Content-Type: text/html\n\n", $template->output;
88
89 ##############################################
90 # This stuff should probably go into C4::Search
91 # database includes
92 use DBI;
93
94 sub itemdatanum {
95     my ($itemnumber)=@_;
96     my $dbh = C4::Context->dbh;
97     my $itm = $dbh->quote("$itemnumber");
98     my $query = "select * from items where itemnumber=$itm";
99     my $sth=$dbh->prepare($query);
100     $sth->execute;
101     my $data=$sth->fetchrow_hashref;
102     $sth->finish;
103     return($data);
104 }
105
106 sub lastmove {
107       my ($itemnumber)=@_;
108       my $dbh = C4::Context->dbh;
109       my $var1 = $dbh->quote($itemnumber);
110       my $sth =$dbh->prepare("select max(branchtransfers.datearrived) from branchtransfers where branchtransfers.itemnumber=$var1");
111       $sth->execute;
112       my ($date) = $sth->fetchrow_array;
113       return(0, "Item has no branch transfers record") if not $date;
114       my $var2 = $dbh->quote($date);
115       $sth=$dbh->prepare("Select * from branchtransfers where branchtransfers.itemnumber=$var1 and branchtransfers.datearrived=$var2");
116       $sth->execute;
117       my ($data) = $sth->fetchrow_hashref;
118       return(0, "Item has no branch transfers record") if not $data;
119       $sth->finish;
120       return($data,"");
121  }
122
123 sub issuessince {
124       my ($itemnumber, $date)=@_;
125       my $dbh = C4::Context->dbh;
126       my $itm = $dbh->quote($itemnumber);
127       my $dat = $dbh->quote($date);
128       my $sth=$dbh->prepare("Select count(*) from issues where issues.itemnumber=$itm and issues.timestamp > $dat");
129       $sth->execute;
130       my $count=$sth->fetchrow_hashref;
131       $sth->finish;
132       return($count->{'count(*)'});
133 }
134
135 sub issuesat {
136       my ($itemnumber, $brcd)=@_;
137       my $dbh = C4::Context->dbh;
138       my $itm = $dbh->quote($itemnumber);
139       my $brc = $dbh->quote($brcd);
140       my $query = "Select count(*) from issues where itemnumber=$itm and branchcode = $brc";
141       my $sth=$dbh->prepare($query);
142       $sth->execute;
143       my ($count)=$sth->fetchrow_array;
144       $sth->finish;
145       return($count);
146 }
147
148 sub lastseenat {
149       my ($itemnumber, $brcd)=@_;
150       my $dbh = C4::Context->dbh;
151       my $itm = $dbh->quote($itemnumber);
152       my $brc = $dbh->quote($brcd);
153       my $query = "Select max(timestamp) from issues where itemnumber=$itm and branchcode = $brc";
154       my $sth=$dbh->prepare($query);
155       $sth->execute;
156       my ($date1)=$sth->fetchrow_array;
157       $sth->finish;
158       $query = "Select max(datearrived) from branchtransfers where itemnumber=$itm and tobranch = $brc";
159       # FIXME - There's already a $sth in this scope.
160       my $sth=$dbh->prepare($query);
161       $sth->execute;
162       my ($date2)=$sth->fetchrow_array;
163       $sth->finish;
164       $date2 =~ s/-//g;
165       $date2 =~ s/://g;
166       $date2 =~ s/ //g;
167       my $date;
168       if ($date1 < $date2) {
169           $date = $date2;
170       } else {
171           $date = $date1;
172       }
173       return($date);
174 }
175
176
177 #####################################################
178 # write date....
179 sub slashdate {
180     my ($date) = @_;
181     if (not $date) {
182         return "never";
183     }
184     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));
185     return "$hr:$mi  $da/$mo/$yr";
186 }