Fixed bug #1013. Works for me, if someone can check.
[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 C4::Koha;
33 use C4::Auth;
34 use HTML::Template;
35 use C4::Date;
36
37 # get all the data ....
38 my %env;
39 my $main='#cccc99';
40 my $secondary='#ffffcc';
41
42 my $input = new CGI;
43 my $itm = $input->param('itm');
44 my $bi = $input->param('bi');
45 my $bib = $input->param('bib');
46 my $branches = getbranches(\%env);
47
48 my $idata = itemdatanum($itm);
49 my $data = bibitemdata($bi);
50
51 my $homebranch = $branches->{$idata->{'homebranch'}}->{'branchname'};
52 my $holdingbranch = $branches->{$idata->{'holdingbranch'}}->{'branchname'};
53
54 my ($lastmove, $message) = lastmove($itm);
55
56 my $lastdate;
57 my $count;
58 if (not $lastmove) {
59     $lastdate = $message;
60     $count = issuessince($itm , 0);
61 } else {
62     $lastdate = $lastmove->{'datearrived'};
63     $count = issuessince($itm ,$lastdate);
64 }
65
66 # make the page ...
67
68 my ($template, $loggedinuser, $cookie)
69       = get_template_and_user({template_name => "bookcount.tmpl",
70                                          query => $input,
71                                          type => "intranet",
72                                          authnotrequired => 0,
73                                          flagsrequired => {borrowers => 1},
74                                          debug => 1,
75                                          });
76
77
78
79 my @branchloop;
80
81 foreach my $branchcode (keys %$branches) {
82         my %linebranch;
83     $linebranch{issues} = issuesat($itm, $branchcode);
84     my $date = lastseenat($itm, $branchcode);
85     $linebranch{seen} = slashdate($date);
86         $linebranch{branchname}=$branches->{$branchcode}->{'branchname'};
87         push(@branchloop,\%linebranch);
88 }
89
90 $template->param(       bib => $bib,
91                                                                 title => $data->{'title'},
92                                                                 author => $data->{'author'},
93                                                                 barcode => $idata->{'barcode'},
94                                                                 homebranch =>$homebranch,
95                                                                 holdingbranch => $holdingbranch,
96                                                                 lastdate =>  format_date($lastdate),
97                                                                 count =>  $count,
98                                                                 branchloop => \@branchloop);
99
100 print "Content-Type: text/html\n\n", $template->output;
101
102 ##############################################
103 # This stuff should probably go into C4::Search
104 # database includes
105 use DBI;
106
107 sub itemdatanum {
108     my ($itemnumber)=@_;
109     my $dbh = C4::Context->dbh;
110     my $sth=$dbh->prepare("select * from items where itemnumber=?");
111     $sth->execute($itemnumber);
112     my $data=$sth->fetchrow_hashref;
113     $sth->finish;
114     return($data);
115 }
116
117 sub lastmove {
118       my ($itemnumber)=@_;
119       my $dbh = C4::Context->dbh;
120       my $sth =$dbh->prepare("select max(branchtransfers.datearrived) from branchtransfers where branchtransfers.itemnumber=?");
121       $sth->execute($itemnumber);
122       my ($date) = $sth->fetchrow_array;
123       return(0, "Item has no branch transfers record") if not $date;
124       $sth=$dbh->prepare("Select * from branchtransfers where branchtransfers.itemnumber=? and branchtransfers.datearrived=?");
125       $sth->execute($itemnumber,$date);
126       my ($data) = $sth->fetchrow_hashref;
127       return(0, "Item has no branch transfers record") if not $data;
128       $sth->finish;
129       return($data,"");
130  }
131
132 sub issuessince {
133       my ($itemnumber, $date)=@_;
134       my $dbh = C4::Context->dbh;
135       my $sth=$dbh->prepare("Select count(*) from issues where issues.itemnumber=? and issues.timestamp > ?");
136       $sth->execute($itemnumber,$date);
137       my $count=$sth->fetchrow_hashref;
138       $sth->finish;
139       return($count->{'count(*)'});
140 }
141
142 sub issuesat {
143       my ($itemnumber, $brcd)=@_;
144       my $dbh = C4::Context->dbh;
145       my $sth=$dbh->prepare("Select count(*) from issues where itemnumber=? and branchcode = ?");
146       $sth->execute($itemnumber,$brcd);
147       my ($count)=$sth->fetchrow_array;
148       $sth->finish;
149       return($count);
150 }
151
152 sub lastseenat {
153       my ($itm, $brc)=@_;
154       my $dbh = C4::Context->dbh;
155       my $sth=$dbh->prepare("Select max(timestamp) from issues where itemnumber=? and branchcode = ?");
156       $sth->execute($itm,$brc);
157       my ($date1)=$sth->fetchrow_array;
158       $sth->finish;
159       $sth=$dbh->prepare("Select max(datearrived) from branchtransfers where itemnumber=? and tobranch = ?");
160       $sth->execute($itm,$brc);
161       my ($date2)=$sth->fetchrow_array;
162       $sth->finish;
163       #FIXME: MJR thinks unsafe
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  " . format_date("$yr-$mo-$da");
186 }