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