removing useless code
[koha.git] / circ / bookcount.pl
1 #!/usr/bin/perl
2
3
4 #written 7/3/2002 by Finlay
5 #script to display reports
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::Circulation;
28 use C4::Output;
29 use C4::Koha;
30 use C4::Auth;
31 use C4::Branch; # GetBranches
32 use C4::Biblio; # GetBiblioItemData
33 use C4::Date;
34
35 my $input        = new CGI;
36 my $itm          = $input->param('itm');
37 my $bi           = $input->param('bi');
38 my $biblionumber = $input->param('biblioitemnumber');
39 my $branches     = GetBranches;
40
41 my $idata = itemdatanum($itm);
42 my $data  = GetBiblioItemData($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 }
55 else {
56     $lastdate = $lastmove->{'datearrived'};
57     $count = issuessince( $itm, $lastdate );
58 }
59
60 # make the page ...
61
62 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
63     {
64         template_name   => "circ/bookcount.tmpl",
65         query           => $input,
66         type            => "intranet",
67         authnotrequired => 0,
68         flagsrequired   => { circulate => 1 },
69         debug           => 1,
70     }
71 );
72
73 my @branchloop;
74
75 foreach my $branchcode ( keys %$branches ) {
76     my %linebranch;
77     $linebranch{issues} = issuesat( $itm, $branchcode );
78     my $date = lastseenat( $itm, $branchcode );
79     $linebranch{seen}       = slashdate($date);
80     $linebranch{branchname} = $branches->{$branchcode}->{'branchname'};
81     push( @branchloop, \%linebranch );
82 }
83
84 $template->param(
85     biblionumber            => $biblionumber,
86     title                   => $data->{'title'},
87     author                  => $data->{'author'},
88     barcode                 => $idata->{'barcode'},
89     biblioitemnumber        => $bi,
90     homebranch              => $homebranch,
91     holdingbranch           => $holdingbranch,
92     lastdate                => format_date($lastdate),
93     count                   => $count,
94     branchloop              => \@branchloop,
95 );
96
97 output_html_with_http_headers $input, $cookie, $template->output;
98
99
100 sub itemdatanum {
101     my ($itemnumber) = @_;
102     my $dbh          = C4::Context->dbh;
103     my $sth          = $dbh->prepare("select * from items where itemnumber=?");
104     $sth->execute($itemnumber);
105     my $data = $sth->fetchrow_hashref;
106     $sth->finish;
107     return ($data);
108 }
109
110 sub lastmove {
111     my ($itemnumber) = @_;
112     my $dbh          = C4::Context->dbh;
113     my $sth          =
114       $dbh->prepare(
115 "select max(branchtransfers.datearrived) from branchtransfers where branchtransfers.itemnumber=?"
116       );
117     $sth->execute($itemnumber);
118     my ($date) = $sth->fetchrow_array;
119     return ( 0, "Item has no branch transfers record" ) if not $date;
120     $sth =
121       $dbh->prepare(
122 "Select * from branchtransfers where branchtransfers.itemnumber=? and branchtransfers.datearrived=?"
123       );
124     $sth->execute( $itemnumber, $date );
125     my ($data) = $sth->fetchrow_hashref;
126     return ( 0, "Item has no branch transfers record" ) if not $data;
127     $sth->finish;
128     return ( $data, "" );
129 }
130
131 sub issuessince {
132     my ( $itemnumber, $date ) = @_;
133     my $dbh = C4::Context->dbh;
134     my $sth =
135       $dbh->prepare(
136 "Select count(*) from issues where issues.itemnumber=? and issues.timestamp > ?"
137       );
138     $sth->execute( $itemnumber, $date );
139     my $count = $sth->fetchrow_hashref;
140     $sth->finish;
141     return ( $count->{'count(*)'} );
142 }
143
144 sub issuesat {
145     my ( $itemnumber, $brcd ) = @_;
146     my $dbh = C4::Context->dbh;
147     my $sth =
148       $dbh->prepare(
149         "Select count(*) from issues where itemnumber=? and branchcode = ?");
150     $sth->execute( $itemnumber, $brcd );
151     my ($count) = $sth->fetchrow_array;
152     $sth->finish;
153     return ($count);
154 }
155
156 sub lastseenat {
157     my ( $itm, $brc ) = @_;
158     my $dbh = C4::Context->dbh;
159     my $sth =
160       $dbh->prepare(
161 "Select max(timestamp) from issues where itemnumber=? and branchcode = ?"
162       );
163     $sth->execute( $itm, $brc );
164     my ($date1) = $sth->fetchrow_array;
165     $sth->finish;
166     $sth =
167       $dbh->prepare(
168 "Select max(datearrived) from branchtransfers where itemnumber=? and tobranch = ?"
169       );
170     $sth->execute( $itm, $brc );
171     my ($date2) = $sth->fetchrow_array;
172     $sth->finish;
173
174     #FIXME: MJR thinks unsafe
175     $date2 =~ s/-//g;
176     $date2 =~ s/://g;
177     $date2 =~ s/ //g;
178     my $date;
179     if ( $date1 < $date2 ) {
180         $date = $date2;
181     }
182     else {
183         $date = $date1;
184     }
185     return ($date);
186 }
187
188 #####################################################
189 # write date....
190 sub slashdate {
191     my ($date) = @_;
192     if ( not $date ) {
193         return "never";
194     }
195     my ( $yr, $mo, $da, $hr, $mi ) = (
196         substr( $date, 0,  4 ),
197         substr( $date, 4,  2 ),
198         substr( $date, 6,  2 ),
199         substr( $date, 8,  2 ),
200         substr( $date, 10, 2 )
201     );
202     return "$hr:$mi  " . format_date("$yr-$mo-$da");
203 }