Unido have far less info about their patrons, cutting down the confirmation screen
[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 $itm = $dbh->quote("$itemnumber");
111     my $query = "select * from items where itemnumber=$itm";
112     my $sth=$dbh->prepare($query);
113     $sth->execute;
114     my $data=$sth->fetchrow_hashref;
115     $sth->finish;
116     return($data);
117 }
118
119 sub lastmove {
120       my ($itemnumber)=@_;
121       my $dbh = C4::Context->dbh;
122       my $var1 = $dbh->quote($itemnumber);
123       my $sth =$dbh->prepare("select max(branchtransfers.datearrived) from branchtransfers where branchtransfers.itemnumber=$var1");
124       $sth->execute;
125       my ($date) = $sth->fetchrow_array;
126       return(0, "Item has no branch transfers record") if not $date;
127       my $var2 = $dbh->quote($date);
128       $sth=$dbh->prepare("Select * from branchtransfers where branchtransfers.itemnumber=$var1 and branchtransfers.datearrived=$var2");
129       $sth->execute;
130       my ($data) = $sth->fetchrow_hashref;
131       return(0, "Item has no branch transfers record") if not $data;
132       $sth->finish;
133       return($data,"");
134  }
135
136 sub issuessince {
137       my ($itemnumber, $date)=@_;
138       my $dbh = C4::Context->dbh;
139       my $itm = $dbh->quote($itemnumber);
140       my $dat = $dbh->quote($date);
141       my $sth=$dbh->prepare("Select count(*) from issues where issues.itemnumber=$itm and issues.timestamp > $dat");
142       $sth->execute;
143       my $count=$sth->fetchrow_hashref;
144       $sth->finish;
145       return($count->{'count(*)'});
146 }
147
148 sub issuesat {
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 count(*) from issues where itemnumber=$itm and branchcode = $brc";
154       my $sth=$dbh->prepare($query);
155       $sth->execute;
156       my ($count)=$sth->fetchrow_array;
157       $sth->finish;
158       return($count);
159 }
160
161 sub lastseenat {
162       my ($itemnumber, $brcd)=@_;
163       my $dbh = C4::Context->dbh;
164       my $itm = $dbh->quote($itemnumber);
165       my $brc = $dbh->quote($brcd);
166       my $query = "Select max(timestamp) from issues where itemnumber=$itm and branchcode = $brc";
167       my $sth=$dbh->prepare($query);
168       $sth->execute;
169       my ($date1)=$sth->fetchrow_array;
170       $sth->finish;
171       $query = "Select max(datearrived) from branchtransfers where itemnumber=$itm and tobranch = $brc";
172       # FIXME - There's already a $sth in this scope.
173       my $sth=$dbh->prepare($query);
174       $sth->execute;
175       my ($date2)=$sth->fetchrow_array;
176       $sth->finish;
177       $date2 =~ s/-//g;
178       $date2 =~ s/://g;
179       $date2 =~ s/ //g;
180       my $date;
181       if ($date1 < $date2) {
182           $date = $date2;
183       } else {
184           $date = $date1;
185       }
186       return($date);
187 }
188
189
190 #####################################################
191 # write date....
192 sub slashdate {
193     my ($date) = @_;
194     if (not $date) {
195         return "never";
196     }
197     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));
198     return "$hr:$mi  " . format_date("$yr-$mo-$da");
199 }